1
1
#include " python_node.h"
2
+ #include < list>
2
3
3
4
const std::string script_text = R"(
4
5
5
- # define Action HelloPythonAction
6
6
import BehaviorTree as BT
7
7
8
8
class HelloPythonAction:
@@ -11,25 +11,38 @@ class HelloPythonAction:
11
11
self.name = name
12
12
13
13
def tick(self):
14
- print("tick called." )
14
+ print("tick called from {}".format(self.name) )
15
15
return BT.NodeStatus.SUCCESS
16
16
17
17
def halt(self):
18
18
print("halt called.")
19
19
20
+ @staticmethod
20
21
def requiredNodeParameters():
21
22
return [( "paramA", "1" ), ( "paramB", "2" )]
22
-
23
- myAction = HelloPythonAction("myAction")
24
- print(myAction.tick())
25
-
26
23
)" ;
27
24
28
25
int main ()
29
26
{
30
- py::scoped_interpreter guard{};
27
+ using namespace py ::literals;
28
+ py::scoped_interpreter guard{};
29
+
30
+ py::exec ( script_text );
31
+ auto locals = py::dict (" _instance_name" _a=" myAction" );
32
+
33
+ py::exec ( " _node_instance = HelloPythonAction(\" {_instance_name}\" .format(**locals())) " ,
34
+ py::globals (), locals);
35
+
36
+ py::exec ( " _tick_result = _node_instance.tick()" , py::globals (), locals );
37
+ std::cout << locals[" _tick_result" ].cast <BT::NodeStatus>() << std::endl;
38
+
39
+ py::exec ( " _required_params = HelloPythonAction.requiredNodeParameters()" , py::globals (), locals );
40
+ auto params = locals[" _required_params" ].cast <std::list<std::pair<std::string, std::string>>>();
31
41
32
- py::exec ( script_text );
42
+ for (const auto & p: params)
43
+ {
44
+ std::cout << p.first << " / " << p.second << std::endl;
45
+ }
33
46
34
- return 0 ;
47
+ return 0 ;
35
48
}
0 commit comments