2
$\begingroup$

This works great:

bpy.types.Object.color_asd = bpy.props.FloatVectorProperty(
                subtype='COLOR',
            )

This errors:

UID_short = "asd"
bpy.types.Object[f"color_{UID_short}"] = bpy.props.FloatVectorProperty(
                subtype='COLOR',
            )

Error:

TypeError: 'bpy_struct_meta_idprop' object does not support item assignment

But the approach is wrong to begin with. Let's say I have 2 cubes, Cube and Cube.001 I then would like to create a custom property only for a specific object like: Cube.color_asd

And for Cube.001 for example Cube.001.color_mega

Like so:

name = f"color_{UID_short}"
obj[name] = bpy.props.FloatVectorProperty(
            subtype='COLOR',
        )

Which then errors with:

Error: Python: Traceback (most recent call last):
  File "C:\Users\bratp\AppData\Roaming\Blender Foundation\Blender\3.6\scripts\addons\Liebherr-Toolbox\material_panel.py", line 1037, in execute
    obj[name] = bpy.props.PointerProperty(type = ObjectCustomColors)
TypeError: invalid id-property type _PropertyDeferred not supported

I can't figure either issue out.

$\endgroup$

1 Answer 1

2
$\begingroup$

Dictionary accessors are for interface-level attributes, not for API-defined ones. You can define them with python but for all intents and purposes they're the same as if you created them using the interface.

bpy.data.objects["Cube"]["my_prop"] = 2

enter image description here

If you want to add an API-defined property to all bpy.types.Object constructs, you need to use setattr.

UID_short = "asd"
setattr(bpy.types.Object, f"color_{UID_short}", bpy.props.FloatVectorProperty(subtype="COLOR"))

enter image description here

If you want to add a color type interface custom property, you can use bpy.types.Struct.id_properties_ui.

import bpy

UID_short = "asd"
name = f"color_{UID_short}"

bpy.data.objects["Cube"][name] = (1.0, 0.0, 0.0, 1.0)
ui_prop = bpy.data.objects["Cube"].id_properties_ui(name)
ui_prop.update(subtype="COLOR", min=0, max=1)

enter image description here

$\endgroup$
1
  • $\begingroup$ I went for the struct.id approach as I want an object pacific approach. It works perfectly. I did have difficulties adding it to the actual UI panel, so I wanted to post this in case someone stumbles upon it. The additional " required was quite unintuitive. ` color_node_name = f"color_{UID_Short}" if helpers.Has_Material_Attribute_Node(material, color_node_name): layout.prop(obj, f'["{color_node_name}"]', text = UID_Short) ` $\endgroup$
    – YupDiDo
    Commented Oct 8, 2023 at 14:45

You must log in to answer this question.

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