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 )
47 # pragma warning( disable: 4275 )
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()
107 class AGXPYTHON_EXPORT StartPlaybackSignalCallback :
public agxSDK::CallbackWrapper_Real
110 StartPlaybackSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
114 virtual ~StartPlaybackSignalCallback() {}
117 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
121 class AGXPYTHON_EXPORT StartRecordingSignalCallback :
public agxSDK::CallbackWrapper_Real
124 StartRecordingSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
128 virtual ~StartRecordingSignalCallback() {}
131 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
135 class AGXPYTHON_EXPORT StopPlaybackSignalCallback :
public agxSDK::CallbackWrapper_Real
138 StopPlaybackSignalCallback() :
agxSDK::CallbackWrapper_Real() {}
142 virtual ~StopPlaybackSignalCallback() {}
145 void callback(agxSDK::SimulationController*,
agx::Real value)
override;
149 class AGXPYTHON_EXPORT StopRecordingSignalCallback :
public agxSDK::CallbackWrapper_Real
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();
183 static bool isStartedFromExternalPython();
189 static ScriptManager* instance(
const std::string& requestor =
"",
const std::string& pythonPaths=
"",
const std::string& pythonHome=
"",
bool ignoreEnvironment =
false);
198 void registerError(
const std::string& prefix =
"");
208 void reportError(
const std::string& errorReport)
const;
231 bool registerReactiveScript(ReactiveScript* script);
232 bool unregisterReactiveScript(
const agx::Uuid& uuid);
233 bool isRegisteredReactiveScript(
const agx::Uuid& uuid)
const;
234 void clearReactiveScripts();
235 void acquireReactiveScriptContext(
const agx::Uuid& scriptId,
const char* func);
236 void releaseReactiveScriptContext(
const agx::Uuid& scriptId,
bool unlock =
true);
239 #if AGX_USE_WEBSOCKETS()
240 void updateReactiveScripts(agxSDK::SimulationController* simulationController);
242 void updateReactiveScripts();
244 void onStartPlaybackReactiveScripts(
agx::Real value);
245 void onStartRecordingReactiveScripts(
agx::Real value);
246 void onStopPlaybackReactiveScripts(
agx::Real value);
247 void onStopRecordingReactiveScripts(
agx::Real value);
254 std::string getLastErrorMessage()
const;
260 int getLastErrorLine()
const;
266 bool receivedErrorMessage()
const;
276 void setLastErrorMessage(
const std::string& errorMessage);
277 void setLastErrorLine(
int line);
286 ScriptIDERef requestVirtualIDE(
const agx::Name& identifyingName);
294 #if AGX_USE_WEBSOCKETS()
295 void registerSimulationControllerCallbacks(agxSDK::SimulationController* simulationController);
296 void unregisterSimulationControllerCallbacks(agxSDK::SimulationController* simulationController);
300 void destroyMainModule(
const agx::Uuid& scriptId);
302 #if !defined(SWIGJAVA)
304 void enableEmbeddedMode(
const std::string& programName);
307 void init(
const std::string& pythonPaths=
"");
314 agxPython::ScriptContextInterface *initScriptContext(agxPython::ScriptContextType contextType);
318 agxPython::ScriptContextInterface* getScriptContext();
321 void createMainModule(
const agx::Uuid& scriptId);
324 void swapMainModule(
const agx::Uuid& scriptId);
327 void restoreGlobalMainModule(
bool unlock =
true);
329 bool hasMainModule(
const agx::Uuid& uuid)
const;
331 void* getMainModule();
338 bool hasReactiveScriptErrors(
bool printWarnings =
true );
346 bool doFile(
const std::string& path,
bool resolvePath =
true);
353 bool doString(
const std::string& code,
const std::string& origin);
362 bool doCompiledScript(
void* compiled);
363 void* doCompiledFunction(
void* funcObject,
void **args,
int numArgs);
371 bool doFunction(
const std::string&
function,
void* args =
nullptr);
377 #if AGX_USE_WEBSOCKETS()
378 agxSDK::SimulationController* getCurrentScriptSimulationController();
387 bool hasFunction(
const std::string&
function);
389 bool getTreatInvalidAsErrors()
const;
390 void setTreatInvalidAsErrors(
const bool treatInvalidAsErrors);
415 std::string getProgramName();
420 typedef std::map<std::string, std::string> PythonDefMap;
422 PythonDefMap dumpGlobals(
void* globals, std::ostream* logStream =
nullptr);
429 static void startProfiling(
void* obj,
const char* decl);
431 static void endProfiling(
void* obj,
const char* decl);
433 static void abortProfiling(
void* obj,
const char* decl);
438 SINGLETON_CLASSNAME_METHOD();
441 virtual ~ScriptManager();
444 ScriptManager(
const std::string& program,
const std::string& pythonPaths,
const std::string& pythonHome,
bool ignoreEnvironment);
448 std::map<agx::Uuid, agx::ref_ptr<ReactiveScript> > m_reactiveScripts;
449 std::map<agx::Uuid, void*> m_reactiveScriptMainModules;
451 void* m_globalMainModule;
452 void* m_currentMainModule;
453 ReactiveScript* m_currentReactiveScriptContext;
459 std::string m_programName;
460 std::set<std::string> m_pythonPath;
461 wchar_t* m_pythonHome;
462 bool m_shutdownInProgress;
463 bool m_treatInvalidAsErrors;
464 std::string m_lastErrorMessage;
466 std::string m_scriptPath;
467 bool m_ignoreEnvironment;
472 #if AGX_USE_WEBSOCKETS()
473 StartPlaybackSignalCallbackRef m_startPlaybackSignalCallback;
474 StartRecordingSignalCallbackRef m_startRecordingSignalCallback;
475 StopPlaybackSignalCallbackRef m_stopPlaybackSignalCallback;
476 StopRecordingSignalCallbackRef m_stopRecordingSignalCallback;
479 agxSDK::SimulationController* m_simulationController;
484 static ScriptManager* s_instance;
489 const char*
const MOMENTUM_PROGRAM_NAME =
"AlgoryxMomentum";
491 friend class ScriptIDEAutocompleteCollection;
504 #define g_pythonScriptManager agxPython::ScriptManager::instance("")
505 #define g_pythonScriptManagerForProgram(PROGRAM) agxPython::ScriptManager::instance(PROGRAM)
506 #define AGXPYTHON_REPORT_ERROR(errorMessage) \
507 if (g_pythonScriptManager->getTreatInvalidAsErrors() || agxUnit::isUnittestingEnabled()) \
509 LOGGER_ERROR() << (errorMessage) << std::endl << LOGGER_END(); \
513 LOGGER_WARNING() << (errorMessage) << std::endl << LOGGER_END(); \
520 # pragma warning(pop)
#define AGX_DECLARE_POINTER_TYPES(type)
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Representation of a name string.
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Smart pointer for handling referenced counted objects.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
void AGXPHYSICS_EXPORT shutdown()
Shutdown agx.
std::recursive_mutex ReentrantMutex
void AGXPHYSICS_EXPORT init()
Initialize agx.