Custom Properties type

Hello! So, I couldn’t google my way out of this one.
I’m (finally) re-writing a script from last year. It served me (surprisingly) well, but there are a bunch of additional functions I’d like to add.

Now, the new script adds objects of four different “types”. These types would define which values from Modifiers/Constraints/whatever will be exposed in a panel and how a certain function will affect the object.
The question is: how do I define a property like this?

Simply create a String Property: objectType = “TYPE_2”?
Or can I make it accept ONLY these four values? Some sort of list or something: (TYPE_0; TYPE_1; etc)?
Where/how should I define this property?

I know only enough programming (C++ mostly, though) to brute-force a script into running, so I’d appreciate simple words :slightly_smiling_face:
Thanks!

Hi StrayBillie,
sounds like you are looking for an enumproperty. If you know cpp, you will likely know what an enum is. But nonetheless there’s an example here:

https://docs.blender.org/api/current/bpy.props.html#getter-setter-example

1 Like

Ah, yes. Somehow never got to use enums in any meaningful way before.

So, to be clear, if I say something like

bpy.types.Object.test_enum = bpy.props.EnumProperty(items=test_items, get=None, set=None)

…it ends up adding the Custom Property to every Object there is, right?
Should I worry about it at all? Is there a way to avoid that?

PS. these docs have a super confusing example of enums :crazy_face:

In principle an enum is just an enumeration. Its a association between a constant number and a value, eg a traffic light might be described like this:

1 is green
2 is yellow
3 is red

Yes, it will. The principle itself has very little to to with it being an enum, so to understand it easier I chose a BoolProperty here instead and started it on the default scene.

import bpy

bpy.types.Object.test_enum = bpy.props.BoolProperty()

bpy.data.objects["Cube"].test_enum = False
bpy.data.objects["Light"].test_enum = True


for obj in bpy.data.objects:
    print( obj.name + " " + repr(obj.test_enum))
   
   
# OUTPUT: 
# Camera False
# Cube False
# Light True

I hope that makes it clearer how its defined and used.

There are IDProperties. These are defined/added individually per object, but its typewise much more restricted and there is no such thing as an enumproperty there. But yeah its an alternative.

1 Like

Yep, things cleared up, I can move forward again.
Thank you!

1 Like