Merge with 2.5 -r 22173:22620.

This commit is contained in:
2009-08-19 09:04:49 +00:00
885 changed files with 34624 additions and 23445 deletions

View File

@@ -43,7 +43,7 @@ CPPFLAGS += -I$(NAN_PYTHON)/include/python$(NAN_PYTHON_VERSION)
# PreProcessor stuff
CPPFLAGS += -I$(NAN_GHOST)/include
CPPFLAGS += -I$(NAN_SOUNDSYSTEM)/include $(NAN_SDLCFLAGS)
CPPFLAGS += $(NAN_SDLCFLAGS)
# modules
CPPFLAGS += -I../../editors/include

View File

@@ -1,126 +0,0 @@
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Contributor(s): Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*/
/* This file is only to contain definitions to functions that enable
* the python api to compile with different python versions.
* no utility functions please
*/
#ifndef BPY_COMPAT_H__
#define BPY_COMPAT_H__
/* if you are NOT using python 3.0 - define these */
#if PY_VERSION_HEX < 0x03000000
#define _PyUnicode_AsString PyString_AsString
#undef PyUnicode_Check
#define PyUnicode_Check PyString_Check
#define PyLong_FromSsize_t PyInt_FromLong
#define PyLong_AsSsize_t PyInt_AsLong
#undef PyLong_Check
#define PyLong_Check PyInt_Check
#ifdef PyUnicode_FromString
#undef PyUnicode_FromString
#endif
#define PyUnicode_FromString PyString_FromString
#ifdef PyUnicode_FromFormat
#undef PyUnicode_FromFormat
#endif
#define PyUnicode_FromFormat PyString_FromFormat
#endif
#ifndef Py_REFCNT
#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
#endif
#ifndef Py_TYPE
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#endif
#ifndef Py_TYPE
#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
#endif
/* older then python 2.6 - define these */
// #if (PY_VERSION_HEX < 0x02060000)
// #endif
/* older then python 2.5 - define these */
#if (PY_VERSION_HEX < 0x02050000)
#define Py_ssize_t ssize_t
typedef Py_ssize_t (*lenfunc)(PyObject *);
#ifndef Py_RETURN_NONE
#define Py_RETURN_NONE return Py_BuildValue("O", Py_None)
#endif
#ifndef Py_RETURN_FALSE
#define Py_RETURN_FALSE return PyBool_FromLong(0)
#endif
#ifndef Py_RETURN_TRUE
#define Py_RETURN_TRUE return PyBool_FromLong(1)
#endif
#define PyInt_FromSsize_t PyInt_FromLong
#define PyNumber_AsSsize_t(ob, exc) PyInt_AsLong(ob)
#define PyIndex_Check(ob) PyInt_Check(ob)
#endif
#if PY_VERSION_HEX < 0x03000000
#ifndef ssizeargfunc
#define ssizeargfunc intargfunc
#endif
#ifndef ssizessizeargfunc
#define ssizessizeargfunc intintargfunc
#endif
#ifndef ssizeobjargproc
#define ssizeobjargproc intobjargproc
#endif
#ifndef ssizessizeobjargproc
#define ssizessizeobjargproc intintobjargproc
#endif
#endif
/* defined in bpy_util.c */
#if PY_VERSION_HEX < 0x03000000
PyObject *Py_CmpToRich(int op, int cmp);
#endif
#ifndef Py_CmpToRich
PyObject *Py_CmpToRich(int op, int cmp); /* bpy_util.c */
#endif
#endif /* BPY_COMPAT_H__ */

View File

