Blender: Creating a Custom Modifier

On my quest to create an OpenCL enabled Particle System I realized that I need a way for the Blender Game Engine to know when to render an object as a particles as opposed to a regular mesh. A straight forward way to achieve this (with other benefits) would be to create a Particle modifier that can be added to an object which the game engine can then check for. I’d like to thank Moguri for this idea!

So I set out today to make a custom modifier that does nothing but printf its existence! I figured this out by copying the SoftBody modifier and scouring the source code for all references to it. I called my modifier RTPS, so just replace any instance of RTPS in this post with your own chosen name.

First we want to create our main modifier code file (all paths start in the source folder)

blender/modifiers/intern/MOD_rtps.c

cp blender/modifiers/intern/MOD_softbody.c blender/modifiers/intern/MOD_rtps.c

In here you will add functionality, initialize default values and of course replace all instances of SoftBody with RTPS. You can see my code MOD_rtps.c

The rest of the instructions are just modifying files (if you are confused about where things go, just look at Softbody!

At line 52 of blender/modifiers/CMakeLists.txt

	intern/MOD_rtps.c

At line 184 of blender/modifiers/intern/MOD_util.c

	INIT_TYPE(RTPS);

At line 70 of blender/modifiers/MOD_modifiertypes.h

extern ModifierTypeInfo modifierType_RTPS;

At line 564 of: blender/makesrna/RNA_access.h

extern StructRNA RNA_RTPSModifier;

At line 303 of: blender/blenkernel/BKE_modifier.h

int modifiers_isRTPSEnabled(struct Object *ob);

We need to edit blender/makesdna/DNA_modifier_types.h in several places:

on line 69, just after eModifierType_Screw and before NUM_MODIFIER_TYPES add

eModifierType_RTPS,

on line 441, after the SoftbodyModifierData struct, add

typedef struct RTPSModifierData {
    ModifierData modifier;
    int system;
} RTPSModifierData;
on line 85 of blender/makesrna/intern/rna_modifier.c (before the {0,NULL,0,NULL,NULL} entry:
{eModifierType_RTPS, "RTPS", ICON_MOD_SOFT, "RTPS", ""},
and on line 171
case eModifierType_RTPS:
  	return &RNA_RTPSModifier;
and on line 2210:
static void rna_def_modifier_rtps(BlenderRNA *brna)
{

    StructRNA *srna;
    PropertyRNA *prop;

    srna= RNA_def_struct(brna, "RTPSModifier", "Modifier");
    RNA_def_struct_ui_text(srna, "RTPS Modifier", "Add a particle system");
    RNA_def_struct_sdna(srna, "RTPSModifierData");
    RNA_def_struct_ui_icon(srna, ICON_MOD_SOFT);

    prop= RNA_def_property(srna, "system", PROP_INT, PROP_NONE);
    RNA_def_property_int_sdna(prop, NULL, "system");
    // we should use an enum but this is hacked together for now
    // this range is to allow the user to select a different system
    RNA_def_property_ui_range(prop, 0, 2, 1, 0);
    RNA_def_property_ui_text(prop, "Systems", "Available particle systems");
    RNA_def_property_update(prop, 0, "rna_Modifier_update");

}
line 2337:
rna_def_modifier_rtps(brna);
To add the modifier to our UI we need to edit one more file: ../release/scripts/ui/properties_data_modifier.py around line 624
def RTPS(self, layout, ob, md):
    layout.label(text="System:")
    layout.prop(md, "system")
    layout.label(text="0: lorentz 1: gravity")
This should build and give you a modifier under the Simulate header that does nothing but print out a message when you add it. My next step is to check for this modifier in the Game Engine and of course, start adding some functionality! I wish I could explain more about what each of these lines do, but I’m still trying to understand the RNA/DNA system and the Blender way of doing things.

3 thoughts on “Blender: Creating a Custom Modifier

  1. shinjin

    Hi enj,

    This is a great tutorial, but it gave me a serious headache to find out why do I get a “propery not found” error from blender all the time.

    There are two more lines of code to be added to rna_modifier.c, into the rna_Modifier_refine function:

    case eModifierType_Enja:
    return &RNA_EnjaModifier;

    Cheers,
    shinjin

  2. Pingback: Particles in BGE: Fluids in Real Time with OpenCL | enj

  3. enj Post author

    @shinjin
    That requirement was added after I posted the tutorial. Thank you for bringing it to my attention! I updated with your changes

Comments are closed.