Blender V2.61 - r43446

SCA_PythonController.cpp

Go to the documentation of this file.
00001 /*
00002  * Execute Python scripts
00003  *
00004  *
00005  * ***** BEGIN GPL LICENSE BLOCK *****
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00022  * All rights reserved.
00023  *
00024  * The Original Code is: all of this file.
00025  *
00026  * Contributor(s): none yet.
00027  *
00028  * ***** END GPL LICENSE BLOCK *****
00029  */
00030 
00036 #include <stddef.h>
00037 
00038 #include "SCA_PythonController.h"
00039 #include "SCA_LogicManager.h"
00040 #include "SCA_ISensor.h"
00041 #include "SCA_IActuator.h"
00042 #include "PyObjectPlus.h"
00043 
00044 #ifdef WITH_PYTHON
00045 #include "compile.h"
00046 #include "eval.h"
00047 #endif // WITH_PYTHON
00048 
00049 #include <algorithm>
00050 
00051 
00052 // initialize static member variables
00053 SCA_PythonController* SCA_PythonController::m_sCurrentController = NULL;
00054 
00055 
00056 SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj, int mode)
00057     : SCA_IController(gameobj),
00058 #ifdef WITH_PYTHON
00059     m_bytecode(NULL),
00060     m_function(NULL),
00061 #endif
00062     m_function_argc(0),
00063     m_bModified(true),
00064     m_debug(false),
00065     m_mode(mode)
00066 #ifdef WITH_PYTHON
00067     , m_pythondictionary(NULL)
00068 #endif
00069 
00070 {
00071     
00072 }
00073 
00074 /*
00075 //debugging
00076 CValue*     SCA_PythonController::AddRef()
00077 {
00078     //printf("AddRef refcount = %i\n",GetRefCount());
00079     return CValue::AddRef();
00080 }
00081 int         SCA_PythonController::Release()
00082 {
00083     //printf("Release refcount = %i\n",GetRefCount());
00084     return CValue::Release();
00085 }
00086 */
00087 
00088 
00089 
00090 SCA_PythonController::~SCA_PythonController()
00091 {
00092 
00093 #ifdef WITH_PYTHON
00094     //printf("released python byte script\n");
00095     
00096     Py_XDECREF(m_bytecode);
00097     Py_XDECREF(m_function);
00098     
00099     if (m_pythondictionary) {
00100         // break any circular references in the dictionary
00101         PyDict_Clear(m_pythondictionary);
00102         Py_DECREF(m_pythondictionary);
00103     }
00104 #endif
00105 }
00106 
00107 
00108 
00109 CValue* SCA_PythonController::GetReplica()
00110 {
00111     SCA_PythonController* replica = new SCA_PythonController(*this);
00112 
00113 #ifdef WITH_PYTHON
00114     /* why is this needed at all??? - m_bytecode is NULL'd below so this doesnt make sense
00115      * but removing it crashes blender (with YoFrankie). so leave in for now - Campbell */
00116     Py_XINCREF(replica->m_bytecode);
00117     
00118     Py_XINCREF(replica->m_function); // this is ok since its not set to NULL
00119     replica->m_bModified = replica->m_bytecode == NULL;
00120     
00121     // The replica->m_pythondictionary is stolen - replace with a copy.
00122     if (m_pythondictionary)
00123         replica->m_pythondictionary = PyDict_Copy(m_pythondictionary);
00124         
00125     /*
00126     // The other option is to incref the replica->m_pythondictionary -
00127     // the replica objects can then share data.
00128     if (m_pythondictionary)
00129         Py_INCREF(replica->m_pythondictionary);
00130     */
00131 #endif
00132     
00133     // this will copy properties and so on...
00134     replica->ProcessReplica();
00135 
00136     return replica;
00137 }
00138 
00139 
00140 
00141 void SCA_PythonController::SetScriptText(const STR_String& text)
00142 { 
00143     m_scriptText = text;
00144     m_bModified = true;
00145 }
00146 
00147 
00148 
00149 void SCA_PythonController::SetScriptName(const STR_String& name)
00150 {
00151     m_scriptName = name;
00152 }
00153 
00154 
00155 #ifdef WITH_PYTHON
00156 void SCA_PythonController::SetNamespace(PyObject*   pythondictionary)
00157 {
00158     if (m_pythondictionary)
00159     {
00160         PyDict_Clear(m_pythondictionary);
00161         Py_DECREF(m_pythondictionary);
00162     }
00163     m_pythondictionary = PyDict_Copy(pythondictionary); /* new reference */
00164     
00165     /* Without __file__ set the sys.argv[0] is used for the filename
00166      * which ends up with lines from the blender binary being printed in the console */
00167     PyDict_SetItemString(m_pythondictionary, "__file__", PyUnicode_From_STR_String(m_scriptName));
00168     
00169 }
00170 #endif
00171 
00172 int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
00173 {
00174     if (std::find(m_triggeredSensors.begin(), m_triggeredSensors.end(), sensor) != 
00175         m_triggeredSensors.end())
00176         return 1;
00177     return 0;
00178 }
00179 
00180 #ifdef WITH_PYTHON
00181 
00182 /* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
00183 PyObject* SCA_PythonController::sPyGetCurrentController(PyObject *self)
00184 {
00185     if(m_sCurrentController==NULL)
00186     {
00187         PyErr_SetString(PyExc_SystemError, "bge.logic.getCurrentController(), this function is being run outside the python controllers context, or blenders internal state is corrupt.");
00188         return NULL;
00189     }
00190     return m_sCurrentController->GetProxy();
00191 }
00192 
00193 SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
00194 {
00195     // for safety, todo: only allow for registered actuators (pointertable)
00196     // we don't want to crash gameengine/blender by python scripts
00197     std::vector<SCA_IActuator*> lacts =  m_sCurrentController->GetLinkedActuators();
00198     std::vector<SCA_IActuator*>::iterator it;
00199     
00200     if (PyUnicode_Check(value)) {
00201         /* get the actuator from the name */
00202         const char *name= _PyUnicode_AsString(value);
00203         for(it = lacts.begin(); it!= lacts.end(); ++it) {
00204             if( name == (*it)->GetName() ) {
00205                 return *it;
00206             }
00207         }
00208     }
00209     else if (PyObject_TypeCheck(value, &SCA_IActuator::Type)) {
00210         PyObjectPlus *value_plus= BGE_PROXY_REF(value);
00211         for(it = lacts.begin(); it!= lacts.end(); ++it) {
00212             if( static_cast<SCA_IActuator*>(value_plus) == (*it) ) {
00213                 return *it;
00214             }
00215         }
00216     }
00217 
00218     /* set the exception */
00219     PyErr_Format(PyExc_ValueError,
00220                  "%R not in this python controllers actuator list", value);
00221 
00222     return NULL;
00223 }
00224 
00225 const char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()";
00226 
00227 PyTypeObject SCA_PythonController::Type = {
00228     PyVarObject_HEAD_INIT(NULL, 0)
00229     "SCA_PythonController",
00230     sizeof(PyObjectPlus_Proxy),
00231     0,
00232     py_base_dealloc,
00233     0,
00234     0,
00235     0,
00236     0,
00237     py_base_repr,
00238     0,0,0,0,0,0,0,0,0,
00239     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00240     0,0,0,0,0,0,0,
00241     Methods,
00242     0,
00243     0,
00244     &SCA_IController::Type,
00245     0,0,0,0,0,0,
00246     py_base_new
00247 };
00248 
00249 PyMethodDef SCA_PythonController::Methods[] = {
00250     {"activate", (PyCFunction) SCA_PythonController::sPyActivate, METH_O},
00251     {"deactivate", (PyCFunction) SCA_PythonController::sPyDeActivate, METH_O},
00252     {NULL,NULL} //Sentinel
00253 };
00254 
00255 PyAttributeDef SCA_PythonController::Attributes[] = {
00256     KX_PYATTRIBUTE_RW_FUNCTION("script", SCA_PythonController, pyattr_get_script, pyattr_set_script),
00257     KX_PYATTRIBUTE_INT_RO("mode", SCA_PythonController, m_mode),
00258     { NULL }    //Sentinel
00259 };
00260 
00261 void SCA_PythonController::ErrorPrint(const char *error_msg)
00262 {
00263     printf("%s - object '%s', controller '%s':\n", error_msg, GetParent()->GetName().Ptr(), GetName().Ptr());
00264     PyErr_Print();
00265     
00266     /* Added in 2.48a, the last_traceback can reference Objects for example, increasing
00267      * their user count. Not to mention holding references to wrapped data.
00268      * This is especially bad when the PyObject for the wrapped data is free'd, after blender 
00269      * has already dealocated the pointer */
00270     PySys_SetObject( (char *)"last_traceback", NULL);
00271     PyErr_Clear(); /* just to be sure */
00272 }
00273 
00274 bool SCA_PythonController::Compile()
00275 {   
00276     //printf("py script modified '%s'\n", m_scriptName.Ptr());
00277     m_bModified= false;
00278     
00279     // if a script already exists, decref it before replace the pointer to a new script
00280     if (m_bytecode) {
00281         Py_DECREF(m_bytecode);
00282         m_bytecode=NULL;
00283     }
00284     
00285     // recompile the scripttext into bytecode
00286     m_bytecode = Py_CompileString(m_scriptText.Ptr(), m_scriptName.Ptr(), Py_file_input);
00287     
00288     if (m_bytecode) {
00289         return true;
00290     } else {
00291         ErrorPrint("Python error compiling script");
00292         return false;
00293     }
00294 }
00295 
00296 bool SCA_PythonController::Import()
00297 {
00298     //printf("py module modified '%s'\n", m_scriptName.Ptr());
00299     m_bModified= false;
00300 
00301     /* incase we re-import */
00302     Py_XDECREF(m_function);
00303     m_function= NULL;
00304     
00305     STR_String mod_path_str= m_scriptText; /* just for storage, use C style string access */
00306     char *mod_path= mod_path_str.Ptr();
00307     char *function_string;
00308 
00309     function_string= strrchr(mod_path, '.');
00310 
00311     if(function_string == NULL) {
00312         printf("Python module name formatting error in object '%s', controller '%s':\n\texpected 'SomeModule.Func', got '%s'\n", GetParent()->GetName().Ptr(), GetName().Ptr(), m_scriptText.Ptr());
00313         return false;
00314     }
00315 
00316     *function_string= '\0';
00317     function_string++;
00318 
00319     // Import the module and print an error if it's not found
00320     PyObject *mod = PyImport_ImportModule(mod_path);
00321 
00322     if (mod == NULL) {
00323         ErrorPrint("Python module can't be imported");
00324         return false;
00325     }
00326 
00327     if(m_debug)
00328         mod = PyImport_ReloadModule(mod);
00329 
00330     if (mod == NULL) {
00331         ErrorPrint("Python module can't be reloaded");
00332         return false;
00333     }
00334 
00335     // Get the function object
00336     m_function = PyObject_GetAttrString(mod, function_string);
00337 
00338     // DECREF the module as we don't need it anymore
00339     Py_DECREF(mod);
00340 
00341     if(m_function==NULL) {
00342         if(PyErr_Occurred())
00343             ErrorPrint("Python controller found the module but could not access the function");
00344         else
00345             printf("Python module error in object '%s', controller '%s':\n '%s' module found but function missing\n", GetParent()->GetName().Ptr(), GetName().Ptr(), m_scriptText.Ptr());
00346         return false;
00347     }
00348     
00349     if(!PyCallable_Check(m_function)) {
00350         Py_DECREF(m_function);
00351         m_function = NULL;
00352         printf("Python module function error in object '%s', controller '%s':\n '%s' not callable\n", GetParent()->GetName().Ptr(), GetName().Ptr(), m_scriptText.Ptr());
00353         return false;
00354     }
00355     
00356     m_function_argc = 0; /* rare cases this could be a function that isnt defined in python, assume zero args */
00357     if (PyFunction_Check(m_function)) {
00358         m_function_argc= ((PyCodeObject *)PyFunction_GET_CODE(m_function))->co_argcount;
00359     }
00360     
00361     if(m_function_argc > 1) {
00362         Py_DECREF(m_function);
00363         m_function = NULL;
00364         printf("Python module function in object '%s', controller '%s':\n '%s' takes %d args, should be zero or 1 controller arg\n", GetParent()->GetName().Ptr(), GetName().Ptr(), m_scriptText.Ptr(), m_function_argc);
00365         return false;
00366     }
00367     
00368     return true;
00369 }
00370 
00371 
00372 void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
00373 {
00374     m_sCurrentController = this;
00375     m_sCurrentLogicManager = logicmgr;
00376     
00377     PyObject *excdict=      NULL;
00378     PyObject* resultobj=    NULL;
00379     
00380     switch(m_mode) {
00381     case SCA_PYEXEC_SCRIPT:
00382     {
00383         if (m_bModified)
00384             if (Compile()==false) // sets m_bModified to false
00385                 return;
00386         if (!m_bytecode)
00387             return;
00388         
00389         /*
00390          * This part here with excdict is a temporary patch
00391          * to avoid python/gameengine crashes when python
00392          * inadvertently holds references to game objects
00393          * in global variables.
00394          * 
00395          * The idea is always make a fresh dictionary, and
00396          * destroy it right after it is used to make sure
00397          * python won't hold any gameobject references.
00398          * 
00399          * Note that the PyDict_Clear _is_ necessary before
00400          * the Py_DECREF() because it is possible for the
00401          * variables inside the dictionary to hold references
00402          * to the dictionary (ie. generate a cycle), so we
00403          * break it by hand, then DECREF (which in this case
00404          * should always ensure excdict is cleared).
00405          */
00406 
00407         excdict= PyDict_Copy(m_pythondictionary);
00408 
00409         resultobj = PyEval_EvalCode((PyObject *)m_bytecode, excdict, excdict);
00410 
00411         /* PyRun_SimpleString(m_scriptText.Ptr()); */
00412         break;
00413     }
00414     case SCA_PYEXEC_MODULE:
00415     {
00416         if (m_bModified || m_debug)
00417             if (Import()==false) // sets m_bModified to false
00418                 return;
00419         if (!m_function)
00420             return;
00421         
00422         PyObject *args= NULL;
00423         
00424         if(m_function_argc==1) {
00425             args = PyTuple_New(1);
00426             PyTuple_SET_ITEM(args, 0, GetProxy());
00427         }
00428         
00429         resultobj = PyObject_CallObject(m_function, args);
00430         Py_XDECREF(args);
00431         break;
00432     }
00433     
00434     } /* end switch */
00435     
00436     
00437     
00438     /* Free the return value and print the error */
00439     if (resultobj)
00440         Py_DECREF(resultobj);
00441     else
00442         ErrorPrint("Python script error");
00443     
00444     if(excdict) /* Only for SCA_PYEXEC_SCRIPT types */
00445     {
00446         /* clear after PyErrPrint - seems it can be using
00447          * something in this dictionary and crash? */
00448         // This doesn't appear to be needed anymore
00449         //PyDict_Clear(excdict);
00450         Py_DECREF(excdict);
00451     }   
00452     
00453     m_triggeredSensors.clear();
00454     m_sCurrentController = NULL;
00455 }
00456 
00457 PyObject* SCA_PythonController::PyActivate(PyObject *value)
00458 {
00459     if(m_sCurrentController != this) {
00460         PyErr_SetString(PyExc_SystemError, "Cannot add an actuator from a non-active controller");
00461         return NULL;
00462     }
00463     
00464     SCA_IActuator* actu = LinkedActuatorFromPy(value);
00465     if(actu==NULL)
00466         return NULL;
00467     
00468     m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, true);
00469     Py_RETURN_NONE;
00470 }
00471 
00472 PyObject* SCA_PythonController::PyDeActivate(PyObject *value)
00473 {
00474     if(m_sCurrentController != this) {
00475         PyErr_SetString(PyExc_SystemError, "Cannot add an actuator from a non-active controller");
00476         return NULL;
00477     }
00478     
00479     SCA_IActuator* actu = LinkedActuatorFromPy(value);
00480     if(actu==NULL)
00481         return NULL;
00482     
00483     m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, false);
00484     Py_RETURN_NONE;
00485 }
00486 
00487 PyObject* SCA_PythonController::pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00488 {
00489     //SCA_PythonController* self= static_cast<SCA_PythonController*>(static_cast<SCA_IController*>(static_cast<SCA_ILogicBrick*>(static_cast<CValue*>(static_cast<PyObjectPlus*>(self_v)))));
00490     // static_cast<void *>(dynamic_cast<Derived *>(obj)) - static_cast<void *>(obj)
00491 
00492     SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
00493     return PyUnicode_From_STR_String(self->m_scriptText);
00494 }
00495 
00496 
00497 
00498 int SCA_PythonController::pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
00499 {
00500     SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
00501     
00502     const char *scriptArg = _PyUnicode_AsString(value);
00503     
00504     if (scriptArg==NULL) {
00505         PyErr_SetString(PyExc_TypeError, "controller.script = string: Python Controller, expected a string script text");
00506         return PY_SET_ATTR_FAIL;
00507     }
00508 
00509     /* set scripttext sets m_bModified to true, 
00510         so next time the script is needed, a reparse into byte code is done */
00511     self->SetScriptText(scriptArg);
00512         
00513     return PY_SET_ATTR_SUCCESS;
00514 }
00515 
00516 #else // WITH_PYTHON
00517 
00518 void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
00519 {
00520     /* intentionally blank */
00521 }
00522 
00523 #endif // WITH_PYTHON
00524 
00525 /* eof */