[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19099] trunk/blender/source/gameengine: BGE Py API

Campbell Barton ideasman42 at gmail.com
Tue Feb 24 06:51:11 CET 2009


Revision: 19099
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19099
Author:   campbellbarton
Date:     2009-02-24 06:50:45 +0100 (Tue, 24 Feb 2009)

Log Message:
-----------
BGE Py API
* Made GameLogic.addActiveActuator(actu, bool) to raise an error if the actuator is not in the list. Before it would allow any value as the actuator and fail silently (makes debugging scripts more difficult).

* Allow the actuator to be a string which is convenient if you dont want to change the settings of the actuator.
* Added activate/deactivate functions to the controller, this is more logical since the GameLogic.addActiveActuator() function is running through the controller anyway.

GameLogic.addActiveActuator(controller.getActuator("SomeAct"), True)
...can be replaced with...
controller.activate("SomeAct")

Modified Paths:
--------------
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h
    trunk/blender/source/gameengine/PyDoc/GameLogic.py
    trunk/blender/source/gameengine/PyDoc/KX_Scene.py
    trunk/blender/source/gameengine/PyDoc/SCA_PythonController.py

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-02-24 03:29:31 UTC (rev 19098)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-02-24 05:50:45 UTC (rev 19099)
@@ -157,10 +157,42 @@
 
 PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self)
 {
-	m_sCurrentController->AddRef();
-	return m_sCurrentController;
+	return m_sCurrentController->AddRef();
 }
 
+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;
+			}
+		}
+	}
+	else {
+		/* Expecting an actuator type */
+		for(it = lacts.begin(); it!= lacts.end(); it++) {
+			if( static_cast<SCA_IActuator*>(value) == (*it) ) {
+				return *it;
+			}
+		}
+	}
+	
+	/* set the exception */
+	PyObject *value_str = PyObject_Repr(value); /* new ref */
+	PyErr_Format(PyExc_ValueError, "'%s' not in this controllers actuator list", PyString_AsString(value_str));
+	Py_DECREF(value_str);
+	
+	return false;
+}
+
 #if 0
 static const char* sPyAddActiveActuator__doc__;
 #endif
@@ -175,26 +207,14 @@
 	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(found){
-		CValue* boolval = new CBoolValue(activate!=0);
-		m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)act,boolval);
-		boolval->Release();
-	}
+	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();
 	Py_RETURN_NONE;
 }
 
@@ -229,6 +249,9 @@
 	NULL
 };
 PyMethodDef SCA_PythonController::Methods[] = {
+	{"activate", (PyCFunction) SCA_PythonController::sPyActivate, METH_O},
+	{"deactivate", (PyCFunction) SCA_PythonController::sPyDeActivate, METH_O},
+	
 	{"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},
@@ -380,7 +403,30 @@
 	return SCA_IController::_setattr(attr, value);
 }
 
+PyObject* SCA_PythonController::PyActivate(PyObject* self, PyObject *value)
+{
+	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;
+}
 
+PyObject* SCA_PythonController::PyDeActivate(PyObject* self, PyObject *value)
+{
+	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;
+}
+
 PyObject* SCA_PythonController::PyGetActuators(PyObject* self)
 {
 	PyObject* resultlist = PyList_New(m_linkedactuators.size());
@@ -437,8 +483,7 @@
 	for (unsigned int index=0;index<m_linkedactuators.size();index++)
 	{
 		SCA_IActuator* actua = m_linkedactuators[index];
-		STR_String realname = actua->GetName();
-		if (realname == scriptArg)
+		if (actua->GetName() == scriptArg)
 		{
 			return actua->AddRef();
 		}

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h	2009-02-24 03:29:31 UTC (rev 19098)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h	2009-02-24 05:50:45 UTC (rev 19099)
@@ -77,9 +77,13 @@
 	static const char* sPyAddActiveActuator__doc__;
 	static PyObject* sPyAddActiveActuator(PyObject* self, 
 										  PyObject* args);
+	static SCA_IActuator* LinkedActuatorFromPy(PyObject *value);
 	virtual PyObject* _getattr(const char *attr);
 	virtual int _setattr(const char *attr, PyObject *value);
 
+		
+	KX_PYMETHOD_O(SCA_PythonController,Activate);
+	KX_PYMETHOD_O(SCA_PythonController,DeActivate);
 	KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetSensors);
 	KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetActuators);
 	KX_PYMETHOD_DOC_O(SCA_PythonController,GetSensor);

Modified: trunk/blender/source/gameengine/PyDoc/GameLogic.py
===================================================================
--- trunk/blender/source/gameengine/PyDoc/GameLogic.py	2009-02-24 03:29:31 UTC (rev 19098)
+++ trunk/blender/source/gameengine/PyDoc/GameLogic.py	2009-02-24 05:50:45 UTC (rev 19099)
@@ -159,7 +159,7 @@
 	"""
 	Activates the given actuator.
 	
-	@type actuator: L{SCA_IActuator}
+	@type actuator: L{SCA_IActuator} or the actuator name as a string.
 	@type activate: boolean
 	@param activate: whether to activate or deactivate the given actuator.
 	"""

Modified: trunk/blender/source/gameengine/PyDoc/KX_Scene.py
===================================================================
--- trunk/blender/source/gameengine/PyDoc/KX_Scene.py	2009-02-24 03:29:31 UTC (rev 19098)
+++ trunk/blender/source/gameengine/PyDoc/KX_Scene.py	2009-02-24 05:50:45 UTC (rev 19099)
@@ -39,6 +39,8 @@
 		
 	@ivar name: The scene's name
 	@type name: string
+	@type objects: A list of objects in the scene.
+	@type objects: list [L{KX_GameObject}]
 	@ivar active_camera: The current active camera
 	@type active_camera: L{KX_Camera}
 	@ivar suspended: True if the scene is suspended.

Modified: trunk/blender/source/gameengine/PyDoc/SCA_PythonController.py
===================================================================
--- trunk/blender/source/gameengine/PyDoc/SCA_PythonController.py	2009-02-24 03:29:31 UTC (rev 19098)
+++ trunk/blender/source/gameengine/PyDoc/SCA_PythonController.py	2009-02-24 05:50:45 UTC (rev 19099)
@@ -15,7 +15,17 @@
 	             This can be used with the GameObject's state to test if the controller is active.
 	@type state: integer
 	"""
-
+	def activate(actuator):
+		"""
+		Activates an actuator attached to this controller.
+		@type actuator: actuator or the actuator name as a string
+		"""
+	def deactivate(actuator):
+		"""
+		Deactivates an actuator attached to this controller.
+		@type actuator: actuator or the actuator name as a string
+		"""
+		
 	def getSensors():
 		"""
 		Gets a list of all sensors attached to this controller.





More information about the Bf-blender-cvs mailing list