ScriptManager.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. void ScriptManager::init()
  42. {
  43. ANKI_INFO("Initializing scripting engine...");
  44. PyImport_AppendInittab((char*)("anki"), &initanki);
  45. Py_Initialize();
  46. mainModule = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
  47. mainNamespace = mainModule.attr("__dict__");
  48. ankiModule = object(handle<>(PyImport_ImportModule("anki")));
  49. ANKI_INFO("Scripting engine initialized");
  50. }
  51. //==============================================================================
  52. void ScriptManager::execScript(const char* script, const char* scriptName)
  53. {
  54. try
  55. {
  56. handle<>ignored(PyRun_String(script, Py_file_input,
  57. mainNamespace.ptr(), mainNamespace.ptr()));
  58. }
  59. catch(error_already_set)
  60. {
  61. PyErr_Print();
  62. throw ANKI_EXCEPTION("Script \"" + scriptName + "\" failed with error");
  63. }
  64. }
  65. } // end namespace