19 #include <agx/config/AGX_USE_PYTHON.h>
20 #include <agx/config/AGX_USE_WEBSOCKETS.h>
21 #include <agx/config.h>
34 #if AGX_USE_WEBSOCKETS()
36 #endif // AGX_USE_WEBSOCKETS
43 # pragma warning( push )
44 # pragma warning( disable: 4512 ) // Disable warning about assignment operator could not be generated due to reference members.
45 # pragma warning( disable: 4275 ) // Disable non dll-interface class std::exception used as base for dll-interface class agxPython::ScriptError
55 #if AGX_USE_WEBSOCKETS()
58 class SimulationController;
60 #endif // AGX_USE_WEBSOCKETS
69 enum ScriptContextType
71 SCRIPTCONTEXT_INVALID = 0,
73 SCRIPTCONTEXT_AGXVIEWER,
75 SCRIPTCONTEXT_QTVIEWER
94 ScriptError(
const ScriptError& other);
95 ScriptError(std::string scriptError, std::string scriptFile);
97 virtual ~ScriptError();
108 #if AGX_USE_WEBSOCKETS()
110 class AGXPYTHON_EXPORT StartPlaybackSignalCallback :
public agxSDK::CallbackWrapper_Real
113 StartPlaybackSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
117 virtual ~StartPlaybackSignalCallback() {}
120 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
124 class AGXPYTHON_EXPORT StartRecordingSignalCallback :
public agxSDK::CallbackWrapper_Real
127 StartRecordingSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
131 virtual ~StartRecordingSignalCallback() {}
134 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
138 class AGXPYTHON_EXPORT StopPlaybackSignalCallback :
public agxSDK::CallbackWrapper_Real
141 StopPlaybackSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
145 virtual ~StopPlaybackSignalCallback() {}
148 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
152 class AGXPYTHON_EXPORT StopRecordingSignalCallback :
public agxSDK::CallbackWrapper_Real
155 StopRecordingSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
159 virtual ~StopRecordingSignalCallback() {}
162 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
164 #endif // AGX_USE_WEBSOCKETS
166 class ReactiveScript;
178 friend class ReactiveInitializer;
183 static ScriptContext* getContext();
185 static bool hasInstance();
186 static bool isStartedFromExternalPython();
192 static ScriptManager* instance(
const std::string& requestor =
"",
const std::string& pythonPaths=
"",
const std::string& pythonHome=
"",
bool ignoreEnvironment =
false);
201 void registerError(
const std::string& prefix =
"");
211 void reportError(
const std::string& errorReport)
const;
234 bool registerReactiveScript(ReactiveScript* script);
235 bool unregisterReactiveScript(
const agx::Uuid& uuid);
236 bool isRegisteredReactiveScript(
const agx::Uuid& uuid)
const;
237 void clearReactiveScripts();
238 void acquireReactiveScriptContext(
const agx::Uuid& scriptId,
const char* func);
239 void releaseReactiveScriptContext(
const agx::Uuid& scriptId,
bool unlock =
true);
242 #if AGX_USE_WEBSOCKETS()
243 void updateReactiveScripts(agxSDK::SimulationController* simulationController);
244 #endif // AGX_USE_WEBSOCKETS
245 void updateReactiveScripts();
247 void onStartPlaybackReactiveScripts(
agx::Real value);
248 void onStartRecordingReactiveScripts(
agx::Real value);
249 void onStopPlaybackReactiveScripts(
agx::Real value);
250 void onStopRecordingReactiveScripts(
agx::Real value);
257 std::string getLastErrorMessage()
const;
263 int getLastErrorLine()
const;
269 bool receivedErrorMessage()
const;
279 void setLastErrorMessage(
const std::string& errorMessage);
280 void setLastErrorLine(
int line);
289 ScriptIDERef requestVirtualIDE(
const agx::Name& identifyingName);
297 #if AGX_USE_WEBSOCKETS()
298 void registerSimulationControllerCallbacks(agxSDK::SimulationController* simulationController);
299 void unregisterSimulationControllerCallbacks(agxSDK::SimulationController* simulationController);
300 #endif // AGX_USE_WEBSOCKETS
303 void destroyMainModule(
const agx::Uuid& scriptId);
305 #if !defined(SWIGJAVA)
307 void enableEmbeddedMode(
const std::string& programName);
310 void init(
const std::string& pythonPaths=
"");
317 agxPython::ScriptContextInterface *initScriptContext(agxPython::ScriptContextType contextType);
321 agxPython::ScriptContextInterface* getScriptContext();
324 void createMainModule(
const agx::Uuid& scriptId);
327 void swapMainModule(
const agx::Uuid& scriptId);
330 void restoreGlobalMainModule(
bool unlock =
true);
332 bool hasMainModule(
const agx::Uuid& uuid)
const;
334 void* getMainModule();
341 bool hasReactiveScriptErrors(
bool printWarnings =
true );
349 bool doFile(
const std::string& path,
bool resolvePath =
true);
356 bool doString(
const std::string& code,
const std::string& origin);
365 bool doCompiledScript(
void* compiled);
366 void* doCompiledFunction(
void* funcObject,
void **args,
int numArgs);
374 bool doFunction(
const std::string&
function,
void* args =
nullptr);
380 #if AGX_USE_WEBSOCKETS()
381 agxSDK::SimulationController* getCurrentScriptSimulationController();
382 #endif // AGX_USE_WEBSOCKETS
390 bool hasFunction(
const std::string&
function);
392 bool getTreatInvalidAsErrors()
const;
393 void setTreatInvalidAsErrors(
const bool treatInvalidAsErrors);
418 std::string getProgramName();
423 typedef std::map<std::string, std::string> PythonDefMap;
425 PythonDefMap dumpGlobals(
void* globals, std::ostream* logStream =
nullptr);
427 #endif // !defined(SWIGJAVA)
432 static void startProfiling(
void* obj,
const char* decl);
434 static void endProfiling(
void* obj,
const char* decl);
436 static void abortProfiling(
void* obj,
const char* decl);
441 SINGLETON_CLASSNAME_METHOD();
444 virtual ~ScriptManager();
447 ScriptManager(
const std::string& program,
const std::string& pythonPaths,
const std::string& pythonHome,
bool ignoreEnvironment);
451 std::map<agx::Uuid, agx::ref_ptr<ReactiveScript> > m_reactiveScripts;
452 std::map<agx::Uuid, void*> m_reactiveScriptMainModules;
454 void* m_globalMainModule;
455 void* m_currentMainModule;
456 ReactiveScript* m_currentReactiveScriptContext;
462 std::string m_programName;
463 std::set<std::string> m_pythonPath;
464 wchar_t* m_pythonHome;
465 bool m_shutdownInProgress;
466 bool m_treatInvalidAsErrors;
467 std::string m_lastErrorMessage;
469 std::string m_scriptPath;
470 bool m_ignoreEnvironment;
475 #if AGX_USE_WEBSOCKETS()
476 StartPlaybackSignalCallbackRef m_startPlaybackSignalCallback;
477 StartRecordingSignalCallbackRef m_startRecordingSignalCallback;
478 StopPlaybackSignalCallbackRef m_stopPlaybackSignalCallback;
479 StopRecordingSignalCallbackRef m_stopRecordingSignalCallback;
482 agxSDK::SimulationController* m_simulationController;
483 #endif // AGX_USE_WEBSOCKETS
487 static ScriptManager* s_instance;
492 const char*
const MOMENTUM_PROGRAM_NAME =
"AlgoryxMomentum";
494 friend class ScriptIDEAutocompleteCollection;
506 #define g_pythonScriptManager agxPython::ScriptManager::instance("")
507 #define g_pythonScriptManagerForProgram(PROGRAM) agxPython::ScriptManager::instance(PROGRAM)
508 #define AGXPYTHON_REPORT_ERROR(errorMessage) \
509 if (g_pythonScriptManager->getTreatInvalidAsErrors() || agxUnit::isUnittestingEnabled()) \
511 LOGGER_ERROR() << (errorMessage) << std::endl << LOGGER_END(); \
515 LOGGER_WARNING() << (errorMessage) << std::endl << LOGGER_END(); \
520 # pragma warning(pop)
523 #endif // AGX_USE_PYTHON