Persistance: Passing variables between python scripts?

I’ve got two scripts running, one which sets up a scene, one which clears it away again. In the first script I stored info about a whole bunch of objects in a big list, which I thought I could just tie to the bpy module. So I had:

bpy.objects_to_process = ['a','b','c'];
print ( bpy.objects_to_process );
#outputs ['a','b','c']

Unfortunately this doesn’t seem to persist after the script terminates - when I tried to run the second script it complains that bpy has no attribute ‘objects_to_process’

Is there a way to do this?

There is no semi colon needed at the end of your lines, in Python. Ditch them :slight_smile:
You could write a scene property which will persist throughout the session/file. Dig around in the documentation about scene properties.

>>> bpy.context.scene['MySceneProperty'] = some_list
>>> bpy.context.scene['MySceneProperty']
<bpy id property array [4]>

>>> bpy.context.scene['MySceneProperty'][:]
...content of 'some_list'

>>> objects._to_process = bpy.context.scene['MySceneProperty']
>>> for i in objects_to_process:
...   print(i)

Followed your suggestion, and it works fine except for one odd little bug. If I put a print() statement after the assignment, the script bombs out:

#pass data over to[0]['objects_to_process'] = objects_to_process

print ('Done. Ready to animate.')

And on the ‘print()’ line, I get:

TypeError: object of type 'Object' has no len()

I can put in as many lines as I want before the print() and it still crashes on the print. Unless one of those lines references bpy. Then it gives no errors and carries on fine.

But the code itself works fine. Thanks again, zeffii.

well, print shouldn’t have a space between the print and the parenthesis. it’s not print () but print() . I doubt that is causing the problem, but stylistically it looks (to my eyes) untidy.

what happens when you use double quotation marks, inside blender there is a slight difference and sometimes it can cause problems.

print("Done, Ready to animate.")

Without seeing the script, that’s as much as i can suggest.

print (‘jflsjfsjfl’) print nicely, so THAT is not the reason of an error message.
Give us the code of the error line! (or even more) as zeffi said already :wink:

it looks like your variable is the object itself, maybe use the name object rather than the object (bpy properties types are limited to string, bool, int, float, pointer and collection if I’m right.
you could also use a class (bpy.propertygroup) that you register, in which you store your common attribute. this will stay also

PKHG: the print() line was the error line.

#pass data over to[0]['objects_to_process'] = objects_to_process;

#bpy.ops.object.select_all(action='DESELECT'); #hackfix

#prep to run animation
tmp_output_path = cwd + "tmp/";
if not os.path.exists(tmp_output_path): os.makedirs( tmp_output_path);

It also bombs out on the last line here, gives the same error message. This is nothing to do with my code, it’s some bug inside blender. Uncomment that DESELECT line and the error vanishes.

For what it’s worth, the list object objects_to_process looks like this:

objects_to_process = [];

for stl_todo in os.listdir(todo_path):
    #mess around to ensure we select the right object
    prior_objects = [object for object in bpy.context.scene.objects];
    new_current_objects = [object for object in bpy.context.scene.objects];
    new_object = (set(new_current_objects)-set(prior_objects)).pop();
    #add to list
    objects_to_process.append( {"object" : new_object, "filename" : stl_todo} );

So new_object is a blender object and stl_todo is a filename. I’m using the Cycles build r36803 on Windows 7 64bit.

As I said, though, the code otherwise works fine. I only really mention this in case a dev wants to know about it.

remove the semi colons at the ends of your lines, they have no place in python.

it also bombs out on the last line here, gives the same error message. This is nothing to do with my code, it’s some bug inside blender. Uncomment that DESELECT line and the error vanishes.

:slight_smile: yep and there’s still nothing stored in[0][‘objects_to_process’] except an empty dict.

if Im correct the print() has nothing to do with it. you just can’t add an object as a value in a bpy module : string, int, float, bool. even with list or dict there’s odd things.

you can have persistent datas using an object/scene/anything in at least, or use a bpy collection : at init your script build a collection class, you feed it with object names then save the blend. datas will be there when you restart blender and your script that will give you the path to your values.
but sure you can use an external file.