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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 | class CreateImage(ComfyUICreator):
"""Creator for image(s) before publishing.
On create, spawn a node that is to be associated with
this publish.
"""
identifier = "io.ayon.creators.comfyui.image"
label = "Image"
product_type = "image"
product_base_type = "image"
description = "Image generated using ComfyUI"
default_img_name = "ayon"
icon = "gears"
enabled = True
def create( # noqa: D102
self,
product_name: str,
data: dict[str, Any],
pre_create_data: dict[str, bool | str],
) -> None:
keep_metadata: bool = pre_create_data.get("keep_metadata")
prefix: str = pre_create_data.get("file_prefix")
use_unique_name: bool = pre_create_data.get("use_unique_name")
unique_name: str = pre_create_data.get("unique_name")
compress_level: float = pre_create_data.get("compress_level")
force_recook: bool = pre_create_data.get("force_recook_on_publish")
context: CreateContext = self.create_context
project_name = context.get_current_project_name()
folder_path = context.get_current_folder_path()
task_name = context.get_current_task_name()
# host_name = context.host_name
prefix = re.sub(f"[^{PRODUCT_NAME_ALLOWED_SYMBOLS}]+", "", prefix)
unique_name = re.sub(
f"[^{PRODUCT_NAME_ALLOWED_SYMBOLS}]+", "", unique_name
)
data.update(
{
"folderPath": folder_path,
"task": task_name,
"variant": self.default_variant,
"projectName": project_name,
}
)
if use_unique_name:
product_name = f"{prefix}_{unique_name}_{product_name}"
else:
product_name = f"{prefix}_{product_name}"
creator_attributes = {
"keep_metadata": keep_metadata,
"use_unique_name": use_unique_name,
"prefix": prefix,
"unique_name": unique_name,
"compression_level": int(compress_level),
"force_recook_on_publish": force_recook,
}
data.update(
{
"productName": product_name,
"creator_attributes": creator_attributes,
}
)
# Check for collisions with existing names.
instances = [
instance
for instance in list_instances()
if instance.get("productType") == self.product_type
]
if any(
instance.get("productName") == product_name
for instance in instances
):
raise CreatorError(
"Name collision. " # noqa: EM101
"please specify a different prefix / unique name"
)
new_instance = CreatedInstance(
self.product_type, product_name, data, self
)
self._add_instance_to_context(new_instance)
self.stub.create_publish_node(new_instance.data_to_store())
self.stub.update_instance(new_instance.data_to_store())
def collect_instances(self):
for instance_data in list_instances():
# Process only instances that were created by this creator
creator_id = instance_data.get("creator_identifier")
if creator_id == self.identifier:
# Create instance object from existing data
instance = CreatedInstance.from_existing(instance_data, self)
# Add instance to create context
self._add_instance_to_context(instance)
def update_instances( # noqa: D102, PLR6301
self, update_list: list[tuple[CreatedInstance, Any]]
) -> None:
updated = [
instance.data_to_store() for instance, _changes in update_list
]
self.stub.update_instance(updated)
def remove_instances(self, instances: list[CreatedInstance]):
self.stub.remove_publish_nodes([i.data_to_store() for i in instances])
self.stub.remove_instance(instances)
for instance in instances:
self._remove_instance_from_context(instance)
def get_pre_create_attr_defs(self):
# NOTE: I do not know if it's possible to
# force image meta data behavior from here.
# NOTE: I actually do know. We force the entire instance dict into
# a json, and then either apply or don't apply the image metadata
# like ComfyUI does inside the node.
return [
BoolDef(
"keep_metadata", default=True, label="Keep image metadata"
),
BoolDef(
"force_recook_on_publish",
default=False,
label="Force re-cook on publish",
),
TextDef(
"file_prefix",
multiline=False,
default=self.default_img_name,
label="Extra prefix for file",
),
BoolDef(
"use_unique_name",
default=True,
label="Use unique product name",
),
TextDef(
"unique_name",
multiline=False,
default=self.default_img_name,
label="Unique name included in saved file",
),
NumberDef(
"compress_level",
minimum=0,
maximum=9,
decimals=0,
default=4,
label="PNG Compression level",
),
]
def get_detail_description(self) -> str: # noqa: D102, PLR6301
return inspect.cleandoc(
"""Creator plugin for publishing ComfyUI images.
Accepts batched images. These will all be loaded together too.
"""
)
|