Is it possible to to customise Blender specific data structures?
For example how could I add a python list
data type to a Blender action
?
2 Answers
You can use a CollectionProperty to store a variable amount of items like a list, the properties per item need to be declared beforehand:
import bpy
class VectorPropGroup(bpy.types.PropertyGroup):
# name = StringProperty() # this is inherited from bpy.types.PropertyGroup
vector = bpy.props.IntVectorProperty(size=3)
bpy.utils.register_class(VectorPropGroup)
bpy.types.Action.my_collection = bpy.props.CollectionProperty(type=VectorPropGroup)
action = bpy.data.actions.new("My Action")
item = action.my_collection.add()
item.name = "First"
item.vector = (1,2,3)
item = action.my_collection.add()
item.name = "Second"
item.vector = (2,3,4)
print(action.my_collection[0].name, action.my_collection[0].vector[:])
print(action.my_collection[1].name, action.my_collection[1].vector[:])
It will print:
First (1, 2, 3)
Second (2, 3, 4)
Note that you can't add()
items from within a draw()
method. You need to call it manually (text editor / Python console) or create an operator for the user to be used in a UI.
You can customize data structures with custom properties.
Note that only bpy.types.ID, bpy.types.Bone and bpy.types.PoseBone classes support custom properties.
bpy.types.ID subclasses: WindowManager, Speaker, MetaBall, ParticleSettings, Palette, PaintCurve, Camera, GreasePencil, NodeTree, Material, Mask, Lamp, Sound, Object, Library, Screen, MovieClip, Brush, Mesh, FreestyleLineStyle, Scene, Text, Curve, Lattice, Texture, Image, Armature, Key, Group, VectorFont, Action, World
So Yes, Action will support custom properties
There are several types of custom properties: static, dynamic and ID
For static type the format to store lists in is for example bpy.props.StringProperty(), so any data needs to be encoded into string and then decoded.
For ID type you can use lists and dictionaries but lists may only contain floats and integers. Also they are declared only for single datablock.
The dynamic accept functions and are not stored with blend file.
-
$\begingroup$ How is
StringProperty
a list format? The only real list type isCollectionProperty
. $\endgroup$– CodeManXCommented Jan 7, 2015 at 16:23 -
$\begingroup$ I meant like you can store lists in them. CollectionProperty is finite. I will edit to make it clear $\endgroup$ Commented Jan 7, 2015 at 16:24
-
2$\begingroup$ You can basically add as many items to a collection as you want, or did you find an upper limit? $\endgroup$– CodeManXCommented Jan 7, 2015 at 16:48
-
1$\begingroup$ You can add as many of items at collection declaration, yes. But you cant add dynamically more or remove. You would have to create a new collection and swap it. The collection is meant to represent finite collection of properties and such behavior would confuse the code reader. Encoding a new string and adding it to old one when list grows (or truncating it) and swapping strings is far more convenient don't you think? If the encoding/decoding is well designed it shouldn't be even a performance bottleneck. $\endgroup$ Commented Jan 7, 2015 at 17:12
-
1$\begingroup$ I don't think you got that right, collections are dynamic. After declaration, they contain zero elements. What properties the
PropertyGroup
has (so every item in the collection) can't be changed after declaration indeed, but that shouldn't be any problem. $\endgroup$– CodeManXCommented Jan 7, 2015 at 21:47