0

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.

Way2Close
  • 470
  • 1
  • 4
  • 23

1 Answers1

1

Annotations

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.utils.register_class(Test)
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
''
batFINGER
  • 84,216
  • 10
  • 108
  • 233