[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20083] trunk/blender/source: moved py controller functions from SCA_PythonController to SCA_IController - the base controller class so python can get the sensors & actuators from any controller (not just SCA_PythonController types)

Campbell Barton ideasman42 at gmail.com
Wed May 6 11:12:08 CEST 2009


Revision: 20083
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20083
Author:   campbellbarton
Date:     2009-05-06 11:12:08 +0200 (Wed, 06 May 2009)

Log Message:
-----------
moved py controller functions from SCA_PythonController to SCA_IController - the base controller class so python can get the sensors & actuators from any controller (not just SCA_PythonController types)

also deprecated getActuators() and getSensors() for 'sensors' and 'actuators' attributes.

an example of getting every sensor connected to an object.
 all_sensors = [s for c in ob.controllers for s in c.sensors]

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/doc/Mathutils.py
    trunk/blender/source/gameengine/GameLogic/SCA_IController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_IController.h
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h
    trunk/blender/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
    trunk/blender/source/gameengine/PyDoc/GameLogic.py
    trunk/blender/source/gameengine/PyDoc/KX_SceneActuator.py
    trunk/blender/source/gameengine/PyDoc/SCA_IController.py
    trunk/blender/source/gameengine/PyDoc/SCA_PythonController.py

Modified: trunk/blender/source/blender/python/api2_2x/doc/Mathutils.py
===================================================================
--- trunk/blender/source/blender/python/api2_2x/doc/Mathutils.py	2009-05-06 08:33:40 UTC (rev 20082)
+++ trunk/blender/source/blender/python/api2_2x/doc/Mathutils.py	2009-05-06 09:12:08 UTC (rev 20083)
@@ -440,7 +440,7 @@
   Example::
       wrappedObject = Object.getAttribute() #this is wrapped data
       print wrappedObject.wrapped #prints 'True'
-      copyOfObject = Object(wrappedObject) #creates a copy of the object
+      copyOfObject = wrappedObject.copy() #creates a copy of the object
       secondPointer = wrappedObject #creates a second pointer to the same data
       print wrappedObject.attribute #prints '5'
       secondPointer.attribute = 10
@@ -564,7 +564,7 @@
   Example::
       wrappedObject = Object.getAttribute() #this is wrapped data
       print wrappedObject.wrapped #prints 'True'
-      copyOfObject = Object(wrappedObject) #creates a copy of the object
+      copyOfObject = wrappedObject.copy() #creates a copy of the object
       secondPointer = wrappedObject #creates a second pointer to the same data
       print wrappedObject.attribute #prints '5'
       secondPointer.attribute = 10
@@ -652,7 +652,7 @@
   Example::
       wrappedObject = Object.getAttribute() #this is wrapped data
       print wrappedObject.wrapped #prints 'True'
-      copyOfObject = Object(wrappedObject) #creates a copy of the object
+      copyOfObject = wrappedObject.copy() #creates a copy of the object
       secondPointer = wrappedObject #creates a second pointer to the same data
       print wrappedObject.attribute #prints '5'
       secondPointer.attribute = 10
@@ -760,7 +760,7 @@
   Example::
       wrappedObject = Object.getAttribute() #this is wrapped data
       print wrappedObject.wrapped #prints 'True'
-      copyOfObject = Object(wrappedObject) #creates a copy of the object
+      copyOfObject = wrappedObject.copy() #creates a copy of the object
       secondPointer = wrappedObject #creates a second pointer to the same data
       print wrappedObject.attribute #prints '5'
       secondPointer.attribute = 10

Modified: trunk/blender/source/gameengine/GameLogic/SCA_IController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_IController.cpp	2009-05-06 08:33:40 UTC (rev 20082)
+++ trunk/blender/source/gameengine/GameLogic/SCA_IController.cpp	2009-05-06 09:12:08 UTC (rev 20083)
@@ -30,6 +30,7 @@
 #include "SCA_LogicManager.h"
 #include "SCA_IActuator.h"
 #include "SCA_ISensor.h"
+#include "PyObjectPlus.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -207,3 +208,176 @@
 	}
 }
 
