Blender V2.61 - r43446

bpy_operator_wrap.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Campbell Barton
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00033 #include <Python.h>
00034 
00035 #include "bpy_operator_wrap.h"
00036 #include "WM_api.h"
00037 #include "WM_types.h"
00038 
00039 #include "BLI_utildefines.h"
00040 
00041 #include "RNA_access.h"
00042 #include "RNA_define.h"
00043 
00044 #include "bpy_rna.h"
00045 
00046 static void operator_properties_init(wmOperatorType *ot)
00047 {
00048     PyObject *py_class = ot->ext.data;
00049     RNA_struct_blender_type_set(ot->ext.srna, ot);
00050 
00051     /* only call this so pyrna_deferred_register_class gives a useful error
00052      * WM_operatortype_append_ptr will call RNA_def_struct_identifier
00053      * later */
00054     RNA_def_struct_identifier(ot->srna, ot->idname);
00055 
00056     if (pyrna_deferred_register_class(ot->srna, py_class) != 0) {
00057         PyErr_Print(); /* failed to register operator props */
00058         PyErr_Clear();
00059     }
00060 }
00061 
00062 void operator_wrapper(wmOperatorType *ot, void *userdata)
00063 {
00064     /* take care not to overwrite anything set in
00065      * WM_operatortype_append_ptr before opfunc() is called */
00066     StructRNA *srna = ot->srna;
00067     *ot = *((wmOperatorType *)userdata);
00068     ot->srna = srna; /* restore */
00069 
00070     operator_properties_init(ot);
00071 
00072     {   /* XXX - not nice, set the first enum as searchable, should have a way for python to set */
00073         PointerRNA ptr;
00074         PropertyRNA *prop;
00075 
00076         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
00077         prop = RNA_struct_find_property(&ptr, "type");
00078         if (prop) {
00079             ot->prop = prop;
00080         }
00081     }
00082 }
00083 
00084 void macro_wrapper(wmOperatorType *ot, void *userdata)
00085 {
00086     wmOperatorType *data = (wmOperatorType *)userdata;
00087 
00088     /* only copy a couple of things, the rest is set by the macro registration */
00089     ot->name = data->name;
00090     ot->idname = data->idname;
00091     ot->description = data->description;
00092     ot->flag |= data->flag; /* append flags to the one set by registration */
00093     ot->pyop_poll = data->pyop_poll;
00094     ot->ui = data->ui;
00095     ot->ext = data->ext;
00096 
00097     operator_properties_init(ot);
00098 }
00099 
00100 PyObject *PYOP_wrap_macro_define(PyObject *UNUSED(self), PyObject *args)
00101 {
00102     wmOperatorType *ot;
00103     wmOperatorTypeMacro *otmacro;
00104     PyObject *macro;
00105     PointerRNA ptr_otmacro;
00106     StructRNA *srna;
00107 
00108     char *opname;
00109     const char *macroname;
00110 
00111     if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &opname))
00112         return NULL;
00113 
00114     if (WM_operatortype_find(opname, TRUE) == NULL) {
00115         PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id", opname);
00116         return NULL;
00117     }
00118 
00119     /* identifiers */
00120     srna = srna_from_self(macro, "Macro Define:");
00121     macroname = RNA_struct_identifier(srna);
00122 
00123     ot = WM_operatortype_find(macroname, TRUE);
00124 
00125     if (!ot) {
00126         PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro or hasn't been registered yet", macroname);
00127         return NULL;
00128     }
00129 
00130     otmacro = WM_operatortype_macro_define(ot, opname);
00131 
00132     RNA_pointer_create(NULL, &RNA_OperatorMacro, otmacro, &ptr_otmacro);
00133 
00134     return pyrna_struct_CreatePyObject(&ptr_otmacro);
00135 }
00136