ScriptManager.cpp 1.9 KB

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