+/* Python api */
+
+PyTypeObject SCA_IController::Type = {
+#if (PY_VERSION_HEX >= 0x02060000)
+	PyVarObject_HEAD_INIT(NULL, 0)
+#else
+	/* python 2.5 and below */
+	PyObject_HEAD_INIT( NULL )  /* required py macro */
+	0,                          /* ob_size */
+#endif
+		"SCA_IController",
+		sizeof(PyObjectPlus_Proxy),
+		0,
+		py_base_dealloc,
+		0,
+		0,
+		0,
+		0,
+		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
+};
+
+PyParentObject SCA_IController::Parents[] = {
+	&SCA_IController::Type,
+	&CValue::Type,
+	NULL
+};
+
+PyMethodDef SCA_IController::Methods[] = {
+	{"getActuator", (PyCFunction) SCA_IController::sPyGetActuator, METH_O},
+	{"getSensor", (PyCFunction) SCA_IController::sPyGetSensor, METH_O},
+	
+	//Deprecated functions ------>
+	{"getSensors", (PyCFunction) SCA_IController::sPyGetSensors, METH_NOARGS},
+	{"getActuators", (PyCFunction) SCA_IController::sPyGetActuators, METH_NOARGS},
+	{"getState", (PyCFunction) SCA_IController::sPyGetState, METH_NOARGS},
+	//<----- Deprecated
+	{NULL,NULL} //Sentinel
+};
+
+PyAttributeDef SCA_IController::Attributes[] = {
+	KX_PYATTRIBUTE_RO_FUNCTION("state", SCA_IController, pyattr_get_state),
+	KX_PYATTRIBUTE_RO_FUNCTION("sensors", SCA_IController, pyattr_get_sensors),
+	KX_PYATTRIBUTE_RO_FUNCTION("actuators", SCA_IController, pyattr_get_actuators),
+	{ NULL }	//Sentinel
+};
+
+PyObject* SCA_IController::py_getattro(PyObject *attr)
+{
+	py_getattro_up(SCA_ILogicBrick);
+}
+
+PyObject* SCA_IController::py_getattro_dict() {
+	py_getattro_dict_up(SCA_ILogicBrick);
+}
+
+int SCA_IController::py_setattro(PyObject *attr, PyObject *value)
+{
+	py_setattro_up(SCA_ILogicBrick);
+}
+
+
+
+PyObject* SCA_IController::PyGetActuators()
+{
+	ShowDeprecationWarning("getActuators()", "the actuators property");
+	
+	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]->GetProxy());
+	}
+
+	return resultlist;
+}
+
+PyObject* SCA_IController::PyGetSensor(PyObject* value)
+{
+
+	char *scriptArg = PyString_AsString(value);
+	if (scriptArg==NULL) {
+		PyErr_SetString(PyExc_TypeError, "controller.getSensor(string): Python Controller, expected a string (sensor name)");
+		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->GetProxy();
+		}
+	}
+	
+	PyErr_Format(PyExc_AttributeError, "controller.getSensor(string): Python Controller, unable to find requested sensor \"%s\"", scriptArg);
+	return NULL;
+}
+
+PyObject* SCA_IController::PyGetActuator(PyObject* value)
+{
+
+	char *scriptArg = PyString_AsString(value);
+	if (scriptArg==NULL) {
+		PyErr_SetString(PyExc_TypeError, "controller.getActuator(string): Python Controller, expected a string (actuator name)");
+		return NULL;
+	}
+	
+	for (unsigned int index=0;index<m_linkedactuators.size();index++)
+	{
+		SCA_IActuator* actua = m_linkedactuators[index];
+		if (actua->GetName() == scriptArg)
+		{
+			return actua->GetProxy();
+		}
+	}
+	
+	PyErr_Format(PyExc_AttributeError, "controller.getActuator(string): Python Controller, unable to find requested actuator \"%s\"", scriptArg);
+	return NULL;
+}
+
+PyObject* SCA_IController::PyGetSensors()
+{
+	ShowDeprecationWarning("getSensors()", "the sensors property");
+	
+	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]->GetProxy());
+	}
+	
+	return resultlist;
+}
+
+PyObject* SCA_IController::PyGetState()
+{
+	ShowDeprecationWarning("getState()", "the state property");
+	return PyInt_FromLong(m_statemask);
+}
+
+PyObject* SCA_IController::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+	SCA_IController* self= static_cast<SCA_IController*>(self_v);
+	return PyInt_FromLong(self->m_statemask);
+}
+
+PyObject* SCA_IController::pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+	SCA_IController* self= static_cast<SCA_IController*>(self_v);
+	vector<SCA_ISensor*> linkedsensors = self->GetLinkedSensors();
+	PyObject* resultlist = PyList_New(linkedsensors.size());
+	
+	for (unsigned int index=0;index<linkedsensors.size();index++)
+		PyList_SET_ITEM(resultlist,index, linkedsensors[index]->GetProxy());
+	
+	return resultlist;
+}
+
+PyObject* SCA_IController::pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+	SCA_IController* self= static_cast<SCA_IController*>(self_v);
+	vector<SCA_IActuator*> linkedactuators = self->GetLinkedActuators();
+	PyObject* resultlist = PyList_New(linkedactuators.size());
+	
+	for (unsigned int index=0;index<linkedactuators.size();index++)
+		PyList_SET_ITEM(resultlist,index, linkedactuators[index]->GetProxy());
+
+	return resultlist;
+}

