[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [25631] trunk/blender: Macro registration using the normal rna registration methods ( like operators).

Martin Poirier theeth at yahoo.com
Wed Dec 30 23:14:35 CET 2009


Revision: 25631
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=25631
Author:   theeth
Date:     2009-12-30 23:14:32 +0100 (Wed, 30 Dec 2009)

Log Message:
-----------
Macro registration using the normal rna registration methods (like operators).

bpy.types.register(MacroClass)

instead of

bpy.ops.add_macro(MacroClass)

The rest is unchanged.

Also remove some now unused code for the old registration methods (there's still some remaining).

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy/ops.py
    trunk/blender/source/blender/makesrna/RNA_access.h
    trunk/blender/source/blender/makesrna/intern/rna_internal.h
    trunk/blender/source/blender/makesrna/intern/rna_wm.c
    trunk/blender/source/blender/makesrna/intern/rna_wm_api.c
    trunk/blender/source/blender/python/intern/bpy_operator.c
    trunk/blender/source/blender/python/intern/bpy_operator_wrap.c
    trunk/blender/source/blender/python/intern/bpy_rna.c
    trunk/blender/source/blender/python/intern/bpy_rna.h
    trunk/blender/source/blender/windowmanager/intern/wm_operators.c

Modified: trunk/blender/release/scripts/modules/bpy/ops.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy/ops.py	2009-12-30 20:15:28 UTC (rev 25630)
+++ trunk/blender/release/scripts/modules/bpy/ops.py	2009-12-30 22:14:32 UTC (rev 25631)
@@ -23,7 +23,6 @@
 
 # op_add = ops_module.add
 op_remove = ops_module.remove
-op_add_macro = ops_module.add_macro
 op_dir = ops_module.dir
 op_call = ops_module.call
 op_as_string = ops_module.as_string
@@ -57,12 +56,6 @@
             raise AttributeError(module)
         return bpy_ops_submodule(module)
 
-    def add(self, pyop):
-        op_add(pyop)
-
-    def add_macro(self, pyop):
-        op_add_macro(pyop)
-
     def remove(self, pyop):
         op_remove(pyop)
 

Modified: trunk/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- trunk/blender/source/blender/makesrna/RNA_access.h	2009-12-30 20:15:28 UTC (rev 25630)
+++ trunk/blender/source/blender/makesrna/RNA_access.h	2009-12-30 22:14:32 UTC (rev 25631)
@@ -319,6 +319,7 @@
 extern StructRNA RNA_ObjectBase;
 extern StructRNA RNA_ObstacleFluidSettings;
 extern StructRNA RNA_Operator;
+extern StructRNA RNA_Macro;
 extern StructRNA RNA_OperatorFileListElement;
 extern StructRNA RNA_OperatorMousePath;
 extern StructRNA RNA_OperatorProperties;

Modified: trunk/blender/source/blender/makesrna/intern/rna_internal.h
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_internal.h	2009-12-30 20:15:28 UTC (rev 25630)
+++ trunk/blender/source/blender/makesrna/intern/rna_internal.h	2009-12-30 22:14:32 UTC (rev 25631)
@@ -211,6 +211,7 @@
 void RNA_api_drivers(StructRNA *srna);
 void RNA_api_image(struct StructRNA *srna);
 void RNA_api_operator(struct StructRNA *srna);
+void RNA_api_macro(struct StructRNA *srna);
 void RNA_api_keyconfig(struct StructRNA *srna);
 void RNA_api_keyingset(struct StructRNA *srna);
 void RNA_api_keymap(struct StructRNA *srna);

Modified: trunk/blender/source/blender/makesrna/intern/rna_wm.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_wm.c	2009-12-30 20:15:28 UTC (rev 25630)
+++ trunk/blender/source/blender/makesrna/intern/rna_wm.c	2009-12-30 22:14:32 UTC (rev 25631)
@@ -692,6 +692,7 @@
 }
 
 void operator_wrapper(wmOperatorType *ot, void *userdata);
+void macro_wrapper(wmOperatorType *ot, void *userdata);
 
 static char _operator_idname[OP_MAX_TYPENAME];
 static char _operator_name[OP_MAX_TYPENAME];
@@ -763,12 +764,84 @@
 	return dummyot.ext.srna;
 }
 
