2
$\begingroup$

I need a LOT of properties in my plugin, and a lot of them are very redundant, they are using a naming system. I'd like to generate them more easily via a function, the only problem is that since 2.8 the properties are done via type declaration, this make code generation much more trickier.

i have an example below, if anyone have another solution i'd gladly hear them out. Cheers

$\endgroup$

1 Answer 1

1
$\begingroup$
import bpy

#I have a series of very redundand properties to define in my propgroup
#i'd like to procedurally generate the properties before registration

print("\n\nScript is running...")

def generate_props(name):
    """will add the properties to annotation"""
    d = {}
    d[f"{name}_str"] = bpy.props.StringProperty(name=f"{name} MyString")
    d[f"{name}_bool"] = bpy.props.BoolProperty(name=f"{name} MyBool")
    d[f"{name}_int"] = bpy.props.IntProperty(name=f"{name} MyInt")
    d[f"{name}_float"] = bpy.props.IntProperty(name=f"{name} Myloat")
    return d

class TEST_PROP_propgroup(bpy.types.PropertyGroup): 

    realprop : bpy.props.StringProperty(name="i'm working, others will not")
    
    __annotations__.update(generate_props("hello"))
    __annotations__.update(generate_props("world"))
    __annotations__.update(generate_props("foo"))
    
    #as we can see, all are evenly considered in this class variables, 
    #from the surface, new element to annotation dict or doing a new
    #type declaration is the same right?
    
    print("\n CLASS LOCALS: \n", locals())
    
try:
    bpy.utils.unregister_class(TEST_PROP_propgroup)
except:
    pass

#we can see here that before the registration, nothing changed. our declarations are still here 
print("\n BEFORE REGISTRATION: \n", TEST_PROP_propgroup.__annotations__)

bpy.utils.register_class(TEST_PROP_propgroup)

bpy.types.Scene.testing = bpy.props.PointerProperty(type=TEST_PROP_propgroup)

print("\n FINAL PROPERTY: \n", bpy.context.scene.testing.bl_rna.properties.keys())
$\endgroup$

You must log in to answer this question.

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