Removed com32 objects

Refactore - changed names to highlight 'stub' approach
Small fixes
This commit is contained in:
Petr Kalis 2020-08-24 17:44:16 +02:00
parent d08b04537d
commit f31fb3e034
11 changed files with 70 additions and 92 deletions

View file

@ -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: <string> 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: <namedTuple Layer("id":XX, "name":"YYY")>
"""
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: <boolean>
"""
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):
"""

View file

@ -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)

View file

@ -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"])}
)

View file

@ -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("\\", "/")

View file

@ -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:

View file

@ -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():

View file

@ -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")

View file

@ -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()

View file

@ -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))

View file

@ -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):

View file

@ -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