Custom property

The Blender API provides a set of simple property types described in bpy.props (IntProperty, BoolProperty, etc.). But the basic types are not always enough, sometimes we need more complex ones. The Blender API allows to group simple properties to make complex properties.

Let’s consider such complex property creation and make the 3×3 matrix property as an example.

First, let’s define a class with the structure of our property.

A 3×3 matrix can be represented as a combination of three vectors, so we can use the three basic properties of FloatVectorProperty type to describe the matrix:

Our custom property class must be registered in the register functions and released in the unregister function.

To assign our matrix property to objects (meshes) we need to create a pointer to it. Do it also in the register function and clear it in unregister.

Combine all together:

We assigned the “matrix_prop” property to each mesh. And we can now access it. For example, to get the value of the first element of the first row of the matrix:

However, it is inconvenient to get the whole matrix that way. Let’s add to the “Matrix3x3_property” class a getter and a setter for convenient getting/setting of its value.

Now we can get and set our property values ​​in a convenient way.

To set value:

and to get it:

Let’s also define the __repr__ function for beauty printing.

Final code:

4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comment
Inline Feedbacks
View all comments