6
$\begingroup$

I have a number of custom string properties that I added to my scene with:

bpy.types.Scene.prop1 = bpy.props.StringProperty()
bpy.types.Scene.prop2 = bpy.props.StringProperty()
# etc...

I want to be able to iterate over these properties to change their values.

I was hoping I would be able to do something like this:

for prop in bpy.types.Scene:
    prop = "new value";

This doesn't work but I would be iterating over all Scene properties instead of only mine anyway.

I also tried adding my properties' names to a list so that I might be able to access only my custom properties and not all the scene properties:

prop_names = ["prop1", "prop2", etc...]

But then I didn't know how to use these, I was hoping to do:

for prop in prop_names:
    bpy.types.Scene. + prop = "new value";

This also didn't work. So how might I achieve something like this?

$\endgroup$
4
$\begingroup$

There are a few ways to use Python to inspect an objects properties, but in this case you're best off using getattr, setattr.

In the case you describe I think the best option is to store a list of properties, mainly because there isn't a good way to know which properties you created. (Blender doesn't track a properties owner).

for prop_id in prop_names:
    setattr(scene, prop_id, value)

... for completeness, to copy scene_src -> scene_dst

for prop_id in prop_names:
    setattr(scene_dst, prop_id, getattr(scene_src, prop_id))

Note, theres nothing spesific to custom propertes here, this is typical Python and world with any properties (called attributes in Python)

$\endgroup$
  • $\begingroup$ Thanks, but using seattr is currently reporting in the console that my property is read-only? $\endgroup$ – Ray Mairlot Jun 30 '13 at 13:07
  • $\begingroup$ @Ray Mairlot, if you have read-only properties you need to either remove them from the list, or write more advanced code that handles them. For example bpy.data.Object.modifiers will report as readonly, not because you can't edit each modifier in the collection, you just cant assign the property directly, I assume you are getting this error on a Collection or PointerProperty. It would be best if you made a small example that fails for the purpose of this question. $\endgroup$ – ideasman42 Jun 30 '13 at 19:46
  • $\begingroup$ I'm actually going to mark this question as solved, as your code does allow me to iterate over my properties as I wanted. I didn't realise having bpy.types.Scene.my_prop="new_value" after it's initial creation but before setattr would cause it to become read-only? I have now corrected this with bpy.context..instead of bpy.types.. but setattr makes this obsolete anyway. Thanks for your help. $\endgroup$ – Ray Mairlot Jun 30 '13 at 21:02
  • $\begingroup$ @Ray Mairlot, not sure your understanding properties correctly? - but I think it would be best to ask this as a separate question. bpy.types.Scene.my_prop = "new_value" is not something your normally would want to do. $\endgroup$ – ideasman42 Jun 30 '13 at 23:53
  • $\begingroup$ It was more a facepalm moment than anything else, I'm probably not aware of all the facts when dealing with properties, but I think my mind just went blank when I was trying to assign to bpy.types...not a mistake I've made before ;) $\endgroup$ – Ray Mairlot Jul 1 '13 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.