I want to create a number of StringProperty's to a property group using a loop. e.g:

class Test(bpy.types.PropertyGroup):

    for n in range(1, 20):
        line_[n] : bpy.props.StringProperty(name = "line_%d" % (n))

I tried enumerating a list but it registers the list too.



Quite possibly this is a dupe of Blender 2.8 - Field property declaration and dynamic class creation

I found that using the type method do define a class in 2.79 was very handy, but for annotations very tricky.

If an __annotations__ is defined on our propertygroup class, can add to it prior to registering.

import bpy

class Test(bpy.types.PropertyGroup):
    __annotations__ = {}

for n in range(1, 20):
    k = "line_%d" % n
    Test.__annotations__[k] = bpy.props.StringProperty(name=k)

bpy.types.Scene.test = bpy.props.PointerProperty(type=Test)

Or in the class

class Test(bpy.types.PropertyGroup):
    __annotations__ = {'line_%d' % i: bpy.props.StringProperty(name="line_%d" % i) 
            for i in range(1, 20)}

Test in py console

>>> C.scene.test.line_1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.