Skip to content

collect_auto_image

CollectAutoImage

Bases: ContextPlugin

Creates auto image in non artist based publishes (Webpublisher).

Source code in client/ayon_photoshop/plugins/publish/collect_auto_image.py
  7
  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
 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
108
class CollectAutoImage(pyblish.api.ContextPlugin):
    """Creates auto image in non artist based publishes (Webpublisher).
    """

    label = "Collect Auto Image"
    hosts = ["photoshop"]

    order = pyblish.api.CollectorOrder - 0.4

    targets = ["automated"]

    def process(self, context):
        proj_settings = context.data["project_settings"]
        create_settings = proj_settings["photoshop"]["create"]
        auto_creator = create_settings["AutoImageCreator"]

        if not auto_creator["enabled"]:
            self.log.debug("Auto image creator disabled, won't create new")
            return

        for instance in context:
            creator_identifier = instance.data.get("creator_identifier")
            if creator_identifier == "auto_image":
                self.log.debug("Auto image instance found, won't create new")
                return

        stub = photoshop.stub()
        stored_items = stub.get_layers_metadata()
        for item in stored_items:
            if item.get("creator_identifier") == "auto_image":
                if not item.get("active"):
                    self.log.debug("Auto_image instance disabled")
                    return

        layer_items = stub.get_layers()

        publishable_ids = [layer.id for layer in layer_items
                           if layer.visible]

        # collect stored image instances
        instance_names = []
        for layer_item in layer_items:
            layer_meta_data = stub.read(layer_item, stored_items)

            # Skip layers without metadata.
            if layer_meta_data is None:
                continue

            # Skip containers.
            if "container" in layer_meta_data["id"]:
                continue

            # active might not be in legacy meta
            if layer_meta_data.get("active", True) and layer_item.visible:
                instance_names.append(layer_meta_data["productName"])

        # NOTE: This return can be moved where is the product name
        #   added to 'instance_names'
        if instance_names:
            return

        variants = create_settings["ImageCreator"]["default_variants"]
        if not variants:
            variants = [""]

        variant = context.data.get("variant") or variants[0]

        product_type = "image"
        # TODO (antirotor): handle product_base_type properly if needed
        product_base_type = product_type

        project_name = context.data["projectName"]
        host_name = context.data["hostName"]
        folder_entity = context.data["folderEntity"]
        task_entity = context.data["taskEntity"]

        product_name = get_product_name(
            project_name=project_name,
            host_name=host_name,
            product_type=product_type,
            variant=variant,
            folder_entity=folder_entity,
            task_entity=task_entity,
            product_base_type=product_base_type,
        )

        instance = context.create_instance(product_name)
        instance.data["folderPath"] = folder_entity["path"]
        instance.data["productType"] = product_type
        instance.data["productBaseType"] = product_base_type
        instance.data["productName"] = product_name
        instance.data["ids"] = publishable_ids
        instance.data["publish"] = True
        instance.data["creator_identifier"] = "auto_image"
        instance.data["family"] = product_base_type
        instance.data["families"] = [product_base_type]

        if auto_creator["mark_for_review"]:
            instance.data["creator_attributes"] = {"mark_for_review": True}
            instance.data["families"].append("review")

        self.log.info("auto image instance: {} ".format(instance.data))