[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19098] trunk/blender/source/gameengine: compile scripts when converting controllers to give more predictable performance and print syntax errors early on rather then when the script is first executed .

Campbell Barton ideasman42 at gmail.com
Tue Feb 24 04:29:34 CET 2009


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

Log Message:
-----------
compile scripts when converting controllers to give more predictable performance and print syntax errors early on rather then when the script is first executed.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp	2009-02-24 03:14:57 UTC (rev 19097)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp	2009-02-24 03:29:31 UTC (rev 19098)
@@ -200,6 +200,13 @@
 			gameobj->AddController(gamecontroller);
 			
 			converter->RegisterGameController(gamecontroller, bcontr);
+			
+			if (bcontr->type==CONT_PYTHON) {
+				/* not strictly needed but gives syntax errors early on and
+				 * gives more pradictable performance for larger scripts */
+				(static_cast<SCA_PythonController*>(gamecontroller))->Compile();
+			}
+			
 			//done with gamecontroller
 			gamecontroller->Release();
 		}

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-02-24 03:14:57 UTC (rev 19097)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-02-24 03:29:31 UTC (rev 19098)
@@ -242,6 +242,43 @@
 };
 
 
+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 */
+		PySys_SetObject( (char *)"last_traceback", Py_None);
+		PyErr_Clear(); /* just to be sure */
+		
+		return false;
+	}
+}
+
 void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
 {
 	m_sCurrentController = this;
@@ -249,33 +286,13 @@
 	
 	if (m_bModified)
 	{
-		// 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);
-		if (!m_bytecode)
-		{
-			// 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 */
-			PySys_SetObject( "last_traceback", Py_None);
-			PyErr_Clear(); /* just to be sure */
-			
+		if (Compile()==false) // sets m_bModified to false
 			return;
-		}
-		m_bModified=false;
 	}
+	if (!m_bytecode) {
+		return;
+	}
+		
 	
 		/*
 		 * This part here with excdict is a temporary patch
@@ -313,7 +330,7 @@
 		 * 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 */
-		PySys_SetObject( "last_traceback", Py_None);
+		PySys_SetObject( (char *)"last_traceback", Py_None);
 		PyErr_Clear(); /* just to be sure */
 		
 		//PyRun_SimpleString(m_scriptText.Ptr());

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h	2009-02-24 03:14:57 UTC (rev 19097)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonController.h	2009-02-24 03:29:31 UTC (rev 19098)
@@ -70,6 +70,7 @@
 	void	AddTriggeredSensor(class SCA_ISensor* sensor)
 		{ m_triggeredSensors.push_back(sensor); }
 	int		IsTriggered(class SCA_ISensor* sensor);
+	bool	Compile();
 
 	static const char* sPyGetCurrentController__doc__;
 	static PyObject* sPyGetCurrentController(PyObject* self);





More information about the Bf-blender-cvs mailing list