Previous Thread  Next Thread

chat icon Custom CollectionProperty different from builtin collections


Posted: Mon Dec 31, 2012 2:17 pm
Joined: 31 Dec 2012
Posts: 2

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,

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
    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

    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...

    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! Smile

PS: I've tried to post in blenderartists, with no success Neutral
Reply with quote


Posted: Sat Jan 05, 2013 11:04 am
Joined: 31 Dec 2012
Posts: 2
Never mind, got helpful replies on the blenderartists thread.
Reply with quote

Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group