+
+static StructRNA *rna_MacroOperator_register(const bContext *C, ReportList *reports, void *data, const char *identifier, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
+{
+	wmOperatorType dummyot = {0};
+	wmOperator dummyop= {0};
+	PointerRNA dummyotr;
+	int have_function[4];
+
+	/* setup dummy operator & operator type to store static properties in */
+	dummyop.type= &dummyot;
+	dummyot.idname= _operator_idname; /* only assigne the pointer, string is NULL'd */
+	dummyot.name= _operator_name; /* only assigne the pointer, string is NULL'd */
+	dummyot.description= _operator_descr; /* only assigne the pointer, string is NULL'd */
+	RNA_pointer_create(NULL, &RNA_Macro, &dummyop, &dummyotr);
+
+	/* validate the python class */
+	if(validate(&dummyotr, data, have_function) != 0)
+		return NULL;
+
+	{	/* convert foo.bar to FOO_OT_bar
+		 * allocate the description and the idname in 1 go */
+		int idlen = strlen(_operator_idname) + 4;
+		int namelen = strlen(_operator_name) + 1;
+		int desclen = strlen(_operator_descr) + 1;
+		char *ch, *ch_arr;
+		ch_arr= ch= MEM_callocN(sizeof(char) * (idlen + namelen + desclen), "_operator_idname"); /* 2 terminators and 3 to convert a.b -> A_OT_b */
+		WM_operator_bl_idname(ch, _operator_idname); /* convert the idname from python */
+		dummyot.idname= ch;
+		ch += idlen;
+		strcpy(ch, _operator_name);
+		dummyot.name = ch;
+		ch += namelen;
+		strcpy(ch, _operator_descr);
+		dummyot.description = ch;
+	}
+
+	if(strlen(identifier) >= sizeof(dummyop.idname)) {
+		BKE_reportf(reports, RPT_ERROR, "registering operator class: '%s' is too long, maximum length is %d.", identifier, sizeof(dummyop.idname));
+		return NULL;
+	}
+
+	/* check if we have registered this operator type before, and remove it */
+	{
+		wmOperatorType *ot= WM_operatortype_exists(dummyot.idname);
+		if(ot && ot->ext.srna)
+			rna_Operator_unregister(C, ot->ext.srna);
+	}
+
+	/* create a new menu type */
+	dummyot.ext.srna= RNA_def_struct(&BLENDER_RNA, dummyot.idname, "Operator");
+	dummyot.ext.data= data;
+	dummyot.ext.call= call;
+	dummyot.ext.free= free;
+
+	dummyot.pyop_poll=	(have_function[0])? operator_poll: NULL;
+	dummyot.ui=			(have_function[3])? operator_draw: NULL;
+
+	WM_operatortype_append_macro_ptr(macro_wrapper, (void *)&dummyot);
+
+	/* update while blender is running */
+	if(C)
+		WM_main_add_notifier(NC_SCREEN|NA_EDITED, NULL);
+
+	return dummyot.ext.srna;
+}
+
 static StructRNA* rna_Operator_refine(PointerRNA *opr)
 {
 	wmOperator *op= (wmOperator*)opr->data;
 	return (op->type && op->type->ext.srna)? op->type->ext.srna: &RNA_Operator;
 }
 
+static StructRNA* rna_MacroOperator_refine(PointerRNA *opr)
+{
+	wmOperator *op= (wmOperator*)opr->data;
+	return (op->type && op->type->ext.srna)? op->type->ext.srna: &RNA_Macro;
+}
+
 #else
 
 static void rna_def_operator(BlenderRNA *brna)
@@ -832,6 +905,8 @@
 	srna= RNA_def_struct(brna, "Macro", NULL);
 	RNA_def_struct_ui_text(srna, "Macro Operator", "Storage of a macro operator being executed, or registered after execution.");
 	RNA_def_struct_sdna(srna, "wmOperator");
+	RNA_def_struct_refine_func(srna, "rna_MacroOperator_refine");
+	RNA_def_struct_register_funcs(srna, "rna_MacroOperator_register", "rna_Operator_unregister");
 
 	prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
 	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
@@ -844,6 +919,27 @@
 	RNA_def_property_struct_type(prop, "OperatorProperties");
 	RNA_def_property_ui_text(prop, "Properties", "");
 	RNA_def_property_pointer_funcs(prop, "rna_Operator_properties_get", NULL, NULL);
+
+	/* Registration */
+	prop= RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
+	RNA_def_property_string_sdna(prop, NULL, "type->idname");
+	RNA_def_property_string_maxlength(prop, OP_MAX_TYPENAME); /* else it uses the pointer size! */
+	RNA_def_property_flag(prop, PROP_REGISTER);
+
+	prop= RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
+	RNA_def_property_string_sdna(prop, NULL, "type->name");
+	RNA_def_property_string_maxlength(prop, 1024); /* else it uses the pointer size! */
+	RNA_def_property_flag(prop, PROP_REGISTER);
+
+	prop= RNA_def_property(srna, "bl_register", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "type->flag", OPTYPE_REGISTER);
+	RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
+
+	prop= RNA_def_property(srna, "bl_undo", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "type->flag", OPTYPE_UNDO);
+	RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
+
+	RNA_api_macro(srna);
 }
 
 static void rna_def_operator_type_macro(BlenderRNA *brna)

