From 68d891a55ef0bfe2e29edcddd53b7908a55e78d5 Mon Sep 17 00:00:00 2001 From: Peter Targett <131255921+peter-targett@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:10:42 +0000 Subject: [PATCH] Add AnyDictionary and AnyVector support for V2d and Box2d (#1811) * Add AnyDictionary and AnyVector support for V2d and Box2d Signed-off-by: Peter Targett --------- Signed-off-by: Peter Targett --- .../opentimelineio-bindings/otio_bindings.cpp | 4 ++++ .../opentimelineio-bindings/otio_utils.cpp | 4 ++++ tests/test_composable.py | 4 +++- tests/test_serializable_object.py | 10 ++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp b/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp index b83c903f5..ffa2da8c1 100644 --- a/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp +++ b/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp @@ -14,6 +14,8 @@ #include "opentimelineio/typeRegistry.h" #include "opentimelineio/stackAlgorithm.h" +#include + namespace py = pybind11; using namespace pybind11::literals; @@ -270,6 +272,8 @@ PYBIND11_MODULE(_otio, m) { .def(py::init([](RationalTime rt) { return new PyAny(rt); })) .def(py::init([](TimeRange tr) { return new PyAny(tr); })) .def(py::init([](TimeTransform tt) { return new PyAny(tt); })) + .def(py::init([](IMATH_NAMESPACE::V2d v2d) { return new PyAny(v2d); })) + .def(py::init([](IMATH_NAMESPACE::Box2d box2d) { return new PyAny(box2d); })) .def(py::init([](AnyVectorProxy* p) { return new PyAny(p->fetch_any_vector()); })) .def(py::init([](AnyDictionaryProxy* p) { return new PyAny(p->fetch_any_dictionary()); })) ; diff --git a/src/py-opentimelineio/opentimelineio-bindings/otio_utils.cpp b/src/py-opentimelineio/opentimelineio-bindings/otio_utils.cpp index 13e406c65..5ad4206f3 100644 --- a/src/py-opentimelineio/opentimelineio-bindings/otio_utils.cpp +++ b/src/py-opentimelineio/opentimelineio-bindings/otio_utils.cpp @@ -11,6 +11,8 @@ #include "opentimelineio/safely_typed_any.h" #include "opentimelineio/stringUtils.h" +#include + #include #include @@ -60,6 +62,8 @@ void _build_any_to_py_dispatch_table() { t[&typeid(RationalTime)] = [](std::any const& a, bool) { return py::cast(safely_cast_rational_time_any(a)); }; t[&typeid(TimeRange)] = [](std::any const& a, bool) { return py::cast(safely_cast_time_range_any(a)); }; t[&typeid(TimeTransform)] = [](std::any const& a, bool) { return py::cast(safely_cast_time_transform_any(a)); }; + t[&typeid(IMATH_NAMESPACE::V2d)] = [](std::any const& a, bool) { return py::cast(safely_cast_point_any(a)); }; + t[&typeid(IMATH_NAMESPACE::Box2d)] = [](std::any const& a, bool) { return py::cast(safely_cast_box_any(a)); }; t[&typeid(SerializableObject::Retainer<>)] = [](std::any const& a, bool) { SerializableObject* so = safely_cast_retainer_any(a); return py::cast(managing_ptr(so)); }; diff --git a/tests/test_composable.py b/tests/test_composable.py index e923ebdc6..fae9c8cab 100644 --- a/tests/test_composable.py +++ b/tests/test_composable.py @@ -19,9 +19,11 @@ def test_constructor(self): self.assertEqual(seqi.metadata, {'foo': 'bar'}) def test_serialize(self): + b = otio.schema.Box2d( + otio.schema.V2d(0.0, 0.0), otio.schema.V2d(16.0, 9.0)) seqi = otio.core.Composable( name="test", - metadata={"foo": "bar"} + metadata={"box": b} ) encoded = otio.adapters.otio_json.write_to_string(seqi) decoded = otio.adapters.otio_json.read_from_string(encoded) diff --git a/tests/test_serializable_object.py b/tests/test_serializable_object.py index 788a3f798..98a7866e3 100755 --- a/tests/test_serializable_object.py +++ b/tests/test_serializable_object.py @@ -136,6 +136,16 @@ def test_cycle_detection(self): with self.assertRaises(ValueError): o.clone() + def test_imath(self): + b = otio.schema.Box2d( + otio.schema.V2d(0.0, 0.0), otio.schema.V2d(16.0, 9.0)) + so = otio.core.SerializableObjectWithMetadata() + so.metadata["box"] = b + self.assertEqual(repr(so.metadata["box"]), repr(b)) + v = [b.min, b.max] + so.metadata["vectors"] = v + self.assertEqual(repr(so.metadata["vectors"]), repr(v)) + class VersioningTests(unittest.TestCase, otio_test_utils.OTIOAssertions): def test_schema_definition(self):