Hi
I want to iterate in a list of object, but i dont want to add them all to a list in every redraw( Im using the script in script link). Its somebody knows a way to have a list of objects that can have persistence.
Any idea would help i just want to make the code faster so i don’t have to look in blender for all the objects i want in the list in every redraw.
Just keep your list global. Then add a reference to the list in your ReDraw. Some outside process is then responsible for populating the list. Either put code in the start of your script or in a button.
Pseudo Code:
############################################################################
# Globals
############################################################################
myObjectList = []
############################################################################
# GUI redraw.
############################################################################
def gui_redraw():
global myObjectList
for n in myObjectList:
#Code here.
pass
############################################################################
# Program starts here.
############################################################################
populateMyList(myObjectList)
Register(gui_redraw, sys_events, gui_events)
thanks atom, but im not taking about global variables in a script, I was taking about using them in a script and link them to an object and run them in all the redraw.
The easy way is to just store it in the Blender module, i.e.
import Blender
# Tries to retrieve the global list (across script link calls)
# If its not existent in the Blender module, then it makes
# the global list and then assigns it into the Blender module
try:
myLocalList = Blender.myGlobalList
except:
myLocalList = buildMyGlobalList()
Blender.myGlobalList = myLocalList
# continue on referencing myLocalList
Its not the best way to do it in terms of name space pollution, but its probably the easiest.
In a little less pseudo-code like
import Blender
import bpy
scn = bpy.data.scenes.active
obList = []
try:
obList = Blender.gObList
except:
obList = [ob for ob in scn.objects if ob.selected]
Blender.gObList = obList