Skip to content

extract_layers

ExtractLayers

Bases: Extractor, ColormanagedPyblishPluginMixin, OptionalPyblishPluginMixin

Export layers within the instance layerset to a PSD file.

Layersets can be merged to reduce the number of layers in the output file.

Source code in client/ayon_photoshop/plugins/publish/extract_layers.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
class ExtractLayers(
    publish.Extractor,
    publish.ColormanagedPyblishPluginMixin,
    publish.OptionalPyblishPluginMixin
):
    """Export layers within the instance layerset to a PSD file.

    Layersets can be merged to reduce the number of layers in the output file.
    """

    label = "Extract Layers"
    order = publish.Extractor.order  # Must be after ExtractImage
    hosts = ["photoshop"]
    families = ["image"]
    optional = False
    merge_layersets = False
    extension = "psd"

    def process(self, instance):
        if (
            instance.data.get("productBaseType") != "image"
            or instance.data.get("creatorIdentifier") == "auto_image"
            or not self.is_active(instance.data)
        ):
            return
        ps_stub = photoshop.stub()
        native_colorspace = ps_stub.get_color_profile_name()
        self.log.info(f"Document colorspace profile: {native_colorspace}")
        host_name = instance.context.data["hostName"]
        project_settings = instance.context.data["project_settings"]
        host_imageio_settings = project_settings["photoshop"]["imageio"]
        ayon_colorspace = get_remapped_colorspace_from_native(
            native_colorspace,
            host_name,
            host_imageio_settings,
        )
        self.log.debug(f"ayon_colorspace: {ayon_colorspace}")
        # Duplicate the document to the staging directory
        filename = ps_stub.get_active_document_name()
        basename = os.path.splitext(filename)[0]
        filename = f"{basename}.{self.extension}"
        filepath = Path(
            get_instance_staging_dir(instance),
            filename
        )
        self.log.info(f"Duplicating document to staging directory: {filepath}")
        with ps_stub.duplicate_document(filepath):
            # Delete all layers except the instance layerset
            layer = instance.data.get("layer")
            if not hasattr(layer, "id"):
                self.log.warning("Instance layer does not have an id, skipping.")
                return
            ps_stub.delete_all_layers(
                exclude_layers=[layer],
                exclude_recursive=True
            )

            # Merge all layersets within the instance set
            if self.merge_layersets:
                self.log.info("Merging all layersets within instance set...")
                ps_stub.merge_all_layersets(
                    parent_set=layer.id
                )

            # Dissolve instance layerset
            self.log.info("Dissolving instance layerset...")
            ps_stub.dissolve_layerset(layer.id)

        instance.data["stagingDir"] = filepath.parent
        representations = instance.data.setdefault("representations", [])
        representation = {
            "name": self.extension,
            "ext": self.extension,
            "files": filepath.name,
            "stagingDir": filepath.parent,
        }
        # inject colorspace data
        self.set_representation_colorspace(
            representation, instance.context,
            colorspace=ayon_colorspace
        )
        self.log.debug(f"Representation: {representation}")
        representations.append(representation)

    @classmethod
    def get_attr_defs_for_instance(
        cls, create_context: "CreateContext", instance: "CreatedInstance"
    ):
        if (
            instance.product_base_type != "image"
            or instance.creator_identifier == "auto_image"
        ):
            return []
        return cls.get_attribute_defs()