Skip to content

Commit 16d2d33

Browse files
committed
Cleanup
1 parent 51a4ade commit 16d2d33

File tree

5 files changed

+52
-61
lines changed

5 files changed

+52
-61
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ find_package(behaviortree_cpp REQUIRED)
1212
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
1313
find_package(pybind11_vendor REQUIRED)
1414
find_package(pybind11 REQUIRED)
15+
find_package(fmt REQUIRED)
1516

1617
ament_python_install_package(behaviortree_py PACKAGE_DIR behaviortree_py)
1718

1819
pybind11_add_module(behaviortree_py src/behaviortree_py.cpp)
1920
target_compile_features(behaviortree_py PRIVATE cxx_std_20)
20-
target_link_libraries(behaviortree_py
21-
PRIVATE behaviortree_cpp::behaviortree_cpp)
21+
target_link_libraries(behaviortree_py PRIVATE behaviortree_cpp::behaviortree_cpp
22+
fmt::fmt)
23+
target_include_directories(
24+
behaviortree_py PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
25+
$<INSTALL_INTERFACE:include>)
2226

2327
add_custom_command(
2428
TARGET behaviortree_py
@@ -28,6 +32,7 @@ add_custom_command(
2832
WORKING_DIRECTORY $<TARGET_FILE_DIR:behaviortree_py>
2933
USES_TERMINAL)
3034

35+
install(DIRECTORY include/ DESTINATION include/${PROJECT_NAME})
3136
install(TARGETS behaviortree_py
3237
LIBRARY DESTINATION ${PYTHON_INSTALL_DIR}/behaviortree_py)
3338
install(

examples/scripting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
def say_something_double(
2626
tree_node: bt.TreeNode,
2727
) -> bt.NodeStatus:
28-
print(tree_node.get_input_double("message"))
28+
print(bt.get_input_double(tree_node, "message"))
2929
return bt.NodeStatus.SUCCESS
3030

3131

3232
def say_something(tree_node: bt.TreeNode) -> bt.NodeStatus:
33-
print(tree_node.get_input_string("message"))
33+
print(bt.get_input_string(tree_node, "message"))
3434
return bt.NodeStatus.SUCCESS
3535

3636

3737
def set_value(tree_node: bt.TreeNode) -> bt.NodeStatus:
38-
if not (result := tree_node.set_output("value", 24.9)):
38+
if not (result := bt.set_output(tree_node, "value", 24.9)):
3939
return bt.NodeStatus.FAILURE
4040
return bt.NodeStatus.SUCCESS
4141

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <behaviortree_cpp/bt_factory.h>
2+
#include <behaviortree_cpp/tree_node.h>
3+
#include <fmt/compile.h>
4+
#include <pybind11/pybind11.h>
5+
6+
template <typename T>
7+
void bind_port_methods(pybind11::module &m, const char *type_suffix) {
8+
const std::string input_port =
9+
fmt::format(FMT_COMPILE("input_port_{}"), type_suffix);
10+
const std::string get_input =
11+
fmt::format(FMT_COMPILE("get_input_{}"), type_suffix);
12+
const std::string output_port =
13+
fmt::format(FMT_COMPILE("output_port_{}"), type_suffix);
14+
15+
m.def(
16+
input_port.c_str(),
17+
[](const std::string &name, const std::string &description) {
18+
return BT::InputPort<T>(name, description);
19+
},
20+
pybind11::arg("name"), pybind11::arg("description") = "");
21+
m.def(
22+
output_port.c_str(),
23+
[](const std::string &name, const std::string &description) {
24+
return BT::OutputPort<T>(name, description);
25+
},
26+
pybind11::arg("name"), pybind11::arg("description") = "");
27+
m.def("set_output",
28+
[](BT::TreeNode &tree, const std::string &key, const T &value) {
29+
return tree.setOutput(key, value);
30+
});
31+
m.def(get_input.c_str(),
32+
[](const BT::TreeNode &tree, const std::string &key) {
33+
return tree.getInput<T>(key).value();
34+
});
35+
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<buildtool_depend>ament_cmake</buildtool_depend>
1111

1212
<depend>behaviortree_cpp</depend>
13+
<depend>fmt</depend>
1314

1415
<export>
1516
<build_type>ament_cmake</build_type>

src/behaviortree_py.cpp

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <behaviortree_cpp/loggers/bt_cout_logger.h>
1717
#include <behaviortree_cpp/loggers/groot2_publisher.h>
1818
#include <behaviortree_cpp/tree_node.h>
19+
#include <behaviortree_py/behaviortree_py.hpp>
1920

2021
namespace py = pybind11;
2122
using namespace BT;
@@ -49,26 +50,7 @@ PYBIND11_MODULE(behaviortree_py, m) {
4950
py::class_<TreeNode, std::shared_ptr<TreeNode>>(m, "TreeNode")
5051
.def("name", &TreeNode::name)
5152
.def("status", &TreeNode::status)
52-
.def("type", &TreeNode::type)
53-
.def("get_input_string",
54-
[](TreeNode *self, const std::string &key) {
55-
return self->getInput<std::string>(key).value();
56-
})
57-
.def("get_input_int",
58-
[](TreeNode *self, const std::string &key) {
59-
return self->getInput<int>(key).value();
60-
})
61-
.def("get_input_double",
62-
[](TreeNode *self, const std::string &key) {
63-
return self->getInput<double>(key).value();
64-
})
65-
.def("set_output",
66-
py::overload_cast<const std::string &, const std::string &>(
67-
&TreeNode::setOutput<std::string>))
68-
.def("set_output", py::overload_cast<const std::string &, const int &>(
69-
&TreeNode::setOutput<int>))
70-
.def("set_output", py::overload_cast<const std::string &, const double &>(
71-
&TreeNode::setOutput<double>));
53+
.def("type", &TreeNode::type);
7254

7355
py::class_<BehaviorTreeFactory>(m, "BehaviorTreeFactory")
7456
.def(py::init<>())
@@ -144,40 +126,8 @@ PYBIND11_MODULE(behaviortree_py, m) {
144126
},
145127
py::arg("root_tree"));
146128

147-
m.def(
148-
"input_port_int",
149-
[](BT::StringView name, BT::StringView description) {
150-
return BT::InputPort<int>(name, description);
151-
},
152-
py::arg("name"), py::arg("description") = "")
153-
.def(
154-
"input_port_double",
155-
[](BT::StringView name, BT::StringView description) {
156-
return BT::InputPort<double>(name, description);
157-
},
158-
py::arg("name"), py::arg("description") = "")
159-
.def(
160-
"input_port_string",
161-
[](BT::StringView name, BT::StringView description) {
162-
return BT::InputPort<std::string>(name, description);
163-
},
164-
py::arg("name"), py::arg("description") = "")
165-
.def(
166-
"output_port_int",
167-
[](BT::StringView name, BT::StringView description) {
168-
return BT::OutputPort<int>(name, description);
169-
},
170-
py::arg("name"), py::arg("description") = "")
171-
.def(
172-
"output_port_double",
173-
[](BT::StringView name, BT::StringView description) {
174-
return BT::OutputPort<double>(name, description);
175-
},
176-
py::arg("name"), py::arg("description") = "")
177-
.def(
178-
"output_port_string",
179-
[](BT::StringView name, BT::StringView description) {
180-
return BT::OutputPort<std::string>(name, description);
181-
},
182-
py::arg("name"), py::arg("description") = "");
129+
bind_port_methods<int>(m, "int");
130+
bind_port_methods<double>(m, "double");
131+
bind_port_methods<std::string>(m, "string");
132+
bind_port_methods<bool>(m, "bool");
183133
}

0 commit comments

Comments
 (0)