[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20420] trunk/blender: BGE Script template for a python module (set EOL to native this time)

Campbell Barton ideasman42 at gmail.com
Tue May 26 09:41:34 CEST 2009


Revision: 20420
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20420
Author:   campbellbarton
Date:     2009-05-26 09:41:34 +0200 (Tue, 26 May 2009)

Log Message:
-----------
BGE Script template for a python module (set EOL to native this time)
BGE PyAPI use defines for error return values
 - del gameOb['var'] error message was wrong.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp
    trunk/blender/source/gameengine/Expressions/Value.cpp
    trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp

Added Paths:
-----------
    trunk/blender/release/scripts/scripttemplate_gamelogic_module.py

Added: trunk/blender/release/scripts/scripttemplate_gamelogic_module.py
===================================================================
--- trunk/blender/release/scripts/scripttemplate_gamelogic_module.py	                        (rev 0)
+++ trunk/blender/release/scripts/scripttemplate_gamelogic_module.py	2009-05-26 07:41:34 UTC (rev 20420)
@@ -0,0 +1,45 @@
+#!BPY
+"""
+Name: 'GameLogic Module'
+Blender: 249
+Group: 'ScriptTemplate'
+Tooltip: 'Basic template for new game logic modules'
+"""
+
+from Blender import Window
+import bpy
+
+script_data = \
+'''
+# This module can be accessed by a python controller with
+# its execution method set to 'Module'
+# * Set the module string to "gamelogic_module.main" (without quotes)
+# * When renaming the script it MUST have a .py extension
+# * External text modules are supported as long as they are at
+#   the same location as the blendfile or one of its libraries.
+
+import GameLogic
+
+# variables defined here will only be set once when the
+# module is first imported. Set object spesific vars
+# inside the function if you intend to use the module
+# with multiple objects.
+
+def main(cont):
+	own = cont.owner
+	
+	sens = cont.sensors['mySensor']
+	actu = cont.actuators['myActuator']
+	
+	if sens.positive:
+		cont.activate(actu)
+	else:
+		cont.deactivate(actu)
+
+# dont call main(GameLogic.getCurrentController()), the py controller will
+'''
+
+new_text = bpy.data.texts.new('gamelogic_module.py')
+new_text.write(script_data)
+bpy.data.texts.active = new_text
+Window.RedrawAll()


Property changes on: trunk/blender/release/scripts/scripttemplate_gamelogic_module.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp	2009-05-26 06:29:15 UTC (rev 20419)
+++ trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp	2009-05-26 07:41:34 UTC (rev 20420)
@@ -362,12 +362,12 @@
 		if (!PySequence_Check(value)) 
 		{
 			PyErr_Format(PyExc_TypeError, "expected a sequence for attribute \"%s\"", attrdef->m_name);
-			return 1;
+			return PY_SET_ATTR_FAIL;
 		}
 		if (PySequence_Size(value) != attrdef->m_length)
 		{
 			PyErr_Format(PyExc_TypeError, "incorrect number of elements in sequence for attribute \"%s\"", attrdef->m_name);
-			return 1;
+			return PY_SET_ATTR_FAIL;
 		}
 		switch (attrdef->m_type) 
 		{
@@ -375,7 +375,7 @@
 			if (attrdef->m_setFunction == NULL) 
 			{
 				PyErr_Format(PyExc_AttributeError, "function attribute without function for attribute \"%s\", report to blender.org", attrdef->m_name);
-				return 1;
+				return PY_SET_ATTR_FAIL;
 			}
 			return (*attrdef->m_setFunction)(self, attrdef, value);
 		case KX_PYATTRIBUTE_TYPE_BOOL:
@@ -394,7 +394,7 @@
 		default:
 			// should not happen
 			PyErr_Format(PyExc_AttributeError, "Unsupported attribute type for attribute \"%s\", report to blender.org", attrdef->m_name);
-			return 1;
+			return PY_SET_ATTR_FAIL;
 		}
 		// let's implement a smart undo method
 		bufferSize *= attrdef->m_length;
