mirror of
https://github.com/ynput/ayon-core.git
synced 2026-01-01 08:24:53 +01:00
modified extract bg main groups to have similir output to extract bg for compositing
This commit is contained in:
parent
69bae8f2ae
commit
f37da8d931
2 changed files with 122 additions and 72 deletions
|
|
@ -46,7 +46,7 @@ class ExtractBGForComp(pype.api.Extractor):
|
||||||
instance.data["transfers"] = []
|
instance.data["transfers"] = []
|
||||||
|
|
||||||
for repre in tuple(repres):
|
for repre in tuple(repres):
|
||||||
# Skip all non files without .psd extension
|
# Skip all files without .psd extension
|
||||||
if repre["ext"] != ".psd":
|
if repre["ext"] != ".psd":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import copy
|
import copy
|
||||||
|
import json
|
||||||
import pype.api
|
import pype.api
|
||||||
import pyblish.api
|
import pyblish.api
|
||||||
from avalon import io
|
from avalon import io
|
||||||
|
|
@ -8,7 +9,6 @@ PSDImage = None
|
||||||
|
|
||||||
|
|
||||||
class ExtractBGMainGroups(pype.api.Extractor):
|
class ExtractBGMainGroups(pype.api.Extractor):
|
||||||
|
|
||||||
label = "Extract Background Main Groups"
|
label = "Extract Background Main Groups"
|
||||||
order = pyblish.api.ExtractorOrder + 0.02
|
order = pyblish.api.ExtractorOrder + 0.02
|
||||||
families = ["imageForLayout"]
|
families = ["imageForLayout"]
|
||||||
|
|
@ -41,105 +41,155 @@ class ExtractBGMainGroups(pype.api.Extractor):
|
||||||
self.log.info("There are no representations on instance.")
|
self.log.info("There are no representations on instance.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
self.redo_global_plugins(instance)
|
||||||
|
|
||||||
|
repres = instance.data.get("representations")
|
||||||
|
if not repres:
|
||||||
|
self.log.info("There are no representations on instance.")
|
||||||
|
return
|
||||||
|
|
||||||
|
if not instance.data.get("transfers"):
|
||||||
|
instance.data["transfers"] = []
|
||||||
|
|
||||||
for repre in tuple(repres):
|
for repre in tuple(repres):
|
||||||
# Skip all files without .psd extension
|
# Skip all files without .psd extension
|
||||||
if repre["ext"] != ".psd":
|
if repre["ext"] != ".psd":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# TODO add check of list of "files" value
|
# Prepare staging dir
|
||||||
|
staging_dir = self.staging_dir(instance)
|
||||||
|
if not os.path.exists(staging_dir):
|
||||||
|
os.makedirs(staging_dir)
|
||||||
|
|
||||||
|
# Prepare publish dir for transfers
|
||||||
|
publish_dir = instance.data["publishDir"]
|
||||||
|
|
||||||
|
# Prepare json filepath where extracted metadata are stored
|
||||||
|
json_filename = "{}.json".format(instance.name)
|
||||||
|
json_full_path = os.path.join(staging_dir, json_filename)
|
||||||
|
|
||||||
|
self.log.debug(f"`staging_dir` is \"{staging_dir}\"")
|
||||||
|
|
||||||
|
# Prepare new repre data
|
||||||
|
new_repre = {
|
||||||
|
"name": "json",
|
||||||
|
"ext": "json",
|
||||||
|
"files": json_filename,
|
||||||
|
"stagingDir": staging_dir
|
||||||
|
}
|
||||||
|
|
||||||
|
# TODO add check of list
|
||||||
psd_filename = repre["files"]
|
psd_filename = repre["files"]
|
||||||
psd_folder_path = repre["stagingDir"]
|
psd_folder_path = repre["stagingDir"]
|
||||||
psd_filepath = os.path.join(psd_folder_path, psd_filename)
|
psd_filepath = os.path.join(psd_folder_path, psd_filename)
|
||||||
self.log.debug(f"psd_filepath: \"{psd_filepath}\"")
|
self.log.debug(f"psd_filepath: \"{psd_filepath}\"")
|
||||||
psd_object = PSDImage.open(psd_filepath)
|
psd_object = PSDImage.open(psd_filepath)
|
||||||
|
|
||||||
self.create_new_instances(instance, psd_object)
|
json_data, transfers = self.export_compositing_images(
|
||||||
|
psd_object, staging_dir, publish_dir
|
||||||
|
)
|
||||||
|
self.log.info("Json file path: {}".format(json_full_path))
|
||||||
|
with open(json_full_path, "w") as json_filestream:
|
||||||
|
json.dump(json_data, json_filestream, indent=4)
|
||||||
|
|
||||||
# Remove the instance from context
|
instance.data["transfers"].extend(transfers)
|
||||||
instance.context.remove(instance)
|
instance.data["representations"].remove(repre)
|
||||||
|
instance.data["representations"].append(new_repre)
|
||||||
|
|
||||||
def create_new_instances(self, instance, psd_object):
|
def export_compositing_images(self, psd_object, output_dir, publish_dir):
|
||||||
asset_doc = instance.data["assetEntity"]
|
json_data = {
|
||||||
for layer in psd_object:
|
"__schema_version__": 1,
|
||||||
|
"children": []
|
||||||
|
}
|
||||||
|
transfers = []
|
||||||
|
for layer_idx, layer in enumerate(psd_object):
|
||||||
|
layer_name = layer.name.replace(" ", "_")
|
||||||
if (
|
if (
|
||||||
not layer.is_visible()
|
not layer.is_visible()
|
||||||
or layer.name.lower() not in self.allowed_group_names
|
or layer_name.lower() not in self.allowed_group_names
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
has_size = layer.width > 0 and layer.height > 0
|
has_size = layer.width > 0 and layer.height > 0
|
||||||
if not has_size:
|
if not has_size:
|
||||||
self.log.debug((
|
self.log.debug((
|
||||||
"Skipping layer \"{}\" because does "
|
"Skipping layer \"{}\" because does not have any content."
|
||||||
"not have any content."
|
|
||||||
).format(layer.name))
|
).format(layer.name))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
layer_name = layer.name.replace(" ", "_")
|
filename = "{}.png".format(layer_name)
|
||||||
instance_name = subset_name = f"image{layer_name}"
|
layer_data = {
|
||||||
self.log.info(
|
"index": layer_idx,
|
||||||
f"Creating new instance with name \"{instance_name}\""
|
"name": layer.name,
|
||||||
)
|
"filename": filename
|
||||||
new_instance = instance.context.create_instance(instance_name)
|
}
|
||||||
for key, value in instance.data.items():
|
|
||||||
if key not in self.ignored_instance_data_keys:
|
|
||||||
new_instance.data[key] = copy.deepcopy(value)
|
|
||||||
|
|
||||||
new_instance.data["label"] = " ".join(
|
output_filepath = os.path.join(output_dir, filename)
|
||||||
(new_instance.data["asset"], instance_name)
|
dst_filepath = os.path.join(publish_dir, filename)
|
||||||
)
|
transfers.append((output_filepath, dst_filepath))
|
||||||
|
|
||||||
# Find latest version
|
|
||||||
latest_version = self.find_last_version(subset_name, asset_doc)
|
|
||||||
version_number = 1
|
|
||||||
if latest_version is not None:
|
|
||||||
version_number += latest_version
|
|
||||||
|
|
||||||
self.log.info(
|
|
||||||
"Next version of instance \"{}\" will be {}".format(
|
|
||||||
instance_name, version_number
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Set family and subset
|
|
||||||
new_instance.data["family"] = self.new_instance_family
|
|
||||||
new_instance.data["subset"] = subset_name
|
|
||||||
new_instance.data["version"] = version_number
|
|
||||||
new_instance.data["latestVersion"] = latest_version
|
|
||||||
|
|
||||||
new_instance.data["anatomyData"].update({
|
|
||||||
"subset": subset_name,
|
|
||||||
"family": self.new_instance_family,
|
|
||||||
"version": version_number
|
|
||||||
})
|
|
||||||
|
|
||||||
# Copy `families` and check if `family` is not in current families
|
|
||||||
families = new_instance.data.get("families") or list()
|
|
||||||
if families:
|
|
||||||
families = list(set(families))
|
|
||||||
|
|
||||||
if self.new_instance_family in families:
|
|
||||||
families.remove(self.new_instance_family)
|
|
||||||
new_instance.data["families"] = families
|
|
||||||
|
|
||||||
# Prepare staging dir for new instance
|
|
||||||
staging_dir = self.staging_dir(new_instance)
|
|
||||||
|
|
||||||
output_filename = "{}.png".format(layer_name)
|
|
||||||
output_filepath = os.path.join(staging_dir, output_filename)
|
|
||||||
pil_object = layer.composite(viewport=psd_object.viewbox)
|
pil_object = layer.composite(viewport=psd_object.viewbox)
|
||||||
pil_object.save(output_filepath, "PNG")
|
pil_object.save(output_filepath, "PNG")
|
||||||
|
|
||||||
new_repre = {
|
json_data["children"].append(layer_data)
|
||||||
"name": "png",
|
|
||||||
"ext": "png",
|
return json_data, transfers
|
||||||
"files": output_filename,
|
|
||||||
"stagingDir": staging_dir
|
def redo_global_plugins(self, instance):
|
||||||
}
|
# TODO do this in collection phase
|
||||||
self.log.debug(
|
# Copy `families` and check if `family` is not in current families
|
||||||
"Creating new representation: {}".format(new_repre)
|
families = instance.data.get("families") or list()
|
||||||
)
|
if families:
|
||||||
new_instance.data["representations"] = [new_repre]
|
families = list(set(families))
|
||||||
|
|
||||||
|
if self.new_instance_family in families:
|
||||||
|
families.remove(self.new_instance_family)
|
||||||
|
|
||||||
|
self.log.debug(
|
||||||
|
"Setting new instance families {}".format(str(families))
|
||||||
|
)
|
||||||
|
instance.data["families"] = families
|
||||||
|
|
||||||
|
# Override instance data with new information
|
||||||
|
instance.data["family"] = self.new_instance_family
|
||||||
|
|
||||||
|
subset_name = instance.data["anatomyData"]["subset"]
|
||||||
|
asset_doc = instance.data["assetEntity"]
|
||||||
|
latest_version = self.find_last_version(subset_name, asset_doc)
|
||||||
|
version_number = 1
|
||||||
|
if latest_version is not None:
|
||||||
|
version_number += latest_version
|
||||||
|
|
||||||
|
instance.data["latestVersion"] = latest_version
|
||||||
|
instance.data["version"] = version_number
|
||||||
|
|
||||||
|
# Same data apply to anatomy data
|
||||||
|
instance.data["anatomyData"].update({
|
||||||
|
"family": self.new_instance_family,
|
||||||
|
"version": version_number
|
||||||
|
})
|
||||||
|
|
||||||
|
# Redo publish and resources dir
|
||||||
|
anatomy = instance.context.data["anatomy"]
|
||||||
|
template_data = copy.deepcopy(instance.data["anatomyData"])
|
||||||
|
template_data.update({
|
||||||
|
"frame": "FRAME_TEMP",
|
||||||
|
"representation": "TEMP"
|
||||||
|
})
|
||||||
|
anatomy_filled = anatomy.format(template_data)
|
||||||
|
if "folder" in anatomy.templates["publish"]:
|
||||||
|
publish_folder = anatomy_filled["publish"]["folder"]
|
||||||
|
else:
|
||||||
|
publish_folder = os.path.dirname(anatomy_filled["publish"]["path"])
|
||||||
|
|
||||||
|
publish_folder = os.path.normpath(publish_folder)
|
||||||
|
resources_folder = os.path.join(publish_folder, "resources")
|
||||||
|
|
||||||
|
instance.data["publishDir"] = publish_folder
|
||||||
|
instance.data["resourcesDir"] = resources_folder
|
||||||
|
|
||||||
|
self.log.debug("publishDir: \"{}\"".format(publish_folder))
|
||||||
|
self.log.debug("resourcesDir: \"{}\"".format(resources_folder))
|
||||||
|
|
||||||
def find_last_version(self, subset_name, asset_doc):
|
def find_last_version(self, subset_name, asset_doc):
|
||||||
subset_doc = io.find_one({
|
subset_doc = io.find_one({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue