Add copy/make_local to movieclip datablock.
authorBastien Montagne <montagne29@wanadoo.fr>
Fri, 29 Jul 2016 21:13:31 +0000 (23:13 +0200)
committerBastien Montagne <montagne29@wanadoo.fr>
Fri, 29 Jul 2016 21:13:31 +0000 (23:13 +0200)
As title says... Copying tracking data from movieclip was not the simplest thing...

Reviewers: sergey

Differential Revision: https://developer.blender.org/D2126

source/blender/blenkernel/BKE_movieclip.h
source/blender/blenkernel/BKE_tracking.h
source/blender/blenkernel/intern/library.c
source/blender/blenkernel/intern/movieclip.c
source/blender/blenkernel/intern/tracking.c

index 3d963ac109c45578cd6049eefe127d18997e7e03..3237c146bc52470d0262d15339a6a13c68800487 100644 (file)
@@ -41,6 +41,9 @@ struct MovieDistortion;
 
 void BKE_movieclip_free(struct MovieClip *clip);
 
+struct MovieClip *BKE_movieclip_copy(struct Main *bmain, struct MovieClip *clip);
+void BKE_movieclip_make_local(struct Main *bmain, struct MovieClip *clip, const bool lib_local);
+
 struct MovieClip *BKE_movieclip_file_add(struct Main *bmain, const char *name);
 struct MovieClip *BKE_movieclip_file_add_exists_ex(struct Main *bmain, const char *name, bool *r_exists);
 struct MovieClip *BKE_movieclip_file_add_exists(struct Main *bmain, const char *name);
index 2ca88425c2941af3da612aed4b448282cddac51e..1938bb08395332f8c2db0a0d8ea7613c646d7bf8 100644 (file)
@@ -52,6 +52,7 @@ struct rcti;
 /* **** Common functions **** */
 
 void BKE_tracking_free(struct MovieTracking *tracking);
+void BKE_tracking_copy(struct MovieTracking *tracking_dst, struct MovieTracking *tracking_src);
 
 void BKE_tracking_settings_init(struct MovieTracking *tracking);
 
index 5a6399145faf2e0a7f4e33e8b903accec77a05f5..50c1b2c8f1e96361e4522e2ebbe078df5fb38ad6 100644 (file)
 #include "BKE_main.h"
 #include "BKE_mball.h"
 #include "BKE_mask.h"
+#include "BKE_movieclip.h"
 #include "BKE_node.h"
 #include "BKE_object.h"
 #include "BKE_paint.h"
@@ -407,6 +408,9 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local)
                case ID_GD:
                        if (!test) BKE_gpencil_make_local(bmain, (bGPdata *)id, lib_local);
                        return true;
+               case ID_MC:
+                       if (!test) BKE_movieclip_make_local(bmain, (MovieClip *)id, lib_local);
+                       return true;
                case ID_MSK:
                        if (!test) BKE_mask_make_local(bmain, (Mask *)id, lib_local);
                        return true;
@@ -419,8 +423,6 @@ bool id_make_local(Main *bmain, ID *id, const bool test, const bool lib_local)
                case ID_PC:
                        if (!test) BKE_paint_curve_make_local(bmain, (PaintCurve *)id, lib_local);
                        return true;
-               case ID_MC:
-                       return false;  /* TODO missing implementation */
                case ID_SCR:
                case ID_LI:
                case ID_KE:
@@ -510,6 +512,9 @@ bool id_copy(Main *bmain, ID *id, ID **newid, bool test)
                case ID_GD:
                        if (!test) *newid = (ID *)gpencil_data_duplicate(bmain, (bGPdata *)id, false);
                        return true;
+               case ID_MC:
+                       if (!test) *newid = (ID *)BKE_movieclip_copy(bmain, (MovieClip *)id);
+                       return true;
                case ID_MSK:
                        if (!test) *newid = (ID *)BKE_mask_copy(bmain, (Mask *)id);
                        return true;
@@ -529,7 +534,6 @@ bool id_copy(Main *bmain, ID *id, ID **newid, bool test)
                        return false;  /* can't be copied from here */
                case ID_VF:
                case ID_SO:
-               case ID_MC:
                        return false;  /* not implemented */
                case ID_IP:
                        return false;  /* deprecated */