Modified: trunk/blender/source/blender/makesrna/intern/rna_wm_api.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_wm_api.c	2009-12-30 20:15:28 UTC (rev 25630)
+++ trunk/blender/source/blender/makesrna/intern/rna_wm_api.c	2009-12-30 22:14:32 UTC (rev 25631)
@@ -237,6 +237,35 @@
 	RNA_def_pointer(func, "context", "Context", "", "");
 }
 
+void RNA_api_macro(StructRNA *srna)
+{
+	FunctionRNA *func;
+	PropertyRNA *parm;
+
+	/* utility, not for registering */
+	func= RNA_def_function(srna, "report", "rna_Operator_report");
+	parm= RNA_def_enum(func, "type", wm_report_items, 0, "Type", "");
+	RNA_def_property_flag(parm, PROP_REQUIRED|PROP_ENUM_FLAG);
+	parm= RNA_def_string(func, "message", "", 0, "Report Message", "");
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+
+
+	/* Registration */
+
+	/* poll */
+	func= RNA_def_function(srna, "poll", NULL);
+	RNA_def_function_ui_description(func, "Test if the operator can be called or not.");
+	RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL);
+	RNA_def_function_return(func, RNA_def_boolean(func, "visible", 1, "", ""));
+	RNA_def_pointer(func, "context", "Context", "", "");
+
+	/* draw */
+	func= RNA_def_function(srna, "draw", NULL);
+	RNA_def_function_ui_description(func, "Draw function for the operator.");
+	RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL);
+	RNA_def_pointer(func, "context", "Context", "", "");
+}
+
 void RNA_api_keyconfig(StructRNA *srna)
 {
 	FunctionRNA *func;

Modified: trunk/blender/source/blender/python/intern/bpy_operator.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_operator.c	2009-12-30 20:15:28 UTC (rev 25630)
+++ trunk/blender/source/blender/python/intern/bpy_operator.c	2009-12-30 22:14:32 UTC (rev 25631)
@@ -245,7 +245,7 @@
 	static PyMethodDef pyop_dir_meth =		{"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL};
 	static PyMethodDef pyop_getrna_meth =	{"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL};
 //	static PyMethodDef pyop_add_meth =		{"add", (PyCFunction) PYOP_wrap_add, METH_O, NULL};
-	static PyMethodDef pyop_add_macro_meth ={"add_macro", (PyCFunction) PYOP_wrap_add_macro, METH_O, NULL};
+//	static PyMethodDef pyop_add_macro_meth ={"add_macro", (PyCFunction) PYOP_wrap_add_macro, METH_O, NULL};
 	static PyMethodDef pyop_macro_def_meth ={"macro_define", (PyCFunction) PYOP_wrap_macro_define, METH_VARARGS, NULL};
 	static PyMethodDef pyop_remove_meth =	{"remove", (PyCFunction) PYOP_wrap_remove, METH_O, NULL};
 
@@ -257,7 +257,7 @@
 	PyModule_AddObject( submodule, "dir",		PyCFunction_New(&pyop_dir_meth,		NULL) );
 	PyModule_AddObject( submodule, "get_rna",	PyCFunction_New(&pyop_getrna_meth,	NULL) );
 //	PyModule_AddObject( submodule, "add",		PyCFunction_New(&pyop_add_meth,		NULL) );
-	PyModule_AddObject( submodule, "add_macro",	PyCFunction_New(&pyop_add_macro_meth,		NULL) );
+//	PyModule_AddObject( submodule, "add_macro",	PyCFunction_New(&pyop_add_macro_meth,		NULL) );
 	PyModule_AddObject( submodule, "macro_define",PyCFunction_New(&pyop_macro_def_meth,		NULL) );

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list