Define properties using list

I’m new in python and I’m not sure if you can do something like this to avoid the definition of all properties one by one:

sX = []
for i in range(10):
sX.extend([FloatProperty(name=‘width’ + str(i),min=0.001,max= 10, default= 0.60,precision=3, description=‘width’)])


def draw(self, context):
layout = self.layout
box=layout.box()
for i in range(10):
box.prop(self,sX[i]) # <---- this is the problematic line

I want to define a list of properties and add the list to the panel. I have tested several alternatives and nothing works.

How can I do this?

You may want to take a look at the 3Delight render engine exporter. It creates a dynamic UI on-the-fly. Basically it is using the python exec command to issue code snippets that define new properties. Then to display them use hasattr.


        if hasattr(ptr, prop_coll) and len(getattr(ptr, prop_coll)) &gt; 0 and getattr(ptr, prop_index) &gt;= 0:
            item = getattr(ptr, prop_coll)[getattr(ptr, prop_index)]
            self.draw_item(layout, context, item)

you may look into CollectionProperty and Property Groups etc. here:

http://www.blender.org/documentation/blender_python_api_2_69_1/bpy.props.html#propertygroup-example

Maybe base some dynamic prop creation on this?
http://www.blender.org/documentation/blender_python_api_2_69_1/info_overview.html#dynamic-defined-classes-advanced

Which reminds me …

The CollectionProperty example uses an add() method, which is not mentioned at the CollectionProperty class reference page, or indeed anywhere else in the API documentation that I can find. I presume that CollectionProperty has other handy but undocumented methods - such as remove(), perhaps?

Best wishes,
Matthew

It’s indeed not listed in API docs. The type isn’t CollectionProperty though:

>>> bpy.types.Scene.my_collection = bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)
>>> type(bpy.context.scene.my_collection)
<class ‘bpy_prop_collection_idprop’>

There’s nothing about this idprop in the docs either however.

You can find it out like so:

>>> dir(bpy.context.scene.my_collection)
[‘doc’, ‘add’, ‘clear’, ‘move’, ‘remove’]