14#include <lagrange/Logger.h>
15#include <lagrange/scene/SceneExtension.h>
18#include <lagrange/utils/warnoff.h>
19#include <nanobind/nanobind.h>
20#include <nanobind/stl/string.h>
21#include <nanobind/stl/variant.h>
22#include <lagrange/utils/warnon.h>
25NAMESPACE_BEGIN(NB_NAMESPACE)
26NAMESPACE_BEGIN(detail)
34 bool try_cast(
const handle& src, uint8_t flags, cleanup_list* cleanup)
36 using CasterT = make_caster<T>;
39 if (!caster.from_python(src, flags, cleanup))
return false;
40 value.set(caster.operator cast_t<T>());
44 bool from_python(handle src, uint8_t flags, cleanup_list* cleanup)
noexcept
46 if (PyNumber_Check(src.ptr())) {
48 return try_cast<int>(src, flags, cleanup) || try_cast<double>(src, flags, cleanup);
49 }
else if (try_cast<std::string>(src, flags, cleanup)) {
52 }
else if (PySequence_Check(src.ptr())) {
58 PyObject** o = seq_get(src.ptr(), &n, &temp);
60 bool success = o !=
nullptr;
63 lagrange::scene::Value::Array arr;
65 make_caster<lagrange::scene::Value> caster;
67 for (
size_t i = 0; i < n; ++i) {
68 if (!caster.from_python(o[i], flags, cleanup)) {
73 arr.push_back(caster.operator cast_t<lagrange::scene::Value>());
80 }
else if (PyMapping_Check(src.ptr())) {
83 PyObject* items = PyMapping_Items(src.ptr());
84 if (items ==
nullptr) {
89 Py_ssize_t size = NB_LIST_GET_SIZE(items);
90 bool success = size >= 0;
92 make_caster<std::string> key_caster;
93 make_caster<lagrange::scene::Value> val_caster;
94 lagrange::scene::Value::Object obj;
96 for (Py_ssize_t i = 0; i < size; ++i) {
97 PyObject* item = NB_LIST_GET_ITEM(items, i);
98 PyObject* key = NB_TUPLE_GET_ITEM(item, 0);
99 PyObject* val = NB_TUPLE_GET_ITEM(item, 1);
101 if (!key_caster.from_python(key, flags, cleanup)) {
106 if (!val_caster.from_python(val, flags, cleanup)) {
112 key_caster.operator cast_t<std::string>(),
113 val_caster.operator cast_t<lagrange::scene::Value>());
128 if (value.is_bool()) {
129 return make_caster<bool>::from_cpp(value.get_bool(), policy, cleanup);
130 }
else if (value.is_int()) {
131 return make_caster<int>::from_cpp(value.get_int(), policy, cleanup);
132 }
else if (value.is_real()) {
133 return make_caster<double>::from_cpp(value.get_real(), policy, cleanup);
134 }
else if (value.is_string()) {
135 return make_caster<std::string>::from_cpp(value.get_string(), policy, cleanup);
136 }
else if (value.is_buffer()) {
137 return make_caster<lagrange::scene::Value::Buffer>::from_cpp(
141 }
else if (value.is_array()) {
142 return make_caster<lagrange::scene::Value::Array>::from_cpp(
146 }
else if (value.is_object()) {
147 return make_caster<lagrange::scene::Value::Object>::from_cpp(
152 return none().release();
157NAMESPACE_END(NB_NAMESPACE)
Definition: SceneExtension.h:33
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
@ Value
Values that are not attached to a specific element.
Definition: AttributeFwd.h:42
Main namespace for Lagrange.
Definition: AABBIGL.h:30