2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
* Execute Python scripts
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* 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
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SCA_PythonController.h"
|
|
|
|
#include "SCA_LogicManager.h"
|
|
|
|
#include "SCA_ISensor.h"
|
|
|
|
#include "SCA_IActuator.h"
|
2008-09-06 14:13:31 +00:00
|
|
|
#include "PyObjectPlus.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "compile.h"
|
|
|
|
#include "eval.h"
|
2008-08-23 11:54:27 +00:00
|
|
|
#include <algorithm>
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2008-09-06 02:46:11 +00:00
|
|
|
|
2002-11-25 15:29:57 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
// initialize static member variables
|
|
|
|
SCA_PythonController* SCA_PythonController::m_sCurrentController = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj,
|
|
|
|
PyTypeObject* T)
|
|
|
|
: SCA_IController(gameobj, T),
|
|
|
|
m_bytecode(NULL),
|
2004-03-22 22:02:18 +00:00
|
|
|
m_bModified(true),
|
|
|
|
m_pythondictionary(NULL)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-14 03:39:07 +00:00
|
|
|
/*
|
|
|
|
//debugging
|
|
|
|
CValue* SCA_PythonController::AddRef()
|
|
|
|
{
|
|
|
|
//printf("AddRef refcount = %i\n",GetRefCount());
|
|
|
|
return CValue::AddRef();
|
|
|
|
}
|
|
|
|
int SCA_PythonController::Release()
|
|
|
|
{
|
|
|
|
//printf("Release refcount = %i\n",GetRefCount());
|
|
|
|
return CValue::Release();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
SCA_PythonController::~SCA_PythonController()
|
|
|
|
{
|
|
|
|
if (m_bytecode)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
//printf("released python byte script\n");
|
|
|
|
Py_DECREF(m_bytecode);
|
|
|
|
}
|
2005-01-23 01:38:41 +00:00
|
|
|
|
|
|
|
if (m_pythondictionary)
|
|
|
|
{
|
|
|
|
// break any circular references in the dictionary
|
|
|
|
PyDict_Clear(m_pythondictionary);
|
|
|
|
Py_DECREF(m_pythondictionary);
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CValue* SCA_PythonController::GetReplica()
|
|
|
|
{
|
|
|
|
SCA_PythonController* replica = new SCA_PythonController(*this);
|
2005-01-23 01:38:41 +00:00
|
|
|
// Copy the compiled bytecode if possible.
|
|
|
|
Py_XINCREF(replica->m_bytecode);
|
|
|
|
replica->m_bModified = replica->m_bytecode == NULL;
|
|
|
|
|
|
|
|
// The replica->m_pythondictionary is stolen - replace with a copy.
|
|
|
|
if (m_pythondictionary)
|
|
|
|
replica->m_pythondictionary = PyDict_Copy(m_pythondictionary);
|
|
|
|
|
|
|
|
/*
|
|
|
|
// The other option is to incref the replica->m_pythondictionary -
|
|
|
|
// the replica objects can then share data.
|
|
|
|
if (m_pythondictionary)
|
|
|
|
Py_INCREF(replica->m_pythondictionary);
|
|
|
|
*/
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
// this will copy properties and so on...
|
|
|
|
CValue::AddDataToReplica(replica);
|
|
|
|
|
|
|
|
return replica;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SCA_PythonController::SetScriptText(const STR_String& text)
|
|
|
|
{
|
2008-07-14 00:47:07 +00:00
|
|
|
m_scriptText = text;
|
2002-10-12 11:37:38 +00:00
|
|
|
m_bModified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SCA_PythonController::SetScriptName(const STR_String& name)
|
|
|
|
{
|
|
|
|
m_scriptName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SCA_PythonController::SetDictionary(PyObject* pythondictionary)
|
|
|
|
{
|
2005-01-23 01:38:41 +00:00
|
|
|
if (m_pythondictionary)
|
|
|
|
{
|
|
|
|
PyDict_Clear(m_pythondictionary);
|
|
|
|
Py_DECREF(m_pythondictionary);
|
|
|
|
}
|
|
|
|
m_pythondictionary = PyDict_Copy(pythondictionary); /* new reference */
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2008-08-23 11:54:27 +00:00
|
|
|
int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
|
|
|
|
{
|
|
|
|
if (std::find(m_triggeredSensors.begin(), m_triggeredSensors.end(), sensor) !=
|
|
|
|
m_triggeredSensors.end())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:10:22 +00:00
|
|
|
#if 0
|
2008-09-20 11:08:35 +00:00
|
|
|
static const char* sPyGetCurrentController__doc__;
|
2005-03-14 20:10:22 +00:00
|
|
|
#endif
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
/* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
|
|
|
|
PyObject* SCA_PythonController::sPyGetCurrentController(PyObject *self)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
return m_sCurrentController->GetProxy();
|
2009-02-24 05:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (PyString_Check(value)) {
|
|
|
|
/* get the actuator from the name */
|
|
|
|
char *name= PyString_AsString(value);
|
|
|
|
for(it = lacts.begin(); it!= lacts.end(); it++) {
|
|
|
|
if( name == (*it)->GetName() ) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-20 15:06:46 +00:00
|
|
|
else if (BGE_PROXY_CHECK_TYPE(value)) {
|
|
|
|
PyObjectPlus *value_plus= BGE_PROXY_REF(value); /* Expecting an actuator type */ // XXX TODO - CHECK TYPE
|
2009-02-24 05:50:45 +00:00
|
|
|
for(it = lacts.begin(); it!= lacts.end(); it++) {
|
2009-04-20 15:06:46 +00:00
|
|
|
if( static_cast<SCA_IActuator*>(value_plus) == (*it) ) {
|
2009-02-24 05:50:45 +00:00
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the exception */
|
|
|
|
PyObject *value_str = PyObject_Repr(value); /* new ref */
|
2009-04-20 15:06:46 +00:00
|
|
|
PyErr_Format(PyExc_ValueError, "'%s' not in this python controllers actuator list", PyString_AsString(value_str));
|
2009-02-24 05:50:45 +00:00
|
|
|
Py_DECREF(value_str);
|
|
|
|
|
|
|
|
return false;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2005-03-14 20:10:22 +00:00
|
|
|
#if 0
|
2008-09-20 11:08:35 +00:00
|
|
|
static const char* sPyAddActiveActuator__doc__;
|
2005-03-14 20:10:22 +00:00
|
|
|
#endif
|
2006-01-06 03:46:54 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
/* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
|
|
|
|
PyObject* SCA_PythonController::sPyAddActiveActuator(PyObject* self, PyObject* args)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
PyObject* ob1;
|
|
|
|
int activate;
|
2009-04-20 15:06:46 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "Oi:addActiveActuator", &ob1,&activate))
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
|
2009-02-24 05:50:45 +00:00
|
|
|
SCA_IActuator* actu = LinkedActuatorFromPy(ob1);
|
|
|
|
if(actu==NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CValue* boolval = new CBoolValue(activate!=0);
|
|
|
|
m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu,boolval);
|
|
|
|
boolval->Release();
|
2008-07-04 00:05:50 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-20 11:08:35 +00:00
|
|
|
const char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()";
|
|
|
|
const char* SCA_PythonController::sPyAddActiveActuator__doc__= "addActiveActuator(actuator,bool)";
|
|
|
|
const char SCA_PythonController::GetActuators_doc[] = "getActuator";
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
PyTypeObject SCA_PythonController::Type = {
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
2002-10-12 11:37:38 +00:00
|
|
|
0,
|
|
|
|
"SCA_PythonController",
|
2009-04-20 15:06:46 +00:00
|
|
|
sizeof(PyObjectPlus_Proxy),
|
2002-10-12 11:37:38 +00:00
|
|
|
0,
|
2009-04-20 15:06:46 +00:00
|
|
|
py_base_dealloc,
|
2002-10-12 11:37:38 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2009-04-20 15:06:46 +00:00
|
|
|
py_base_repr,
|
|
|
|
0,0,0,0,0,0,
|
|
|
|
py_base_getattro,
|
|
|
|
py_base_setattro,
|
|
|
|
0,0,0,0,0,0,0,0,0,
|
|
|
|
Methods
|
2002-10-12 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PyParentObject SCA_PythonController::Parents[] = {
|
|
|
|
&SCA_PythonController::Type,
|
|
|
|
&SCA_IController::Type,
|
|
|
|
&CValue::Type,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
PyMethodDef SCA_PythonController::Methods[] = {
|
2009-02-24 05:50:45 +00:00
|
|
|
{"activate", (PyCFunction) SCA_PythonController::sPyActivate, METH_O},
|
|
|
|
{"deactivate", (PyCFunction) SCA_PythonController::sPyDeActivate, METH_O},
|
|
|
|
|
2008-10-02 00:22:28 +00:00
|
|
|
{"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},
|
2008-12-29 16:36:58 +00:00
|
|
|
//Deprecated functions ------>
|
2009-02-19 13:42:07 +00:00
|
|
|
{"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O},
|
2008-12-29 16:36:58 +00:00
|
|
|
{"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS},
|
2008-07-04 00:05:50 +00:00
|
|
|
{"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS},
|
2008-12-29 16:36:58 +00:00
|
|
|
//<----- Deprecated
|
2002-10-12 11:37:38 +00:00
|
|
|
{NULL,NULL} //Sentinel
|
|
|
|
};
|
|
|
|
|
2009-02-26 09:04:06 +00:00
|
|
|
PyAttributeDef SCA_PythonController::Attributes[] = {
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYATTRIBUTE_RO_FUNCTION("state", SCA_PythonController, pyattr_get_state),
|
|
|
|
KX_PYATTRIBUTE_RW_FUNCTION("script", SCA_PythonController, pyattr_get_script, pyattr_set_script),
|
2009-02-26 09:04:06 +00:00
|
|
|
{ NULL } //Sentinel
|
|
|
|
};
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-02-24 03:29:31 +00:00
|
|
|
bool SCA_PythonController::Compile()
|
|
|
|
{
|
|
|
|
//printf("py script modified '%s'\n", m_scriptName.Ptr());
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
m_bModified=false;
|
|
|
|
|
|
|
|
if (m_bytecode)
|
|
|
|
{
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// 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 */
|
2009-04-20 15:06:46 +00:00
|
|
|
PySys_SetObject( (char *)"last_traceback", NULL);
|
2009-02-24 03:29:31 +00:00
|
|
|
PyErr_Clear(); /* just to be sure */
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
|
|
|
|
{
|
|
|
|
m_sCurrentController = this;
|
|
|
|
m_sCurrentLogicManager = logicmgr;
|
|
|
|
|
|
|
|
if (m_bModified)
|
|
|
|
{
|
2009-02-24 03:29:31 +00:00
|
|
|
if (Compile()==false) // sets m_bModified to false
|
2004-06-04 02:10:58 +00:00
|
|
|
return;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2009-02-24 03:29:31 +00:00
|
|
|
if (!m_bytecode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This part here with excdict is a temporary patch
|
|
|
|
* to avoid python/gameengine crashes when python
|
|
|
|
* inadvertently holds references to game objects
|
|
|
|
* in global variables.
|
|
|
|
*
|
|
|
|
* The idea is always make a fresh dictionary, and
|
|
|
|
* destroy it right after it is used to make sure
|
|
|
|
* python won't hold any gameobject references.
|
|
|
|
*
|
|
|
|
* Note that the PyDict_Clear _is_ necessary before
|
|
|
|
* the Py_DECREF() because it is possible for the
|
|
|
|
* variables inside the dictionary to hold references
|
|
|
|
* to the dictionary (ie. generate a cycle), so we
|
|
|
|
* break it by hand, then DECREF (which in this case
|
|
|
|
* should always ensure excdict is cleared).
|
|
|
|
*/
|
2006-02-13 05:45:32 +00:00
|
|
|
|
2006-01-06 03:46:54 +00:00
|
|
|
PyObject *excdict= PyDict_Copy(m_pythondictionary);
|
|
|
|
PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
|
2008-08-13 10:19:47 +00:00
|
|
|
excdict, excdict);
|
2006-01-06 03:46:54 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (resultobj)
|
|
|
|
{
|
|
|
|
Py_DECREF(resultobj);
|
2008-08-13 10:19:47 +00:00
|
|
|
}
|
|
|
|
else
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
// something is wrong, tell the user what went wrong
|
2008-08-16 00:06:51 +00:00
|
|
|
printf("Python script error from controller \"%s\": \n", GetName().Ptr());
|
2005-01-23 01:38:41 +00:00
|
|
|
PyErr_Print();
|
2008-10-22 03:10:00 +00:00
|
|
|
|
|
|
|
/* 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 */
|
2009-04-20 15:06:46 +00:00
|
|
|
PySys_SetObject( (char *)"last_traceback", NULL);
|
2008-10-22 07:09:15 +00:00
|
|
|
PyErr_Clear(); /* just to be sure */
|
2008-10-22 03:10:00 +00:00
|
|
|
|
2005-01-23 01:38:41 +00:00
|
|
|
//PyRun_SimpleString(m_scriptText.Ptr());
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2008-08-13 10:19:47 +00:00
|
|
|
|
|
|
|
// clear after PyErrPrint - seems it can be using
|
|
|
|
// something in this dictionary and crash?
|
|
|
|
PyDict_Clear(excdict);
|
|
|
|
Py_DECREF(excdict);
|
2008-08-23 11:54:27 +00:00
|
|
|
m_triggeredSensors.erase(m_triggeredSensors.begin(), m_triggeredSensors.end());
|
2002-10-12 11:37:38 +00:00
|
|
|
m_sCurrentController = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::py_getattro(PyObject *attr)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
py_getattro_up(SCA_IController);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
int SCA_PythonController::py_setattro(PyObject *attr, PyObject *value)
|
2008-12-29 16:36:58 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
py_setattro_up(SCA_IController);
|
2008-12-29 16:36:58 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::PyActivate(PyObject *value)
|
2009-02-24 05:50:45 +00:00
|
|
|
{
|
|
|
|
SCA_IActuator* actu = LinkedActuatorFromPy(value);
|
|
|
|
if(actu==NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CValue* boolval = new CBoolValue(true);
|
|
|
|
m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, boolval);
|
|
|
|
boolval->Release();
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::PyDeActivate(PyObject *value)
|
2009-02-24 05:50:45 +00:00
|
|
|
{
|
|
|
|
SCA_IActuator* actu = LinkedActuatorFromPy(value);
|
|
|
|
if(actu==NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CValue* boolval = new CBoolValue(false);
|
|
|
|
m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)actu, boolval);
|
|
|
|
boolval->Release();
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::PyGetActuators()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
PyObject* resultlist = PyList_New(m_linkedactuators.size());
|
2004-03-22 22:02:18 +00:00
|
|
|
for (unsigned int index=0;index<m_linkedactuators.size();index++)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
PyList_SET_ITEM(resultlist,index, m_linkedactuators[index]->GetProxy());
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resultlist;
|
|
|
|
}
|
|
|
|
|
2008-09-20 11:08:35 +00:00
|
|
|
const char SCA_PythonController::GetSensor_doc[] =
|
2009-04-20 15:06:46 +00:00
|
|
|
"getSensor (char sensorname) return linked sensor that is named [sensorname]\n";
|
2002-10-12 11:37:38 +00:00
|
|
|
PyObject*
|
2009-04-20 15:06:46 +00:00
|
|
|
SCA_PythonController::PyGetSensor(PyObject* value)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
2008-07-04 00:05:50 +00:00
|
|
|
char *scriptArg = PyString_AsString(value);
|
|
|
|
if (scriptArg==NULL) {
|
2009-04-20 15:06:46 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "controller.getSensor(string): Python Controller, expected a string (sensor name)");
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:02:18 +00:00
|
|
|
for (unsigned int index=0;index<m_linkedsensors.size();index++)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
SCA_ISensor* sensor = m_linkedsensors[index];
|
|
|
|
STR_String realname = sensor->GetName();
|
|
|
|
if (realname == scriptArg)
|
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
return sensor->GetProxy();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 00:47:07 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError, "controller.getSensor(string): Python Controller, unable to find requested sensor \"%s\"", scriptArg);
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-09-20 11:08:35 +00:00
|
|
|
const char SCA_PythonController::GetActuator_doc[] =
|
2009-04-20 15:06:46 +00:00
|
|
|
"getActuator (char sensorname) return linked actuator that is named [actuatorname]\n";
|
2002-10-12 11:37:38 +00:00
|
|
|
PyObject*
|
2009-04-20 15:06:46 +00:00
|
|
|
SCA_PythonController::PyGetActuator(PyObject* value)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
2008-07-04 00:05:50 +00:00
|
|
|
char *scriptArg = PyString_AsString(value);
|
|
|
|
if (scriptArg==NULL) {
|
2009-04-20 15:06:46 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "controller.getActuator(string): Python Controller, expected a string (actuator name)");
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:02:18 +00:00
|
|
|
for (unsigned int index=0;index<m_linkedactuators.size();index++)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
SCA_IActuator* actua = m_linkedactuators[index];
|
2009-02-24 05:50:45 +00:00
|
|
|
if (actua->GetName() == scriptArg)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
return actua->GetProxy();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 00:47:07 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError, "controller.getActuator(string): Python Controller, unable to find requested actuator \"%s\"", scriptArg);
|
2002-10-12 11:37:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-20 11:08:35 +00:00
|
|
|
const char SCA_PythonController::GetSensors_doc[] = "getSensors returns a list of all attached sensors";
|
2002-10-12 11:37:38 +00:00
|
|
|
PyObject*
|
2009-04-20 15:06:46 +00:00
|
|
|
SCA_PythonController::PyGetSensors()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
PyObject* resultlist = PyList_New(m_linkedsensors.size());
|
2004-03-22 22:02:18 +00:00
|
|
|
for (unsigned int index=0;index<m_linkedsensors.size();index++)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
PyList_SET_ITEM(resultlist,index, m_linkedsensors[index]->GetProxy());
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resultlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1. getScript */
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::PyGetScript()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2008-12-29 16:36:58 +00:00
|
|
|
ShowDeprecationWarning("getScript()", "the script property");
|
2002-10-12 11:37:38 +00:00
|
|
|
return PyString_FromString(m_scriptText);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. setScript */
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::PySetScript(PyObject* value)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2008-07-04 00:05:50 +00:00
|
|
|
char *scriptArg = PyString_AsString(value);
|
2009-02-19 13:42:07 +00:00
|
|
|
|
|
|
|
ShowDeprecationWarning("setScript()", "the script property");
|
|
|
|
|
2008-07-04 00:05:50 +00:00
|
|
|
if (scriptArg==NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "expected a string (script name)");
|
2002-10-12 11:37:38 +00:00
|
|
|
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);
|
|
|
|
|
2008-07-04 00:05:50 +00:00
|
|
|
Py_RETURN_NONE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 21:52:23 +00:00
|
|
|
/* 1. getScript */
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::PyGetState()
|
2008-06-29 21:52:23 +00:00
|
|
|
{
|
2008-12-29 16:36:58 +00:00
|
|
|
ShowDeprecationWarning("getState()", "the state property");
|
2008-06-29 21:52:23 +00:00
|
|
|
return PyInt_FromLong(m_statemask);
|
|
|
|
}
|
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* SCA_PythonController::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
|
|
|
|
{
|
|
|
|
SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
|
|
|
|
return PyInt_FromLong(self->m_statemask);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* SCA_PythonController::pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
|
|
|
|
{
|
|
|
|
SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
|
|
|
|
return PyString_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 = PyString_AsString(value);
|
|
|
|
|
|
|
|
if (scriptArg==NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "controller.script = string: Python Controller, expected a string script text");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/* eof */
|