index f99457a4c26c91ce3f972ff5abb6d7bb56e673ba..0d3620861347b7a38d80722a91a12b20e858afd7 100644 (file)
@@ -1491,6 +1491,32 @@ void BKE_movieclip_free(MovieClip *clip)
        BKE_tracking_free(&clip->tracking);
 }
 
+MovieClip *BKE_movieclip_copy(Main *bmain, MovieClip *clip)
+{
+       MovieClip *clip_new;
+
+       clip_new = BKE_libblock_copy(bmain, &clip->id);
+
+       clip_new->anim = NULL;
+       clip_new->cache = NULL;
+
+       BKE_tracking_copy(&clip_new->tracking, &clip->tracking);
+       clip_new->tracking_context = NULL;
+
+       id_us_plus((ID *)clip_new->gpd);
+
+       BKE_color_managed_colorspace_settings_copy(&clip_new->colorspace_settings, &clip->colorspace_settings);
+
+       BKE_id_copy_ensure_local(bmain, &clip->id, &clip_new->id);
+
+       return clip_new;
+}
+
+void BKE_movieclip_make_local(Main *bmain, MovieClip *clip, const bool lib_local)
+{
+       BKE_id_make_local_generic(bmain, &clip->id, true, lib_local);
+}
+
 float BKE_movieclip_remap_scene_to_clip_frame(MovieClip *clip, float framenr)
 {
        return framenr - (float) clip->start_frame + 1.0f;
index 3b76e456ff7d353ce4ebf65f20f997212b546b4e..ceed44d9113dd6cfefb768fa12646231802ab17e 100644 (file)
@@ -44,6 +44,7 @@
 #include "DNA_scene_types.h"
 
 #include "BLI_utildefines.h"
+#include "BLI_ghash.h"
 #include "BLI_math.h"
 #include "BLI_math_base.h"
 #include "BLI_listbase.h"
@@ -55,6 +56,7 @@
 
 #include "BKE_fcurve.h"
 #include "BKE_tracking.h"
+#include "BKE_library.h"
 #include "BKE_movieclip.h"
 #include "BKE_object.h"
 #include "BKE_scene.h"
@@ -186,6 +188,133 @@ void BKE_tracking_free(MovieTracking *tracking)
        tracking_dopesheet_free(&tracking->dopesheet);
 }
 
