Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2010-09-01 15:16:11 +0400
committerCampbell Barton <ideasman42@gmail.com>2010-09-01 15:16:11 +0400
commit7532bc23254b6efbee26f69b02da04d66b32c0bb (patch)
treeb4b2d582d124a09577492a29c70f4ea1014d7754 /source/blender/python/intern/bpy_operator.c
parent4a427fa4ff7cc2bebbbe9b9be3d89e947752fc29 (diff)
poll function for py api operator access
eg: if bpy.ops.object.mode_set.poll(): ...
Diffstat (limited to 'source/blender/python/intern/bpy_operator.c')
-rw-r--r--source/blender/python/intern/bpy_operator.c53
1 files changed, 47 insertions, 6 deletions
diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c
index c6c34fbcaf5..1e7ace3b237 100644
--- a/source/blender/python/intern/bpy_operator.c
+++ b/source/blender/python/intern/bpy_operator.c
@@ -40,6 +40,45 @@
#include "MEM_guardedalloc.h"
#include "BKE_report.h"
+static PyObject *pyop_poll( PyObject * self, PyObject * args)
+{
+ wmOperatorType *ot;
+ char *opname;
+ PyObject *context_dict= NULL; /* optional args */
+ PyObject *context_dict_back;
+ PyObject *ret;
+
+ // 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...
+ bContext *C = BPy_GetContext();
+
+ if (!PyArg_ParseTuple(args, "s|O:_bpy.ops.poll", &opname, &context_dict))
+ return NULL;
+
+ ot= WM_operatortype_find(opname, TRUE);
+
+ if (ot == NULL) {
+ PyErr_Format(PyExc_SystemError, "Polling operator \"bpy.ops.%s\" error, could not be found", opname);
+ return NULL;
+ }
+
+ if(!PyDict_Check(context_dict))
+ context_dict= NULL;
+
+ context_dict_back= CTX_py_dict_get(C);
+
+ CTX_py_dict_set(C, (void *)context_dict);
+ Py_XINCREF(context_dict); /* so we done loose it */
+
+ /* main purpose of thsi function */
+ ret= WM_operator_poll((bContext*)C, ot) ? Py_True : Py_False;
+
+ /* restore with original context dict, probably NULL but need this for nested operator calls */
+ Py_XDECREF(context_dict);
+ CTX_py_dict_set(C, (void *)context_dict_back);
+
+ Py_INCREF(ret);
+ return ret;
+}
static PyObject *pyop_call( PyObject * self, PyObject * args)
{
@@ -66,7 +105,7 @@ static PyObject *pyop_call( PyObject * self, PyObject * args)
ot= WM_operatortype_find(opname, TRUE);
if (ot == NULL) {
- PyErr_Format( PyExc_SystemError, "Calling operator \"bpy.ops.%s\" error, could not be found", opname);
+ PyErr_Format(PyExc_SystemError, "Calling operator \"bpy.ops.%s\" error, could not be found", opname);
return NULL;
}
@@ -252,6 +291,7 @@ static PyObject *pyop_getrna(PyObject *self, PyObject *value)
PyObject *BPY_operator_module( void )
{
+ static PyMethodDef pyop_poll_meth = {"poll", (PyCFunction) pyop_poll, METH_VARARGS, NULL};
static PyMethodDef pyop_call_meth = {"call", (PyCFunction) pyop_call, METH_VARARGS, NULL};
static PyMethodDef pyop_as_string_meth ={"as_string", (PyCFunction) pyop_as_string, METH_VARARGS, NULL};
static PyMethodDef pyop_dir_meth = {"dir", (PyCFunction) pyop_dir, METH_NOARGS, NULL};
@@ -261,11 +301,12 @@ PyObject *BPY_operator_module( void )
PyObject *submodule = PyModule_New("_bpy.ops");
PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy.ops", submodule);
- PyModule_AddObject( submodule, "call", PyCFunction_New(&pyop_call_meth, NULL) );
- PyModule_AddObject( submodule, "as_string",PyCFunction_New(&pyop_as_string_meth,NULL) );
- PyModule_AddObject( submodule, "dir", PyCFunction_New(&pyop_dir_meth, NULL) );
- PyModule_AddObject( submodule, "get_rna", PyCFunction_New(&pyop_getrna_meth, NULL) );
- PyModule_AddObject( submodule, "macro_define",PyCFunction_New(&pyop_macro_def_meth, NULL) );
+ PyModule_AddObject( submodule, "poll", PyCFunction_New(&pyop_poll_meth, NULL) );
+ PyModule_AddObject( submodule, "call", PyCFunction_New(&pyop_call_meth, NULL) );
+ PyModule_AddObject( submodule, "as_string",PyCFunction_New(&pyop_as_string_meth, NULL) );
+ PyModule_AddObject( submodule, "dir", PyCFunction_New(&pyop_dir_meth, NULL) );
+ PyModule_AddObject( submodule, "get_rna", PyCFunction_New(&pyop_getrna_meth, NULL) );
+ PyModule_AddObject( submodule, "macro_define",PyCFunction_New(&pyop_macro_def_meth, NULL) );
return submodule;
}