Adding a new ID Type to Blender

I don’t know if this post belongs to this forum, but I am new to Blender development and I want to add a new ID to Blenders library. Can anybody tell me how to do this safely?
I tried it looking at the vfont implementation, but currently I am getting a SIGABRT error / stack smashing detected, and I do not know where this comes from. (Sorry, I am not a professional developer)

Any suggestions? I am thankful for every help I can get!

Kind Regards

Adding a new ID types requires changes in dozens of files. There doesn’t exist a guide for it, you should find the most similar existing ID type and search for it throughout the code, adding your ID type in all the same places.

To get an idea of the kinds of changes needed, this commit adds a volume datablock type:
https://developer.blender.org/rB74f7cdcf6bb8032b6bf8881007e0344366849859

1 Like

Thanks for the quick response!
I will have a look at it. Even when searching throughout the code it is sometimes pretty difficult, because you might have to add it to another enum or list or so. So your commit might be very useful and educational, thanks a lot!

Sorry if that sounds lazy or so, but do you maybe have a commit that shows how to add operators for loading a new ID type via the template_ID ?

I don’t know of a specific commit that shows this, I would just start from existing operators for a similar ID type.

It is quite easy.

First of all, you have to define your function uiTemplateNewIDType in the space editor your type will be edited. Usually this function is declared in blender\source\blender\editors\include\UI_interface.h and defined in the file editor_buttons.c. I would suggest to check uiTemplateMovieClip in clip_buttons.c as it is quite compact and easy to understand.

After that, you have to define the function in the RNA. This is done in blender\source\blender\makesrna\intern\rna_ui_api.c in the function RNA_api_ui_layout. For instance, for the movie clip the code is:

func = RNA_def_function(srna, "template_movieclip", "uiTemplateMovieClip");
RNA_def_function_ui_description(func, "Item(s). User interface for selecting movie clips and their source paths");
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
api_ui_item_rna_common(func);
RNA_def_boolean(func, "compact", false, "", "Use more compact layout");

You can just copy this part and adapt it for your new ID type.

After this you can use template_ID to create a new instance of your type.

Normally the generic template_ID should be used, which I think the question was about? How to add operators that you pass to template_ID.

Oh you’re right, I misunderstood the question :sweat_smile:

Ok this is done usually in the editor_ops.c file. They are not very different from operators you’d usually define for a space editor. Here few references to define new operators:
https://en.blender.org/index.php/Dev:2.5/Source/Architecture/Operators
https://en.blender.org/index.php/Dev:2.5/Source/Architecture/Operators/Tutorial

Now let’s take again the movie clip as an example. The CLIP_OT_open function, in clip_ops.c, is a standard operator. You have to add two things, one in the invoke and one in the exec.

In open_invoke, this code is used (the open_init function):

PropertyPointerRNA *pprop;

op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop);

In open_exec, this:

PropertyPointerRNA *pprop;
if (!op->customdata)
	open_init(C, op);

/* hook into UI */
pprop = op->customdata;

if (pprop->prop) {
	/* when creating new ID blocks, use is already 1, but RNA
	 * pointer use also increases user, so this compensates it */
	id_us_min(&clip->id);

	RNA_id_pointer_create(&clip->id, &idptr);
	RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
	RNA_property_update(C, &pprop->ptr, pprop->prop);
}

where clip is loaded from the selected file.

This is it actually. With this, you can pass it as operator to template_ID. For the movie clip:

template_ID(sc, "clip", open="clip.open")

Thanks a lot.
I added the files but now in spacetypes.c , it doesn’t find my ED_operatortypes function to append the new operators…
I found out that the CMakefile in editors/util includes the new ED file, but it does not work either…
May need some research on my side…