+/* Copy the whole list of tracks. */
+static void tracking_tracks_copy(ListBase *tracks_dst, ListBase *tracks_src, GHash *tracks_mapping)
+{
+       MovieTrackingTrack *track_dst, *track_src;
+
+       BLI_listbase_clear(tracks_dst);
+       BLI_ghash_clear(tracks_mapping, NULL, NULL);
+
+       for (track_src = tracks_src->first; track_src != NULL; track_src = track_src->next) {
+               track_dst = MEM_dupallocN(track_src);
+               if (track_src->markers) {
+                       track_dst->markers = MEM_dupallocN(track_src->markers);
+               }
+               id_us_plus(&track_dst->gpd->id);
+               BLI_addtail(tracks_dst, track_dst);
+               BLI_ghash_insert(tracks_mapping, track_src, track_dst);
+       }
+}
+
+/* copy the whole list of plane tracks (need whole MovieTracking structures due to embeded pointers to tracks).
+ * WARNING: implies tracking_[dst/src] and their tracks have already been copied. */
+static void tracking_plane_tracks_copy(ListBase *plane_tracks_dst, ListBase *plane_tracks_src, GHash *tracks_mapping)
+{
+       MovieTrackingPlaneTrack *plane_track_dst, *plane_track_src;
+
+       BLI_listbase_clear(plane_tracks_dst);
+
+       for (plane_track_src = plane_tracks_src->first; plane_track_src != NULL; plane_track_src = plane_track_src->next) {
+               plane_track_dst = MEM_dupallocN(plane_tracks_src);
+               if (plane_track_src->markers) {
+                       plane_track_dst->markers = MEM_dupallocN(plane_track_src->markers);
+               }
+               plane_track_dst->point_tracks = MEM_mallocN(sizeof(*plane_track_dst->point_tracks) * plane_track_dst->point_tracksnr, __func__);
+               for (int i = 0; i < plane_track_dst->point_tracksnr; i++) {
+                       plane_track_dst->point_tracks[i] = BLI_ghash_lookup(tracks_mapping, plane_track_src->point_tracks[i]);
+               }
+               id_us_plus(&plane_track_dst->image->id);
+               BLI_addtail(plane_tracks_dst, plane_track_dst);
+       }
+}
+
+/* Copy reconstruction structure. */
+static void tracking_reconstruction_copy(
+        MovieTrackingReconstruction *reconstruction_dst, MovieTrackingReconstruction *reconstruction_src)
+{
+       *reconstruction_dst = *reconstruction_src;
+       if (reconstruction_src->cameras) {
+               reconstruction_dst->cameras = MEM_dupallocN(reconstruction_src->cameras);
+       }
+}
+
+/* Copy stabilization structure. */
+static void tracking_stabilization_copy(
+        MovieTrackingStabilization *stabilization_dst, MovieTrackingStabilization *stabilization_src,
+        GHash *tracks_mapping)
+{
+       *stabilization_dst = *stabilization_src;
+       if (stabilization_src->rot_track) {
+               stabilization_dst->rot_track = BLI_ghash_lookup(tracks_mapping, stabilization_src->rot_track);
+       }
+}
+
+/* Copy tracking object. */
+static void tracking_object_copy(
+        MovieTrackingObject *object_dst, MovieTrackingObject *object_src, GHash *tracks_mapping)
+{
+       *object_dst = *object_src;
+       tracking_tracks_copy(&object_dst->tracks, &object_src->tracks, tracks_mapping);
+       tracking_plane_tracks_copy(&object_dst->plane_tracks, &object_src->plane_tracks, tracks_mapping);
+       tracking_reconstruction_copy(&object_dst->reconstruction, &object_src->reconstruction);
+}
+
+/* Copy list of tracking objects. */
+static void tracking_objects_copy(ListBase *objects_dst, ListBase *objects_src, GHash *tracks_mapping)
+{
+       MovieTrackingObject *object_dst, *object_src;
+
+       BLI_listbase_clear(objects_dst);
+
+       for (object_src = objects_src->first; object_src != NULL; object_src = object_src->next) {
+               object_dst = MEM_mallocN(sizeof(*object_dst), __func__);
+               tracking_object_copy(object_dst, object_src, tracks_mapping);
+               BLI_addtail(objects_dst, object_dst);
+       }
+}
+
+/* Copy tracking structure content. */
+void BKE_tracking_copy(MovieTracking *tracking_dst, MovieTracking *tracking_src)
+{
+       GHash *tracks_mapping = BLI_ghash_ptr_new(__func__);
+
+       *tracking_dst = *tracking_src;
+
+       tracking_tracks_copy(&tracking_dst->tracks, &tracking_src->tracks, tracks_mapping);
+       tracking_plane_tracks_copy(&tracking_dst->plane_tracks, &tracking_src->plane_tracks, tracks_mapping);
+       tracking_reconstruction_copy(&tracking_dst->reconstruction, &tracking_src->reconstruction);
+       tracking_stabilization_copy(&tracking_dst->stabilization, &tracking_src->stabilization, tracks_mapping);
+       if (tracking_src->act_track) {
+               tracking_dst->act_track = BLI_ghash_lookup(tracks_mapping, tracking_src->act_track);
+       }
+       if (tracking_src->act_plane_track) {
+               MovieTrackingPlaneTrack *plane_track_src, *plane_track_dst;
+               for (plane_track_src = tracking_src->plane_tracks.first, plane_track_dst = tracking_dst->plane_tracks.first;
+                    !ELEM(NULL, plane_track_src, plane_track_dst);
+                    plane_track_src = plane_track_src->next, plane_track_dst = plane_track_dst->next)
+               {
+                       if (plane_track_src == tracking_src->act_plane_track) {
+                               tracking_dst->act_plane_track = plane_track_dst;
+                               break;
+                       }
+               }
+       }
+
+       /* Warning! Will override tracks_mapping. */
+       tracking_objects_copy(&tracking_dst->objects, &tracking_src->objects, tracks_mapping);
+
+       /* Those remaining are runtime data, they will be reconstructed as needed, do not bother copying them. */
+       tracking_dst->dopesheet.ok = false;
+       BLI_listbase_clear(&tracking_dst->dopesheet.channels);
+       BLI_listbase_clear(&tracking_dst->dopesheet.coverage_segments);
+
+       tracking_dst->camera.intrinsics = NULL;
+       tracking_dst->stats = NULL;
+
+       BLI_ghash_free(tracks_mapping, NULL, NULL);
+}
+
 /* Initialize motion tracking settings to default values,
  * used when new movie clip datablock is creating.
  */