2
$\begingroup$

How can I dynamically add or remove properties in a class like this? I want to make a user interface where you can click on a button that will add new options and properties in that same user interface but for that I need a way to append new settings into this class.

import bpy

class MyPropertyGroup(bpy.types.PropertyGroup):
    custom_1: bpy.props.FloatProperty(name="My Float")
    custom_2: bpy.props.IntProperty(name="My Int")


bpy.utils.register_class(MyPropertyGroup)

bpy.types.Object.my_prop_grp = bpy.props.PointerProperty(type=MyPropertyGroup)


# test this worked
bpy.data.objects[0].my_prop_grp.custom_1 = 22.0

So how would I add a custom_3 property to this class for example?

$\endgroup$
4
  • $\begingroup$ Hello, it seems you want to explore CollectionProperty $\endgroup$
    – Gorgious
    Commented Aug 21, 2023 at 10:09
  • $\begingroup$ Hello, thank you for your answer. Do you happen to know how exactly I should use the Collection Property feature? Unfortunately I could not find any examples anywhere. If I replace the PropertyGroup with CollectionProperty I get this error: ""ValueError: register_class(...): expected a subclass of a registerable RNA type (CollectionProperty does not support registration)"". I don't really know what I am doing at this point so any tip would be super helpful. Thanks! $\endgroup$
    – meto pop6
    Commented Aug 21, 2023 at 10:17
  • $\begingroup$ Let MyPropertyGroup inherit from bpy.types.PropertyGroup. Then bpy.types.Object.my_prop_grp = bpy.props.CollectionProperty(type = MyPropertyGroup) then any object will have a my_prop_grp and also have an add and remove method. $\endgroup$ Commented Oct 17, 2023 at 22:17
  • $\begingroup$ @BlenderEnthusiast if I understand the docs correctly the bpy.props.CollectionProperty would take one PropertyGroup (like custom_1, custom_2 in the example) and add() and remove() would just duplicate that in a list. You can't add another custom property like custom_3. $\endgroup$
    – commonpike
    Commented Sep 28 at 19:35

1 Answer 1

0
$\begingroup$

custom_2: bpy.props.IntProperty(name="My Int") is a class property with a type annotation. I don't know what Voodoo Blender uses behind the scenes, but it seems to use that annotation.

This seems to be what you were looking for:

MyPropertyGroup.__annotations__['custom_3'] = bpy.props.FloatProperty(name="The Latter")

Or you could obviously just subclass the group and use that instead

class MyPropertySubGroup(MyPropertyGroup):
   custom_3: bpy.props.IntProperty(name="The Latter")

bpy.utils.register_class(MyPropertySubGroup)
bpy.types.Object.my_prop_grp = bpy.props.PointerProperty(type=MyPropertySubGroup)

or you can subclass it more dynamically, again using annotations

MyPropertySubGroup = type('MyPropertySubGroup', (MyPropertyGroup,), {
    '__annotations__': {'custom_3': bpy.props.FloatProperty(name="The Latter")},
})
bpy.utils.register_class(MyPropertySubGroup)
bpy.types.Object.my_prop_grp = bpy.props.PointerProperty(type=MyPropertySubGroup)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .