Here we see two ways to get a UI property stored in the Scene

>>> print(scn.repGroups)
<bpy_struct, RepGroup("")>

>>> print(scn['repGroups'])
<bpy id prop: owner="SCScene", name="repGroups", address=0x0000003F0CE7D278>

Typically I'm use to accessing a property like in JavaScript by passing an argument that is the string that holds the value that is the property like

def findIt(propName):
   scn = bpy.context.scene
   return scn[propName]

I would like to access the scene/scn property by name but get returned the item like you do in a normal reference (bpy_struct NOT bpy id prop). Please advise?


Defined properties use ID properties as storage

A defined property bpy.types.Scene.xxxx = bpy.props.BlahProperty(default=default) is stored as an ID property scene["xxxx"] once it is set (non default). See get set example

>>> bpy.types.Scene.xxxx = bpy.props.IntProperty(default=11)
>>> C.scene.xxxx

>>> C.scene['xxxx']
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
KeyError: 'bpy_struct[key]: key "xxxx" not found'

>>> C.scene.xxxx = 12
>>> C.scene['xxxx']

You see a struct printed for the property scene.xxxx if it is a pointer or collection, or an int for IntProperty, a str for StringProperty.

Henceforth, rather than scene[propname] a more generic pythonic way would be using

getattr(scene, propname, None)

to return, if propname == "xxxx", scene.xxxx if it exists, else None.

for the defined default

>>> C.scene.bl_rna.properties['xxxx'].default
| improve this answer | |
  • $\begingroup$ Right okay thanks! That's working. It's a PropertyGroup with a PropertyGroup and index for Items and List in a multi list selector in the UI. If that makes sense just to add to my confusion. Normally in Python it works like JavaScript so kind of weird to see this seems to initially appear scrambled and inaccessible in Blender?! $\endgroup$ – Master James Aug 15 '18 at 10:47
  • $\begingroup$ IMO the idea here is to only save non default values in the blend file. Not sure re normally in python .. type.prop = property(get, set) works for both ob.prop and ob["prop"] $\endgroup$ – batFINGER Aug 15 '18 at 11:02

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.