@@ -542,12 +542,12 @@
 					memcpy(sourceBuffer, undoBuffer, bufferSize);
 					free(undoBuffer);
 				}
-				return 1;
+				return PY_SET_ATTR_FAIL;
 			}
 		}
 		if (undoBuffer)
 			free(undoBuffer);
-		return 0;
+		return PY_SET_ATTR_SUCCESS;
 	}
 	else	// simple attribute value
 	{
@@ -556,7 +556,7 @@
 			if (attrdef->m_setFunction == NULL)
 			{
 				PyErr_Format(PyExc_AttributeError, "function attribute without function \"%s\", report to blender.org", attrdef->m_name);
-				return 1;
+				return PY_SET_ATTR_FAIL;
 			}
 			return (*attrdef->m_setFunction)(self, attrdef, value);
 		}
@@ -589,7 +589,7 @@
 				break;
 			default:
 				PyErr_Format(PyExc_AttributeError, "unknown type for attribute \"%s\", report to blender.org", attrdef->m_name);
-				return 1;
+				return PY_SET_ATTR_FAIL;
 			}
 			if (bufferSize)
 			{
@@ -712,7 +712,7 @@
 				if (!PySequence_Check(value) || PySequence_Size(value) != 3) 
 				{
 					PyErr_Format(PyExc_TypeError, "expected a sequence of 3 floats for attribute \"%s\"", attrdef->m_name);
-					return 1;
+					return PY_SET_ATTR_FAIL;
 				}
 				MT_Vector3 *var = reinterpret_cast<MT_Vector3*>(ptr);
 				for (int i=0; i<3; i++)

Modified: trunk/blender/source/gameengine/Expressions/Value.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/Value.cpp	2009-05-26 06:29:15 UTC (rev 20419)
+++ trunk/blender/source/gameengine/Expressions/Value.cpp	2009-05-26 07:41:34 UTC (rev 20420)
@@ -662,7 +662,7 @@
 		return 0;
 	
 	PyErr_Format(PyExc_AttributeError, "attribute \"%s\" dosnt exist", attr_str);
-	return 1;
+	return PY_SET_ATTR_MISSING;
 }
 
 int	CValue::py_setattro(PyObject *attr, PyObject* pyobj)

Modified: trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp	2009-05-26 06:29:15 UTC (rev 20419)
+++ trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp	2009-05-26 07:41:34 UTC (rev 20420)
@@ -1342,7 +1342,7 @@
 		
 		if (del==0) {
 			if(attr_str)	PyErr_Format(PyExc_KeyError, "gameOb[key] = value: KX_GameObject, key \"%s\" could not be set", attr_str);
-			else			PyErr_SetString(PyExc_KeyError, "gameOb[key] = value: KX_GameObject, key could not be set");
+			else			PyErr_SetString(PyExc_KeyError, "del gameOb[key]: KX_GameObject, key could not be deleted");
 			return -1;
 		}
 		else if (self->m_attr_dict) {
@@ -1902,13 +1902,13 @@
 	char *attr_str= PyString_AsString(attr); 
 	
 	if (RemoveProperty(attr_str)) // XXX - should call CValues instead but its only 2 lines here
-		return 0;
+		return PY_SET_ATTR_SUCCESS;
 	
 	if (m_attr_dict && (PyDict_DelItem(m_attr_dict, attr) == 0))
-		return 0;
+		return PY_SET_ATTR_SUCCESS;
 	
 	PyErr_Format(PyExc_AttributeError, "del gameOb.myAttr: KX_GameObject, attribute \"%s\" dosnt exist", attr_str);
-	return 1;
+	return PY_SET_ATTR_MISSING;
 }
 
 





More information about the Bf-blender-cvs mailing list