AnimatLab  2
Test
ScriptProcessorPy.cpp
1 #include "StdAfx.h"
2 #include "ScriptProcessorPy.h"
3 #include "PyEmbedder.h"
4 
5 #if defined(_DEBUG) && defined(SWIG_PYTHON_INTERPRETER_NO_DEBUG)
6 /* Use debug wrappers with the Python release dll */
7 # undef _DEBUG
8 # include <Python.h>
9 # define _DEBUG
10 #else
11 # include <Python.h>
12 #endif
13 
14 namespace AnimatSimPy
15 {
16 
17 ScriptProcessorPy::ScriptProcessorPy(void)
18 {
19 }
20 
21 ScriptProcessorPy::~ScriptProcessorPy(void)
22 {
23 try
24 {
25 }
26 catch(...)
27 {Std_TraceMsg(0, "Caught Error in desctructor of ScriptProcessorPy\r\n", "", -1, false, true);}
28 }
29 
30 void ScriptProcessorPy::InitPy(std::string strVal)
31 {
32  m_strInitPy = strVal;
33  Initialize();
34 }
35 
36 std::string ScriptProcessorPy::InitPy() {return m_strInitPy;}
37 
38 void ScriptProcessorPy::ResetSimPy(std::string strVal) {m_strResetSimPy = strVal;}
39 
40 std::string ScriptProcessorPy::ResetSimPy() {return m_strResetSimPy;}
41 
42 void ScriptProcessorPy::BeforeStepPhysicsEnginePy(std::string strVal) {m_strBeforeStepPhysicsEnginePy = strVal;}
43 
44 std::string ScriptProcessorPy::BeforeStepPhysicsEnginePy() {return m_strBeforeStepPhysicsEnginePy;}
45 
46 void ScriptProcessorPy::AfterStepPhysicsEnginePy(std::string strVal) {m_strAfterStepPhysicsEnginePy = strVal;}
47 
48 std::string ScriptProcessorPy::AfterStepPhysicsEnginePy() {return m_strAfterStepPhysicsEnginePy;}
49 
50 void ScriptProcessorPy::BeforeStepNeuralEnginePy(std::string strVal) {m_strBeforeStepNeuralEnginePy = strVal;}
51 
52 std::string ScriptProcessorPy::BeforeStepNeuralEnginePy() {return m_strBeforeStepNeuralEnginePy;}
53 
54 void ScriptProcessorPy::AfterStepNeuralEnginePy(std::string strVal) {m_strAfterStepNeuralEnginePy = strVal;}
55 
56 std::string ScriptProcessorPy::AfterStepNeuralEnginePy() {return m_strAfterStepNeuralEnginePy;}
57 
58 void ScriptProcessorPy::KillPy(std::string strVal) {m_strKillPy = strVal;}
59 
60 std::string ScriptProcessorPy::KillPy() {return m_strKillPy;}
61 
62 void ScriptProcessorPy::KillResetPy(std::string strVal) {m_strKillResetPy = strVal;}
63 
64 std::string ScriptProcessorPy::KillResetPy() {return m_strKillResetPy;}
65 
66 void ScriptProcessorPy::SimStartingPy(std::string strVal) {m_strSimStartingPy = strVal;}
67 
68 std::string ScriptProcessorPy::SimStartingPy() {return m_strSimStartingPy;}
69 
70 void ScriptProcessorPy::SimPausingPy(std::string strVal) {m_strSimPausingPy = strVal;}
71 
72 std::string ScriptProcessorPy::SimPausingPy() {return m_strSimPausingPy;}
73 
74 void ScriptProcessorPy::SimStoppingPy(std::string strVal) {m_strSimStoppingPy = strVal;}
75 
76 std::string ScriptProcessorPy::SimStoppingPy() {return m_strSimStoppingPy;}
77 
78 bool ScriptProcessorPy::ExecutePythonScript(const std::string &strPy, bool bThrowError)
79 {
80  //Create a static python embedder to init python. Using static to ensure that this is only done once per process.
81  static PyEmbedder g_pyEmbedder;
82  if(m_bEnabled)
83  return g_pyEmbedder.ExecutePythonScript(strPy, bThrowError);
84  else
85  return true;
86 }
87 
89 {
90  ScriptProcessor::Initialize();
91 
92  ExecutePythonScript(m_strInitPy);
93 }
94 
96 {
97  ScriptProcessor::ResetSimulation();
98  ExecutePythonScript(m_strResetSimPy);
99 }
100 
101 void ScriptProcessorPy::BeforeStepPhysicsEngine()
102 {
103  ScriptProcessor::BeforeStepPhysicsEngine();
104  ExecutePythonScript(m_strBeforeStepPhysicsEnginePy);
105 }
106 
107 void ScriptProcessorPy::AfterStepPhysicsEngine()
108 {
109  ScriptProcessor::AfterStepPhysicsEngine();
110  ExecutePythonScript(m_strAfterStepPhysicsEnginePy);
111 }
112 
113 void ScriptProcessorPy::BeforeStepNeuralEngine()
114 {
115  ScriptProcessor::BeforeStepNeuralEngine();
116  ExecutePythonScript(m_strBeforeStepNeuralEnginePy);
117 }
118 
119 void ScriptProcessorPy::AfterStepNeuralEngine()
120 {
121  ScriptProcessor::AfterStepNeuralEngine();
122  ExecutePythonScript(m_strAfterStepNeuralEnginePy);
123 }
124 
125 void ScriptProcessorPy::Kill(bool bState)
126 {
127  ScriptProcessor::Kill(bState);
128 
129  if(bState)
130  ExecutePythonScript(m_strKillPy);
131  else
132  ExecutePythonScript(m_strKillResetPy);
133 }
134 
136 {
137  ScriptProcessor::SimStarting();
138  ExecutePythonScript(m_strSimStartingPy);
139 }
140 
142 {
143  ScriptProcessor::SimPausing();
144  ExecutePythonScript(m_strSimPausingPy);
145 }
146 
148 {
149  ScriptProcessor::SimStopping();
150  ExecutePythonScript(m_strSimStoppingPy);
151 }
152 
153 bool ScriptProcessorPy::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
154 {
155  std::string strType = Std_CheckString(strDataType);
156 
157  if(ScriptProcessor::SetData(strDataType, strValue, false))
158  return true;
159 
160  if(strType == "ENABLED")
161  {
162  Enabled(Std_ToBool(strValue));
163  return true;
164  }
165 
166  if(strType == "RESETSIMPY")
167  {
168  ResetSimPy(strValue);
169  return true;
170  }
171 
172  if(strType == "BEFORESTEPPHYSICSENGINEPY")
173  {
174  BeforeStepPhysicsEnginePy(strValue);
175  return true;
176  }
177 
178  if(strType == "AFTERSTEPPHYSICSENGINEPY")
179  {
180  AfterStepPhysicsEnginePy(strValue);
181  return true;
182  }
183 
184  if(strType == "BEFORESTEPNEURALENGINEPY")
185  {
186  BeforeStepNeuralEnginePy(strValue);
187  return true;
188  }
189 
190  if(strType == "AFTERSTEPNEURALENGINEPY")
191  {
192  AfterStepNeuralEnginePy(strValue);
193  return true;
194  }
195 
196  if(strType == "INITPY")
197  {
198  InitPy(strValue);
199  return true;
200  }
201 
202  if(strType == "KILLPY")
203  {
204  KillPy(strValue);
205  return true;
206  }
207 
208  if(strType == "KILLRESETPY")
209  {
210  KillResetPy(strValue);
211  return true;
212  }
213 
214  if(strType == "SIMSTARTINGPY")
215  {
216  SimStartingPy(strValue);
217  return true;
218  }
219 
220  if(strType == "SIMPAUSINGPY")
221  {
222  SimPausingPy(strValue);
223  return true;
224  }
225 
226  if(strType == "SIMSTOPPINGPY")
227  {
228  SimStoppingPy(strValue);
229  return true;
230  }
231 
232  //If it was not one of those above then we have a problem.
233  if(bThrowError)
234  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Data Type", strDataType);
235 
236  return false;
237 }
238 
239 void ScriptProcessorPy::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
240 {
241  ScriptProcessor::QueryProperties(aryProperties);
242 
243  aryProperties.Add(new TypeProperty("Enabled", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
244  aryProperties.Add(new TypeProperty("InitPy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
245  aryProperties.Add(new TypeProperty("ResetSimPy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
246  aryProperties.Add(new TypeProperty("BeforeStepPhysicsEnginePy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
247  aryProperties.Add(new TypeProperty("AfterStepPhysicsEnginePy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
248  aryProperties.Add(new TypeProperty("BeforeStepNeuralEnginePy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
249  aryProperties.Add(new TypeProperty("AfterStepNeuralEnginePy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
250  aryProperties.Add(new TypeProperty("KillPy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
251  aryProperties.Add(new TypeProperty("KillResetPy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
252  aryProperties.Add(new TypeProperty("SimStartingPy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
253  aryProperties.Add(new TypeProperty("SimPausingPy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
254  aryProperties.Add(new TypeProperty("SimStoppingPy", AnimatPropertyType::String, AnimatPropertyDirection::Set));
255 }
256 
258 {
259  ScriptProcessor::Load(oXml);
260 
261  oXml.IntoElem(); //Into Script Element
262 
263  Enabled(oXml.GetChildBool("Enabled", m_bEnabled));
264  InitPy(oXml.GetChildString("InitPy", m_strInitPy));
265  ResetSimPy(oXml.GetChildString("ResetSimPy", m_strResetSimPy));
266  BeforeStepPhysicsEnginePy(oXml.GetChildString("BeforeStepPhysicsEnginePy", m_strBeforeStepPhysicsEnginePy));
267  AfterStepPhysicsEnginePy(oXml.GetChildString("AfterStepPhysicsEnginePy", m_strAfterStepPhysicsEnginePy));
268  BeforeStepNeuralEnginePy(oXml.GetChildString("BeforeStepNeuralEnginePy", m_strBeforeStepNeuralEnginePy));
269  AfterStepNeuralEnginePy(oXml.GetChildString("AfterStepNeuralEnginePy", m_strAfterStepNeuralEnginePy));
270  KillPy(oXml.GetChildString("KillPy", m_strKillPy));
271  KillResetPy(oXml.GetChildString("KillResetPy", m_strKillResetPy));
272  SimStartingPy(oXml.GetChildString("SimStartingPy", m_strSimStartingPy));
273  SimPausingPy(oXml.GetChildString("SimPausingPy", m_strSimPausingPy));
274  SimStoppingPy(oXml.GetChildString("SimStoppingPy", m_strSimStoppingPy));
275 
276  oXml.OutOfElem(); //OutOf Script Element
277 }
278 
279 
280 }
virtual void Kill(bool bState=true)
Kills.
virtual void Initialize()
Initializes this object.
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual void SimStopping()
Called just before the simulation stops.
virtual void SimPausing()
Called just before the simulation pauses.
virtual bool Enabled()
Tells whether this item is enabled or not. This is not actually used for all objects, only specific ones. I am putting it in the base class though to prevent numerous duplications.
Definition: AnimatBase.cpp:113
A standard xml manipulation class.
Definition: StdXml.h:19
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
bool Std_ToBool(int iVal)
Converts a value toa bool.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
Definition: StdXml.cpp:699
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void ResetSimulation()
Resets the simulation back to time 0.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
virtual void SimStarting()
Called just before the simulation starts.