py/rna internals

- rna internal deferred properties now store the functions as PyObjects rather then C function pointers
- Property functions now allow the first non keyword argument to be a class.
This commit is contained in:
Campbell Barton 2010-09-08 10:43:36 +00:00
parent 6d82951947
commit 128bd96c01
2 changed files with 112 additions and 128 deletions

@ -77,16 +77,36 @@ EnumPropertyItem property_subtype_array_items[] = {
{PROP_NONE, "NONE", 0, "None", ""},
{0, NULL, 0, NULL, NULL}};
/* PyObject's */
static PyObject *pymeth_BoolProperty = NULL;
static PyObject *pymeth_BoolVectorProperty = NULL;
static PyObject *pymeth_IntProperty = NULL;
static PyObject *pymeth_IntVectorProperty = NULL;
static PyObject *pymeth_FloatProperty = NULL;
static PyObject *pymeth_FloatVectorProperty = NULL;
static PyObject *pymeth_StringProperty = NULL;
static PyObject *pymeth_EnumProperty = NULL;
static PyObject *pymeth_PointerProperty = NULL;
static PyObject *pymeth_CollectionProperty = NULL;
static PyObject *pymeth_RemoveProperty = NULL;
/* operators use this so it can store the args given but defer running
* it until the operator runs where these values are used to setup the
* default args for that operator instance */
static PyObject *bpy_prop_deferred_return(void *func, PyObject *kw)
static PyObject *bpy_prop_deferred_return(PyObject *func, PyObject *kw)
{
PyObject *ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, PyCapsule_New(func, NULL, NULL));
if(kw==NULL) kw= PyDict_New();
else Py_INCREF(kw);
PyTuple_SET_ITEM(ret, 0, func);
Py_INCREF(func);
if(kw==NULL)
kw= PyDict_New();
else
Py_INCREF(kw);
PyTuple_SET_ITEM(ret, 1, kw);
return ret;
}
@ -95,14 +115,20 @@ static PyObject *bpy_prop_deferred_return(void *func, PyObject *kw)
PyObject *ret; \
self= PyTuple_GET_ITEM(args, 0); \
args= PyTuple_New(0); \
ret= _func(self, args, kw); \
ret= BPy_##_func(self, args, kw); \
Py_DECREF(args); \
return ret; \
} \
if (PyTuple_GET_SIZE(args) > 0) { \
else if (PyTuple_GET_SIZE(args) > 1) { \
PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
return NULL; \
} \
srna= srna_from_self(self, "##_func(...):"); \
if(srna==NULL) { \
if(PyErr_Occurred()) \
return NULL; \
return bpy_prop_deferred_return((void *)pymeth_##_func, kw); \
} \
#if 0
@ -131,13 +157,9 @@ PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_BoolProperty)
BPY_PROPDEF_HEAD(BoolProperty)
srna= srna_from_self(self, "BoolProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static char *kwlist[] = {"attr", "name", "description", "default", "options", "subtype", NULL};
char *id=NULL, *name="", *description="";
int def=0;
@ -173,12 +195,10 @@ PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_BoolProperty, kw);
}
}
char BPy_BoolVectorProperty_doc[] =
".. function:: BoolVectorProperty(name=\"\", description=\"\", default=(False, False, False), options={'ANIMATABLE'}, subtype='NONE', size=3)\n"
@ -193,13 +213,9 @@ PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_BoolVectorProperty)
BPY_PROPDEF_HEAD(BoolVectorProperty)
srna= srna_from_self(self, "BoolVectorProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "name", "description", "default", "options", "subtype", "size", NULL};
char *id=NULL, *name="", *description="";
int def[PYRNA_STACK_ARRAY]={0};
@ -246,12 +262,10 @@ PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_BoolVectorProperty, kw);
}
}
char BPy_IntProperty_doc[] =
".. function:: IntProperty(name=\"\", description=\"\", default=0, min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, step=1, options={'ANIMATABLE'}, subtype='NONE')\n"
@ -266,13 +280,9 @@ PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_IntProperty)
BPY_PROPDEF_HEAD(IntProperty)
srna= srna_from_self(self, "IntProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", NULL};
char *id=NULL, *name="", *description="";
int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0;
@ -309,12 +319,9 @@ PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_IntProperty, kw);
}
}
char BPy_IntVectorProperty_doc[] =
".. function:: IntVectorProperty(name=\"\", description=\"\", default=(0, 0, 0), min=-sys.maxint, max=sys.maxint, soft_min=-sys.maxint, soft_max=sys.maxint, options={'ANIMATABLE'}, subtype='NONE', size=3)\n"
@ -329,13 +336,9 @@ PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_IntVectorProperty)
BPY_PROPDEF_HEAD(IntVectorProperty)
srna= srna_from_self(self, "IntVectorProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "options", "subtype", "size", NULL};
char *id=NULL, *name="", *description="";
int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def[PYRNA_STACK_ARRAY]={0};
@ -383,12 +386,9 @@ PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_IntVectorProperty, kw);
}
}
char BPy_FloatProperty_doc[] =
@ -406,13 +406,9 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_FloatProperty)
BPY_PROPDEF_HEAD(FloatProperty)
srna= srna_from_self(self, "FloatProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", NULL};
char *id=NULL, *name="", *description="";
float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f;
@ -457,12 +453,9 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_FloatProperty, kw);
}
}
char BPy_FloatVectorProperty_doc[] =
".. function:: FloatVectorProperty(name=\"\", description=\"\", default=(0.0, 0.0, 0.0), min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE', size=3)\n"
@ -477,13 +470,9 @@ PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_FloatVectorProperty)
BPY_PROPDEF_HEAD(FloatVectorProperty)
srna= srna_from_self(self, "FloatVectorProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "size", NULL};
char *id=NULL, *name="", *description="";
float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def[PYRNA_STACK_ARRAY]={0.0f};
@ -531,12 +520,9 @@ PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_FloatVectorProperty, kw);
}
}
char BPy_StringProperty_doc[] =
".. function:: StringProperty(name=\"\", description=\"\", default=\"\", maxlen=0, options={'ANIMATABLE'}, subtype='NONE')\n"
@ -551,13 +537,9 @@ PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_StringProperty)
BPY_PROPDEF_HEAD(StringProperty)
srna= srna_from_self(self, "StringProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "name", "description", "default", "maxlen", "options", "subtype", NULL};
char *id=NULL, *name="", *description="", *def="";
int maxlen=0;
@ -593,12 +575,9 @@ PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_StringProperty, kw);
}
}
static EnumPropertyItem *enum_items_from_py(PyObject *value, const char *def, int *defvalue)
{
@ -659,13 +638,9 @@ PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_EnumProperty)
BPY_PROPDEF_HEAD(EnumProperty)
srna= srna_from_self(self, "EnumProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "items", "name", "description", "default", "options", NULL};
char *id=NULL, *name="", *description="", *def="";
int defvalue=0;
@ -697,13 +672,9 @@ PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw)
}
RNA_def_property_duplicate_pointers(srna, prop);
MEM_freeN(eitems);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_EnumProperty, kw);
}
}
static StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix)
{
@ -740,13 +711,9 @@ PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_PointerProperty)
BPY_PROPDEF_HEAD(PointerProperty)
srna= srna_from_self(self, "PointerProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
char *id=NULL, *name="", *description="";
PropertyRNA *prop;
@ -776,13 +743,9 @@ PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_PointerProperty, kw);
}
return NULL;
}
char BPy_CollectionProperty_doc[] =
".. function:: CollectionProperty(items, type=\"\", description=\"\", default=\"\", options={'ANIMATABLE'})\n"
@ -797,13 +760,9 @@ PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
BPY_PROPDEF_HEAD(BPy_CollectionProperty)
BPY_PROPDEF_HEAD(CollectionProperty)
srna= srna_from_self(self, "CollectionProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
}
else if(srna) {
if(srna) {
static const char *kwlist[] = {"attr", "type", "name", "description", "options", NULL};
char *id=NULL, *name="", *description="";
PropertyRNA *prop;
@ -833,13 +792,9 @@ PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw)
if((opts & PROP_ANIMATABLE)==0) RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
}
RNA_def_property_duplicate_pointers(srna, prop);
}
Py_RETURN_NONE;
}
else { /* operators defer running this function */
return bpy_prop_deferred_return((void *)BPy_CollectionProperty, kw);
}
return NULL;
}
char BPy_RemoveProperty_doc[] =
".. function:: RemoveProperty(attr)\n"
@ -852,6 +807,19 @@ PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
{
StructRNA *srna;
if(PyTuple_GET_SIZE(args) == 1) {
PyObject *ret;
self= PyTuple_GET_ITEM(args, 0);
args= PyTuple_New(0);
ret= BPy_RemoveProperty(self, args, kw);
Py_DECREF(args);
return ret;
}
else if (PyTuple_GET_SIZE(args) > 1) {
PyErr_SetString(PyExc_ValueError, "all args must be keywords"); \
return NULL;
}
srna= srna_from_self(self, "RemoveProperty(...):");
if(srna==NULL && PyErr_Occurred()) {
return NULL; /* self's type was compatible but error getting the srna */
@ -872,9 +840,8 @@ PyObject *BPy_RemoveProperty(PyObject *self, PyObject *args, PyObject *kw)
PyErr_Format(PyExc_TypeError, "RemoveProperty(): '%s' not a defined dynamic property.", id);
return NULL;
}
Py_RETURN_NONE;
}
Py_RETURN_NONE;
}
static struct PyMethodDef props_methods[] = {
@ -890,7 +857,7 @@ static struct PyMethodDef props_methods[] = {
{"CollectionProperty", (PyCFunction)BPy_CollectionProperty, METH_VARARGS|METH_KEYWORDS, BPy_CollectionProperty_doc},
/* only useful as a bpy_struct method */
/* {"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc}, */
{"RemoveProperty", (PyCFunction)BPy_RemoveProperty, METH_VARARGS|METH_KEYWORDS, BPy_RemoveProperty_doc},
{NULL, NULL, 0, NULL}
};
@ -907,6 +874,8 @@ static struct PyModuleDef props_module = {
PyObject *BPY_rna_props( void )
{
PyObject *submodule;
PyObject *submodule_dict;
submodule= PyModule_Create(&props_module);
PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
@ -915,5 +884,22 @@ PyObject *BPY_rna_props( void )
* PyModule_AddObject which steals a ref */
Py_INCREF(submodule);
/* api needs the PyObjects internally */
submodule_dict= PyModule_GetDict(submodule);
#define ASSIGN_STATIC(_name) pymeth_##_name = PyDict_GetItemString(submodule_dict, #_name)
ASSIGN_STATIC(BoolProperty);
ASSIGN_STATIC(BoolVectorProperty);
ASSIGN_STATIC(IntProperty);
ASSIGN_STATIC(IntVectorProperty);
ASSIGN_STATIC(FloatProperty);
ASSIGN_STATIC(FloatVectorProperty);
ASSIGN_STATIC(StringProperty);
ASSIGN_STATIC(EnumProperty);
ASSIGN_STATIC(PointerProperty);
ASSIGN_STATIC(CollectionProperty);
ASSIGN_STATIC(RemoveProperty);
return submodule;
}

@ -2911,7 +2911,7 @@ static char pyrna_struct_as_pointer_doc[] =
"\n"
" Returns the memory address which holds a pointer to blenders internal data\n"
"\n"
" :return: capsule with a name set from the struct type.\n"
" :return: int (memory address).\n"
" :rtype: int\n"
"\n"
" .. note:: This is intended only for advanced script writers who need to pass blender data to their own C/Python modules.\n";
@ -4604,29 +4604,32 @@ StructRNA *srna_from_self(PyObject *self, const char *error_prefix)
return pyrna_struct_as_srna(self, 0, error_prefix);
}
static int deferred_register_prop(StructRNA *srna, PyObject *item, PyObject *key, PyObject *dummy_args)
static int deferred_register_prop(StructRNA *srna, PyObject *item, PyObject *key)
{
/* We only care about results from C which
* are for sure types, save some time with error */
if(PyTuple_CheckExact(item) && PyTuple_GET_SIZE(item)==2) {
PyObject *py_func_ptr, *py_kw, *py_srna_cobject, *py_ret;
PyObject *(*pyfunc)(PyObject *, PyObject *, PyObject *);
PyObject *py_func, *py_kw, *py_srna_cobject, *py_ret;
if(PyArg_ParseTuple(item, "O!O!", &PyCapsule_Type, &py_func_ptr, &PyDict_Type, &py_kw)) {
if(PyArg_ParseTuple(item, "OO!", &py_func, &PyDict_Type, &py_kw)) {
PyObject *args_fake;
if(*_PyUnicode_AsString(key)=='_') {
PyErr_Format(PyExc_ValueError, "bpy_struct \"%.200s\" registration error: %.200s could not register because the property starts with an '_'\n", RNA_struct_identifier(srna), _PyUnicode_AsString(key));
return -1;
}
pyfunc = PyCapsule_GetPointer(py_func_ptr, NULL);
py_srna_cobject = PyCapsule_New(srna, NULL, NULL);
/* not 100% nice :/, modifies the dict passed, should be ok */
PyDict_SetItemString(py_kw, "attr", key);
py_ret = pyfunc(py_srna_cobject, dummy_args, py_kw);
Py_DECREF(py_srna_cobject);
args_fake= PyTuple_New(1);
PyTuple_SET_ITEM(args_fake, 0, py_srna_cobject);
py_ret = PyObject_Call(py_func, args_fake, py_kw);
Py_DECREF(args_fake); /* free's py_srna_cobject too */
if(py_ret) {
Py_DECREF(py_ret);
@ -4657,12 +4660,9 @@ static int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
{
PyObject *item, *key;
PyObject *order;
PyObject *dummy_args;
Py_ssize_t pos = 0;
int ret;
dummy_args = PyTuple_New(0);
if( !PyDict_CheckExact(class_dict) &&
(order= PyDict_GetItemString(class_dict, "order")) &&
PyList_CheckExact(order)
@ -4670,22 +4670,20 @@ static int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
for(pos= 0; pos<PyList_GET_SIZE(order); pos++) {
key= PyList_GET_ITEM(order, pos);
item= PyDict_GetItem(class_dict, key);
ret= deferred_register_prop(srna, item, key, dummy_args);
ret= deferred_register_prop(srna, item, key);
if(ret==-1)
break;
}
}
else {
while (PyDict_Next(class_dict, &pos, &key, &item)) {
ret= deferred_register_prop(srna, item, key, dummy_args);
ret= deferred_register_prop(srna, item, key);
if(ret==-1)
break;
}
}
Py_DECREF(dummy_args);
return 0;
}