5 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Contributor(s): Campbell Barton
23 * ***** END GPL LICENSE BLOCK *****
26 /* Note, this module is not to be used directly by the user.
27 * Internally its exposed as '_bpy.ops', which provides functions for 'bpy.ops', a python package.
32 #include "bpy_operator.h"
33 #include "bpy_operator_wrap.h"
34 #include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
37 #include "BLI_utildefines.h"
39 #include "RNA_enum_types.h"
44 #include "MEM_guardedalloc.h"
45 #include "BKE_report.h"
46 #include "BKE_context.h"
48 static PyObject *pyop_poll(PyObject *UNUSED(self), PyObject *args)
52 PyObject *context_dict= NULL; /* optional args */
53 PyObject *context_dict_back;
54 char *context_str= NULL;
57 int context= WM_OP_EXEC_DEFAULT;
59 // XXX Todo, work out a better solution for passing on context, could make a tuple from self and pack the name and Context into it...
60 bContext *C= (bContext *)BPy_GetContext();
63 PyErr_SetString(PyExc_RuntimeError, "Context is None, cant poll any operators");
67 if (!PyArg_ParseTuple(args, "s|Os:_bpy.ops.poll", &opname, &context_dict, &context_str))
70 ot= WM_operatortype_find(opname, TRUE);
73 PyErr_Format(PyExc_AttributeError, "Polling operator \"bpy.ops.%s\" error, could not be found", opname);
78 if(RNA_enum_value_from_id(operator_context_items, context_str, &context)==0) {
79 char *enum_str= BPy_enum_as_string(operator_context_items);
80 PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s.poll\" error, expected a string enum in (%.200s)", opname, enum_str);
86 if(context_dict==NULL || context_dict==Py_None) {
89 else if (!PyDict_Check(context_dict)) {
90 PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s.poll\" error, custom context expected a dict or None, got a %.200s", opname, Py_TYPE(context_dict)->tp_name);
94 context_dict_back= CTX_py_dict_get(C);
96 CTX_py_dict_set(C, (void *)context_dict);
97 Py_XINCREF(context_dict); /* so we done loose it */
99 /* main purpose of thsi function */
100 ret= WM_operator_poll_context((bContext*)C, ot, context) ? Py_True : Py_False;
102 /* restore with original context dict, probably NULL but need this for nested operator calls */
103 Py_XDECREF(context_dict);
104 CTX_py_dict_set(C, (void *)context_dict_back);
110 static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args)
115 int operator_ret= OPERATOR_CANCELLED;
118 char *context_str= NULL;
119 PyObject *kw= NULL; /* optional args */
120 PyObject *context_dict= NULL; /* optional args */
121 PyObject *context_dict_back;
123 /* note that context is an int, python does the conversion in this case */
124 int context= WM_OP_EXEC_DEFAULT;
126 // XXX Todo, work out a better solution for passing on context, could make a tuple from self and pack the name and Context into it...
127 bContext *C = (bContext *)BPy_GetContext();
130 PyErr_SetString(PyExc_RuntimeError, "Context is None, cant poll any operators");
134 if (!PyArg_ParseTuple(args, "sO|O!s:_bpy.ops.call", &opname, &context_dict, &PyDict_Type, &kw, &context_str))
137 ot= WM_operatortype_find(opname, TRUE);
140 PyErr_Format(PyExc_AttributeError, "Calling operator \"bpy.ops.%s\" error, could not be found", opname);
144 if(!pyrna_write_check()) {
145 PyErr_Format(PyExc_RuntimeError, "Calling operator \"bpy.ops.%s\" error, can't modify blend data in this state (drawing/rendering)", opname);
150 if(RNA_enum_value_from_id(operator_context_items, context_str, &context)==0) {
151 char *enum_str= BPy_enum_as_string(operator_context_items);
152 PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s\" error, expected a string enum in (%.200s)", opname, enum_str);
158 if(context_dict==NULL || context_dict==Py_None) {
161 else if (!PyDict_Check(context_dict)) {
162 PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s\" error, custom context expected a dict or None, got a %.200s", opname, Py_TYPE(context_dict)->tp_name);
166 context_dict_back= CTX_py_dict_get(C);
168 CTX_py_dict_set(C, (void *)context_dict);
169 Py_XINCREF(context_dict); /* so we done loose it */
171 if(WM_operator_poll_context((bContext*)C, ot, context) == FALSE) {
172 const char *msg= CTX_wm_operator_poll_msg_get(C);
173 PyErr_Format(PyExc_RuntimeError, "Operator bpy.ops.%.200s.poll() %.200s", opname, msg ? msg : "failed, context is incorrect");
174 CTX_wm_operator_poll_msg_set(C, NULL); /* better set to NULL else it could be used again */
178 WM_operator_properties_create_ptr(&ptr, ot);
179 WM_operator_properties_sanitize(&ptr, 0);
181 if(kw && PyDict_Size(kw))
182 error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
188 reports= MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
189 BKE_reports_init(reports, RPT_STORE);
191 operator_ret= WM_operator_call_py(C, ot, context, &ptr, reports);
193 if(BPy_reports_to_error(reports, FALSE))
196 /* operator output is nice to have in the terminal/console too */
197 if(reports->list.first) {
198 char *report_str= BKE_reports_string(reports, 0); /* all reports */
201 PySys_WriteStdout("%s\n", report_str);
202 MEM_freeN(report_str);
206 BKE_reports_clear(reports);
207 if ((reports->flag & RPT_FREE) == 0)
213 WM_operator_properties_free(&ptr);
216 /* if there is some way to know an operator takes args we should use this */
220 PyErr_Format(PyExc_AttributeError, "Operator \"%s\" does not take any args", opname);
224 WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
229 /* restore with original context dict, probably NULL but need this for nested operator calls */
230 Py_XDECREF(context_dict);
231 CTX_py_dict_set(C, (void *)context_dict_back);
237 /* when calling bpy.ops.wm.read_factory_settings() bpy.data's main pointer is freed by clear_globals(),
238 * further access will crash blender. setting context is not needed in this case, only calling because this
239 * function corrects bpy.data (internal Main pointer) */
240 BPY_modules_update(C);
243 /* return operator_ret as a bpy enum */
244 return pyrna_enum_bitfield_to_py(operator_return_items, operator_ret);
248 static PyObject *pyop_as_string(PyObject *UNUSED(self), PyObject *args)
254 PyObject *kw= NULL; /* optional args */
261 bContext *C= (bContext *)BPy_GetContext();
264 PyErr_SetString(PyExc_RuntimeError, "Context is None, cant get the string representation of this object.");
268 if (!PyArg_ParseTuple(args, "s|O!i:_bpy.ops.as_string", &opname, &PyDict_Type, &kw, &all_args))
271 ot= WM_operatortype_find(opname, TRUE);
274 PyErr_Format(PyExc_AttributeError, "_bpy.ops.as_string: operator \"%.200s\"could not be found", opname);
278 /* WM_operator_properties_create(&ptr, opname); */
279 /* Save another lookup */
280 RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
282 if(kw && PyDict_Size(kw))
283 error_val= pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");
286 buf= WM_operator_pystring(C, ot, &ptr, all_args);
288 WM_operator_properties_free(&ptr);
295 pybuf= PyUnicode_FromString(buf);
299 pybuf= PyUnicode_FromString("");
305 static PyObject *pyop_dir(PyObject *UNUSED(self))
307 PyObject *list = PyList_New(0), *name;
310 for(ot= WM_operatortype_first(); ot; ot= ot->next) {
311 name = PyUnicode_FromString(ot->idname);
312 PyList_Append(list, name);
319 static PyObject *pyop_getrna(PyObject *UNUSED(self), PyObject *value)
323 char *opname= _PyUnicode_AsString(value);
324 BPy_StructRNA *pyrna= NULL;
327 PyErr_SetString(PyExc_TypeError, "_bpy.ops.get_rna() expects a string argument");
330 ot= WM_operatortype_find(opname, TRUE);
332 PyErr_Format(PyExc_KeyError, "_bpy.ops.get_rna(\"%s\") not found", opname);
337 //RNA_pointer_create(NULL, &RNA_Struct, ot->srna, &ptr);
339 /* XXX - should call WM_operator_properties_free */
340 WM_operator_properties_create_ptr(&ptr, ot);
341 WM_operator_properties_sanitize(&ptr, 0);
344 pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
345 pyrna->freeptr= TRUE;
346 return (PyObject *)pyrna;
349 static struct PyMethodDef bpy_ops_methods[] = {
350 {"poll", (PyCFunction) pyop_poll, METH_VARARGS, NULL},
351 {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL},
352 {"as_string", (PyCFunction) pyop_as_string, METH_VARARGS, NULL},
353 {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL},
354 {"get_rna", (PyCFunction) pyop_getrna, METH_O, NULL},
355 {"macro_define", (PyCFunction) PYOP_wrap_macro_define, METH_VARARGS, NULL},
356 {NULL, NULL, 0, NULL}
359 static struct PyModuleDef bpy_ops_module = {
360 PyModuleDef_HEAD_INIT,
363 -1,/* multiple "initialization" just copies the module dict. */
365 NULL, NULL, NULL, NULL
368 PyObject *BPY_operator_module(void)
372 submodule= PyModule_Create(&bpy_ops_module);