python operators (in bpy_opwrapper.*)

This means you can define an operator in python that is called from C or Python - like any other operator.

Python functions for invoke and exec can be registered with an operator name.

keywords are read from the python exec() function, then used to create operator properties. The default python values are used to set the property type and defaults.

def exec(size=2.0, text="blah"): ...

is equivalent to...
prop = RNA_def_property(ot->srna, "size", PROP_FLOAT, PROP_NONE);
RNA_def_property_float_default(prop, 2.0f);

prop = RNA_def_property(ot->srna, "size", PROP_STRING, PROP_NONE);
RNA_def_property_string_default(prop, "blah");


TODO -
* make use of events
* return OPERATOR_CANCELLED/OPERATOR_FINISHED.. etc
* add support for array args
* more testing
This commit is contained in:
2008-12-27 14:52:49 +00:00
parent 86886cbc55
commit 0714d28236
10 changed files with 362 additions and 4 deletions

View File

@@ -24,11 +24,13 @@
*/
#include "bpy_operator.h"
#include "bpy_opwrapper.h"
#include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
#include "bpy_compat.h"
//#include "blendef.h"
#include "BLI_dynstr.h"
#include "WM_api.h"
#include "WM_types.h"
@@ -40,6 +42,20 @@ extern ListBase global_ops; /* evil, temp use */
/* floats bigger then this are displayed as inf in the docstrings */
#define MAXFLOAT_DOC 10000000
#if 0
void PyObSpit(char *name, PyObject *var) {
fprintf(stderr, "<%s> : ", name);
if (var==NULL) {
fprintf(stderr, "<NIL>");
}
else {
PyObject_Print(var, stderr, 0);
}
fprintf(stderr, "\n");
}
#endif
static int pyop_func_compare( BPy_OperatorFunc * a, BPy_OperatorFunc * b )
{
return (strcmp(a->name, b->name)==0) ? 0 : -1;
@@ -56,6 +72,7 @@ static PyObject *pyop_func_repr( BPy_OperatorFunc * self )
return PyUnicode_FromFormat( "[BPy_OperatorFunc \"%s\"]", self->name);
}
//---------------getattr--------------------------------------------
static PyObject *pyop_base_getattro( BPy_OperatorBase * self, PyObject *pyname )
{
@@ -73,7 +90,17 @@ static PyObject *pyop_base_getattro( BPy_OperatorBase * self, PyObject *pyname )
PyList_Append(ret, item);
Py_DECREF(item);
}
} else {
item = PyUnicode_FromString("add"); PyList_Append(ret, item); Py_DECREF(item);
item = PyUnicode_FromString("remove"); PyList_Append(ret, item); Py_DECREF(item);
}
else if ( strcmp( name, "add" ) == 0 ) {
ret= PYOP_wrap_add_func();
}
else if ( strcmp( name, "remove" ) == 0 ) {
ret= PYOP_wrap_remove_func();
}
else {
ot = WM_operatortype_find(name);
if (ot) {