From cb61f914f70a287e26a58fcea00a6d052102c7b6 Mon Sep 17 00:00:00 2001 From: Brenno Date: Mon, 18 Jan 2021 15:30:11 -0300 Subject: [PATCH] Tracker effect and export video fixes Fixed bug that made Openshot crash if two or more Tracker effects were added to the same video. Fixed bug that made the clips to be incorrectly attached to tracked objects in the exported video. --- src/Clip.cpp | 45 ++++++++++++++++++++++----------------- src/Timeline.cpp | 7 ++++++ src/TrackedObjectBBox.cpp | 2 +- src/TrackedObjectBBox.h | 8 +++---- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/Clip.cpp b/src/Clip.cpp index b6c968a2..1f52fd9b 100644 --- a/src/Clip.cpp +++ b/src/Clip.cpp @@ -248,19 +248,21 @@ Clip::~Clip() void Clip::AttachToTracker(std::string tracked_id) { // Search for the tracked object on the timeline - Timeline *parentTimeline = (Timeline *) ParentTimeline(); + Timeline* parentTimeline = (Timeline *) ParentTimeline(); - // Create a smart pointer to the tracked object from the timeline - std::shared_ptr trackedObject = parentTimeline->GetTrackedObject(tracked_id); - - // Check for valid tracked object - if (trackedObject){ - SetAttachedObject(trackedObject); - return; - } - else{ - return; + // Check if the clip has a parent timeline + if (parentTimeline){ + + // Create a smart pointer to the tracked object from the timeline + std::shared_ptr trackedObject = parentTimeline->GetTrackedObject(tracked_id); + + // Check for valid tracked object + if (trackedObject){ + SetAttachedObject(trackedObject); + } + } + return; } // Set the pointer to the trackedObject this clip is attached to @@ -1138,17 +1140,22 @@ void Clip::AddEffect(EffectBase* effect) Timeline* parentTimeline = (Timeline *) ParentTimeline(); - // Downcast effect as Tracker - Tracker* tracker = (Tracker *) effect; + // Check if this clip has a parent timeline + if (parentTimeline){ - // Get tracked data from the Tracker effect - std::shared_ptr trackedData = tracker->trackedData; + // Downcast effect as Tracker + Tracker* tracker = (Tracker *) effect; - // Set tracked data parent clip to this - trackedData->ParentClip(this); + // Get tracked data from the Tracker effect + std::shared_ptr trackedData = tracker->trackedData; - // Add tracked data to the timeline - parentTimeline->AddTrackedObject(trackedData); + // Set tracked data parent clip to this + trackedData->ParentClip(this); + + // Add tracked data to the timeline + parentTimeline->AddTrackedObject(trackedData); + + } } // Clear cache diff --git a/src/Timeline.cpp b/src/Timeline.cpp index 8cbfeb84..de69bcce 100644 --- a/src/Timeline.cpp +++ b/src/Timeline.cpp @@ -1071,6 +1071,13 @@ void Timeline::SetJsonValue(const Json::Value root) { // Create Clip Clip *c = new Clip(); + // When a clip is attached to an object, it searches for the object + // on it's parent timeline. Setting the parent timeline of the clip here + // allows attaching it to an object when exporting the project (because) + // the exporter script initializes the clip and it's effects + // before setting it's parent timeline. + c->ParentTimeline(this); + // Load Json into Clip c->SetJsonValue(existing_clip); diff --git a/src/TrackedObjectBBox.cpp b/src/TrackedObjectBBox.cpp index 119ec083..b926d448 100644 --- a/src/TrackedObjectBBox.cpp +++ b/src/TrackedObjectBBox.cpp @@ -475,7 +475,7 @@ std::map TrackedObjectBBox::GetParentClipProperties(int64_t // Calculate parentClip's frame number long parentClip_start_position = round( parentClip->Position() * parentClip->info.fps.ToDouble() ) + 1; long parentClip_start_frame = ( parentClip->Start() * parentClip->info.fps.ToDouble() ) + 1; - float parentClip_frame_number = frame_number - parentClip_start_position + parentClip_start_frame; + float parentClip_frame_number = round(frame_number - parentClip_start_position) + parentClip_start_frame; // Get parentClip's Keyframes float parentClip_location_x = parentClip->location_x.GetValue(parentClip_frame_number); diff --git a/src/TrackedObjectBBox.h b/src/TrackedObjectBBox.h index 3e05529d..d7903b73 100644 --- a/src/TrackedObjectBBox.h +++ b/src/TrackedObjectBBox.h @@ -214,10 +214,10 @@ namespace openshot void clear(); /// Get and Set JSON methods - std::string Json() const; ///< Generate JSON string of this object - Json::Value JsonValue() const; ///< Generate Json::Value for this object - void SetJson(const std::string value); ///< Load JSON string into this object - void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object + std::string Json() const override; ///< Generate JSON string of this object + Json::Value JsonValue() const override; ///< Generate Json::Value for this object + void SetJson(const std::string value) override; ///< Load JSON string into this object + void SetJsonValue(const Json::Value root) override; ///< Load Json::Value into this object /// Get all properties for a specific frame (perfect for a UI to display the current state /// of all properties at any time)