ScriptManager.cpp 2.1 KB

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