@@ -14,8 +14,6 @@
#include "compile.h" /* for the PyCodeObject */
#include "eval.h" /* for PyEval_EvalCode */
#include "bpy_compat.h"
#include "bpy_rna.h"
#include "bpy_operator.h"
#include "bpy_ui.h"
@@ -46,6 +44,80 @@
#include "../generic/BGL.h"
/* for internal use, when starting and ending python scripts */
/* incase a python script triggers another python call, stop bpy_context_clear from invalidating */
static int py_call_level= 0;
// only for tests
#define TIME_PY_RUN
#ifdef TIME_PY_RUN
#include "PIL_time.h"
static int bpy_timer_count = 0;
static double bpy_timer; /* time since python starts */
static double bpy_timer_run; /* time for each python script run */
static double bpy_timer_run_tot; /* accumulate python runs */
#endif
void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
{
py_call_level++;
if(gilstate)
*gilstate = PyGILState_Ensure();
if(py_call_level==1) {
BPY_update_modules(); /* can give really bad results if this isnt here */
if(C) { // XXX - should always be true.
BPy_SetContext(C);
bpy_import_main_set(CTX_data_main(C));
}
else {
fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n");
}
#ifdef TIME_PY_RUN
if(bpy_timer_count==0) {
/* record time from the beginning */
bpy_timer= PIL_check_seconds_timer();
bpy_timer_run = bpy_timer_run_tot = 0.0;
}
bpy_timer_run= PIL_check_seconds_timer();
bpy_timer_count++;
#endif
}
}
void bpy_context_clear(bContext *C, PyGILState_STATE *gilstate)
{
py_call_level--;
if(gilstate)
PyGILState_Release(*gilstate);
if(py_call_level < 0) {
fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
}
else if(py_call_level==0) {
// XXX - Calling classes currently wont store the context :\, cant set NULL because of this. but this is very flakey still.
//BPy_SetContext(NULL);
//bpy_import_main_set(NULL);
#ifdef TIME_PY_RUN
bpy_timer_run_tot += PIL_check_seconds_timer() - bpy_timer_run;
bpy_timer_count++;
#endif
}
}
void BPY_free_compiled_text( struct Text *text )
{
if( text->compiled ) {
@@ -69,34 +141,31 @@ static void bpy_init_modules( void )
PyModule_AddObject( mod, "props", BPY_rna_props() );
PyModule_AddObject( mod, "__ops__", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent
/* Arystan: this module is used by i/o scripts for bpy.sys.expandpath() and others. It should be renamed/move? */
PyModule_AddObject( mod, "sys", BPY_sys_module() );
/* add the module so we can import it */
PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod);
Py_DECREF(mod);
/* stand alone utility modules not related to blender directly */
Geometry_Init("Geometry");
Mathutils_Init("Mathutils");
BGL_Init("BGL");
Geometry_Init();
Mathutils_Init();
BGL_Init();
}
#if (PY_VERSION_HEX < 0x02050000)
PyObject *PyImport_ImportModuleLevel(char *name, void *a, void *b, void *c, int d)
{
return PyImport_ImportModule(name);
}
#endif
void BPY_update_modules( void )
{
#if 0 // slow, this runs all the time poll, draw etc 100's of time a sec.
PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
PyModule_AddObject( mod, "data", BPY_rna_module() );
PyModule_AddObject( mod, "types", BPY_rna_types() );
/* PyModule_AddObject( mod, "util", BPY_util_module() ); */
PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating
#endif
/* XXX this will move to bpy.util */
PyModule_AddObject( mod, "sys", BPY_sys_module() );
/* refreshes the main struct */
BPY_update_rna_module();
}
/*****************************************************************************
@@ -111,9 +180,6 @@ static PyObject *CreateGlobalDictionary( bContext *C )
PyDict_SetItemString( dict, "__name__", item );
Py_DECREF(item);
// XXX - evil, need to access context
BPy_SetContext(C);
// XXX - put somewhere more logical
{
PyMethodDef *ml;
@@ -178,9 +244,7 @@ void BPY_start_python( int argc, char **argv )
Py_Initialize( );
#if (PY_VERSION_HEX < 0x03000000)
PySys_SetArgv( argc, argv);
#else
// PySys_SetArgv( argc, argv); // broken in py3, not a huge deal
/* sigh, why do python guys not have a char** version anymore? :( */
{
int i;
@@ -192,7 +256,6 @@ void BPY_start_python( int argc, char **argv )
PySys_SetObject("argv", py_argv);
Py_DECREF(py_argv);
}
#endif
/* Initialize thread support (also acquires lock) */
PyEval_InitThreads();
@@ -210,38 +273,57 @@ void BPY_start_python( int argc, char **argv )
PyDict_SetItemString(d, "__import__", item=PyCFunction_New(bpy_import_meth, NULL)); Py_DECREF(item);
}
pyrna_alloc_types();
py_tstate = PyGILState_GetThisThreadState();
PyEval_ReleaseThread(py_tstate);
}
void BPY_end_python( void )
{
// fprintf(stderr, "Ending Python!\n");
PyGILState_Ensure(); /* finalizing, no need to grab the state */
// free other python data.
//BPY_rna_free_types();
pyrna_free_types();
/* clear all python data from structs */
Py_Finalize( );
return;
#ifdef TIME_PY_RUN
// measure time since py started
bpy_timer = PIL_check_seconds_timer() - bpy_timer;
printf("*bpy stats* - ");
printf("tot exec: %d, ", bpy_timer_count);
printf("tot run: %.4fsec, ", bpy_timer_run_tot);
if(bpy_timer_count>0)
printf("average run: %.6fsec, ", (bpy_timer_run_tot/bpy_timer_count));
if(bpy_timer>0.0)
printf("tot usage %.4f%%", (bpy_timer_run_tot/bpy_timer)*100.0);
printf("\n");
// fprintf(stderr, "Ending Python Done!\n");
#endif
}
/* Can run a file or text block */
int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports)
{
PyObject *py_dict, *py_result;
PyObject *py_dict, *py_result= NULL;
PyGILState_STATE gilstate;
if (fn==NULL && text==NULL) {
return 0;
}
//BPY_start_python();
gilstate = PyGILState_Ensure();
BPY_update_modules(); /* can give really bad results if this isnt here */
bpy_import_main_set(CTX_data_main(C));
bpy_context_set(C, &gilstate);
py_dict = CreateGlobalDictionary(C);
@@ -256,13 +338,11 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
MEM_freeN( buf );
if( PyErr_Occurred( ) ) {
BPy_errors_to_report(reports);
BPY_free_compiled_text( text );
PyGILState_Release(gilstate);
return 0;
}
}
py_result = PyEval_EvalCode( text->compiled, py_dict, py_dict );
if(text->compiled)
py_result = PyEval_EvalCode( text->compiled, py_dict, py_dict );
} else {
#if 0
@@ -292,10 +372,9 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
}
Py_DECREF(py_dict);
PyGILState_Release(gilstate);
bpy_import_main_set(NULL);
//BPY_end_python();
bpy_context_clear(C, &gilstate);
return py_result ? 1:0;
}
@@ -478,12 +557,7 @@ void BPY_run_ui_scripts(bContext *C, int reload)
PyGILState_STATE gilstate;
PyObject *sys_path;
gilstate = PyGILState_Ensure();
// XXX - evil, need to access context
BPy_SetContext(C);
bpy_import_main_set(CTX_data_main(C));
bpy_context_set(C, &gilstate);
sys_path= PySys_GetObject("path"); /* borrow */
PyList_Insert(sys_path, 0, Py_None); /* place holder, resizes the list */
@@ -542,12 +616,14 @@ void BPY_run_ui_scripts(bContext *C, int reload)
PyList_SetSlice(sys_path, 0, 1, NULL); /* remove the first item */
bpy_import_main_set(NULL);
bpy_context_clear(C, &gilstate);
PyGILState_Release(gilstate);
#ifdef TIME_REGISTRATION
printf("script time %f\n", (PIL_check_seconds_timer()-time));
#endif
/* reset the timer so as not to take loading into the stats */
bpy_timer_count = 0;
}
/* ****************************************** */
@@ -745,3 +821,56 @@ float BPY_pydriver_eval (ChannelDriver *driver)
return result;
}
int BPY_button_eval(bContext *C, char *expr, double *value)
{
PyGILState_STATE gilstate;
PyObject *dict, *retval;
int error_ret = 0;
if (!value || !expr || expr[0]=='\0') return -1;
bpy_context_set(C, &gilstate);
dict= CreateGlobalDictionary(C);
retval = PyRun_String(expr, Py_eval_input, dict, dict);
if (retval == NULL) {
error_ret= -1;
}
else {
double val;
if(PyTuple_Check(retval)) {
/* Users my have typed in 10km, 2m
* add up all values */
int i;
val= 0.0;
for(i=0; i<PyTuple_GET_SIZE(retval); i++) {
val+= PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
}
}
else {
val = PyFloat_AsDouble(retval);
}
Py_DECREF(retval);
if(val==-1 && PyErr_Occurred()) {
error_ret= -1;
}
else {
*value= val;
}
}
if(error_ret) {
BPy_errors_to_report(CTX_wm_reports(C));
}
Py_DECREF(dict);
bpy_context_clear(C, &gilstate);
return error_ret;
}

