Custom CollectionProperty different from builtin collections

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Posts: 2
Joined: Mon Dec 31, 2012 2:05 pm

Custom CollectionProperty different from builtin collections

Postby mg007 » Mon Dec 31, 2012 2:17 pm


It has been a year since I started learning python scripting for Blender. Recently I've noticed that the builtin collections such as data.objects, scene.objects, and many more are different from the ones we create with CollectionProperty. Maybe they are entirely different data structures, but I think there must be a way to manually add similarly functionality to our own properties. For example,

Code: Select all

bpy.types.Object.my_coll = bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)

creates a simple collection property (PropertyGrpup directly used here for simplicity, but subclassing it makes no difference). It lacks "active" subproperty (for example I want to keep track of active item in the collection), a "new" method, etc. Of course I can use following workarounds for them:
  • Use other integer property to keep track of active item

    Code: Select all

    bpy.types.Object.my_coll_active = bpy.types.IntProperty()

    We could use it with template_list, for example. But would be much more intuitive than having 2 different properties.
  • Use add() method to add new items instead of new(). But add() lacks arguments that we need to explicitly set which becomes cumbersome if the item type has update() callback set

    Code: Select all

    class MyItemType(bpy.types.PropertyGroup):
        prop1 = bpy.props.StringProperty(update=prop12_combined_update)
        prop2 = bpy.props.IntProperty(update=prop12_combined_update)

    bpy.types.Object.my_items_coll = bpy.props.CollectionProperty(type=MyItemType)

    ...and later...

    Code: Select all

    newitem = bpy.context.object.my_items_coll.add()
    newitem.prop1 = "blah blah"
    newitem.prop2 = 42

    So here, the prop12_combined_update() is called twice, which is bad if prop12_combined_update() is time-consuming. I think with new() it could be prevented (callback isn't called, but we could manually call it later)

So my question is: Is there a way to add these functionality (and maybe other that are provided by built-in collections) from Python script?
(FYKI I've seen this example by Campbell, but that did not work, and I don't even know if that is related to what I am looking for)

Looking forward for quick reply on my first post! :)

PS: I've tried to post in blenderartists, with no success :|

Posts: 2
Joined: Mon Dec 31, 2012 2:05 pm

Postby mg007 » Sat Jan 05, 2013 11:04 am

Never mind, got helpful replies on the blenderartists thread.

Return to “Coding Blender”

Who is online

Users browsing this forum: No registered users and 0 guests