From a9f146e2fca896a8558c3c8d65d6df1a0f3af3ec Mon Sep 17 00:00:00 2001 From: Petr Kalis Date: Wed, 19 Aug 2020 12:54:42 +0200 Subject: [PATCH] Fixed fullName, implemented imprint --- .../clients/photoshop_client.py | 73 ++++++++++++++++--- .../photoshop/publish/collect_current_file.py | 6 +- .../photoshop/publish/collect_instances.py | 6 ++ .../photoshop/publish/extract_save_scene.py | 6 +- .../photoshop/publish/increment_workfile.py | 5 +- .../publish/validate_instance_asset.py | 11 ++- .../photoshop/publish/validate_naming.py | 9 ++- 7 files changed, 96 insertions(+), 20 deletions(-) diff --git a/pype/modules/websocket_server/clients/photoshop_client.py b/pype/modules/websocket_server/clients/photoshop_client.py index 0090c10db2..6d3615e1a4 100644 --- a/pype/modules/websocket_server/clients/photoshop_client.py +++ b/pype/modules/websocket_server/clients/photoshop_client.py @@ -13,15 +13,34 @@ class PhotoshopClientStub(): self.client = self.websocketserver.get_client() def read(self, layer): - layers_data = {} - res = self.websocketserver.call(self.client.call('Photoshop.read')) - try: - layers_data = json.loads(res) - except json.decoder.JSONDecodeError: - pass + layers_data = self._get_layers_metadata() return layers_data.get(str(layer.id)) + def imprint(self, layer, data): + layers_data = self._get_layers_metadata() + # json.dumps writes integer values in a dictionary to string, so + # anticipating it here. + if str(layer.id) in layers_data: + layers_data[str(layer.id)].update(data) + else: + layers_data[str(layer.id)] = data + + # Ensure only valid ids are stored. + layer_ids = [layer.id for layer in self.get_layers()] + cleaned_data = {} + + for id in layers_data: + if int(id) in layer_ids: + cleaned_data[id] = layers_data[id] + + payload = json.dumps(cleaned_data, indent=4) + + res = self.websocketserver.call(self.client.call + ('Photoshop.imprint', + payload=payload) + ) + def get_layers(self): """ Returns JSON document with all(?) layers in active document. @@ -77,18 +96,34 @@ class PhotoshopClientStub(): layers=layer_ids) ) + def get_active_document_full_name(self): + """ + Returns full name with path of active document via ws call + :return: full path with name + """ + res = self.websocketserver.call( + self.client.call('Photoshop.get_active_document_full_name')) + + return res + def get_active_document_name(self): + """ + Returns just a name of active document via ws call + :return: file name + """ res = self.websocketserver.call(self.client.call ('Photoshop.get_active_document_name')) return res - def set_visible(self, layer_id, visibility): - print("set_visible {}, {}".format(layer_id, visibility)) + def save(self): + """ + Saves active document + :return: None + """ res = self.websocketserver.call(self.client.call - ('Photoshop.set_visible', - layer_id=layer_id, - visibility=visibility)) + ('Photoshop.save')) + def saveAs(self, image_path, ext, as_copy): res = self.websocketserver.call(self.client.call @@ -97,6 +132,22 @@ class PhotoshopClientStub(): ext=ext, as_copy=as_copy)) + def set_visible(self, layer_id, visibility): + print("set_visible {}, {}".format(layer_id, visibility)) + res = self.websocketserver.call(self.client.call + ('Photoshop.set_visible', + layer_id=layer_id, + visibility=visibility)) + + def _get_layers_metadata(self): + layers_data = {} + res = self.websocketserver.call(self.client.call('Photoshop.read')) + try: + layers_data = json.loads(res) + except json.decoder.JSONDecodeError: + pass + return layers_data + def close(self): self.client.close() diff --git a/pype/plugins/photoshop/publish/collect_current_file.py b/pype/plugins/photoshop/publish/collect_current_file.py index 4308588559..bb81718bcc 100644 --- a/pype/plugins/photoshop/publish/collect_current_file.py +++ b/pype/plugins/photoshop/publish/collect_current_file.py @@ -3,6 +3,9 @@ import os import pyblish.api from avalon import photoshop +from pype.modules.websocket_server.clients.photoshop_client import \ + PhotoshopClientStub + class CollectCurrentFile(pyblish.api.ContextPlugin): """Inject the current working file into context""" @@ -12,6 +15,7 @@ class CollectCurrentFile(pyblish.api.ContextPlugin): hosts = ["photoshop"] def process(self, context): + photoshop_client = PhotoshopClientStub() context.data["currentFile"] = os.path.normpath( - photoshop.app().ActiveDocument.FullName + photoshop_client.get_active_document_full_name() ).replace("\\", "/") diff --git a/pype/plugins/photoshop/publish/collect_instances.py b/pype/plugins/photoshop/publish/collect_instances.py index cc7341f384..d94adde00b 100644 --- a/pype/plugins/photoshop/publish/collect_instances.py +++ b/pype/plugins/photoshop/publish/collect_instances.py @@ -33,8 +33,14 @@ class CollectInstances(pyblish.api.ContextPlugin): # for timing photoshop_client = PhotoshopClientStub() layers = photoshop_client.get_layers() + for layer in layers: layer_data = photoshop_client.read(layer) + self.log.info("layer_data {}".format(layer_data)) + + photoshop_client.imprint(layer, layer_data) + new_layer_data = photoshop_client.read(layer) + assert layer_data == new_layer_data # Skip layers without metadata. if layer_data is None: diff --git a/pype/plugins/photoshop/publish/extract_save_scene.py b/pype/plugins/photoshop/publish/extract_save_scene.py index e2068501db..ea7bdda9af 100644 --- a/pype/plugins/photoshop/publish/extract_save_scene.py +++ b/pype/plugins/photoshop/publish/extract_save_scene.py @@ -1,6 +1,9 @@ import pype.api from avalon import photoshop +from pype.modules.websocket_server.clients.photoshop_client import \ + PhotoshopClientStub + from datetime import datetime class ExtractSaveScene(pype.api.Extractor): """Save scene before extraction.""" @@ -11,7 +14,8 @@ class ExtractSaveScene(pype.api.Extractor): families = ["workfile"] def process(self, instance): + photoshop_client = PhotoshopClientStub() start = datetime.now() - photoshop.app().ActiveDocument.Save() + photoshop_client.save() self.log.info( "ExtractSaveScene took {}".format(datetime.now() - start)) diff --git a/pype/plugins/photoshop/publish/increment_workfile.py b/pype/plugins/photoshop/publish/increment_workfile.py index ba9ab8606a..0ae7e9772f 100644 --- a/pype/plugins/photoshop/publish/increment_workfile.py +++ b/pype/plugins/photoshop/publish/increment_workfile.py @@ -3,6 +3,8 @@ from pype.action import get_errored_plugins_from_data from pype.lib import version_up from avalon import photoshop +from pype.modules.websocket_server.clients.photoshop_client import \ + PhotoshopClientStub class IncrementWorkfile(pyblish.api.InstancePlugin): """Increment the current workfile. @@ -24,6 +26,7 @@ class IncrementWorkfile(pyblish.api.InstancePlugin): ) scene_path = version_up(instance.context.data["currentFile"]) - photoshop.app().ActiveDocument.SaveAs(scene_path) + photoshop_client = PhotoshopClientStub() + photoshop_client.saveAs(scene_path, 'psd', True) self.log.info("Incremented workfile to: {}".format(scene_path)) diff --git a/pype/plugins/photoshop/publish/validate_instance_asset.py b/pype/plugins/photoshop/publish/validate_instance_asset.py index ab1d02269f..6a0a408878 100644 --- a/pype/plugins/photoshop/publish/validate_instance_asset.py +++ b/pype/plugins/photoshop/publish/validate_instance_asset.py @@ -4,6 +4,8 @@ import pyblish.api import pype.api from avalon import photoshop +from pype.modules.websocket_server.clients.photoshop_client import \ + PhotoshopClientStub class ValidateInstanceAssetRepair(pyblish.api.Action): """Repair the instance asset.""" @@ -23,11 +25,14 @@ class ValidateInstanceAssetRepair(pyblish.api.Action): # Apply pyblish.logic to get the instances for the plug-in instances = pyblish.api.instances_by_plugin(failed, plugin) - + photoshop_client = PhotoshopClientStub() for instance in instances: - data = photoshop.read(instance[0]) + self.log.info("validate_instance_asset instance[0] {}".format(instance[0])) + self.log.info("validate_instance_asset instance {}".format(instance)) + data = photoshop_client.read(instance[0]) + data["asset"] = os.environ["AVALON_ASSET"] - photoshop.imprint(instance[0], data) + photoshop_client.imprint(instance[0], data) class ValidateInstanceAsset(pyblish.api.InstancePlugin): diff --git a/pype/plugins/photoshop/publish/validate_naming.py b/pype/plugins/photoshop/publish/validate_naming.py index 51e00da352..7734a0e5a0 100644 --- a/pype/plugins/photoshop/publish/validate_naming.py +++ b/pype/plugins/photoshop/publish/validate_naming.py @@ -2,6 +2,8 @@ import pyblish.api import pype.api from avalon import photoshop +from pype.modules.websocket_server.clients.photoshop_client import \ + PhotoshopClientStub class ValidateNamingRepair(pyblish.api.Action): """Repair the instance asset.""" @@ -21,13 +23,14 @@ class ValidateNamingRepair(pyblish.api.Action): # Apply pyblish.logic to get the instances for the plug-in instances = pyblish.api.instances_by_plugin(failed, plugin) - + photoshop_client = PhotoshopClientStub() for instance in instances: + self.log.info("validate_naming instance {}".format(instance)) name = instance.data["name"].replace(" ", "_") instance[0].Name = name - data = photoshop.read(instance[0]) + data = photoshop_client.read(instance[0]) data["subset"] = "image" + name - photoshop.imprint(instance[0], data) + photoshop_client.imprint(instance[0], data) return True