Skip to content
panzergame edited this page May 30, 2018 · 3 revisions

Adding an UI property needs to modify 4 places, DNA, RNA, python UI and converter. The branch wiki_property_ui (https://github.com/UPBGE/blender/commit/05a01981fc0316373a3e46a976010cca9a273232) shows a light example. This example explains how to add a int variable named my_var in Scene.

The first step is to add a variable in DNA structures located in blender/makesdna. Also the variable needs to follow a strict memory alignment https://wiki.blender.org/index.php/Dev:Source/Architecture/SDNA_Notes.

This variable is declared for python in blender/makesrna like:

	// Definition of python name, type and flag.
	prop = RNA_def_property(srna, "my_var", PROP_INT, PROP_NONE);
	// Link to the DNA variable by name.
	RNA_def_property_int_sdna(prop, NULL, "myVar");
	RNA_def_property_range(prop, 1, 10000);
	// The clamping range and step in UI.
	RNA_def_property_ui_range(prop, 1, 50, 1, 1);
	// The default value when resetting in UI.
	RNA_def_property_int_default(prop, 5);
	// Description.
	RNA_def_property_ui_text(prop, "My Var", "Example variable");
	// Description of update to proceed when changing of value.
	RNA_def_property_update(prop, NC_SCENE, NULL);

Function prefixed by RNA_def_property are often followed by the type name.

Next the new python variable is used in python and put in a UI panel in release/scripts/startup/bl_ui:

class SCENE_PT_my_var(SceneButtonsPanel, Panel):
    bl_label = "My Var Panel"
    COMPAT_ENGINES = {'BLENDER_GAME'}

    @classmethod
    def poll(cls, context):
        scene = context.scene
        # Hide in other engine context.
        return (scene.render.engine in cls.COMPAT_ENGINES)

    def draw(self, context):
        layout = self.layout

        # Get the GameData in python.
        gs = context.scene.game_settings

        # Add a property in the panel.
        layout.prop(gs, "my_var")

And registered at the end of the file by adding it in the classes tuple.

Finally this variable is visible during game engine conversion from function like BL_ConvertBlenderObjects by accessing blenderscene->gm.myVar.

Clone this wiki locally