From f31fb3e03426830a1e1b3d10502230e453fa9e6f Mon Sep 17 00:00:00 2001 From: Petr Kalis Date: Mon, 24 Aug 2020 17:44:16 +0200 Subject: [PATCH] Removed com32 objects Refactore - changed names to highlight 'stub' approach Small fixes --- .../photoshop_server_stub.py} | 35 +++++++++++++++---- pype/plugins/photoshop/create/create_image.py | 18 +++++----- pype/plugins/photoshop/load/load_image.py | 12 +++---- .../photoshop/publish/collect_current_file.py | 7 ++-- .../photoshop/publish/collect_instances.py | 12 +++---- .../photoshop/publish/extract_image.py | 22 ++++-------- .../photoshop/publish/extract_review.py | 22 ++++-------- .../photoshop/publish/extract_save_scene.py | 7 +--- .../photoshop/publish/increment_workfile.py | 7 ++-- .../publish/validate_instance_asset.py | 10 ++---- .../photoshop/publish/validate_naming.py | 10 ++---- 11 files changed, 70 insertions(+), 92 deletions(-) rename pype/modules/websocket_server/{clients/photoshop_client.py => stubs/photoshop_server_stub.py} (89%) diff --git a/pype/modules/websocket_server/clients/photoshop_client.py b/pype/modules/websocket_server/stubs/photoshop_server_stub.py similarity index 89% rename from pype/modules/websocket_server/clients/photoshop_client.py rename to pype/modules/websocket_server/stubs/photoshop_server_stub.py index 4f0bca99cc..f798a09b92 100644 --- a/pype/modules/websocket_server/clients/photoshop_client.py +++ b/pype/modules/websocket_server/stubs/photoshop_server_stub.py @@ -7,17 +7,28 @@ import json from collections import namedtuple -class PhotoshopClientStub(): +class PhotoshopServerStub(): """ Stub for calling function on client (Photoshop js) side. Expects that client is already connected (started when avalon menu is opened). + 'self.websocketserver.call' is used as async wrapper """ def __init__(self): self.websocketserver = WebSocketServer.get_instance() self.client = self.websocketserver.get_client() + def open(self, path): + """ + Open file located at 'path' (local). + :param path: file path locally + :return: None + """ + self.websocketserver.call(self.client.call + ('Photoshop.open', path=path) + ) + def read(self, layer, layers_meta=None): """ Parses layer metadata from Headline field of active document @@ -107,9 +118,9 @@ class PhotoshopClientStub(): Create new group (eg. LayerSet) :return: """ - ret = self.websocketserver.call(self.client.call - ('Photoshop.create_group', - name=name)) + ret = self.websocketserver.call(self.client.call + ('Photoshop.create_group', + name=name)) # create group on PS is asynchronous, returns only id layer = {"id": ret, "name": name, "group": True} return namedtuple('Layer', layer.keys())(*layer.values()) @@ -168,6 +179,14 @@ class PhotoshopClientStub(): return res + def is_saved(self): + """ + Returns true if no changes in active document + :return: + """ + return self.websocketserver.call(self.client.call + ('Photoshop.is_saved')) + def save(self): """ Saves active document @@ -223,9 +242,11 @@ class PhotoshopClientStub(): Args: path (str): File path to import. """ - self.websocketserver.call(self.client.call - ('Photoshop.import_smart_object', - path=path)) + res = self.websocketserver.call(self.client.call + ('Photoshop.import_smart_object', + path=path)) + + return self._to_records(res).pop() def replace_smart_object(self, layer, path): """ diff --git a/pype/plugins/photoshop/create/create_image.py b/pype/plugins/photoshop/create/create_image.py index 0a019fe2f8..c1a7d92a2c 100644 --- a/pype/plugins/photoshop/create/create_image.py +++ b/pype/plugins/photoshop/create/create_image.py @@ -1,8 +1,6 @@ from avalon import api from avalon.vendor import Qt -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) +from avalon import photoshop class CreateImage(api.Creator): @@ -17,10 +15,10 @@ class CreateImage(api.Creator): layers = [] create_group = False - photoshopClient = PhotoshopClientStub() + stub = photoshop.stub() if (self.options or {}).get("useSelection"): multiple_instances = False - selection = photoshopClient.get_selected_layers() + selection = stub.get_selected_layers() self.log.info("selection {}".format(selection)) if len(selection) > 1: # Ask user whether to create one image or image per selected @@ -49,7 +47,7 @@ class CreateImage(api.Creator): else: layers.append(item) else: - group = photoshopClient.group_selected_layers(self.name) + group = stub.group_selected_layers(self.name) groups.append(group) elif len(selection) == 1: @@ -66,14 +64,14 @@ class CreateImage(api.Creator): create_group = True if create_group: - group = photoshopClient.create_group(self.name) + group = stub.create_group(self.name) groups.append(group) for layer in layers: - photoshopClient.select_layers([layer]) - group = photoshopClient.group_selected_layers(layer.name) + stub.select_layers([layer]) + group = stub.group_selected_layers(layer.name) groups.append(group) for group in groups: self.data.update({"subset": "image" + group.name}) - photoshopClient.imprint(group, self.data) + stub.imprint(group, self.data) diff --git a/pype/plugins/photoshop/load/load_image.py b/pype/plugins/photoshop/load/load_image.py index 1856155b2a..75c02bb327 100644 --- a/pype/plugins/photoshop/load/load_image.py +++ b/pype/plugins/photoshop/load/load_image.py @@ -1,10 +1,6 @@ from avalon import api, photoshop -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) - -photoshopClient = PhotoshopClientStub() +stub = photoshop.stub() class ImageLoader(api.Loader): @@ -18,7 +14,7 @@ class ImageLoader(api.Loader): def load(self, context, name=None, namespace=None, data=None): with photoshop.maintained_selection(): - layer = photoshopClient.import_smart_object(self.fname) + layer = stub.import_smart_object(self.fname) self[:] = [layer] @@ -34,11 +30,11 @@ class ImageLoader(api.Loader): layer = container.pop("layer") with photoshop.maintained_selection(): - photoshopClient.replace_smart_object( + stub.replace_smart_object( layer, api.get_representation_path(representation) ) - photoshopClient.imprint( + stub.imprint( layer, {"representation": str(representation["_id"])} ) diff --git a/pype/plugins/photoshop/publish/collect_current_file.py b/pype/plugins/photoshop/publish/collect_current_file.py index 7877caa137..3cc3e3f636 100644 --- a/pype/plugins/photoshop/publish/collect_current_file.py +++ b/pype/plugins/photoshop/publish/collect_current_file.py @@ -2,9 +2,7 @@ import os import pyblish.api -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) +from avalon import photoshop class CollectCurrentFile(pyblish.api.ContextPlugin): @@ -15,7 +13,6 @@ class CollectCurrentFile(pyblish.api.ContextPlugin): hosts = ["photoshop"] def process(self, context): - photoshop_client = PhotoshopClientStub() context.data["currentFile"] = os.path.normpath( - photoshop_client.get_active_document_full_name() + photoshop.stub().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 7e433bc92f..81d1c80bf6 100644 --- a/pype/plugins/photoshop/publish/collect_instances.py +++ b/pype/plugins/photoshop/publish/collect_instances.py @@ -2,9 +2,7 @@ import pythoncom import pyblish.api -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) +from avalon import photoshop class CollectInstances(pyblish.api.ContextPlugin): @@ -29,11 +27,11 @@ class CollectInstances(pyblish.api.ContextPlugin): # can be. pythoncom.CoInitialize() - photoshop_client = PhotoshopClientStub() - layers = photoshop_client.get_layers() - layers_meta = photoshop_client.get_layers_metadata() + stub = photoshop.stub() + layers = stub.get_layers() + layers_meta = stub.get_layers_metadata() for layer in layers: - layer_data = photoshop_client.read(layer, layers_meta) + layer_data = stub.read(layer, layers_meta) # Skip layers without metadata. if layer_data is None: diff --git a/pype/plugins/photoshop/publish/extract_image.py b/pype/plugins/photoshop/publish/extract_image.py index e32444c641..38920b5557 100644 --- a/pype/plugins/photoshop/publish/extract_image.py +++ b/pype/plugins/photoshop/publish/extract_image.py @@ -3,10 +3,6 @@ import os import pype.api from avalon import photoshop -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) - class ExtractImage(pype.api.Extractor): """Produce a flattened image file from instance @@ -25,23 +21,21 @@ class ExtractImage(pype.api.Extractor): self.log.info("Outputting image to {}".format(staging_dir)) # Perform extraction - photoshop_client = PhotoshopClientStub() + stub = photoshop.stub() files = {} with photoshop.maintained_selection(): self.log.info("Extracting %s" % str(list(instance))) with photoshop.maintained_visibility(): # Hide all other layers. - extract_ids = set([ll.id for ll in photoshop_client. + extract_ids = set([ll.id for ll in stub. get_layers_in_layers([instance[0]])]) - for layer in photoshop_client.get_layers(): + for layer in stub.get_layers(): # limit unnecessary calls to client if layer.visible and layer.id not in extract_ids: - photoshop_client.set_visible(layer.id, - False) + stub.set_visible(layer.id, False) if not layer.visible and layer.id in extract_ids: - photoshop_client.set_visible(layer.id, - True) + stub.set_visible(layer.id, True) save_options = [] if "png" in self.formats: @@ -50,16 +44,14 @@ class ExtractImage(pype.api.Extractor): save_options.append('jpg') file_basename = os.path.splitext( - photoshop_client.get_active_document_name() + stub.get_active_document_name() )[0] for extension in save_options: _filename = "{}.{}".format(file_basename, extension) files[extension] = _filename full_filename = os.path.join(staging_dir, _filename) - photoshop_client.saveAs(full_filename, - extension, - True) + stub.saveAs(full_filename, extension, True) representations = [] for extension, filename in files.items(): diff --git a/pype/plugins/photoshop/publish/extract_review.py b/pype/plugins/photoshop/publish/extract_review.py index 806b59341b..6fb50bba9f 100644 --- a/pype/plugins/photoshop/publish/extract_review.py +++ b/pype/plugins/photoshop/publish/extract_review.py @@ -4,10 +4,6 @@ import pype.api import pype.lib from avalon import photoshop -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) - class ExtractReview(pype.api.Extractor): """Produce a flattened image file from all instances.""" @@ -20,7 +16,7 @@ class ExtractReview(pype.api.Extractor): staging_dir = self.staging_dir(instance) self.log.info("Outputting image to {}".format(staging_dir)) - photoshop_client = PhotoshopClientStub() + stub = photoshop.stub() layers = [] for image_instance in instance.context: @@ -30,26 +26,22 @@ class ExtractReview(pype.api.Extractor): # Perform extraction output_image = "{}.jpg".format( - os.path.splitext(photoshop_client.get_active_document_name())[0] + os.path.splitext(stub.get_active_document_name())[0] ) output_image_path = os.path.join(staging_dir, output_image) with photoshop.maintained_visibility(): # Hide all other layers. - extract_ids = set([ll.id for ll in photoshop_client. + extract_ids = set([ll.id for ll in stub. get_layers_in_layers(layers)]) self.log.info("extract_ids {}".format(extract_ids)) - for layer in photoshop_client.get_layers(): + for layer in stub.get_layers(): # limit unnecessary calls to client if layer.visible and layer.id not in extract_ids: - photoshop_client.set_visible(layer.id, - False) + stub.set_visible(layer.id, False) if not layer.visible and layer.id in extract_ids: - photoshop_client.set_visible(layer.id, - True) + stub.set_visible(layer.id, True) - photoshop_client.saveAs(output_image_path, - 'jpg', - True) + stub.saveAs(output_image_path, 'jpg', True) ffmpeg_path = pype.lib.get_ffmpeg_tool_path("ffmpeg") diff --git a/pype/plugins/photoshop/publish/extract_save_scene.py b/pype/plugins/photoshop/publish/extract_save_scene.py index c56e5418c9..63a4b7b7ea 100644 --- a/pype/plugins/photoshop/publish/extract_save_scene.py +++ b/pype/plugins/photoshop/publish/extract_save_scene.py @@ -1,10 +1,6 @@ import pype.api from avalon import photoshop -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) - class ExtractSaveScene(pype.api.Extractor): """Save scene before extraction.""" @@ -15,5 +11,4 @@ class ExtractSaveScene(pype.api.Extractor): families = ["workfile"] def process(self, instance): - photoshop_client = PhotoshopClientStub() - photoshop_client.save() + photoshop.stub().save() diff --git a/pype/plugins/photoshop/publish/increment_workfile.py b/pype/plugins/photoshop/publish/increment_workfile.py index af8ba0b6ae..eca2583595 100644 --- a/pype/plugins/photoshop/publish/increment_workfile.py +++ b/pype/plugins/photoshop/publish/increment_workfile.py @@ -2,9 +2,7 @@ import pyblish.api from pype.action import get_errored_plugins_from_data from pype.lib import version_up -from pype.modules.websocket_server.clients.photoshop_client import ( - PhotoshopClientStub -) +from avalon import photoshop class IncrementWorkfile(pyblish.api.InstancePlugin): @@ -27,7 +25,6 @@ class IncrementWorkfile(pyblish.api.InstancePlugin): ) scene_path = version_up(instance.context.data["currentFile"]) - photoshop_client = PhotoshopClientStub() - photoshop_client.saveAs(scene_path, 'psd', True) + photoshop.stub().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 aa8d2661ff..f05d9601dd 100644 --- a/pype/plugins/photoshop/publish/validate_instance_asset.py +++ b/pype/plugins/photoshop/publish/validate_instance_asset.py @@ -4,10 +4,6 @@ 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.""" @@ -27,12 +23,12 @@ 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() + stub = photoshop.stub() for instance in instances: - data = photoshop_client.read(instance[0]) + data = stub.read(instance[0]) data["asset"] = os.environ["AVALON_ASSET"] - photoshop_client.imprint(instance[0], data) + stub.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 c612270802..2483adcb5e 100644 --- a/pype/plugins/photoshop/publish/validate_naming.py +++ b/pype/plugins/photoshop/publish/validate_naming.py @@ -2,10 +2,6 @@ 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.""" @@ -25,14 +21,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() + stub = photoshop.stub() for instance in instances: self.log.info("validate_naming instance {}".format(instance)) name = instance.data["name"].replace(" ", "_") instance[0].Name = name - data = photoshop_client.read(instance[0]) + data = stub.read(instance[0]) data["subset"] = "image" + name - photoshop_client.imprint(instance[0], data) + stub.imprint(instance[0], data) return True