1
$\begingroup$

I have the following custom prop:

class Settings(bpy.types.PropertyGroup):
    enabled = False 
    state = {
        "some_data": None, # edit: this points to numpy array
        "some_other_data": None
    }        
    string_prop = StringProperty(
        name='Heyyy')

Assigned to:

bpy.types.Object.my_settings = PointerProperty(type=Settings)

There are two issues I am having.

1) I cannot change the value of enabled, like so:

class Init(bpy.types.Operator):
    bl_idname = "object.init"
    bl_label = "Init"
    def execute(self, context):
        context.object.my_settings.enabled = True
        return {'FINISHED'}

2) All of the objects share and control values of state

There are no issues with blender props like the string_prop above.

I assume this is just how Blender handles custom PropertyGroups. Is there a way to make this work? I need to have information stored per object. The only other solution I can think of so far is to store an array of custom props per scene and check which one is currently active based on the id of the selected object, but that is more complicated than I would like it to be.

$\endgroup$
  • 1
    $\begingroup$ The issue as I see it is in storing your custom data. Your enabled property above is a class property. bpy.props.stringproperty is used to add a property of that type on each instance. See docs on property getter setter which shows how the properties are stored using custom properties of the same name. You can also use the ol' fashioned python property(getter, setter) and a global (if you don't need to save) or could save to a string json style etc. $\endgroup$ – batFINGER Feb 26 '18 at 11:44
1
$\begingroup$

The boolean flag is the simple part here. As you already assumed, those PropertyGroups are designed to hold Blender Properties only. So instead of

enabled = False

you should use

enabled = BoolProperty(default = False)  // which is anyways the default

The state property is more difficult. There are no Dictionary properties available to you, but maybe you can work around your issue by using a CollectionProperty instead?

Create a class first to hold your collection, for example:

class MyCollection(PropertyGroup):
    val1 = StringProperty()
    val2 = IntProperty(min = 0, max = 999)
    // etc etc

Then, in your Settings class, reference it like this:

variableCollection = CollectionProperty(type = MyCollection)

To add items to the collection, use this in your operator code:

x = context.object.my_settings.variableCollection.add()
x.val1 = 'This works'
x.val2 = 34

Alternatively, you can also nest PropertyGroups of course. That would be

nestedProperties = PointerProperty(type = MyCollection)

That would allow you to do:

context.object.my_settings.nestedProperties.val1 = 'This is nested'
context.object.my_settings.nestedProperties.val2 = 33

It depends on your situation in terms of what works best. But keep in mind that Blender's own property types are the only ones it can store with the file.

One last way to store Python data - but this does not save that data with the file - is simply using a key - value approach on the object. You can always use something like:

x = bpy.context.object
x['myCustomKey'] = myDesiredValue

The resulting data can then even be viewed in the viewport:

enter image description here

$\endgroup$
  • $\begingroup$ thanks for a great answer! Unfortunately I am storing numpy arrays and lists in the state dic.. $\endgroup$ – VSB Feb 21 '18 at 20:14
  • $\begingroup$ @VSB Maybe you should use a different approach then: try x = context.object followed by x['myCustomData'] = whateverYouNeed. This can hold arrays, lists, whatnot, and is attatched to the object in question. Unfortunately, you cannot save the results this way in the file. $\endgroup$ – aliasguru Feb 21 '18 at 20:19
  • $\begingroup$ haha that I think should work perfectly, thanks!! $\endgroup$ – VSB Feb 21 '18 at 20:33
  • $\begingroup$ looks like we almost had it.. for some reason Blender converts numpy arrays stored per object to IDPropertyArray :( $\endgroup$ – VSB Feb 23 '18 at 18:17
  • $\begingroup$ @VSB maybe Richard Colburn can help? He did a few numpy for Blender Tutorials here m.youtube.com/channel/UCavaCSVksKt6RvrBmohmSoA $\endgroup$ – aliasguru Feb 23 '18 at 18:59

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.