From c0584eded70c2d63ab4be82484089e263bf15988 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 11 Feb 2020 18:49:40 +0100 Subject: [PATCH 1/6] integrate new will remove old representations if republishing version and set new repres IDs to those previous --- pype/plugins/global/publish/integrate_new.py | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index 7d95534897..4499445e6e 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -207,6 +207,7 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): 'parent': subset["_id"], 'name': next_version }) + existing_repres = None if existing_version is None: version_id = io.insert_one(version).inserted_id else: @@ -217,6 +218,11 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): }, {'$set': version} ) version_id = existing_version['_id'] + existing_repres = {repre["name"]: repre for repre in io.find({ + "type": "representation", + "parent": version_id + })} + instance.data['version'] = version['name'] # Write to disk @@ -249,6 +255,7 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): if 'transfers' not in instance.data: instance.data['transfers'] = [] + new_repre_names = [] for idx, repre in enumerate(instance.data["representations"]): # Collection @@ -419,8 +426,16 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): continue repre_context[key] = template_data[key] + repre_name = repre['name'] + new_repre_names.append(repre_name) + # Use previous + if existing_repres and repre_name in existing_repres: + repre_id = existing_repres[repre_name]["_id"] + else: + repre_id = io.ObjectId() + representation = { - "_id": io.ObjectId(), + "_id": repre_id, "schema": "pype:representation-2.0", "type": "representation", "parent": version_id, @@ -446,6 +461,13 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): representations.append(representation) self.log.debug("__ representations: {}".format(representations)) + # Remove old representations if there are any (before insertion of new) + if existing_repres: + repre_ids_to_remove = [] + for repre in existing_repres.values(): + repre_ids_to_remove.append(repre["_id"]) + io.delete_many({"_id": {"$in": repre_ids_to_remove}}) + self.log.debug("__ representations: {}".format(representations)) for rep in instance.data["representations"]: self.log.debug("__ represNAME: {}".format(rep['name'])) From 85ba7f17f494a4324c0be113fff563a9edf9d597 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 12 Feb 2020 11:17:08 +0100 Subject: [PATCH 2/6] representations are not deleted but their type changes to archived_representations and their id is changed --- pype/plugins/global/publish/integrate_new.py | 39 +++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index 4499445e6e..c8e6a0188e 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -4,6 +4,8 @@ import logging import sys import clique import errno + +from pymongo import DeleteOne, InsertOne import pyblish.api from avalon import api, io from avalon.vendor import filelink @@ -207,21 +209,48 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): 'parent': subset["_id"], 'name': next_version }) - existing_repres = None + if existing_version is None: version_id = io.insert_one(version).inserted_id else: + # Update version data io.update_many({ 'type': 'version', 'parent': subset["_id"], 'name': next_version - }, {'$set': version} - ) + }, { + '$set': version + }) version_id = existing_version['_id'] - existing_repres = {repre["name"]: repre for repre in io.find({ + + # Find representations of existing version and archive them + current_repres = list(io.find({ "type": "representation", "parent": version_id - })} + })) + bulk_writes = [] + for repre in current_repres: + # Representation must change type, + # `_id` must be stored to other key and replaced with new + # - that is because new representations should have same ID + repre_id = repre["_id"] + bulk_writes.append(DeleteOne({"_id": repre_id})) + + repre["orig_id"] = repre_id + repre["_id"] = io.ObjectId() + repre["type"] = "archived_representation" + bulk_writes.append(InsertOne(repre)) + + # bulk updates + if bulk_writes: + io._database[io.Session["AVALON_PROJECT"]].bulk_write( + bulk_writes + ) + + existing_repres = list(io.find({ + "parent": version_id, + "type": "archived_representation" + })) instance.data['version'] = version['name'] From 7f49ed9fb3e353ce0be37c41d70a3da45d368ebb Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 12 Feb 2020 11:17:27 +0100 Subject: [PATCH 3/6] check of existing representations was updated --- pype/plugins/global/publish/integrate_new.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index c8e6a0188e..b5b6b10aa2 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -411,7 +411,6 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): if not dst_start_frame: dst_start_frame = dst_padding - dst = "{0}{1}{2}".format( dst_head, dst_start_frame, @@ -457,10 +456,17 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): repre_name = repre['name'] new_repre_names.append(repre_name) - # Use previous - if existing_repres and repre_name in existing_repres: - repre_id = existing_repres[repre_name]["_id"] - else: + + # Use previous representation's id if there are any + repre_id = None + for _repre in existing_repres: + # NOTE should we check lowered names? + if repre_name == _repre["name"]: + repre_id = _repre["orig_id"] + break + + # Create new id if existing representations does not match + if repre_id is None: repre_id = io.ObjectId() representation = { From d9ffc411a4d65559e436e7d220b8023c8eba5dc6 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Mon, 17 Feb 2020 16:48:36 +0100 Subject: [PATCH 4/6] integrate new's version override is ready to handle "append" method per instance --- pype/plugins/global/publish/integrate_new.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index b5b6b10aa2..2e2094dfc8 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -204,6 +204,9 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): data=version_data) self.log.debug("Creating version ...") + + new_repre_names_low = [_repre["name"].lower() for _repre in repres] + existing_version = io.find_one({ 'type': 'version', 'parent': subset["_id"], @@ -213,6 +216,10 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): if existing_version is None: version_id = io.insert_one(version).inserted_id else: + # Check if instance have set `append` mode which cause that + # only replicated representations are set to archive + append_repres = instance.data.get("append", False) + # Update version data io.update_many({ 'type': 'version', @@ -230,6 +237,10 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): })) bulk_writes = [] for repre in current_repres: + if append_repres: + # archive only duplicated representations + if repre["name"].lower() not in new_repre_names_low: + continue # Representation must change type, # `_id` must be stored to other key and replaced with new # - that is because new representations should have same ID @@ -284,7 +295,6 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): if 'transfers' not in instance.data: instance.data['transfers'] = [] - new_repre_names = [] for idx, repre in enumerate(instance.data["representations"]): # Collection @@ -454,9 +464,6 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): continue repre_context[key] = template_data[key] - repre_name = repre['name'] - new_repre_names.append(repre_name) - # Use previous representation's id if there are any repre_id = None for _repre in existing_repres: From 4256eccc2b797d1e8af4d800e11a14c78222c669 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Mon, 17 Feb 2020 17:05:08 +0100 Subject: [PATCH 5/6] fixed few merge issues --- pype/plugins/global/publish/integrate_new.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index 9de29cd387..8d41aa7907 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -196,6 +196,7 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): append_repres = instance.data.get("append", False) # Update version data + # TODO query by _id and io.update_many({ 'type': 'version', 'parent': subset["_id"], @@ -322,7 +323,9 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): index_frame_start = None if repre.get("frameStart"): - frame_start_padding = anatomy.templates["render"]["padding"] + frame_start_padding = ( + anatomy.templates["render"]["padding"] + ) index_frame_start = int(repre.get("frameStart")) # exception for slate workflow @@ -407,9 +410,10 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): # Use previous representation's id if there are any repre_id = None + repre_name_low = repre["name"].lower() for _repre in existing_repres: # NOTE should we check lowered names? - if repre_name == _repre["name"]: + if repre_name_low == _repre["name"]: repre_id = _repre["orig_id"] break @@ -435,7 +439,9 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): representation["context"]["output"] = repre['outputName'] if sequence_repre and repre.get("frameStart"): - representation['context']['frame'] = src_padding_exp % int(repre.get("frameStart")) + representation['context']['frame'] = ( + src_padding_exp % int(repre.get("frameStart")) + ) self.log.debug("__ representation: {}".format(representation)) destination_list.append(dst) From 7296e86475077d014085be296e7747b7e01fbb06 Mon Sep 17 00:00:00 2001 From: Milan Kolar Date: Tue, 18 Feb 2020 14:49:11 +0100 Subject: [PATCH 6/6] fix forgotten .value() call --- pype/plugins/global/publish/integrate_new.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index 8d41aa7907..a2343ce8a9 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -453,7 +453,7 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): # Remove old representations if there are any (before insertion of new) if existing_repres: repre_ids_to_remove = [] - for repre in existing_repres.values(): + for repre in existing_repres: repre_ids_to_remove.append(repre["_id"]) io.delete_many({"_id": {"$in": repre_ids_to_remove}})