While I was reading the modifier code I found many data types such as PointerRNA and StructRNA. 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:

  1. Manage the size, alignment, endianness, padding of data structures.
  2. Record meta information of data structures, such as minimum, maximum, default value, display name for GUI, etc.
  3. Enumerate properties or fields of data structure.
  4. 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. Typically, you have a global static variable called MetaClassManager, and use a predefined macro DECLARE_METACLASS(Object) to declare a meta class for the concrete class Object and insert an instance of meta class into MetaClassManager.

You will see some code looks like this:

class Object {
    float mass;

    // Other properties for Object...

    DECLARE_FLOAT_METAPROPERTY(mass, 0.0f, 100.0f, 1.0f, "Mass");
    // Other meta properties...

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?