merge with 2.5 at r20783

This commit is contained in:
2009-06-10 19:15:27 +00:00
831 changed files with 65877 additions and 43636 deletions

View File

@@ -19,6 +19,7 @@
#include "bpy_rna.h"
#include "bpy_operator.h"
#include "bpy_ui.h"
#include "bpy_util.h"
#include "DNA_anim_types.h"
#include "DNA_space_types.h"
@@ -90,9 +91,7 @@ static PyObject *CreateGlobalDictionary( bContext *C )
Py_DECREF(item);
// XXX - evil, need to access context
item = PyCObject_FromVoidPtr( C, NULL );
PyDict_SetItemString( dict, "__bpy_context__", item );
Py_DECREF(item);
BPy_SetContext(C);
// XXX - put somewhere more logical
{
@@ -386,6 +385,8 @@ void BPY_run_ui_scripts(bContext *C, int reload)
PySys_SetObject("path", sys_path_new);
Py_DECREF(sys_path_new);
// XXX - evil, need to access context
BPy_SetContext(C);
while((de = readdir(dir)) != NULL) {
/* We could stat the file but easier just to let python

View File

@@ -41,73 +41,6 @@
extern ListBase global_ops; /* evil, temp use */
/* This function is only used by operators right now
* Its used for taking keyword args and filling in property values */
int PYOP_props_from_dict(PointerRNA *ptr, PyObject *kw)
{
int error_val = 0;
int totkw;
const char *arg_name= NULL;
PyObject *item;
PropertyRNA *prop, *iterprop;
CollectionPropertyIterator iter;
iterprop= RNA_struct_iterator_property(ptr->type);
RNA_property_collection_begin(ptr, iterprop, &iter);
totkw = kw ? PyDict_Size(kw):0;
for(; iter.valid; RNA_property_collection_next(&iter)) {
prop= iter.ptr.data;
arg_name= RNA_property_identifier(prop);
if (strcmp(arg_name, "rna_type")==0) continue;
if (kw==NULL) {
PyErr_Format( PyExc_AttributeError, "no args, expected \"%s\"", arg_name ? arg_name : "<UNKNOWN>");
error_val= -1;
break;
}
item= PyDict_GetItemString(kw, arg_name);
if (item == NULL) {
PyErr_Format( PyExc_AttributeError, "argument \"%s\" missing", arg_name ? arg_name : "<UNKNOWN>");
error_val = -1; /* pyrna_py_to_prop sets the error */
break;
}
if (pyrna_py_to_prop(ptr, prop, NULL, item)) {
error_val= -1;
break;
}
totkw--;
}
RNA_property_collection_end(&iter);
if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(kw, &pos, &key, &value)) {
arg_name= _PyUnicode_AsString(key);
if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
arg_name= NULL;
}
PyErr_Format( PyExc_AttributeError, "argument \"%s\" unrecognized", arg_name ? arg_name : "<UNKNOWN>");
error_val = -1;
}
return error_val;
}
static PyObject *pyop_base_dir(PyObject *self);
static PyObject *pyop_base_rna(PyObject *self, PyObject *pyname);
static struct PyMethodDef pyop_base_methods[] = {
@@ -126,7 +59,7 @@ static PyObject *pyop_base_call( PyObject * self, PyObject * args, PyObject * k
PointerRNA ptr;
// 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 = (bContext *)PyCObject_AsVoidPtr(PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__"));
bContext *C = BPy_GetContext();
char *opname = _PyUnicode_AsString(self);
@@ -148,7 +81,7 @@ static PyObject *pyop_base_call( PyObject * self, PyObject * args, PyObject * k
WM_operator_properties_create(&ptr, opname);
error_val= PYOP_props_from_dict(&ptr, kw);
error_val= pyrna_pydict_to_props(&ptr, kw, "Converting py args to operator properties: ");
if (error_val==0) {
ReportList reports;

View File

@@ -42,7 +42,4 @@ typedef struct {
PyObject *BPY_operator_module(void);
/* fill in properties from a python dict */
int PYOP_props_from_dict(PointerRNA *ptr, PyObject *kw);
#endif

View File

@@ -189,6 +189,8 @@ static int PYTHON_OT_generic(int mode, bContext *C, wmOperator *op, wmEvent *eve
PyObject *args;
PyObject *ret= NULL, *py_class_instance, *item= NULL;
int ret_flag= (mode==PYOP_POLL ? 0:OPERATOR_CANCELLED);
PointerRNA ptr_context;
PyObject *py_context;
PyGILState_STATE gilstate = PyGILState_Ensure();
@@ -233,7 +235,11 @@ static int PYTHON_OT_generic(int mode, bContext *C, wmOperator *op, wmEvent *eve
}
else if (mode==PYOP_EXEC) {
item= PyObject_GetAttrString(py_class, "exec");
args = PyTuple_New(1);
args = PyTuple_New(2);
RNA_pointer_create(NULL, &RNA_Context, C, &ptr_context);
py_context = pyrna_struct_CreatePyObject(&ptr_context);
PyTuple_SET_ITEM(args, 1, py_context);
}
else if (mode==PYOP_POLL) {
item= PyObject_GetAttrString(py_class, "poll");
@@ -272,7 +278,7 @@ static int PYTHON_OT_generic(int mode, bContext *C, wmOperator *op, wmEvent *eve
* thrown away anyway
*
* If we ever want to do this and use the props again,
* it can be done with - PYOP_props_from_dict(op->ptr, kw)
* it can be done with - pyrna_pydict_to_props(op->ptr, kw, "")
*/
Py_DECREF(ret);
@@ -391,7 +397,7 @@ PyObject *PYOP_wrap_add(PyObject *self, PyObject *py_class)
{PYOP_ATTR_UINAME, 's', 0, BPY_CLASS_ATTR_OPTIONAL},
{PYOP_ATTR_PROP, 'l', 0, BPY_CLASS_ATTR_OPTIONAL},
{PYOP_ATTR_DESCRIPTION, 's', 0, BPY_CLASS_ATTR_NONE_OK},
{"exec", 'f', 1, BPY_CLASS_ATTR_OPTIONAL},
{"exec", 'f', 2, BPY_CLASS_ATTR_OPTIONAL},
{"invoke", 'f', 2, BPY_CLASS_ATTR_OPTIONAL},
{"poll", 'f', 2, BPY_CLASS_ATTR_OPTIONAL},
{NULL, 0, 0, 0}

View File

@@ -212,6 +212,71 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
return ret;
}
/* This function is only used by operators right now
* Its used for taking keyword args and filling in property values */
int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const char *error_prefix)
{
int error_val = 0;
int totkw;
const char *arg_name= NULL;
PyObject *item;
PropertyRNA *prop, *iterprop;
CollectionPropertyIterator iter;
iterprop= RNA_struct_iterator_property(ptr->type);
RNA_property_collection_begin(ptr, iterprop, &iter);
totkw = kw ? PyDict_Size(kw):0;
for(; iter.valid; RNA_property_collection_next(&iter)) {
prop= iter.ptr.data;
arg_name= RNA_property_identifier(prop);
if (strcmp(arg_name, "rna_type")==0) continue;
if (kw==NULL) {
PyErr_Format( PyExc_AttributeError, "%s: no keywords, expected \"%s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
error_val= -1;
break;
}
item= PyDict_GetItemString(kw, arg_name);
if (item == NULL) {
PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
error_val = -1; /* pyrna_py_to_prop sets the error */
break;
}
if (pyrna_py_to_prop(ptr, prop, NULL, item)) {
error_val= -1;
break;
}
totkw--;
}
RNA_property_collection_end(&iter);
if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(kw, &pos, &key, &value)) {
arg_name= _PyUnicode_AsString(key);
if (RNA_struct_find_property(ptr, arg_name) == NULL) break;
arg_name= NULL;
}
PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
error_val = -1;
}
return error_val;
}
static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw);
PyObject *pyrna_func_to_py(PointerRNA *ptr, FunctionRNA *func)
@@ -413,9 +478,9 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
}
case PROP_POINTER:
{
StructRNA *ptype= RNA_property_pointer_type(prop);
StructRNA *ptype= RNA_property_pointer_type(ptr, prop);
if(!BPy_StructRNA_Check(value)) {
if(!BPy_StructRNA_Check(value) && value != Py_None) {
PointerRNA tmp;
RNA_pointer_create(NULL, ptype, NULL, &tmp);
PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
@@ -425,19 +490,32 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
int raise_error= 0;
if(data) {
if(ptype == &RNA_AnyType) {
*((PointerRNA*)data)= param->ptr;
if(value == Py_None)
memset(data, 0, sizeof(PointerRNA));
else
*((PointerRNA*)data)= param->ptr;
}
else if(value == Py_None) {
*((void**)data)= NULL;
}
else if(RNA_struct_is_a(param->ptr.type, ptype)) {
*((void**)data)= param->ptr.data;
} else {
}
else {
raise_error= 1;
}
}
else {
/* data==NULL, assign to RNA */
if(RNA_struct_is_a(param->ptr.type, ptype)) {
if(value == Py_None) {
PointerRNA valueptr;
memset(&valueptr, 0, sizeof(valueptr));
RNA_property_pointer_set(ptr, prop, valueptr);
}
else if(RNA_struct_is_a(param->ptr.type, ptype)) {
RNA_property_pointer_set(ptr, prop, param->ptr);
} else {
}
else {
PointerRNA tmp;
RNA_pointer_create(NULL, ptype, NULL, &tmp);
PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
@@ -455,9 +533,36 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
break;
}
case PROP_COLLECTION:
PyErr_SetString(PyExc_AttributeError, "cant convert collections yet");
return -1;
{
int seq_len, i;
PyObject *item;
PointerRNA itemptr;
/* convert a sequence of dict's into a collection */
if(!PySequence_Check(value)) {
PyErr_SetString(PyExc_TypeError, "expected a sequence of dicts for an RNA collection");
return -1;
}
seq_len = PySequence_Length(value);
for(i=0; i<seq_len; i++) {
item= PySequence_GetItem(value, i);
if(item==NULL || PyDict_Check(item)==0) {
PyErr_SetString(PyExc_TypeError, "expected a sequence of dicts for an RNA collection");
Py_XDECREF(item);
return -1;
}
RNA_property_collection_add(ptr, prop, &itemptr);
if(pyrna_pydict_to_props(&itemptr, item, "Converting a python list to an RNA collection")==-1) {
Py_DECREF(item);
return -1;
}
Py_DECREF(item);
}
break;
}
default:
PyErr_SetString(PyExc_AttributeError, "unknown property type (pyrna_py_to_prop)");
return -1;
@@ -1078,7 +1183,7 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
case PROP_POINTER:
{
PointerRNA newptr;
StructRNA *type= RNA_property_pointer_type(prop);
StructRNA *type= RNA_property_pointer_type(ptr, prop);
if(type == &RNA_AnyType) {
/* in this case we get the full ptr */
@@ -1372,7 +1477,14 @@ static void pyrna_subtype_set_rna(PyObject *newclass, StructRNA *srna)
{
PointerRNA ptr;
PyObject *item;
Py_INCREF(newclass);
if (RNA_struct_py_type_get(srna))
PyObSpit("RNA WAS SET - ", RNA_struct_py_type_get(srna));
Py_XDECREF(((PyObject *)RNA_struct_py_type_get(srna)));
RNA_struct_py_type_set(srna, (void *)newclass); /* Store for later use */
/* Not 100% needed but useful,
@@ -1928,6 +2040,10 @@ static int bpy_class_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *par
static void bpy_class_free(void *pyob_ptr)
{
if(G.f&G_DEBUG) {
if(((PyObject *)pyob_ptr)->ob_refcnt > 1)
PyObSpit("zombie class - ref should be 1", (PyObject *)pyob_ptr);
}
Py_DECREF((PyObject *)pyob_ptr);
}
@@ -1952,9 +2068,7 @@ PyObject *pyrna_basetype_register(PyObject *self, PyObject *args)
item= PyObject_GetAttrString(py_class, "__rna__");
if(!item || !BPy_StructRNA_Check(item)) {
if(item) {
Py_DECREF(item);
}
Py_XDECREF(item);
PyErr_SetString(PyExc_AttributeError, "expected a Type subclassed from a registerable rna type (no __rna__ property).");
return NULL;
}
@@ -1977,10 +2091,7 @@ PyObject *pyrna_basetype_register(PyObject *self, PyObject *args)
}
/* get the context, so register callback can do necessary refreshes */
item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__"); /* borrow ref */
if(item)
C= (bContext*)PyCObject_AsVoidPtr(item);
C= BPy_GetContext();
/* call the register callback */
BKE_reports_init(&reports, RPT_PRINT);
@@ -1994,8 +2105,7 @@ PyObject *pyrna_basetype_register(PyObject *self, PyObject *args)
BKE_reports_clear(&reports);
pyrna_subtype_set_rna(py_class, srna);
Py_INCREF(py_class);
pyrna_subtype_set_rna(py_class, srna); /* takes a ref to py_class */
Py_RETURN_NONE;
}
@@ -2040,10 +2150,8 @@ PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args)
}
/* get the context, so register callback can do necessary refreshes */
item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__"); /* borrow ref */
if(item)
C= (bContext*)PyCObject_AsVoidPtr(item);
C= BPy_GetContext();
/* call unregister */
unreg(C, py_srna->ptr.data);

View File

@@ -69,6 +69,7 @@ PyObject *pyrna_prop_CreatePyObject( PointerRNA *ptr, PropertyRNA *prop );
/* operators also need this to set args */
int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value);
int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const char *error_prefix);
PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
/* functions for setting up new props - experemental */

View File

@@ -78,7 +78,7 @@ static PyObject *Method_defButO( PyObject * self, PyObject * args )
/* Optional python doctionary used to set python properties, just like how keyword args are used */
if (py_keywords && PyDict_Size(py_keywords)) {
if (PYOP_props_from_dict(uiButGetOperatorPtrRNA(but), py_keywords) == -1)
if (pyrna_pydict_to_props(uiButGetOperatorPtrRNA(but), py_keywords, "") == -1)
return NULL;
}
@@ -296,24 +296,16 @@ static PyObject *Method_registerKey( PyObject * self, PyObject * args )
/* Optional python doctionary used to set python properties, just like how keyword args are used */
if (py_keywords && PyDict_Size(py_keywords)) {
if (PYOP_props_from_dict(km->ptr, py_keywords) == -1)
if (pyrna_pydict_to_props(km->ptr, py_keywords, "Registering keybinding") == -1)
return NULL;
}
Py_RETURN_NONE;
}
/* internal use only */
static bContext *get_py_context__internal(void)
{
PyObject *globals = PyEval_GetGlobals();
PyObject *val= PyDict_GetItemString(globals, "__bpy_context__"); /* borrow ref */
return PyCObject_AsVoidPtr(val);
}
static PyObject *Method_getRegonPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
ARegion *ar = CTX_wm_region(C);
return PyCObject_FromVoidPtr(ar, NULL);
@@ -321,7 +313,7 @@ static PyObject *Method_getRegonPtr( PyObject * self )
static PyObject *Method_getAreaPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
ScrArea *area = CTX_wm_area(C);
return PyCObject_FromVoidPtr(area, NULL);
@@ -329,7 +321,7 @@ static PyObject *Method_getAreaPtr( PyObject * self )
static PyObject *Method_getScreenPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
bScreen *screen= CTX_wm_screen(C);
return PyCObject_FromVoidPtr(screen, NULL);
@@ -337,7 +329,7 @@ static PyObject *Method_getScreenPtr( PyObject * self )
static PyObject *Method_getSpacePtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
SpaceLink *sl= CTX_wm_space_data(C);
return PyCObject_FromVoidPtr(sl, NULL);
@@ -345,7 +337,7 @@ static PyObject *Method_getSpacePtr( PyObject * self )
static PyObject *Method_getWindowPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
wmWindow *window= CTX_wm_window(C);
return PyCObject_FromVoidPtr(window, NULL);
@@ -395,7 +387,7 @@ PyObject *BPY_ui_module( void )
#if PY_VERSION_HEX >= 0x03000000
submodule= PyModule_Create(&ui_module);
#else /* Py2.x */
submodule= Py_InitModule3( "bpyui", ui_methods, "" );
submodule= Py_InitModule3( "bpy.ui", ui_methods, "" );
#endif
/* uiBlock->flag (controls) */
@@ -406,7 +398,6 @@ PyObject *BPY_ui_module( void )
PyModule_AddObject( mod, "BLOCK_NUMSELECT", PyLong_FromSsize_t(UI_BLOCK_NUMSELECT) );
PyModule_AddObject( mod, "BLOCK_ENTER_OK", PyLong_FromSsize_t(UI_BLOCK_ENTER_OK) );
PyModule_AddObject( mod, "BLOCK_NOSHADOW", PyLong_FromSsize_t(UI_BLOCK_NOSHADOW) );
PyModule_AddObject( mod, "BLOCK_NO_HILITE", PyLong_FromSsize_t(UI_BLOCK_NO_HILITE) );
PyModule_AddObject( mod, "BLOCK_MOVEMOUSE_QUIT", PyLong_FromSsize_t(UI_BLOCK_MOVEMOUSE_QUIT) );
PyModule_AddObject( mod, "BLOCK_KEEP_OPEN", PyLong_FromSsize_t(UI_BLOCK_KEEP_OPEN) );
PyModule_AddObject( mod, "BLOCK_POPUP", PyLong_FromSsize_t(UI_BLOCK_POPUP) );
@@ -567,8 +558,10 @@ PyObject *BPY_ui_module( void )
PyModule_AddObject( mod, "TIME", PyLong_FromSsize_t(SPACE_TIME) );
PyModule_AddObject( mod, "NODE", PyLong_FromSsize_t(SPACE_NODE) );
/* INCREF since its its assumed that all these functions return the
* module with a new ref like PyDict_New, since they are passed to
* PyModule_AddObject which steals a ref */
Py_INCREF(submodule);
return submodule;
}

View File

@@ -29,6 +29,13 @@
#include "MEM_guardedalloc.h"
#include "BKE_report.h"
#include "BKE_context.h"
bContext* __py_context = NULL;
bContext* BPy_GetContext(void) { return __py_context; };
void BPy_SetContext(bContext *C) { __py_context= C; };
PyObject *BPY_flag_to_list(struct BPY_flag_def *flagdef, int flag)
{
PyObject *list = PyList_New(0);
@@ -159,6 +166,8 @@ void PyObSpit(char *name, PyObject *var) {
}
else {
PyObject_Print(var, stderr, 0);
fprintf(stderr, " ref:%d ", var->ob_refcnt);
fprintf(stderr, " ptr:%ld", (long)var);
}
fprintf(stderr, "\n");
}

View File

@@ -74,4 +74,8 @@ char *BPy_enum_as_string(struct EnumPropertyItem *item);
/* error reporting */
int BPy_reports_to_error(struct ReportList *reports);
/* TODO - find a better solution! */
struct bContext *BPy_GetContext(void);
void BPy_SetContext(struct bContext *C);
#endif