diff --git a/source/blender/python/BPY_interface.c b/source/blender/python/BPY_interface.c index 1f35ebad9f7..72cc4f6cfde 100644 --- a/source/blender/python/BPY_interface.c +++ b/source/blender/python/BPY_interface.c @@ -2780,7 +2780,11 @@ static PyObject *RunPython( Text * text, PyObject * globaldict ) } } - + + /* Without __file__ set the sys.argv[0] is used for the filename + * which ends up with lines from the blender binary being printed in the console */ + PyDict_SetItemString(globaldict, "__file__", PyString_FromString(text->id.name+2)); + return PyEval_EvalCode( text->compiled, globaldict, globaldict ); } diff --git a/source/gameengine/GameLogic/SCA_PythonController.cpp b/source/gameengine/GameLogic/SCA_PythonController.cpp index 52f6bece132..80e4f54c9c5 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.cpp +++ b/source/gameengine/GameLogic/SCA_PythonController.cpp @@ -147,6 +147,11 @@ void SCA_PythonController::SetDictionary(PyObject* pythondictionary) Py_DECREF(m_pythondictionary); } m_pythondictionary = PyDict_Copy(pythondictionary); /* new reference */ + + /* Without __file__ set the sys.argv[0] is used for the filename + * which ends up with lines from the blender binary being printed in the console */ + PyDict_SetItemString(m_pythondictionary, "__file__", PyString_FromString(m_scriptName.Ptr())); + } int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor) @@ -266,6 +271,7 @@ PyMethodDef SCA_PythonController::Methods[] = { PyAttributeDef SCA_PythonController::Attributes[] = { KX_PYATTRIBUTE_RW_FUNCTION("script", SCA_PythonController, pyattr_get_script, pyattr_set_script), + KX_PYATTRIBUTE_INT_RO("mode", SCA_PythonController, m_mode), { NULL } //Sentinel }; diff --git a/source/gameengine/PyDoc/GameTypes.py b/source/gameengine/PyDoc/GameTypes.py index 4f176c17af2..4ab175a8f6c 100644 --- a/source/gameengine/PyDoc/GameTypes.py +++ b/source/gameengine/PyDoc/GameTypes.py @@ -5205,8 +5205,15 @@ class SCA_PythonController(SCA_IController): Properties: - @ivar script: the Python script this controller executes + @ivar script: The value of this variable depends on the execution methid. + - When 'Script' execution mode is set this value contains the entire python script as a single string (not the script name as you might expect) which can be modified to run different scripts. + - When 'Module' execution mode is set this value will contain a single line string - module name and function "module.func" or "package.modile.func" where the module names are python textblocks or external scripts. + note: once this is set the script name given for warnings will remain unchanged. @type script: string + @ivar mode: the execution mode for this controller (read-only). + - Script: 0, Execite the L{script} as a python code. + - Module: 1, Execite the L{script} as a module and function. + @type mode: int @group Deprecated: getScript, setScript """ @@ -5222,17 +5229,17 @@ class SCA_PythonController(SCA_IController): """ def getScript(): """ - Gets the Python script this controller executes. + Gets the Python script body this controller executes. @deprecated: Use the L{script} attribute instead. @rtype: string """ - def setScript(script): + def setScript(script_body): """ - Sets the Python script this controller executes. + Sets the Python script string this controller executes. @deprecated: Use the L{script} attribute instead. - @type script: string. + @type script_body: string. """ class SCA_RandomActuator(SCA_IActuator):