Dynamic Blender Properties

As part of my most recent adventures in game engine programming, I came across a small problem—I needed a way to edit levels (both their geometry, and the entities within the level and their associated components). Writing an editor to do this is a rather daunting task. Thankfully, Blender is a free, open-source 3D application that is "easily" extended (well, easy-ish). So, instead of writing my own editor, I can write a Blender addon to make it do what I need it to. First up in that, was presenting an interface for editing which components an object has, and setting the values of each components' attributes. I found this to be more difficult that I expected, thanks to the way Blender handles and presents data. I will show you here how I got things working, as there doesn't seem to be documentation on this and I had to wade through a lot of half-expired forum posts to get things working.

Basically, in order to display any data in a panel in Blender, you must pre-define the property classes that it will render. The following code listing shows a simple panel which will show up in the object section of the properties panel:

A sample panel displaying the pre-defined properties in the pre-defined `SamplePropertyGroup` class.

A problem arises, however, when you don't know ahead of time what that SamplePropertyGroup class will look like. Or even if it will exist (or what custom property groups will exist). For my use case, I want to have each component exist in blender as it's own subclass of the PropertyGroup class, with its list of attributes defined as class members (as relevant PropertyTypes). Forunately for us, Python is a scripted language, and comes with some tools to dynamically create classes and interact with them via the type(), setattr(), getattr(), and delattr() built-in functions.

Now, whenever we register we just call the above code, and our PropertyGroup classes will be defined and registered in Blender as properties of all objects! Don't forget to clean up the registered objects in unregister()!

Finally, to actually show our properties in a Blender panel is relatively straightforward:

There are of course many more things that need to be done to make this a fully working system, but hopefully this can help you get started. I will continue working on my engine tools addon and release it when it's ready (you can follow progress at BlazingMammothGames/mammoth_blender_tools if you really like). The tool, when done, will hopefully server as a solid example for adapting Blender to your own needs in the future. For now, if you run into any trouble, don't hesitate to ask for help in the comments!