Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 184b9f8

Browse files
author
Davide Faconti
committed
WIP
1 parent 5b8cac6 commit 184b9f8

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

python/py_main.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "python_node.h"
2+
#include <list>
23

34
const std::string script_text = R"(
45
5-
# define Action HelloPythonAction
66
import BehaviorTree as BT
77
88
class HelloPythonAction:
@@ -11,25 +11,38 @@ class HelloPythonAction:
1111
self.name = name
1212
1313
def tick(self):
14-
print("tick called.")
14+
print("tick called from {}".format(self.name) )
1515
return BT.NodeStatus.SUCCESS
1616
1717
def halt(self):
1818
print("halt called.")
1919
20+
@staticmethod
2021
def requiredNodeParameters():
2122
return [( "paramA", "1" ), ( "paramB", "2" )]
22-
23-
myAction = HelloPythonAction("myAction")
24-
print(myAction.tick())
25-
2623
)";
2724

2825
int main()
2926
{
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>>>();
3141

32-
py::exec( script_text );
42+
for (const auto& p: params)
43+
{
44+
std::cout << p.first << " / " << p.second << std::endl;
45+
}
3346

34-
return 0;
47+
return 0;
3548
}

python/python_node.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@ PYBIND11_EMBEDDED_MODULE(BehaviorTree, m)
3131
[](NodeParameters &par) { return pybind11::make_key_iterator(par.begin(), par.end()); },
3232
pybind11::keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
3333
);
34-
//------------------------------------------
3534
}
3635

0 commit comments

Comments
 (0)