From 2d72e54798a62a0804d198054f04cece0614c671 Mon Sep 17 00:00:00 2001 From: Darby Johnston Date: Wed, 21 Oct 2026 12:07:20 -0700 Subject: [PATCH] Add Python bindings for the edit algorithms The functions in algo/editAlgorithm.h (overwrite, insert, trim, slice, slip, slide, ripple, roll, fill and remove) and the ReferencePoint enum are available as otio.algorithms.edit. The tests check that each one can be called from Python; the algorithms are tested in C++. Signed-off-by: Darby Johnston --- .../opentimelineio-bindings/CMakeLists.txt | 1 + .../opentimelineio-bindings/otio_bindings.cpp | 1 + .../opentimelineio-bindings/otio_bindings.h | 1 + .../otio_editAlgorithm.cpp | 326 ++++++++++++++++++ .../opentimelineio/algorithms/__init__.py | 2 + .../opentimelineio/algorithms/edit.py | 18 + tests/test_edit_algo.py | 151 ++++++++ 7 files changed, 500 insertions(+) create mode 100644 src/py-opentimelineio/opentimelineio-bindings/otio_editAlgorithm.cpp create mode 100644 src/py-opentimelineio/opentimelineio/algorithms/edit.py create mode 100644 tests/test_edit_algo.py diff --git a/src/py-opentimelineio/opentimelineio-bindings/CMakeLists.txt b/src/py-opentimelineio/opentimelineio-bindings/CMakeLists.txt index 5362e62655..5fb55a3b0c 100644 --- a/src/py-opentimelineio/opentimelineio-bindings/CMakeLists.txt +++ b/src/py-opentimelineio/opentimelineio-bindings/CMakeLists.txt @@ -17,6 +17,7 @@ pybind11_add_module(_otio otio_serializableObjects.cpp otio_utils.cpp otio_bundle.cpp + otio_editAlgorithm.cpp ${_OTIO_HEADER_FILES}) target_include_directories(_otio diff --git a/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp b/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp index 071c88eab1..27982a2d31 100644 --- a/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp +++ b/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.cpp @@ -194,6 +194,7 @@ PYBIND11_MODULE(_otio, m) otio_serializable_object_bindings(m); otio_tests_bindings(m); otio_bundle_bindings(m); + otio_edit_algorithm_bindings(m); m.def( "_serialize_json_to_string", diff --git a/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.h b/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.h index cd03a9ae51..76ab43fec4 100644 --- a/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.h +++ b/src/py-opentimelineio/opentimelineio-bindings/otio_bindings.h @@ -12,3 +12,4 @@ void otio_imath_bindings(pybind11::module); void otio_serializable_object_bindings(pybind11::module); void otio_tests_bindings(pybind11::module); void otio_bundle_bindings(pybind11::module); +void otio_edit_algorithm_bindings(pybind11::module); diff --git a/src/py-opentimelineio/opentimelineio-bindings/otio_editAlgorithm.cpp b/src/py-opentimelineio/opentimelineio-bindings/otio_editAlgorithm.cpp new file mode 100644 index 0000000000..54d434cd79 --- /dev/null +++ b/src/py-opentimelineio/opentimelineio-bindings/otio_editAlgorithm.cpp @@ -0,0 +1,326 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Contributors to the OpenTimelineIO project + +#include + +#include "otio_errorStatusHandler.h" + +#include + +using namespace OTIO_NS; +using namespace OTIO_NS::algo; + +namespace py = pybind11; +using namespace pybind11::literals; + +void otio_edit_algorithm_bindings(pybind11::module m) +{ + auto malgo = m.def_submodule("algo"); + + py::enum_(malgo, "ReferencePoint", +R"docstring( +The reference point used by :func:`fill` for a 3/4 point edit. +)docstring") + .value( + "Source", + ReferencePoint::Source, + "Place the item with its own duration, overwriting past the gap if " + "it is longer.") + .value( + "Sequence", + ReferencePoint::Sequence, + "Place a copy of the item, trimmed to fit the gap.") + .value( + "Fit", + ReferencePoint::Fit, + "Place a new item with a time warp that fits it to the gap."); + + malgo.def( + "overwrite", + [](Item* item, + Composition* composition, + TimeRange const& range, + bool remove_transitions, + Item* fill_template) { + overwrite( + item, + composition, + range, + remove_transitions, + fill_template, + ErrorStatusHandler()); + }, + "item"_a, + "composition"_a, + "range"_a, + "remove_transitions"_a = true, + "fill_template"_a = nullptr, + R"docstring( +Overwrite the items in a range of a composition with an item. + +:: + + | A | B | -> |A| C |B| + ^ ^ + | C | + +Items partly in the range are trimmed and items wholly in it are removed. If +the range starts after the end of the composition, the hole is filled with +``fill_template``. + +:param Item item: The item to overwrite with (usually a clip). +:param Composition composition: The composition (usually a track). +:param TimeRange range: The range to overwrite. +:param bool remove_transitions: Whether to remove transitions in the range. +:param Item fill_template: The item that fills holes; a gap if None. +)docstring"); + + malgo.def( + "insert", + [](Item* item, + Composition* composition, + RationalTime const& time, + bool remove_transitions, + Item* fill_template) { + insert( + item, + composition, + time, + remove_transitions, + fill_template, + ErrorStatusHandler()); + }, + "item"_a, + "composition"_a, + "time"_a, + "remove_transitions"_a = true, + "fill_template"_a = nullptr, + R"docstring( +Insert an item into a composition, splitting the item at that time. + +:: + + | A | B | -> | A | C | A | B | + ^ + | C | + +The composition becomes longer by the item's duration. A time before the start +inserts at the start, and a time after the end appends, filling the hole with +``fill_template``. + +:param Item item: The item to insert (usually a clip). +:param Composition composition: The composition (usually a track). +:param RationalTime time: The time to insert at. +:param bool remove_transitions: Whether to remove transitions at the time. +:param Item fill_template: The item that fills holes; a gap if None. +)docstring"); + + malgo.def( + "trim", + [](Item* item, + RationalTime const& delta_in, + RationalTime const& delta_out, + Item* fill_template) { + trim( + item, + delta_in, + delta_out, + fill_template, + ErrorStatusHandler()); + }, + "item"_a, + "delta_in"_a, + "delta_out"_a, + "fill_template"_a = nullptr, + R"docstring( +Adjust the start or end of an item without moving the other items. + +:: + + | A | B | C | -> | A |FILL| B | C | + <--* + +Time left empty is filled with ``fill_template``, or added to a neighboring +gap. + +:param Item item: The item to trim (usually a clip). +:param RationalTime delta_in: The amount to adjust the start by. +:param RationalTime delta_out: The amount to adjust the end by. +:param Item fill_template: The item that fills holes; a gap if None. +)docstring"); + + malgo.def( + "slice", + [](Composition* composition, + RationalTime const& time, + bool remove_transitions) { + slice(composition, time, remove_transitions, ErrorStatusHandler()); + }, + "composition"_a, + "time"_a, + "remove_transitions"_a = true, + R"docstring( +Split the item at a time into two items. + +:: + + | A | B | -> |A|A| B | + ^ + +:param Composition composition: The composition (usually a track). +:param RationalTime time: The time to split at. +:param bool remove_transitions: Whether to remove transitions at the time. +)docstring"); + + malgo.def( + "slip", + [](Item* item, RationalTime const& delta) { slip(item, delta); }, + "item"_a, + "delta"_a, + R"docstring( +Move an item's source range without changing its duration or the other items. + +:: + + | A | + <-----> + +The source range is clamped to the media's available range, if there is one. + +:param Item item: The item to slip (usually a clip). +:param RationalTime delta: The amount to move the source range by. +)docstring"); + + malgo.def( + "slide", + [](Item* item, RationalTime const& delta) { slide(item, delta); }, + "item"_a, + "delta"_a, + R"docstring( +Move an item by changing the duration of the item before it. + +:: + + | A | B | C | -> | A | B | C | + *---> + +The previous item is clamped to its media's available range, if there is one. +The first item in a composition is not changed. + +:param Item item: The item to slide (usually a clip). +:param RationalTime delta: The amount to move the item by. +)docstring"); + + malgo.def( + "ripple", + [](Item* item, + RationalTime const& delta_in, + RationalTime const& delta_out) { + ripple(item, delta_in, delta_out, ErrorStatusHandler()); + }, + "item"_a, + "delta_in"_a, + "delta_out"_a, + R"docstring( +Adjust the start or end of an item without changing the other items, so +the items after it move. + +:: + + | A | B | -> | A | B | + <--* + +:param Item item: The item to ripple (usually a clip). +:param RationalTime delta_in: The amount to adjust the start by. +:param RationalTime delta_out: The amount to adjust the end by. +)docstring"); + + malgo.def( + "roll", + [](Item* item, + RationalTime const& delta_in, + RationalTime const& delta_out) { + roll(item, delta_in, delta_out, ErrorStatusHandler()); + }, + "item"_a, + "delta_in"_a, + "delta_out"_a, + R"docstring( +Adjust the start or end of an item, adjusting the neighboring items to fit. + +:: + + | A | B | -> | A | B | + <--* + +No items are added, and the items before and after keep their positions. +Adjustments are clamped to the media's available range, if there is one. + +:param Item item: The item to roll (usually a clip). +:param RationalTime delta_in: The amount to adjust the start by. +:param RationalTime delta_out: The amount to adjust the end by. +)docstring"); + + malgo.def( + "fill", + [](Item* item, + Composition* track, + RationalTime const& track_time, + ReferencePoint reference_point) { + fill( + item, + track, + track_time, + reference_point, + ErrorStatusHandler()); + }, + "item"_a, + "track"_a, + "track_time"_a, + "reference_point"_a = ReferencePoint::Source, + R"docstring( +Place an item into the gap at a time (a 3/4 point edit). + +:: + + | A |GAP| B | -> | A | C | B | + ^ ^ + C--| C |--C + +:param Item item: The item to place (usually a clip). +:param Composition track: The track to place the item in. +:param RationalTime track_time: A time in the gap. +:param ReferencePoint reference_point: How to fit the item to the gap. +)docstring"); + + malgo.def( + "remove", + [](Composition* composition, + RationalTime const& time, + bool fill, + Item* fill_template) { + remove( + composition, + time, + fill, + fill_template, + ErrorStatusHandler()); + }, + "composition"_a, + "time"_a, + "fill"_a = true, + "fill_template"_a = nullptr, + R"docstring( +Remove the item at a time. + +:: + + | A | C | B | -> | A |GAP| B | + ^ + +:param Composition composition: The composition (usually a track). +:param RationalTime time: The time of the item to remove. +:param bool fill: Whether to fill the hole; if False the items after move up. +:param Item fill_template: The item that fills the hole; a gap if None. +)docstring"); +} diff --git a/src/py-opentimelineio/opentimelineio/algorithms/__init__.py b/src/py-opentimelineio/opentimelineio/algorithms/__init__.py index d6f2eaea24..941f3f6ab0 100644 --- a/src/py-opentimelineio/opentimelineio/algorithms/__init__.py +++ b/src/py-opentimelineio/opentimelineio/algorithms/__init__.py @@ -21,3 +21,5 @@ from .timeline_algo import ( timeline_trimmed_to_range ) + +from . import edit diff --git a/src/py-opentimelineio/opentimelineio/algorithms/edit.py b/src/py-opentimelineio/opentimelineio/algorithms/edit.py new file mode 100644 index 0000000000..feed1f27b8 --- /dev/null +++ b/src/py-opentimelineio/opentimelineio/algorithms/edit.py @@ -0,0 +1,18 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright Contributors to the OpenTimelineIO project + +__doc__ = """ Algorithms for editing compositions. """ + +from .. import _otio + +ReferencePoint = _otio.algo.ReferencePoint +overwrite = _otio.algo.overwrite +insert = _otio.algo.insert +trim = _otio.algo.trim +slice = _otio.algo.slice +slip = _otio.algo.slip +slide = _otio.algo.slide +ripple = _otio.algo.ripple +roll = _otio.algo.roll +fill = _otio.algo.fill +remove = _otio.algo.remove diff --git a/tests/test_edit_algo.py b/tests/test_edit_algo.py new file mode 100644 index 0000000000..152d90eaed --- /dev/null +++ b/tests/test_edit_algo.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright Contributors to the OpenTimelineIO project + +"""Test file for the edit algorithms bindings. + +The algorithms are tested in C++ (test_editAlgorithm.cpp), these tests check +that each one can be called from Python. +""" + +import unittest + +import opentimelineio as otio + + +def _clip(name, duration, start=0): + return otio.schema.Clip( + name=name, + source_range=otio.opentime.TimeRange( + otio.opentime.RationalTime(start, 24), + otio.opentime.RationalTime(duration, 24) + ) + ) + + +def _track(*durations): + return otio.schema.Track( + children=[_clip("ABC"[i], d) for i, d in enumerate(durations)] + ) + + +def _rt(value): + return otio.opentime.RationalTime(value, 24) + + +def _ranges(track): + return [ + (c.name or c.schema_name(), + c.source_range.start_time.value, + c.source_range.duration.value) + for c in track + ] + + +class EditAlgoTests(unittest.TestCase): + + def test_overwrite(self): + track = _track(10, 10) + otio.algorithms.edit.overwrite( + _clip("C", 4), + track, + otio.opentime.TimeRange(_rt(3), _rt(4)) + ) + self.assertEqual( + _ranges(track), + [("A", 0, 3), ("C", 0, 4), ("A", 7, 3), ("B", 0, 10)] + ) + + def test_insert(self): + track = _track(10, 10) + otio.algorithms.edit.insert(_clip("C", 4), track, _rt(5)) + self.assertEqual( + [(c.name, c.source_range.duration.value) for c in track], + [("A", 5), ("C", 4), ("A", 5), ("B", 10)] + ) + + def test_trim(self): + track = _track(10, 10) + otio.algorithms.edit.trim(track[0], _rt(0), _rt(-3)) + self.assertEqual( + _ranges(track), + [("A", 0, 7), ("Gap", 0, 3), ("B", 0, 10)] + ) + + def test_slice(self): + track = _track(10, 10) + otio.algorithms.edit.slice(track, _rt(5)) + self.assertEqual( + _ranges(track), + [("A", 0, 5), ("A", 5, 5), ("B", 0, 10)] + ) + + def test_slip(self): + track = _track(10, 10) + otio.algorithms.edit.slip(track[0], _rt(3)) + self.assertEqual(_ranges(track), [("A", 3, 10), ("B", 0, 10)]) + + def test_slide(self): + track = _track(10, 10, 10) + otio.algorithms.edit.slide(track[1], _rt(2)) + self.assertEqual( + _ranges(track), + [("A", 0, 12), ("B", 0, 10), ("C", 0, 10)] + ) + + def test_ripple(self): + track = _track(10, 10) + otio.algorithms.edit.ripple(track[0], _rt(0), _rt(-3)) + self.assertEqual(_ranges(track), [("A", 0, 7), ("B", 0, 10)]) + + def test_roll(self): + track = otio.schema.Track(children=[_clip("A", 10), _clip("B", 10, 5)]) + otio.algorithms.edit.roll(track[1], _rt(-3), _rt(0)) + self.assertEqual(_ranges(track), [("A", 0, 7), ("B", 2, 13)]) + + def test_fill(self): + track = _track(10, 10, 10) + track[1] = otio.schema.Gap(duration=_rt(10)) + otio.algorithms.edit.fill(_clip("C", 4), track, _rt(12)) + self.assertEqual( + _ranges(track), + [("A", 0, 10), ("Gap", 0, 2), ("C", 0, 4), ("Gap", 6, 4), + ("C", 0, 10)] + ) + + track = _track(10, 10, 10) + track[1] = otio.schema.Gap(duration=_rt(10)) + otio.algorithms.edit.fill( + _clip("C", 20), + track, + _rt(10), + otio.algorithms.edit.ReferencePoint.Sequence + ) + self.assertEqual( + _ranges(track), + [("A", 0, 10), ("C", 0, 10), ("C", 0, 10)] + ) + + with self.assertRaises(ValueError): + otio.algorithms.edit.fill(_clip("C", 4), _track(10, 10), _rt(5)) + + def test_remove(self): + track = _track(10, 10, 10) + otio.algorithms.edit.remove(track, _rt(15)) + self.assertEqual( + _ranges(track), + [("A", 0, 10), ("Gap", 0, 10), ("C", 0, 10)] + ) + + track = _track(10, 10, 10) + otio.algorithms.edit.remove(track, _rt(15), fill=False) + self.assertEqual(_ranges(track), [("A", 0, 10), ("C", 0, 10)]) + + def test_error(self): + with self.assertRaises(otio.exceptions.NotAChildError): + otio.algorithms.edit.roll(_clip("C", 4), _rt(1), _rt(0)) + + +if __name__ == '__main__': + unittest.main()