'Accessing non-subscriptable properties
Scripting the Blender, I successfully did bpy.ops.render.render(some_args)
but bpy.ops.render['render']
fails with BPyOpsSubMod object is not subscriptable. This puzzles me since I expected that, likewise in Javascript, any Python object is a dictionary and I can access object methods either by obj.member
or obj['member']
. How do I work around the non-subscriptable properties when I want to reference them by name?
Solution 1:[1]
It's not true that every object is a dictionary. But most objects have a dictionary, accessible through the name .__dict__
.
You can use either
bpy.ops.render.__dict__['render']
or
getattr(bpy.ops.render, 'render')
Solution 2:[2]
If you want to know what is inside a non-subscriptable object that does not have a dict you can do this:
import csv # use csv dialect as example object
a = csv.get_dialect('excel')
field_names = [v for v in dir(a) if not v.startswith('__')]
fields = [(getattr(a, v),v) for v in field_names]
print(fields)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | botenvouwer |