-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathScriptObject.cpp
More file actions
148 lines (129 loc) · 3.62 KB
/
Copy pathScriptObject.cpp
File metadata and controls
148 lines (129 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "ScriptObject.h"
#include "pySPlisHSPlasH/Embedded.h"
#include "Utilities/FileSystem.h"
#include "Utilities/Logger.h"
#include "SceneConfiguration.h"
#include "SimulatorBase.h"
#include "Simulator/GUI/Simulator_GUI_Base.h"
using namespace SPH;
using namespace std;
using namespace GenParam;
using namespace Utilities;
int ScriptObject::SCRIPT_FILE = -1;
ScriptObject::ScriptObject(SimulatorBase *base)
{
m_base = base;
m_scriptLoaded = false;
m_scriptModule = "";
m_scriptFile = "";
}
ScriptObject::~ScriptObject(void)
{
if (m_scriptLoaded)
{
Embedded::getCurrent()->releaseModule(m_scriptModule);
}
}
void ScriptObject::initParameters()
{
ParameterObject::initParameters();
auto tmp = createFunctionParameter("reload", "Reload script", [&]() {
if (m_scriptModule != "")
Embedded::getCurrent()->reloadModule(m_scriptModule);
updateFunctionParameters();
m_base->updateGUI();
});
setGroup(tmp, "Scripts");
setDescription(tmp, "Reload the Python script.");
SCRIPT_FILE = createStringParameter("scriptFile", "Script file", [&]() { return m_scriptFile; },
[&](std::string str)
{
m_scriptFile = str;
if (FileSystem::isRelativePath(m_scriptFile))
{
const std::string& sceneFile = SceneConfiguration::getCurrent()->getSceneFile();
m_scriptFile = FileSystem::normalizePath(FileSystem::getFilePath(sceneFile) + "/" + str);
}
m_scriptModule = loadScriptFile(m_scriptFile);
if (m_scriptModule != "")
{
m_scriptLoaded = true;
Embedded::getCurrent()->exec_init(m_scriptModule, m_base);
updateFunctionParameters();
#ifdef DL_OUTPUT
// copy script files in output so that the simulation can be reproduced
std::string sceneFilePath = FileSystem::normalizePath(m_base->getOutputPath() + "/scene");
FileSystem::makeDirs(sceneFilePath);
FileSystem::copyFile(m_scriptFile, sceneFilePath + "/" + FileSystem::getFileNameWithExt(m_scriptFile));
#endif
}
else
{
m_scriptLoaded = false;
removeFunctionParameters();
}
m_base->updateGUI();
});
setGroup(SCRIPT_FILE, "Scripts");
setDescription(SCRIPT_FILE, "Embedded Python script that is executed after each time step of the simulator.");
}
void ScriptObject::updateFunctionParameters()
{
removeFunctionParameters();
addFunctionParameters();
}
void ScriptObject::addFunctionParameters()
{
auto functions = Embedded::getCurrent()->getFunctions();
int counter = 1;
for (auto iter = functions.begin(); iter != functions.end(); iter++)
{
std::string command_name = *iter;
std::string name = "embedded_function_" + std::to_string(counter);
auto tmp = createFunctionParameter(name, command_name, [&, command_name]() {
if (m_scriptLoaded)
Embedded::getCurrent()->exec_fct(m_scriptModule, command_name);
});
//setHotKey(tmp, "x");
setGroup(tmp, "Scripts");
setDescription(tmp, "Execute a function in the script.");
counter++;
}
}
void ScriptObject::removeFunctionParameters()
{
while (m_parameters.size() > 2)
m_parameters.pop_back();
}
void ScriptObject::init()
{
m_parameters.clear();
initParameters();
}
std::string ScriptObject::loadScriptFile(const std::string& fileName)
{
std::ifstream fp;
fp.open(fileName.c_str(), std::ios_base::in);
if (fp)
{
std::ostringstream output;
output << fp.rdbuf();
fp.close();
return Embedded::getCurrent()->import_script(fileName);
}
else
{
LOG_ERR << "Error occurred while loading script: " << fileName;
return "";
}
}
void ScriptObject::execResetFct()
{
if (m_scriptLoaded)
Embedded::getCurrent()->exec_fct(m_scriptModule, "reset");
}
void ScriptObject::execStepFct()
{
if (m_scriptLoaded)
Embedded::getCurrent()->exec_fct(m_scriptModule, "step");
}