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