Skip to content

extract_layers

ExtractLayers

Bases: Extractor

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
 8
 9
10
11
12
13
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
class ExtractLayers(publish.Extractor):
    """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"]
    merge_layersets = False

    def process(self, instance):
        ps_stub = photoshop.stub()

        # Duplicate the document to the staging directory
        filepath = Path(
            get_instance_staging_dir(instance),
            ps_stub.get_active_document_name()
        )
        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")
            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", [])
        representations.append(
            {
                "name": "psd",
                "ext": "psd",
                "files": filepath.name,
                "stagingDir": filepath.parent,
            }
        )