Modified: trunk/blender/source/gameengine/GameLogic/SCA_IController.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_IController.h	2009-05-06 08:33:40 UTC (rev 20082)
+++ trunk/blender/source/gameengine/GameLogic/SCA_IController.h	2009-05-06 09:12:08 UTC (rev 20083)
@@ -30,9 +30,11 @@
 #define __KX_ICONTROLLER
 
 #include "SCA_ILogicBrick.h"
+#include "PyObjectPlus.h"
 
 class SCA_IController : public SCA_ILogicBrick
 {
+	Py_Header;
 protected:
 	std::vector<class SCA_ISensor*>		m_linkedsensors;
 	std::vector<class SCA_IActuator*>	m_linkedactuators;
@@ -51,8 +53,21 @@
 	void	UnlinkSensor(class SCA_ISensor* sensor);
 	void    SetState(unsigned int state) { m_statemask = state; }
 	void    ApplyState(unsigned int state);
+	
+	virtual PyObject* py_getattro(PyObject *attr);
+	virtual PyObject* py_getattro_dict();
+	virtual int py_setattro(PyObject *attr, PyObject *value);
+	
+	KX_PYMETHOD_NOARGS(SCA_IController,GetSensors);
+	KX_PYMETHOD_NOARGS(SCA_IController,GetActuators);
+	KX_PYMETHOD_O(SCA_IController,GetSensor);
+	KX_PYMETHOD_O(SCA_IController,GetActuator);
+	KX_PYMETHOD_NOARGS(SCA_IController,GetState);
+	
+	static PyObject*	pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+	static PyObject*	pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+	static PyObject*	pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
 
-
 };
 
 #endif

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-05-06 08:33:40 UTC (rev 20082)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-05-06 09:12:08 UTC (rev 20083)
@@ -157,10 +157,6 @@
 	return 0;
 }
 
-#if 0
-static const char* sPyGetCurrentController__doc__;
-#endif
-
 /* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
 PyObject* SCA_PythonController::sPyGetCurrentController(PyObject *self)
 {
@@ -189,7 +185,7 @@
 		}
 	}
 	else if (BGE_PROXY_CHECK_TYPE(value)) {
-		PyObjectPlus *value_plus= BGE_PROXY_REF(value); /* Expecting an actuator type */ // XXX TODO - CHECK 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;
@@ -205,13 +201,11 @@
 	return false;
 }
 
-#if 0
-static const char* sPyAddActiveActuator__doc__;
-#endif
-
 /* warning, self is not the SCA_PythonController, its a PyObjectPlus_Proxy */
 PyObject* SCA_PythonController::sPyAddActiveActuator(PyObject* self, PyObject* args)
 {

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list