While I was reading the modifier code
I found many data types such as
These data types are parts of Blender’s reflection system.
The reflection system plays an important rule in Blender native development,
so I plan to spend some days on reading its source code.
A reflection system usually handles following tasks:
- Manage the size, alignment, endianness, padding of data structures.
- Record meta information of data structures, such as minimum, maximum, default value, display name for GUI, etc.
- Enumerate properties or fields of data structure.
- Provide foreign language binding.
The first task is handled by Blender’s DNA system. And the rest tasks are handled by Blender’s RNA system. Since the general Blender programming involves with meta information retrieval and properties enumeration only, we concentrate on the RNA system in this and following posts.
Currently C/C++ does not have built-in reflection syntax, so reflection is usually implemented manually.
One way to implement reflection is to define a meta-class
while you are defining a concrete class.
you have a global static variable called
and use a predefined macro
to declare a meta class for the concrete class
and insert an instance of meta class into
You will see some code looks like this:
We will see the similar implementation in Blender soon.
Blender also has a tool
makesrna running at build time
to automatically generate information(getter, setter, etc)
for each data structures.
These informations are used for Python binding.
I will start to read the source code in the next post. And I want to arrange our topics from the aspect of a user of reflection system. Questions such as following ones will be answered:
- How to access the corresponding meta part of a Blender module?
- How to attach custom properties to the meta classes hierarchy?
- What is type inheritance in RNA and how to express “is-a” relations?
- How to generate Python bindings automatically for custom types?