View File

@@ -30,7 +30,6 @@
#include "bpy_operator.h"
#include "bpy_operator_wrap.h"
#include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
#include "bpy_compat.h"
#include "bpy_util.h"
#include "WM_api.h"

View File

@@ -37,7 +37,6 @@
#include "RNA_define.h"
#include "bpy_rna.h"
#include "bpy_compat.h"
#include "bpy_util.h"
#include "../generic/bpy_internal_import.h" // our own imports
@@ -93,11 +92,9 @@ static int PYTHON_OT_generic(int mode, bContext *C, wmOperator *op, wmEvent *eve
PointerRNA ptr_event;
PyObject *py_operator;
PyGILState_STATE gilstate = PyGILState_Ensure();
PyGILState_STATE gilstate;
bpy_import_main_set(CTX_data_main(C));
BPY_update_modules(); // XXX - the RNA pointers can change so update before running, would like a nicer solutuon for this.
bpy_context_set(C, &gilstate);
args = PyTuple_New(1);
PyTuple_SET_ITEM(args, 0, PyObject_GetAttrString(py_class, "__rna__")); // need to use an rna instance as the first arg
@@ -221,8 +218,7 @@ static int PYTHON_OT_generic(int mode, bContext *C, wmOperator *op, wmEvent *eve
}
#endif
PyGILState_Release(gilstate);
bpy_import_main_set(NULL);
bpy_context_clear(C, &gilstate);
return ret_flag;
}
@@ -299,7 +295,7 @@ void PYTHON_OT_wrapper(wmOperatorType *ot, void *userdata)
PyObject *py_func_ptr, *py_kw, *py_srna_cobject, *py_ret;
item = PyList_GET_ITEM(props, i);
if (PyArg_ParseTuple(item, "O!O!", &PyCObject_Type, &py_func_ptr, &PyDict_Type, &py_kw)) {
if (PyArg_ParseTuple(item, "O!O!:PYTHON_OT_wrapper", &PyCObject_Type, &py_func_ptr, &PyDict_Type, &py_kw)) {
PyObject *(*pyfunc)(PyObject *, PyObject *, PyObject *);
pyfunc = PyCObject_AsVoidPtr(py_func_ptr);
@@ -309,6 +305,8 @@ void PYTHON_OT_wrapper(wmOperatorType *ot, void *userdata)
if (py_ret) {
Py_DECREF(py_ret);
} else {
fprintf(stderr, "BPy Operator \"%s\" registration error: %s item %d could not run\n", ot->idname, PYOP_ATTR_PROP, i);
PyLineSpit();
PyErr_Print();
PyErr_Clear();
}

File diff suppressed because it is too large Load Diff

View File

@@ -61,6 +61,7 @@ typedef struct {
#define BPy_BaseTypeRNA BPy_PropertyRNA
PyObject *BPY_rna_module( void );
void BPY_update_rna_module( void );
/*PyObject *BPY_rna_doc( void );*/
PyObject *BPY_rna_types( void );
PyObject *BPY_rna_props( void );
@@ -74,13 +75,20 @@ int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, int all_args, const cha
PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
/* functions for setting up new props - experemental */
PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_EnumProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_PointerProperty(PyObject *self, PyObject *args, PyObject *kw);
PyObject *BPy_CollectionProperty(PyObject *self, PyObject *args, PyObject *kw);
/* function for registering types */
PyObject *pyrna_basetype_register(PyObject *self, PyObject *args);
PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args);
/* called before stopping python */
void pyrna_alloc_types(void);
void pyrna_free_types(void);
#endif

View File

@@ -26,7 +26,6 @@
#include "bpy_util.h"
#include "bpy_rna.h" /* for rna buttons */
#include "bpy_operator.h" /* for setting button operator properties */
#include "bpy_compat.h"
#include "WM_types.h" /* for WM_OP_INVOKE_DEFAULT & friends */
@@ -47,7 +46,6 @@ static struct PyMethodDef ui_methods[] = {
{NULL, NULL, 0, NULL}
};
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef ui_module = {
PyModuleDef_HEAD_INIT,
"bpy.ui",
@@ -56,16 +54,11 @@ static struct PyModuleDef ui_module = {
ui_methods,
NULL, NULL, NULL, NULL
};
#endif
PyObject *BPY_ui_module( void )
{
PyObject *submodule, *mod;
#if PY_VERSION_HEX >= 0x03000000
PyObject *submodule;
submodule= PyModule_Create(&ui_module);
#else /* Py2.x */
submodule= Py_InitModule3( "bpy.ui", ui_methods, "" );
#endif
/* INCREF since its its assumed that all these functions return the
* module with a new ref like PyDict_New, since they are passed to

View File

@@ -129,7 +129,6 @@ int BPY_flag_from_seq(BPY_flag_def *flagdef, PyObject *seq, int *flag)
/* Copied from pythons 3's Object.c */
#ifndef Py_CmpToRich
PyObject *
Py_CmpToRich(int op, int cmp)
{
@@ -165,7 +164,6 @@ Py_CmpToRich(int op, int cmp)
Py_INCREF(res);
return res;
}
#endif
/* for debugging */
void PyObSpit(char *name, PyObject *var) {
@@ -175,7 +173,7 @@ void PyObSpit(char *name, PyObject *var) {
}
else {
PyObject_Print(var, stderr, 0);
fprintf(stderr, " ref:%d ", var->ob_refcnt);
fprintf(stderr, " ref:%d ", (int)var->ob_refcnt);
fprintf(stderr, " ptr:%p", (void *)var);
fprintf(stderr, " type:");
@@ -378,11 +376,7 @@ PyObject *BPY_exception_buffer(void)
* string_io = StringIO.StringIO()
*/
#if PY_VERSION_HEX < 0x03000000
if(! (string_io_mod= PyImport_ImportModule("StringIO")) ) {
#else
if(! (string_io_mod= PyImport_ImportModule("io")) ) {
#endif
goto error_cleanup;
} else if (! (string_io = PyObject_CallMethod(string_io_mod, "StringIO", NULL))) {
goto error_cleanup;

View File

@@ -27,7 +27,10 @@
#ifndef BPY_UTIL_H
#define BPY_UTIL_H
#include "bpy_compat.h"
#if PY_VERSION_HEX < 0x03010000
#error "Python versions below 3.1 are not supported anymore, you'll need to update your python."
#endif
#include "RNA_types.h" /* for EnumPropertyItem only */
struct EnumPropertyItem;
@@ -47,6 +50,8 @@ void PyObSpit(char *name, PyObject *var);
void PyLineSpit(void);
void BPY_getFileAndNum(char **filename, int *lineno);
PyObject *Py_CmpToRich(int op, int cmp);
PyObject *BPY_exception_buffer(void);
/* own python like utility function */
@@ -72,7 +77,7 @@ int BPY_class_validate(const char *class_type, PyObject *class, PyObject *base_c
char *BPy_enum_as_string(struct EnumPropertyItem *item);
#define BLANK_PYTHON_TYPE {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
#define BLANK_PYTHON_TYPE {PyVarObject_HEAD_INIT(NULL, 0) 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
/* error reporting */
int BPy_reports_to_error(struct ReportList *reports);
@@ -82,6 +87,8 @@ int BPy_errors_to_report(struct ReportList *reports);
struct bContext *BPy_GetContext(void);
void BPy_SetContext(struct bContext *C);
PyObject *BPY_util_module(void);
extern void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate);
extern void bpy_context_clear(struct bContext *C, PyGILState_STATE *gilstate);
#endif