fix [#34113] operator_menu_enum() - Tooltip not showing descriptions
[blender.git] / source / blender / python / intern / bpy_operator_wrap.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_operator_wrap.c
24  *  \ingroup pythonintern
25  *
26  * This file is so python can define operators that C can call into.
27  * The generic callback functions for python operators are defines in
28  * 'rna_wm.c', some calling into functions here to do python specific
29  * functionality.
30  */
31
32 #include <Python.h>
33
34 #include "WM_api.h"
35 #include "WM_types.h"
36
37 #include "BLI_utildefines.h"
38
39 #include "RNA_access.h"
40 #include "RNA_define.h"
41
42 #include "bpy_rna.h"
43 #include "bpy_intern_string.h"
44 #include "bpy_operator_wrap.h"  /* own include */
45
46 static void operator_properties_init(wmOperatorType *ot)
47 {
48         PyObject *py_class = ot->ext.data;
49         RNA_struct_blender_type_set(ot->ext.srna, ot);
50
51         /* only call this so pyrna_deferred_register_class gives a useful error
52          * WM_operatortype_append_ptr will call RNA_def_struct_identifier
53          * later */
54         RNA_def_struct_identifier(ot->srna, ot->idname);
55
56         if (pyrna_deferred_register_class(ot->srna, py_class) != 0) {
57                 PyErr_Print(); /* failed to register operator props */
58                 PyErr_Clear();
59         }
60
61         /* set the default property: ot->prop */
62         {
63                 /* picky developers will notice that 'bl_property' won't work with inheritance
64                  * get direct from the dict to avoid raising a load of attribute errors (yes this isnt ideal) - campbell */
65                 PyTypeObject *py_class = ot->ext.data;
66                 PyObject *py_class_dict = py_class->tp_dict;
67                 PyObject *bl_property = PyDict_GetItem(py_class_dict, bpy_intern_str_bl_property);
68                 const char *prop_id;
69                 bool prop_raise_error;
70
71                 if (bl_property) {
72                         if (PyUnicode_Check(bl_property)) {
73                                 /* since the property is explicitly given, raise an error if its not found */
74                                 prop_id = _PyUnicode_AsString(bl_property);
75                                 prop_raise_error = true;
76                         }
77                         else {
78                                 PyErr_Format(PyExc_ValueError,
79                                              "%.200s.bl_property should be a string, not %.200s",
80                                              ot->idname, Py_TYPE(bl_property)->tp_name);
81
82                                 /* this could be done cleaner, for now its OK */
83                                 PyErr_Print();
84                                 PyErr_Clear();
85
86                                 prop_id = NULL;
87                                 prop_raise_error = false;
88                         }
89                 }
90                 else {
91                         prop_id = "type";
92                         prop_raise_error = false;
93                 }
94
95                 if (prop_id) {
96                         /* fallback to hard-coded string */
97                         PointerRNA ptr;
98                         PropertyRNA *prop;
99
100                         RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
101                         prop = RNA_struct_find_property(&ptr, prop_id);
102                         if (prop) {
103                                 ot->prop = prop;
104                         }
105                         else {
106                                 if (prop_raise_error) {
107                                         PyErr_Format(PyExc_ValueError,
108                                                      "%.200s.bl_property '%.200s' not found",
109                                                      ot->idname, prop_id);
110
111                                         /* this could be done cleaner, for now its OK */
112                                         PyErr_Print();
113                                         PyErr_Clear();
114                                 }
115                         }
116                 }
117         }
118         /* end 'ot->prop' assignment */
119
120 }
121
122 void operator_wrapper(wmOperatorType *ot, void *userdata)
123 {
124         /* take care not to overwrite anything set in
125          * WM_operatortype_append_ptr before opfunc() is called */
126         StructRNA *srna = ot->srna;
127         *ot = *((wmOperatorType *)userdata);
128         ot->srna = srna; /* restore */
129
130         operator_properties_init(ot);
131 }
132
133 void macro_wrapper(wmOperatorType *ot, void *userdata)
134 {
135         wmOperatorType *data = (wmOperatorType *)userdata;
136
137         /* only copy a couple of things, the rest is set by the macro registration */
138         ot->name = data->name;
139         ot->idname = data->idname;
140         ot->description = data->description;
141         ot->flag |= data->flag; /* append flags to the one set by registration */
142         ot->pyop_poll = data->pyop_poll;
143         ot->ui = data->ui;
144         ot->ext = data->ext;
145
146         operator_properties_init(ot);
147 }
148
149 PyObject *PYOP_wrap_macro_define(PyObject *UNUSED(self), PyObject *args)
150 {
151         wmOperatorType *ot;
152         wmOperatorTypeMacro *otmacro;
153         PyObject *macro;
154         PointerRNA ptr_otmacro;
155         StructRNA *srna;
156
157         char *opname;
158         const char *macroname;
159
160         if (!PyArg_ParseTuple(args, "Os:_bpy.ops.macro_define", &macro, &opname))
161                 return NULL;
162
163         if (WM_operatortype_find(opname, true) == NULL) {
164                 PyErr_Format(PyExc_ValueError,
165                              "Macro Define: '%s' is not a valid operator id",
166                              opname);
167                 return NULL;
168         }
169
170         /* identifiers */
171         srna = srna_from_self(macro, "Macro Define:");
172         macroname = RNA_struct_identifier(srna);
173
174         ot = WM_operatortype_find(macroname, true);
175
176         if (!ot) {
177                 PyErr_Format(PyExc_ValueError,
178                              "Macro Define: '%s' is not a valid macro or hasn't been registered yet",
179                              macroname);
180                 return NULL;
181         }
182
183         otmacro = WM_operatortype_macro_define(ot, opname);
184
185         RNA_pointer_create(NULL, &RNA_OperatorMacro, otmacro, &ptr_otmacro);
186
187         return pyrna_struct_CreatePyObject(&ptr_otmacro);
188 }
189