Skip to content

Commit 5f3220e

Browse files
committed
fix: guards for template specialization of pyobject fromJson()
1 parent 38fb950 commit 5f3220e

File tree

9 files changed

+36
-161
lines changed

9 files changed

+36
-161
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ target_link_libraries(${BTCPP_LIBRARY}
192192
if(BTCPP_PYTHON)
193193
find_package(Python COMPONENTS Interpreter Development)
194194
find_package(pybind11 CONFIG)
195+
message("PYTHON_EXECUTABLE: ${Python_EXECUTABLE}")
195196

196197
pybind11_add_module(btpy_cpp src/python/bindings.cpp)
197198
target_compile_options(btpy_cpp PRIVATE -Wno-gnu-zero-variadic-macro-arguments)

include/behaviortree_cpp/contrib/pybind11_json.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef PYBIND11_JSON_HPP
1010
#define PYBIND11_JSON_HPP
1111

12+
#include <pybind11/pytypes.h>
13+
#include <pybind11/numpy.h>
1214
#include <string>
1315
#include <vector>
1416

@@ -134,6 +136,10 @@ namespace pyjson
134136
}
135137
return out;
136138
}
139+
if (py::isinstance<py::array>(obj))
140+
{
141+
return obj.cast<nl::json::array_t>();
142+
}
137143
throw std::runtime_error("to_json not implemented for this type of object: " + py::repr(obj).cast<std::string>());
138144
}
139145
}

include/behaviortree_cpp/json_export.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
#include "behaviortree_cpp/utils/safe_any.hpp"
44
#include "behaviortree_cpp/basic_types.h"
5-
65
// Use the version nlohmann::json embedded in BT.CPP
76
#include "behaviortree_cpp/contrib/json.hpp"
87

8+
#ifdef BTCPP_PYTHON
9+
#include <pybind11/pybind11.h>
10+
#include <pybind11/pytypes.h>
11+
#include "behaviortree_cpp/contrib/pybind11_json.hpp"
12+
#endif
13+
914
namespace BT
1015
{
1116

@@ -82,13 +87,6 @@ class JsonExporter
8287
template <typename T>
8388
Expected<T> fromJson(const nlohmann::json& source) const;
8489

85-
template <typename T>
86-
void fromJsonHelper(const nlohmann::json& src, T& dst) const
87-
{
88-
dst = *fromJson<T>(src);
89-
}
90-
91-
9290
/**
9391
* @brief Register new JSON converters with addConverter<Foo>().
9492
* You should used first the macro BT_JSON_CONVERTER.
@@ -126,7 +124,14 @@ class JsonExporter
126124
std::unordered_map<std::type_index, FromJonConverter> from_json_array_converters_;
127125
std::unordered_map<std::string, BT::TypeInfo> type_names_;
128126
};
129-
127+
#ifdef BTCPP_PYTHON
128+
template <>
129+
inline Expected<pybind11::object>
130+
JsonExporter::fromJson(const nlohmann::json& source) const
131+
{
132+
return pyjson::from_json(source);
133+
}
134+
#endif
130135
template <typename T>
131136
inline Expected<T> JsonExporter::fromJson(const nlohmann::json& source) const
132137
{

include/behaviortree_cpp/python/types.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
#include <pybind11/pybind11.h>
44

5-
#include "behaviortree_cpp/basic_types.h"
65
#include "behaviortree_cpp/json_export.h"
7-
#include "behaviortree_cpp/contrib/json.hpp"
8-
#include "behaviortree_cpp/contrib/pybind11_json.hpp"
6+
97
#include "behaviortree_cpp/utils/safe_any.hpp"
108

119
namespace BT
@@ -20,12 +18,12 @@ namespace BT
2018
template <typename T>
2119
bool fromPythonObject(const pybind11::object& obj, T& dest)
2220
{
23-
if constexpr(nlohmann::detail::is_getable<nlohmann::json, T>::value)
21+
auto dest_maybe = JsonExporter::get().fromJson<T>(obj);
22+
if(dest_maybe.has_value())
2423
{
25-
JsonExporter::get().fromJsonHelper<T>(obj, dest);
24+
dest = dest_maybe.value();
2625
return true;
2726
}
28-
2927
return false;
3028
}
3129

python_examples/ex02_generic_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import numpy as np
88
from btpy import BehaviorTreeFactory, SyncActionNode, NodeStatus, ports
99

10-
1110
xml_text = """
1211
<root BTCPP_format="4" >
1312
@@ -24,7 +23,7 @@
2423
"""
2524

2625

27-
@ports(inputs=["position", "theta"], outputs=["out"])
26+
@ports(inputs=["position", "theta"], outputs=["out", "position"])
2827
class Rotate(SyncActionNode):
2928
def tick(self):
3029
# Build a rotation matrix which rotates points by `theta` degrees.
@@ -38,6 +37,7 @@ def tick(self):
3837

3938
# Set the output.
4039
self.set_output("out", rotated)
40+
self.set_output("position", position)
4141

4242
return NodeStatus.SUCCESS
4343

python_examples/ex05_type_interop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def tick(self):
6060

6161

6262
factory = BehaviorTreeFactory()
63-
factory.register_from_plugin("sample_nodes/bin/libdummy_nodes_dyn.so")
63+
factory.register_from_plugin("build/sample_nodes/bin/libdummy_nodes_dyn.so")
6464
factory.register(PutVector)
6565
factory.register(Print)
6666

sample_nodes/dummy_nodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ inline void RegisterNodes(BT::BehaviorTreeFactory& factory)
198198
std::bind(&GripperInterface::close, &grip_singleton));
199199
factory.registerNodeType<ApproachObject>("ApproachObject");
200200
factory.registerNodeType<SaySomething>("SaySomething");
201+
factory.registerNodeType<RandomVector>("RandomVector");
202+
factory.registerNodeType<PrintMapOfVectors>("PrintMapOfVectors");
201203
}
202204

203205
} // namespace DummyNodes

setup.py

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/python/types.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
#include "behaviortree_cpp/json_export.h"
77
#include "behaviortree_cpp/contrib/json.hpp"
8-
#include "behaviortree_cpp/contrib/pybind11_json.hpp"
98

109
namespace BT
1110
{
1211

13-
bool toPythonObject(const BT::Any& val, pybind11::object& dest)
12+
#if defined(_WIN32)
13+
__declspec(dllexport)
14+
#else
15+
__attribute__((visibility("default")))
16+
#endif
17+
bool toPythonObject(const BT::Any& val, pybind11::object& dest)
1418
{
1519
nlohmann::json json;
1620
if(JsonExporter::get().toJson(val, json))

0 commit comments

Comments
 (0)