ScriptManager.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "anki/script/ScriptManager.h"
  2. #include "anki/util/Exception.h"
  3. #include "anki/core/Logger.h"
  4. #include "anki/core/Globals.h"
  5. #include "anki/script/Common.h"
  6. #include <Python.h>
  7. using namespace boost;
  8. using namespace boost::python;
  9. /// Define the classes
  10. BOOST_PYTHON_MODULE(anki)
  11. {
  12. CALL_WRAP(HighRezTimer);
  13. CALL_WRAP(Vec2);
  14. CALL_WRAP(Vec3);
  15. CALL_WRAP(Vec4);
  16. CALL_WRAP(Logger);
  17. CALL_WRAP(SceneNode);
  18. CALL_WRAP(Camera);
  19. CALL_WRAP(MaterialRuntimeVariable);
  20. CALL_WRAP(MaterialRuntime);
  21. CALL_WRAP(PatchNode);
  22. CALL_WRAP(ModelPatchNode);
  23. CALL_WRAP(ModelNode);
  24. CALL_WRAP(Scene);
  25. CALL_WRAP(SwitchableRenderingPass);
  26. CALL_WRAP(Hdr);
  27. CALL_WRAP(Bl);
  28. CALL_WRAP(Pps);
  29. CALL_WRAP(Renderer);
  30. CALL_WRAP(Dbg);
  31. CALL_WRAP(MainRenderer);
  32. CALL_WRAP(SceneColorEvent);
  33. CALL_WRAP(MainRendererPpsHdrEvent);
  34. CALL_WRAP(EventManager);
  35. CALL_WRAP(App);
  36. CALL_WRAP(Input);
  37. CALL_WRAP(AllGlobals);
  38. }
  39. namespace anki {
  40. //==============================================================================
  41. // init =
  42. //==============================================================================
  43. void ScriptManager::init()
  44. {
  45. ANKI_INFO("Initializing scripting engine...");
  46. PyImport_AppendInittab((char*)("anki"), &initanki);
  47. Py_Initialize();
  48. mainModule = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
  49. mainNamespace = mainModule.attr("__dict__");
  50. ankiModule = object(handle<>(PyImport_ImportModule("anki")));
  51. ANKI_INFO("Scripting engine initialized");
  52. }
  53. //==============================================================================
  54. // execScript =
  55. //==============================================================================
  56. void ScriptManager::execScript(const char* script, const char* scriptName)
  57. {
  58. try
  59. {
  60. handle<>ignored(PyRun_String(script, Py_file_input,
  61. mainNamespace.ptr(), mainNamespace.ptr()));
  62. }
  63. catch(error_already_set)
  64. {
  65. PyErr_Print();
  66. throw ANKI_EXCEPTION("Script \"" + scriptName + "\" failed with error");
  67. }
  68. }
  69. } // end namespace