I want to create some integer properties using loops just to save time and lines of code. So basically, the new properties should be something like this:


Here is my approach. First, I created a list of desired variables, "prop1...prop10".

prop_name = ["prop%d"%(i+1) for i in range(10)]

Then I use a for loop to create these properties.

for i, name in enumerate(prop_name):
    bpy.types.Scene.name = bpy.props.IntProperty(name="prop %d"%(i+1))

With this, I expect 10 properties. Now I have 2 issue:

The name that should be shown inside the properties is not changing with respect to their index values.

2nd Issue: I'm not actually able to put them in my Panel. So, I guess, there are no properties created with this method.

(<built-in function Intproperty>, {'name':'prop10','attr':'name'})
(<built-in function Intproperty>, {'name':'prop10','attr':'name'})
(<built-in function Intproperty>, {'name':'prop10','attr':'name'})
(<built-in function Intproperty>, {'name':'prop10','attr':'name'})

How can I change this name with index value, or let me know if there is an another way to create multiple properties.


Use the python built in method setattr

The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.

import bpy
from bpy.props import IntProperty
from bpy.types import Scene

prop_name = ["prop%d" % (i+1) for i in range(10)]

for i, name in enumerate(prop_name):
    setattr(Scene, name, IntProperty(name="prop %d" % (i+1)))

On your code, you are setting the name property of Scene class ten times to an IntProperty.

for i, name in enumerate(prop_name):
    bpy.types.Scene.name = bpy.props.IntProperty(name="prop %d"%(i+1))

is equivalent to

bpy.types.Scene.name = IntProperty(name="prop 1")
bpy.types.Scene.name = IntProperty(name="prop 2")
bpy.types.Scene.name = IntProperty(name="prop 10")

This is a double whammy, as Scene already has a StringProperty name. (eg bpy.data.scenes["NAME"] has "NAME" for a name) The ID class is a base class for all blender objects in the subclass list. Another clue this is BAD is the blank scene name in UI after running question code. Click on scene name and it will most likely crash blender.

  • $\begingroup$ That works for me but then I stuck again at some time. How do I make the following shorten: bcs = bpy.context.scene and the a variable called props contain all these class objects in a list [bcs.prop1, bcs.prop2, bcs.prop3, bcs.prop4, bcs.prop5, bcs.prop6, bcs.prop7, bcs.prop8, bcs.prop9, bcs.prop10] $\endgroup$ – mGm Feb 9 '17 at 14:03
  • $\begingroup$ docs.python.org/3/library/functions.html#getattr $\endgroup$ – batFINGER Feb 9 '17 at 15:22
  • $\begingroup$ It seems to be a bit complicated to understand this method when one need to use it other way around. Now I understand how to create properties and register it. But now how do I un-register these properties, like del bpy.types.Scene.prop1 in a loop. $\endgroup$ – mGm Feb 10 '17 at 16:00
  • $\begingroup$ Perhaps you could edit your question, or ask a new question, with all these new requests. AFAIC my answer covers the Q. Surely after seeing setattr in use , getattr, eg ( [getattr(obj, p) for p in props] ) is not too complicated? $\endgroup$ – batFINGER Feb 12 '17 at 14:20

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.