[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19198] branches/blender2.5/blender/source /blender/python: Make RNA an Operator dir() work in py 2.5 - 3.0
Campbell Barton
ideasman42 at gmail.com
Thu Mar 5 13:09:30 CET 2009
Revision: 19198
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19198
Author: campbellbarton
Date: 2009-03-05 13:09:30 +0100 (Thu, 05 Mar 2009)
Log Message:
-----------
Make RNA an Operator dir() work in py 2.5 - 3.0
removed epy docstrings from RNA python api, since Python can get this info from rna. (could be redone in python if getting doc's on RNA is needed)
epy_doc_gen works again
Modified Paths:
--------------
branches/blender2.5/blender/source/blender/python/epy_doc_gen.py
branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c
branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
Modified: branches/blender2.5/blender/source/blender/python/epy_doc_gen.py
===================================================================
--- branches/blender2.5/blender/source/blender/python/epy_doc_gen.py 2009-03-05 09:50:16 UTC (rev 19197)
+++ branches/blender2.5/blender/source/blender/python/epy_doc_gen.py 2009-03-05 12:09:30 UTC (rev 19198)
@@ -121,7 +121,14 @@
try: return rna_struct.base.identifier
except: return '' # invalid id
- structs = [(base_id(rna_struct), rna_struct.identifier, rna_struct) for rna_struct in bpydoc.structs.values()]
+ #structs = [(base_id(rna_struct), rna_struct.identifier, rna_struct) for rna_struct in bpydoc.structs.values()]
+
+ structs = []
+ for rna_struct in bpydoc.structs.values():
+ structs.append( (base_id(rna_struct), rna_struct.identifier, rna_struct) )
+
+
+
structs.sort() # not needed but speeds up sort below, setting items without an inheritance first
# Arrange so classes are always defined in the correct order
@@ -173,12 +180,13 @@
operators = dir(bpyoperator)
operators.remove('add')
operators.remove('remove')
- operators.remove('__dir__')
operators.sort()
for op in operators:
+ if op.startswith('__'):
+ continue
# Keyword attributes
kw_args = [] # "foo = 1", "bar=0.5", "spam='ENUM'"
Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c 2009-03-05 09:50:16 UTC (rev 19197)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c 2009-03-05 12:09:30 UTC (rev 19198)
@@ -116,7 +116,7 @@
}
/* For some reason python3 needs these :/ */
-static PyObject *pyop_func_richcmp(BPy_StructRNA * a, BPy_StructRNA * b, int op)
+static PyObject *pyop_func_richcmp(BPy_OperatorFunc * a, BPy_OperatorFunc * b, int op)
{
int cmp_result= -1; /* assume false */
if (BPy_OperatorFunc_Check(a) && BPy_OperatorFunc_Check(b)) {
@@ -138,6 +138,11 @@
return PyUnicode_FromFormat( "[BPy_OperatorFunc \"%s\"]", self->name);
}
+static struct PyMethodDef pyop_base_methods[] = {
+ {"add", (PyCFunction)PYOP_wrap_add, METH_VARARGS, ""},
+ {"remove", (PyCFunction)PYOP_wrap_remove, METH_VARARGS, ""},
+ {NULL, NULL, 0, NULL}
+};
//---------------getattr--------------------------------------------
static PyObject *pyop_base_getattro( BPy_OperatorBase * self, PyObject *pyname )
@@ -149,6 +154,20 @@
if ((ot = WM_operatortype_find(name))) {
ret= pyop_func_CreatePyObject(self->C, name);
}
+ else if (strcmp(name, "__dict__")==0) {
+ ret = PyDict_New();
+
+ wmOperatorType *ot;
+ PyMethodDef *meth;
+
+ for(ot= WM_operatortype_first(); ot; ot= ot->next) {
+ PyDict_SetItemString(ret, ot->idname, Py_None);
+ }
+
+ for(meth=pyop_base_methods; meth->ml_name; meth++) {
+ PyDict_SetItemString(ret, meth->ml_name, Py_None);
+ }
+ }
else if ((ret = PyObject_GenericGetAttr((PyObject *)self, pyname))) {
/* do nothing, this accounts for methoddef's add and remove */
}
@@ -156,7 +175,7 @@
PyErr_Format( PyExc_AttributeError, "Operator \"%s\" not found", name);
ret= NULL;
}
-
+
return ret;
}
@@ -256,37 +275,6 @@
Py_RETURN_NONE;
}
-PyObject *pyop_base_dir(PyObject *self);
-
-static struct PyMethodDef pyop_base_methods[] = {
- {"add", (PyCFunction)PYOP_wrap_add, METH_VARARGS, ""},
- {"remove", (PyCFunction)PYOP_wrap_remove, METH_VARARGS, ""},
- {"__dir__", (PyCFunction)pyop_base_dir, METH_NOARGS, ""},
- {NULL, NULL, 0, NULL}
-};
-
-PyObject *pyop_base_dir(PyObject *self)
-{
- PyObject *ret = PyList_New(0);
- PyObject *item;
- wmOperatorType *ot;
- PyMethodDef *meth;
-
- for(ot= WM_operatortype_first(); ot; ot= ot->next) {
- item = PyUnicode_FromString( ot->idname );
- PyList_Append(ret, item);
- Py_DECREF(item);
- }
-
- for(meth=pyop_base_methods; meth->ml_name; meth++) {
- item = PyUnicode_FromString( meth->ml_name );
- PyList_Append(ret, item);
- Py_DECREF(item);
- }
-
- return ret;
-}
-
/*-----------------------BPy_OperatorBase method def------------------------------*/
PyTypeObject pyop_base_Type = {
#if (PY_VERSION_HEX >= 0x02060000)
Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c 2009-03-05 09:50:16 UTC (rev 19197)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c 2009-03-05 12:09:30 UTC (rev 19198)
@@ -172,8 +172,7 @@
break;
}
case PROP_COLLECTION:
- PyErr_SetString(PyExc_AttributeError, "RNA Anomaly: Collection of length 0?");
- ret = NULL; /*pyrna_prop_CreatePyObject(ptr, prop);*/
+ ret = pyrna_prop_CreatePyObject(ptr, prop);
break;
default:
PyErr_Format(PyExc_AttributeError, "RNA Error: unknown type \"%d\" (pyrna_prop_to_py)", type);
@@ -550,7 +549,7 @@
if (keynum >= len){
PyErr_SetString(PyExc_AttributeError, "index out of range");
ret = -1;
- } else { /* not an array*/
+ } else {
ret = pyrna_py_to_prop_index(&self->ptr, self->prop, keynum, value);
}
}
@@ -566,138 +565,6 @@
( objobjargproc ) pyrna_prop_assign_subscript, /* mp_ass_subscript */
};
-
-PyObject *pyrna_struct_to_docstring(BPy_StructRNA *self)
-{
- PyObject *ret;
- PropertyRNA *prop;
-
- DynStr *dynstr;
- const char *identifier;
- const char *desc;
- char *readonly;
- char *result;
- int len;
- int i; /* general iter */
-
- dynstr= BLI_dynstr_new();
- BLI_dynstr_appendf(dynstr, "RNA %s: %s\n", RNA_struct_identifier(&self->ptr), RNA_struct_ui_name(&self->ptr));
-
- /* Add EPI ===='s */
- i = BLI_dynstr_get_len(dynstr);
- while (--i)
- BLI_dynstr_append(dynstr, "=");
-
- BLI_dynstr_append(dynstr, "\n");
- /* done */
-
- {
- PropertyRNA *iterprop;
- CollectionPropertyIterator iter;
-
- iterprop= RNA_struct_iterator_property(&self->ptr);
- RNA_property_collection_begin(&self->ptr, iterprop, &iter);
-
- for(; iter.valid; RNA_property_collection_next(&iter)) {
- prop = iter.ptr.data;
- identifier = RNA_property_identifier(&iter.ptr, prop);
- desc = RNA_property_ui_description(&iter.ptr, prop);
-
- readonly = (RNA_property_editable(&self->ptr, prop)) ? "" : " *readonly*";
- len = RNA_property_array_length(&iter.ptr, prop);
-
- switch(RNA_property_type(&iter.ptr, prop)) {
- case PROP_BOOLEAN:
- {
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s%s\n", identifier, desc, readonly);
-
- if (len==0) BLI_dynstr_appendf(dynstr, "@type %s: bool\n", identifier);
- else BLI_dynstr_appendf(dynstr, "@type %s: bool[%d]\n", identifier, len);
- break;
- }
- case PROP_INT:
- {
- int hardmin, hardmax;
- RNA_property_int_range(&iter.ptr, prop, &hardmin, &hardmax);
-
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s in (%d, %d)%s\n", identifier, desc, hardmin, hardmax, readonly);
-
- if (len==0) BLI_dynstr_appendf(dynstr, "@type %s: int\n", identifier);
- else BLI_dynstr_appendf(dynstr, "@type %s: int[%d]\n", identifier, len);
- break;
- }
- case PROP_FLOAT:
- {
- float hardmin, hardmax;
- RNA_property_float_range(&iter.ptr, prop, &hardmin, &hardmax);
-
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s in (", identifier, desc);
-
- if (hardmin < -MAXFLOAT_DOC)BLI_dynstr_append(dynstr, "-inf, ");
- else BLI_dynstr_appendf(dynstr, "%.3f, ", hardmin);
-
- if (hardmax > MAXFLOAT_DOC)BLI_dynstr_append(dynstr, "inf");
- else BLI_dynstr_appendf(dynstr, "%.3f", hardmax);
-
- BLI_dynstr_appendf(dynstr, ")%s\n", readonly);
-
-
- if (len==0) BLI_dynstr_appendf(dynstr, "@type %s: float\n", identifier);
- else BLI_dynstr_appendf(dynstr, "@type %s: float[%d]\n", identifier, len);
- break;
- }
- case PROP_STRING:
- {
- int maxlen = RNA_property_string_maxlength(&iter.ptr, prop);
-
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s (%d maximum length)%s\n", identifier, desc, maxlen, readonly);
- BLI_dynstr_appendf(dynstr, "@type %s: string\n", identifier);
- break;
- }
- case PROP_ENUM:
- {
- char *enum_str= pyrna_enum_as_string(&iter.ptr, prop);
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s%s\n", identifier, desc, readonly);
- BLI_dynstr_appendf(dynstr, "@type %s: enum in [%s]\n", identifier, enum_str);
- MEM_freeN(enum_str);
- break;
- }
- case PROP_POINTER:
- {
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s%s\n", identifier, desc, readonly);
-
- // TODO - why does this crash sometimes
- // PointerRNA newptr;
- // newptr= RNA_property_pointer_get(&iter.ptr, prop);
-
- // Use this instead, its not that useful
- BLI_dynstr_appendf(dynstr, "@type %s: PyRNA %s\n", identifier, RNA_struct_identifier(&iter.ptr));
- break;
- }
- case PROP_COLLECTION:
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s%s\n", identifier, desc, readonly);
- BLI_dynstr_appendf(dynstr, "@type %s: PyRNA Collection\n", identifier);
- break;
- default:
- BLI_dynstr_appendf(dynstr, "@ivar %s: %s%s\n", identifier, desc, readonly);
- BLI_dynstr_appendf(dynstr, "@type %s: <unknown>\n", identifier);
- break;
- }
- }
-
- RNA_property_collection_end(&iter);
- }
-
- result= BLI_dynstr_get_cstring(dynstr);
- BLI_dynstr_free(dynstr);
-
- ret = PyUnicode_FromString(result);
- MEM_freeN(result);
-
- return ret;
-}
-
-
//---------------getattr--------------------------------------------
static PyObject *pyrna_struct_getattro( BPy_StructRNA * self, PyObject *pyname )
{
@@ -712,19 +579,38 @@
if (ret) return ret;
else PyErr_Clear();
/* done with subtypes */
+
+ prop = RNA_struct_find_property(&self->ptr, name);
- if ( strcmp( name, "__doc__" ) == 0 ) {
- ret = pyrna_struct_to_docstring(self);
- } else {
- prop = RNA_struct_find_property(&self->ptr, name);
-
- if (prop==NULL) {
- PyErr_Format( PyExc_AttributeError, "Attribute \"%s\" not found", name);
- ret = NULL;
- } else {
- ret = pyrna_prop_to_py(&self->ptr, prop);
+ if (prop) {
+ ret = pyrna_prop_to_py(&self->ptr, prop);
+ }
+ else if (strcmp(name, "__dict__")==0) { /* Not quite correct, adding this so dir() gives good feedback */
+ PropertyRNA *prop, *iterprop, *nameprop;
+ CollectionPropertyIterator iter;
+ char name[256], *nameptr;
+
+ iterprop= RNA_struct_iterator_property(&self->ptr);
+ RNA_property_collection_begin(&self->ptr, iterprop, &iter);
+
+ ret = PyDict_New();
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list