Merge from trunk -r 25003:25745.
[blender.git] / source / blender / python / intern / bpy_operator_wrap.c
1
2 /**
3  * $Id$
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * Contributor(s): Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26
27 #include "bpy_operator_wrap.h"
28 #include "BLI_listbase.h"
29 #include "BKE_context.h"
30 #include "BKE_report.h"
31 #include "DNA_windowmanager_types.h"
32 #include "MEM_guardedalloc.h"
33 #include "WM_api.h"
34 #include "WM_types.h"
35 #include "UI_interface.h"
36 #include "ED_screen.h"
37
38 #include "RNA_define.h"
39
40 #include "bpy_rna.h"
41 #include "bpy_util.h"
42
43 #include "../generic/bpy_internal_import.h" // our own imports
44
45 void operator_wrapper(wmOperatorType *ot, void *userdata)
46 {
47         /* take care not to overwrite anything set in
48          * WM_operatortype_append_ptr before opfunc() is called */
49         StructRNA *srna = ot->srna;
50         *ot= *((wmOperatorType *)userdata);
51         ot->srna= srna; /* restore */
52
53         RNA_struct_blender_type_set(ot->ext.srna, ot);
54
55
56         /* Can't use this because it returns a dict proxy
57          *
58          * item= PyObject_GetAttrString(py_class, "__dict__");
59          */
60         {
61                 PyObject *py_class = ot->ext.data;
62                 PyObject *item= ((PyTypeObject*)py_class)->tp_dict;
63                 if(item) {
64                         /* only call this so pyrna_deferred_register_props gives a useful error
65                          * WM_operatortype_append_ptr will call RNA_def_struct_identifier
66                          * later */
67                         RNA_def_struct_identifier(ot->srna, ot->idname);
68
69                         if(pyrna_deferred_register_props(ot->srna, item)!=0) {
70                                 /* failed to register operator props */
71                                 PyErr_Print();
72                                 PyErr_Clear();
73
74                         }
75                 }
76                 else {
77                         PyErr_Clear();
78                 }
79         }
80 }
81
82 void macro_wrapper(wmOperatorType *ot, void *userdata)
83 {
84         wmOperatorType *data = (wmOperatorType *)userdata;
85
86         /* only copy a couple of things, the rest is set by the macro registration */
87         ot->name = data->name;
88         ot->idname = data->idname;
89         ot->description = data->description;
90         ot->flag |= data->flag; /* append flags to the one set by registration */
91         ot->pyop_poll = data->pyop_poll;
92         ot->ui = data->ui;
93         ot->ext = data->ext;
94
95         RNA_struct_blender_type_set(ot->ext.srna, ot);
96
97
98         /* Can't use this because it returns a dict proxy
99          *
100          * item= PyObject_GetAttrString(py_class, "__dict__");
101          */
102         {
103                 PyObject *py_class = ot->ext.data;
104                 PyObject *item= ((PyTypeObject*)py_class)->tp_dict;
105                 if(item) {
106                         /* only call this so pyrna_deferred_register_props gives a useful error
107                          * WM_operatortype_append_ptr will call RNA_def_struct_identifier
108                          * later */
109                         RNA_def_struct_identifier(ot->srna, ot->idname);
110
111                         if(pyrna_deferred_register_props(ot->srna, item)!=0) {
112                                 /* failed to register operator props */
113                                 PyErr_Print();
114                                 PyErr_Clear();
115
116                         }
117                 }
118                 else {
119                         PyErr_Clear();
120                 }
121         }
122 }
123
124 PyObject *PYOP_wrap_macro_define(PyObject *self, PyObject *args)
125 {
126         wmOperatorType *ot;
127         wmOperatorTypeMacro *otmacro;
128         PyObject *macro;
129         PointerRNA ptr_otmacro;
130         StructRNA *srna;
131
132         char *opname;
133         const char *macroname;
134
135         if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &opname))
136                 return NULL;
137
138         if (WM_operatortype_exists(opname) == NULL) {
139                 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid operator id", opname);
140                 return NULL;
141         }
142
143         /* identifiers */
144         srna= srna_from_self(macro);
145         macroname = RNA_struct_identifier(srna);
146
147         ot = WM_operatortype_exists(macroname);
148
149         if (!ot) {
150                 PyErr_Format(PyExc_ValueError, "Macro Define: '%s' is not a valid macro or hasn't been registered yet", macroname);
151                 return NULL;
152         }
153
154         otmacro = WM_operatortype_macro_define(ot, opname);
155
156         RNA_pointer_create(NULL, &RNA_OperatorTypeMacro, otmacro, &ptr_otmacro);
157
158         return pyrna_struct_CreatePyObject(&ptr_otmacro);
159 }
160