https://svn.blender.org/svnroot/bf-blender/branches/soc-2008-mxcurioni (r22789) and
https://svn.blender.org/svnroot/bf-blender/trunk/blender (r23338)
with the "Ignore ancestry" and "Ignore line endings" options enabled (using
TortoiseSVN on Windows).

After the merge operation, all changes (i.e., deletion) in source/blender/freestyle/
were reverted in order to keep the primary source tree of the Freestyle renderer.
This commit is contained in:
2009-09-18 22:25:49 +00:00
parent 0a4d70f4d0
commit be50ce61be
3509 changed files with 612152 additions and 643374 deletions

View File

@@ -47,13 +47,17 @@
SCA_PythonController* SCA_PythonController::m_sCurrentController = NULL;
SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj,
PyTypeObject* T)
: SCA_IController(gameobj, T),
SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj, int mode)
: SCA_IController(gameobj),
m_bytecode(NULL),
m_function(NULL),
m_function_argc(0),
m_bModified(true),
m_debug(false),
m_mode(mode),
m_pythondictionary(NULL)
{
}
/*
@@ -74,15 +78,12 @@ int SCA_PythonController::Release()
SCA_PythonController::~SCA_PythonController()
{
if (m_bytecode)
{
//
//printf("released python byte script\n");
Py_DECREF(m_bytecode);
}
//printf("released python byte script\n");
if (m_pythondictionary)
{
Py_XDECREF(m_bytecode);
Py_XDECREF(m_function);
if (m_pythondictionary) {
// break any circular references in the dictionary
PyDict_Clear(m_pythondictionary);
Py_DECREF(m_pythondictionary);
@@ -94,8 +95,12 @@ SCA_PythonController::~SCA_PythonController()
CValue* SCA_PythonController::GetReplica()
{
SCA_PythonController* replica = new SCA_PythonController(*this);
// Copy the compiled bytecode if possible.
/* why is this needed at all??? - m_bytecode is NULL'd below so this doesnt make sense
* but removing it crashes blender (with YoFrankie). so leave in for now - Campbell */
Py_XINCREF(replica->m_bytecode);
Py_XINCREF(replica->m_function); // this is ok since its not set to NULL
replica->m_bModified = replica->m_bytecode == NULL;
// The replica->m_pythondictionary is stolen - replace with a copy.
@@ -110,7 +115,7 @@ CValue* SCA_PythonController::GetReplica()
*/
// this will copy properties and so on...
CValue::AddDataToReplica(replica);
replica->ProcessReplica();
return replica;
}
@@ -140,6 +145,11 @@ void SCA_PythonController::SetDictionary(PyObject* pythondictionary)
Py_DECREF(m_pythondictionary);
}
m_pythondictionary = PyDict_Copy(pythondictionary); /* new reference */
/* Without __file__ set the sys.argv[0] is used for the filename
* which ends up with lines from the blender binary being printed in the console */
PyDict_SetItemString(m_pythondictionary, "__file__", PyUnicode_FromString(m_scriptName.Ptr()));
}
int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
@@ -150,131 +160,223 @@ int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
return 0;
}
#if 0
static const char* sPyGetCurrentController__doc__;
#endif
PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self)
/* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
PyObject* SCA_PythonController::sPyGetCurrentController(PyObject *self)
{
m_sCurrentController->AddRef();
return m_sCurrentController;
if(m_sCurrentController==NULL)
{
PyErr_SetString(PyExc_SystemError, "GameLogic.getCurrentController(), this function is being run outside the python controllers context, or blenders internal state is corrupt.");
return NULL;
}
return m_sCurrentController->GetProxy();
}
#if 0
static const char* sPyAddActiveActuator__doc__;
#endif
PyObject* SCA_PythonController::sPyAddActiveActuator(
PyObject* self,
PyObject* args)
SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
{
PyObject* ob1;
int activate;
if (!PyArg_ParseTuple(args, "Oi", &ob1,&activate))
return NULL;
// for safety, todo: only allow for registered actuators (pointertable)
// we don't want to crash gameengine/blender by python scripts
std::vector<SCA_IActuator*> lacts = m_sCurrentController->GetLinkedActuators();
std::vector<SCA_IActuator*>::iterator it;
bool found = false;
CValue* act = (CValue*)ob1;
for(it = lacts.begin(); it!= lacts.end(); it++) {
if( static_cast<SCA_IActuator*>(act) == (*it) ) {
found=true;
break;
if (PyUnicode_Check(value)) {
/* get the actuator from the name */
char *name= _PyUnicode_AsString(value);
for(it = lacts.begin(); it!= lacts.end(); ++it) {
if( name == (*it)->GetName() ) {
return *it;
}
}
}
if(found){
CValue* boolval = new CBoolValue(activate!=0);
m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)act,boolval);
boolval->Release();
else if (PyObject_TypeCheck(value, &SCA_IActuator::Type)) {
PyObjectPlus *value_plus= BGE_PROXY_REF(value);
for(it = lacts.begin(); it!= lacts.end(); ++it) {
if( static_cast<SCA_IActuator*>(value_plus) == (*it) ) {
return *it;
}
}
}
Py_RETURN_NONE;
/* set the exception */
PyObject *value_str = PyObject_Repr(value); /* new ref */
PyErr_Format(PyExc_ValueError, "'%s' not in this python controllers actuator list", _PyUnicode_AsString(value_str));
Py_DECREF(value_str);
return false;
}
const char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()";
const char* SCA_PythonController::sPyAddActiveActuator__doc__= "addActiveActuator(actuator,bool)";
const char SCA_PythonController::GetActuators_doc[] = "getActuator";
PyTypeObject SCA_PythonController::Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"SCA_PythonController",
sizeof(SCA_PythonController),
0,
PyDestructor,
0,
__getattr,
__setattr,
0, //&MyPyCompare,
__repr,
0, //&cvalue_as_number,
0,
0,
0,
0
PyVarObject_HEAD_INIT(NULL, 0)
"SCA_PythonController",
sizeof(PyObjectPlus_Proxy),
0,
py_base_dealloc,
0,
0,
0,
0,
py_base_repr,
0,0,0,0,0,0,0,0,0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
0,0,0,0,0,0,0,
Methods,
0,
0,
&SCA_IController::Type,
0,0,0,0,0,0,
py_base_new
};
PyParentObject SCA_PythonController::Parents[] = {
&SCA_PythonController::Type,
&SCA_IController::Type,
&CValue::Type,
NULL
};
PyMethodDef SCA_PythonController::Methods[] = {
{"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetActuators_doc},
{"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, METH_O, (PY_METHODCHAR)SCA_PythonController::GetActuator_doc},
{"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetSensors_doc},
{"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, METH_O, (PY_METHODCHAR)SCA_PythonController::GetSensor_doc},
{"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS},
{"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O},
{"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS},
{"activate", (PyCFunction) SCA_PythonController::sPyActivate, METH_O},
{"deactivate", (PyCFunction) SCA_PythonController::sPyDeActivate, METH_O},
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_PythonController::Attributes[] = {
KX_PYATTRIBUTE_RW_FUNCTION("script", SCA_PythonController, pyattr_get_script, pyattr_set_script),
KX_PYATTRIBUTE_INT_RO("mode", SCA_PythonController, m_mode),
{ NULL } //Sentinel
};
void SCA_PythonController::ErrorPrint(const char *error_msg)
{
// didn't compile, so instead of compile, complain
// something is wrong, tell the user what went wrong
printf("%s - controller \"%s\":\n", error_msg, GetName().Ptr());
//PyRun_SimpleString(m_scriptText.Ptr());
PyErr_Print();
/* Added in 2.48a, the last_traceback can reference Objects for example, increasing
* their user count. Not to mention holding references to wrapped data.
* This is especially bad when the PyObject for the wrapped data is free'd, after blender
* has alredy dealocated the pointer */
PySys_SetObject( (char *)"last_traceback", NULL);
PyErr_Clear(); /* just to be sure */
}
bool SCA_PythonController::Compile()
{
//printf("py script modified '%s'\n", m_scriptName.Ptr());
m_bModified= false;
// if a script already exists, decref it before replace the pointer to a new script
if (m_bytecode) {
Py_DECREF(m_bytecode);
m_bytecode=NULL;
}
// recompile the scripttext into bytecode
m_bytecode = Py_CompileString(m_scriptText.Ptr(), m_scriptName.Ptr(), Py_file_input);
if (m_bytecode) {
return true;
} else {
ErrorPrint("Python error compiling script");
return false;
}
}
bool SCA_PythonController::Import()
{
//printf("py module modified '%s'\n", m_scriptName.Ptr());
m_bModified= false;
/* incase we re-import */
Py_XDECREF(m_function);
m_function= NULL;
vector<STR_String> py_function_path = m_scriptText.Explode('.');
if(py_function_path.size() < 2) {
printf("Python module name formatting error \"%s\":\n\texpected \"SomeModule.Func\", got \"%s\"\n", GetName().Ptr(), m_scriptText.Ptr());
return false;
}
PyObject *mod = PyImport_ImportModule((char *)py_function_path[0].Ptr());
/* Dont reload yet, do this within the loop so packages reload too */
if(mod==NULL) {
ErrorPrint("Python module not found");
return false;
}
/* 'mod' will be DECREF'd as 'base'
* 'm_function' will be left holding a reference that the controller owns */
PyObject *base= mod;
for(unsigned int i=1; i < py_function_path.size(); i++) {
if(m_debug && PyModule_Check(base)) { /* base could be a class */
Py_DECREF(base); /* getting a new one so dont hold a ref to the old one */
base= PyImport_ReloadModule(base);
if (base==NULL) {
m_function= NULL;
break;
}
}
m_function = PyObject_GetAttrString(base, py_function_path[i].Ptr());
Py_DECREF(base);
base = m_function; /* for the next loop if there is on */
if(m_function==NULL) {
break;
}
}
if(m_function==NULL) {
if(PyErr_Occurred())
ErrorPrint("Python controller found the module but could not access the function");
else
printf("Python module error \"%s\":\n \"%s\" module found but function missing\n", GetName().Ptr(), m_scriptText.Ptr());
return false;
}
if(!PyCallable_Check(m_function)) {
Py_DECREF(m_function);
printf("Python module function error \"%s\":\n \"%s\" not callable\n", GetName().Ptr(), m_scriptText.Ptr());
return false;
}
m_function_argc = 0; /* rare cases this could be a function that isnt defined in python, assume zero args */
if (PyFunction_Check(m_function)) {
PyObject *py_arg_count = PyObject_GetAttrString(PyFunction_GET_CODE(m_function), "co_argcount");
if(py_arg_count) {
m_function_argc = PyLong_AsLong(py_arg_count);
Py_DECREF(py_arg_count);
}
else {
PyErr_Clear(); /* unlikely to fail but just incase */
}
}
if(m_function_argc > 1) {
Py_DECREF(m_function);
printf("Python module function has \"%s\":\n \"%s\" takes %d args, should be zero or 1 controller arg\n", GetName().Ptr(), m_scriptText.Ptr(), m_function_argc);
return false;
}
return true;
}
void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
{
m_sCurrentController = this;
m_sCurrentLogicManager = logicmgr;
if (m_bModified)
{
// if a script already exists, decref it before replace the pointer to a new script
if (m_bytecode)
{
Py_DECREF(m_bytecode);
m_bytecode=NULL;
}
// recompile the scripttext into bytecode
m_bytecode = Py_CompileString(m_scriptText.Ptr(), m_scriptName.Ptr(), Py_file_input);
if (!m_bytecode)
{
// didn't compile, so instead of compile, complain
// something is wrong, tell the user what went wrong
printf("Python compile error from controller \"%s\": \n", GetName().Ptr());
//PyRun_SimpleString(m_scriptText.Ptr());
PyErr_Print();
/* Added in 2.48a, the last_traceback can reference Objects for example, increasing
* their user count. Not to mention holding references to wrapped data.
* This is especially bad when the PyObject for the wrapped data is free'd, after blender
* has alredy dealocated the pointer */
PySys_SetObject( "last_traceback", Py_None);
PyErr_Clear(); /* just to be sure */
return;
}
m_bModified=false;
}
PyObject *excdict= NULL;
PyObject* resultobj= NULL;
switch(m_mode) {
case SCA_PYEXEC_SCRIPT:
{
if (m_bModified)
if (Compile()==false) // sets m_bModified to false
return;
if (!m_bytecode)
return;
/*
* This part here with excdict is a temporary patch
* to avoid python/gameengine crashes when python
@@ -293,10 +395,36 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
* should always ensure excdict is cleared).
*/
PyObject *excdict= PyDict_Copy(m_pythondictionary);
PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
excdict, excdict);
excdict= PyDict_Copy(m_pythondictionary);
resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode, excdict, excdict);
/* PyRun_SimpleString(m_scriptText.Ptr()); */
break;
}
case SCA_PYEXEC_MODULE:
{
if (m_bModified || m_debug)
if (Import()==false) // sets m_bModified to false
return;
if (!m_function)
return;
PyObject *args= NULL;
if(m_function_argc==1) {
args = PyTuple_New(1);
PyTuple_SET_ITEM(args, 0, GetProxy());
}
resultobj = PyObject_CallObject(m_function, args);
Py_XDECREF(args);
break;
}
} /* end switch */
/* Free the return value and print the error */
if (resultobj)
{
Py_DECREF(resultobj);
@@ -304,146 +432,87 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
else
{
// something is wrong, tell the user what went wrong
printf("Python script error from controller \"%s\": \n", GetName().Ptr());
printf("Python script error from controller \"%s\":\n", GetName().Ptr());
PyErr_Print();
/* Added in 2.48a, the last_traceback can reference Objects for example, increasing
* their user count. Not to mention holding references to wrapped data.
* This is especially bad when the PyObject for the wrapped data is free'd, after blender
* has alredy dealocated the pointer */
PySys_SetObject( "last_traceback", Py_None);
PySys_SetObject( (char *)"last_traceback", NULL);
PyErr_Clear(); /* just to be sure */
//PyRun_SimpleString(m_scriptText.Ptr());
}
// clear after PyErrPrint - seems it can be using
// something in this dictionary and crash?
PyDict_Clear(excdict);
Py_DECREF(excdict);
m_triggeredSensors.erase(m_triggeredSensors.begin(), m_triggeredSensors.end());
if(excdict) /* Only for SCA_PYEXEC_SCRIPT types */
{
/* clear after PyErrPrint - seems it can be using
* something in this dictionary and crash? */
PyDict_Clear(excdict);
Py_DECREF(excdict);
}
m_triggeredSensors.clear();
m_sCurrentController = NULL;
}
PyObject* SCA_PythonController::_getattr(const STR_String& attr)
PyObject* SCA_PythonController::PyActivate(PyObject *value)
{
_getattr_up(SCA_IController);
}
PyObject* SCA_PythonController::PyGetActuators(PyObject* self)
{
PyObject* resultlist = PyList_New(m_linkedactuators.size());
for (unsigned int index=0;index<m_linkedactuators.size();index++)
{
PyList_SET_ITEM(resultlist,index,m_linkedactuators[index]->AddRef());
}
return resultlist;
}
const char SCA_PythonController::GetSensor_doc[] =
"GetSensor (char sensorname) return linked sensor that is named [sensorname]\n";
PyObject*
SCA_PythonController::PyGetSensor(PyObject* self, PyObject* value)
{
char *scriptArg = PyString_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "expected a string (sensor name)");
if(m_sCurrentController != this) {
PyErr_SetString(PyExc_SystemError, "Cannot add an actuator from a non-active controller");
return NULL;
}
for (unsigned int index=0;index<m_linkedsensors.size();index++)
{
SCA_ISensor* sensor = m_linkedsensors[index];
STR_String realname = sensor->GetName();
if (realname == scriptArg)
{
return sensor->AddRef();
}
}
char emsg[96];
PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested sensor \"%s\"", scriptArg );
PyErr_SetString(PyExc_AttributeError, emsg);
return NULL;
}
const char SCA_PythonController::GetActuator_doc[] =
"GetActuator (char sensorname) return linked actuator that is named [actuatorname]\n";
PyObject*
SCA_PythonController::PyGetActuator(PyObject* self, PyObject* value)
{
char *scriptArg = PyString_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "expected a string (actuator name)");
SCA_IActuator* actu = LinkedActuatorFromPy(value);
if(actu==NULL)
return NULL;
}
for (unsigned int index=0;index<m_linkedactuators.size();index++)
{
SCA_IActuator* actua = m_linkedactuators[index];
STR_String realname = actua->GetName();
if (realname == scriptArg)
{
return actua->AddRef();
}
}
char emsg[96];
PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested actuator \"%s\"", scriptArg );
PyErr_SetString(PyExc_AttributeError, emsg);
return NULL;
}
const char SCA_PythonController::GetSensors_doc[] = "getSensors returns a list of all attached sensors";
PyObject*
SCA_PythonController::PyGetSensors(PyObject* self)
{
PyObject* resultlist = PyList_New(m_linkedsensors.size());
for (unsigned int index=0;index<m_linkedsensors.size();index++)
{
PyList_SET_ITEM(resultlist,index,m_linkedsensors[index]->AddRef());
}
return resultlist;
}
/* 1. getScript */
PyObject* SCA_PythonController::PyGetScript(PyObject* self)
{
return PyString_FromString(m_scriptText);
}
/* 2. setScript */
PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value)
{
char *scriptArg = PyString_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "expected a string (script name)");
return NULL;
}
/* set scripttext sets m_bModified to true,
so next time the script is needed, a reparse into byte code is done */
this->SetScriptText(scriptArg);
m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, true);
Py_RETURN_NONE;
}
/* 1. getScript */
PyObject* SCA_PythonController::PyGetState(PyObject* self)
PyObject* SCA_PythonController::PyDeActivate(PyObject *value)
{
return PyInt_FromLong(m_statemask);
if(m_sCurrentController != this) {
PyErr_SetString(PyExc_SystemError, "Cannot add an actuator from a non-active controller");
return NULL;
}
SCA_IActuator* actu = LinkedActuatorFromPy(value);
if(actu==NULL)
return NULL;
m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, false);
Py_RETURN_NONE;
}
PyObject* SCA_PythonController::pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
//SCA_PythonController* self= static_cast<SCA_PythonController*>(static_cast<SCA_IController*>(static_cast<SCA_ILogicBrick*>(static_cast<CValue*>(static_cast<PyObjectPlus*>(self_v)))));
// static_cast<void *>(dynamic_cast<Derived *>(obj)) - static_cast<void *>(obj)
SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
return PyUnicode_FromString(self->m_scriptText);
}
int SCA_PythonController::pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
{
SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
char *scriptArg = _PyUnicode_AsString(value);
if (scriptArg==NULL) {
PyErr_SetString(PyExc_TypeError, "controller.script = string: Python Controller, expected a string script text");
return PY_SET_ATTR_FAIL;
}
/* set scripttext sets m_bModified to true,
so next time the script is needed, a reparse into byte code is done */
self->SetScriptText(scriptArg);
return PY_SET_ATTR_SUCCESS;
}
/* eof */