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


1 Answer 1

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")
    #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())

#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.types.Scene.testing = bpy.props.PointerProperty(type=TEST_PROP_propgroup)

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

You must log in to answer this question.

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