17 #ifndef AGXPYTHON_SCRIPTMANAGER_H 18 #define AGXPYTHON_SCRIPTMANAGER_H 20 #include <agx/config/AGX_USE_PYTHON.h> 21 #include <agx/config/AGX_USE_WEBSOCKETS.h> 22 #include <agx/config.h> 35 #if AGX_USE_WEBSOCKETS() 45 # pragma warning( push ) 46 # pragma warning( disable: 4512 ) // Disable warning about assignment operator could not be generated due to reference members. 47 # pragma warning( disable: 4275 ) // Disable non dll-interface class std::exception used as base for dll-interface class agxPython::ScriptError 57 #if AGX_USE_WEBSOCKETS() 60 class SimulationController;
66 enum ScriptContextType
68 SCRIPTCONTEXT_INVALID = 0,
70 SCRIPTCONTEXT_AGXVIEWER,
72 SCRIPTCONTEXT_QTVIEWER
91 ScriptError(
const ScriptError& other);
92 ScriptError(std::string scriptError, std::string scriptFile);
94 virtual ~ScriptError();
105 #if AGX_USE_WEBSOCKETS() 110 StartPlaybackSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
114 virtual ~StartPlaybackSignalCallback() {}
117 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
124 StartRecordingSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
128 virtual ~StartRecordingSignalCallback() {}
131 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
138 StopPlaybackSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
142 virtual ~StopPlaybackSignalCallback() {}
145 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
152 StopRecordingSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
156 virtual ~StopRecordingSignalCallback() {}
159 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
163 class ReactiveScript;
175 friend class ReactiveInitializer;
180 static ScriptContext* getContext();
182 static bool hasInstance();
188 static ScriptManager* instance(
const std::string& requestor =
"",
const std::string& pythonPaths=
"",
const std::string& pythonHome=
"",
bool ignoreEnvironment =
false);
197 void registerError(
const std::string& prefix =
"");
207 void reportError(
const std::string& errorReport)
const;
230 bool registerReactiveScript(ReactiveScript* script);
231 bool unregisterReactiveScript(
const agx::Uuid& uuid);
232 bool isRegisteredReactiveScript(
const agx::Uuid& uuid)
const;
233 void clearReactiveScripts();
234 void acquireReactiveScriptContext(
const agx::Uuid& scriptId,
const char* func);
235 void releaseReactiveScriptContext(
const agx::Uuid& scriptId,
bool unlock =
true);
238 #if AGX_USE_WEBSOCKETS() 239 void updateReactiveScripts(agxSDK::SimulationController* simulationController);
241 void updateReactiveScripts();
243 void onStartPlaybackReactiveScripts(
agx::Real value);
244 void onStartRecordingReactiveScripts(
agx::Real value);
245 void onStopPlaybackReactiveScripts(
agx::Real value);
246 void onStopRecordingReactiveScripts(
agx::Real value);
253 std::string getLastErrorMessage()
const;
259 int getLastErrorLine()
const;
265 bool receivedErrorMessage()
const;
275 void setLastErrorMessage(
const std::string& errorMessage);
276 void setLastErrorLine(
int line);
285 ScriptIDERef requestVirtualIDE(
const agx::Name& identifyingName);
293 #if AGX_USE_WEBSOCKETS() 294 void registerSimulationControllerCallbacks(agxSDK::SimulationController* simulationController);
295 void unregisterSimulationControllerCallbacks(agxSDK::SimulationController* simulationController);
299 void destroyMainModule(
const agx::Uuid& scriptId);
301 #if !defined(SWIGJAVA) 303 void enableEmbeddedMode(
const std::string& programName);
306 void init(
const std::string& pythonPaths=
"");
313 agxPython::ScriptContextInterface *initScriptContext(agxPython::ScriptContextType contextType);
317 agxPython::ScriptContextInterface* getScriptContext();
320 void createMainModule(
const agx::Uuid& scriptId);
323 void swapMainModule(
const agx::Uuid& scriptId);
326 void restoreGlobalMainModule(
bool unlock =
true);
328 bool hasMainModule(
const agx::Uuid& uuid)
const;
330 void* getMainModule();
337 bool hasReactiveScriptErrors(
bool printWarnings =
true );
344 bool doFile(
const std::string& path);
351 bool doString(
const std::string& code,
const std::string& origin);
360 bool doCompiledScript(
void* compiled);
361 void* doCompiledFunction(
void* funcObject,
void **args,
int numArgs);
369 bool doFunction(
const std::string&
function,
void* args =
nullptr);
375 #if AGX_USE_WEBSOCKETS() 376 agxSDK::SimulationController* getCurrentScriptSimulationController();
385 bool hasFunction(
const std::string&
function);
387 bool getTreatInvalidAsErrors()
const;
388 void setTreatInvalidAsErrors(
const bool treatInvalidAsErrors);
413 std::string getProgramName();
418 typedef std::map<std::string, std::string> PythonDefMap;
420 PythonDefMap dumpGlobals(
void* globals, std::ostream* logStream =
nullptr);
427 static void startProfiling(
void* obj,
const char* decl);
429 static void endProfiling(
void* obj,
const char* decl);
431 static void abortProfiling(
void* obj,
const char* decl);
436 SINGLETON_CLASSNAME_METHOD();
439 virtual ~ScriptManager();
442 ScriptManager(
const std::string& program,
const std::string& pythonPaths,
const std::string& pythonHome,
bool ignoreEnvironment);
446 std::map<agx::Uuid, agx::ref_ptr<ReactiveScript> > m_reactiveScripts;
447 std::map<agx::Uuid, void*> m_reactiveScriptMainModules;
449 void* m_globalMainModule;
450 void* m_currentMainModule;
451 ReactiveScript* m_currentReactiveScriptContext;
457 std::string m_programName;
458 std::set<std::string> m_pythonPath;
459 wchar_t* m_pythonHome;
460 bool m_shutdownInProgress;
461 bool m_treatInvalidAsErrors;
462 std::string m_lastErrorMessage;
464 std::string m_scriptPath;
465 bool m_ignoreEnvironment;
470 #if AGX_USE_WEBSOCKETS() 471 StartPlaybackSignalCallbackRef m_startPlaybackSignalCallback;
472 StartRecordingSignalCallbackRef m_startRecordingSignalCallback;
473 StopPlaybackSignalCallbackRef m_stopPlaybackSignalCallback;
474 StopRecordingSignalCallbackRef m_stopRecordingSignalCallback;
477 agxSDK::SimulationController* m_simulationController;
482 static ScriptManager* s_instance;
487 const char*
const MOMENTUM_PROGRAM_NAME =
"AlgoryxMomentum";
489 friend class ScriptIDEAutocompleteCollection;
502 #define g_pythonScriptManager agxPython::ScriptManager::instance("") 503 #define g_pythonScriptManagerForProgram(PROGRAM) agxPython::ScriptManager::instance(PROGRAM) 504 #define AGXPYTHON_REPORT_ERROR(errorMessage) \ 505 if (g_pythonScriptManager->getTreatInvalidAsErrors() || agxUnit::isUnittestingEnabled()) \ 507 LOGGER_ERROR() << (errorMessage) << std::endl << LOGGER_END(); \ 511 LOGGER_WARNING() << (errorMessage) << std::endl << LOGGER_END(); \ 518 # pragma warning(pop)
void AGXPHYSICS_EXPORT init()
Initialize agx.
void AGXPHYSICS_EXPORT shutdown()
Shutdown agx.
#define AGX_DECLARE_POINTER_TYPES(type)
Representation of a name string.
Smart pointer for handling referenced counted objects.
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...