Merge branch 'develop' of https://github.com/ynput/ayon-core into enhancement/AY-5539_define-creators-per-task

This commit is contained in:
Petr Kalis 2024-05-29 13:01:22 +02:00
commit 1ac4223e18
15 changed files with 361 additions and 54 deletions

View file

@ -3,6 +3,8 @@ import re
import json
from collections import defaultdict
import contextlib
import substance_painter
import substance_painter.project
import substance_painter.resource
import substance_painter.js
@ -640,3 +642,88 @@ def prompt_new_file_with_mesh(mesh_filepath):
return
return project_mesh
def get_filtered_export_preset(export_preset_name, channel_type_names):
"""Return export presets included with specific channels
requested by users.
Args:
export_preset_name (str): Name of export preset
channel_type_list (list): A list of channel type requested by users
Returns:
dict: export preset data
"""
target_maps = []
export_presets = get_export_presets()
export_preset_nice_name = export_presets[export_preset_name]
resource_presets = substance_painter.export.list_resource_export_presets()
preset = next(
(
preset for preset in resource_presets
if preset.resource_id.name == export_preset_nice_name
), None
)
if preset is None:
return {}
maps = preset.list_output_maps()
for channel_map in maps:
for channel_name in channel_type_names:
if not channel_map.get("fileName"):
continue
if channel_name in channel_map["fileName"]:
target_maps.append(channel_map)
# Create a new preset
return {
"exportPresets": [
{
"name": export_preset_name,
"maps": target_maps
}
],
}
@contextlib.contextmanager
def set_layer_stack_opacity(node_ids, channel_types):
"""Function to set the opacity of the layer stack during
context
Args:
node_ids (list[int]): Substance painter root layer node ids
channel_types (list[str]): Channel type names as defined as
attributes in `substance_painter.textureset.ChannelType`
"""
# Do nothing
if not node_ids or not channel_types:
yield
return
stack = substance_painter.textureset.get_active_stack()
stack_root_layers = (
substance_painter.layerstack.get_root_layer_nodes(stack)
)
node_ids = set(node_ids) # lookup
excluded_nodes = [
node for node in stack_root_layers
if node.uid() not in node_ids
]
original_opacity_values = []
for node in excluded_nodes:
for channel in channel_types:
chan = getattr(substance_painter.textureset.ChannelType, channel)
original_opacity_values.append((chan, node.get_opacity(chan)))
try:
for node in excluded_nodes:
for channel, _ in original_opacity_values:
node.set_opacity(0.0, channel)
yield
finally:
for node in excluded_nodes:
for channel, opacity in original_opacity_values:
node.set_opacity(opacity, channel)

View file

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""Creator plugin for creating textures."""
from ayon_core.pipeline import CreatedInstance, Creator, CreatorError
from ayon_core.lib import (
EnumDef,
@ -17,6 +16,7 @@ from ayon_core.hosts.substancepainter.api.pipeline import (
)
from ayon_core.hosts.substancepainter.api.lib import get_export_presets
import substance_painter
import substance_painter.project
@ -28,9 +28,16 @@ class CreateTextures(Creator):
icon = "picture-o"
default_variant = "Main"
channel_mapping = []
def apply_settings(self, project_settings):
settings = project_settings["substancepainter"].get("create", []) # noqa
if settings:
self.channel_mapping = settings["CreateTextures"].get(
"channel_mapping", [])
def create(self, product_name, instance_data, pre_create_data):
if not substance_painter.project.is_open():
raise CreatorError("Can't create a Texture Set instance without "
"an open project.")
@ -42,11 +49,20 @@ class CreateTextures(Creator):
"exportFileFormat",
"exportSize",
"exportPadding",
"exportDilationDistance"
"exportDilationDistance",
"useCustomExportPreset",
"exportChannel"
]:
if key in pre_create_data:
creator_attributes[key] = pre_create_data[key]
if pre_create_data.get("use_selection"):
stack = substance_painter.textureset.get_active_stack()
instance_data["selected_node_id"] = [
node_number.uid() for node_number in
substance_painter.layerstack.get_selected_nodes(stack)]
instance = self.create_instance_in_context(product_name,
instance_data)
set_instance(
@ -88,8 +104,53 @@ class CreateTextures(Creator):
return instance
def get_instance_attr_defs(self):
if self.channel_mapping:
export_channel_enum = {
item["value"]: item["name"]
for item in self.channel_mapping
}
else:
export_channel_enum = {
"BaseColor": "Base Color",
"Metallic": "Metallic",
"Roughness": "Roughness",
"SpecularEdgeColor": "Specular Edge Color",
"Emissive": "Emissive",
"Opacity": "Opacity",
"Displacement": "Displacement",
"Glossiness": "Glossiness",
"Anisotropylevel": "Anisotropy Level",
"AO": "Ambient Occulsion",
"Anisotropyangle": "Anisotropy Angle",
"Transmissive": "Transmissive",
"Reflection": "Reflection",
"Diffuse": "Diffuse",
"Ior": "Index of Refraction",
"Specularlevel": "Specular Level",
"BlendingMask": "Blending Mask",
"Translucency": "Translucency",
"Scattering": "Scattering",
"ScatterColor": "Scatter Color",
"SheenOpacity": "Sheen Opacity",
"SheenRoughness": "Sheen Roughness",
"SheenColor": "Sheen Color",
"CoatOpacity": "Coat Opacity",
"CoatColor": "Coat Color",
"CoatRoughness": "Coat Roughness",
"CoatSpecularLevel": "Coat Specular Level",
"CoatNormal": "Coat Normal",
}
return [
EnumDef("exportChannel",
items=export_channel_enum,
multiselection=True,
default=None,
label="Export Channel(s)",
tooltip="Choose the channel which you "
"want to solely export. The value "
"is 'None' by default which exports "
"all channels"),
EnumDef("exportPresetUrl",
items=get_export_presets(),
label="Output Template"),
@ -149,7 +210,6 @@ class CreateTextures(Creator):
},
default=None,
label="Size"),
EnumDef("exportPadding",
items={
"passthrough": "No padding (passthrough)",
@ -172,4 +232,10 @@ class CreateTextures(Creator):
def get_pre_create_attr_defs(self):
# Use same attributes as for instance attributes
return self.get_instance_attr_defs()
attr_defs = []
if substance_painter.application.version_info()[0] >= 10:
attr_defs.append(
BoolDef("use_selection", label="Use selection",
tooltip="Select Layer Stack(s) for exporting")
)
return attr_defs + self.get_instance_attr_defs()

View file

@ -8,6 +8,7 @@ import substance_painter.textureset
from ayon_core.pipeline import publish
from ayon_core.hosts.substancepainter.api.lib import (
get_parsed_export_maps,
get_filtered_export_preset,
strip_template
)
from ayon_core.pipeline.create import get_product_name
@ -207,5 +208,8 @@ class CollectTextureSet(pyblish.api.InstancePlugin):
for key, value in dict(parameters).items():
if value is None:
parameters.pop(key)
channel_layer = creator_attrs.get("exportChannel", [])
if channel_layer:
maps = get_filtered_export_preset(preset_url, channel_layer)
config.update(maps)
return config

View file

@ -1,6 +1,6 @@
import substance_painter.export
from ayon_core.pipeline import KnownPublishError, publish
from ayon_core.hosts.substancepainter.api.lib import set_layer_stack_opacity
class ExtractTextures(publish.Extractor,
@ -25,19 +25,24 @@ class ExtractTextures(publish.Extractor,
def process(self, instance):
config = instance.data["exportConfig"]
result = substance_painter.export.export_project_textures(config)
creator_attrs = instance.data["creator_attributes"]
export_channel = creator_attrs.get("exportChannel", [])
node_ids = instance.data.get("selected_node_id", [])
if result.status != substance_painter.export.ExportStatus.Success:
raise KnownPublishError(
"Failed to export texture set: {}".format(result.message)
)
with set_layer_stack_opacity(node_ids, export_channel):
result = substance_painter.export.export_project_textures(config)
# Log what files we generated
for (texture_set_name, stack_name), maps in result.textures.items():
# Log our texture outputs
self.log.info(f"Exported stack: {texture_set_name} {stack_name}")
for texture_map in maps:
self.log.info(f"Exported texture: {texture_map}")
if result.status != substance_painter.export.ExportStatus.Success:
raise KnownPublishError(
"Failed to export texture set: {}".format(result.message)
)
# Log what files we generated
for (texture_set_name, stack_name), maps in result.textures.items():
# Log our texture outputs
self.log.info(f"Exported stack: {texture_set_name} {stack_name}")
for texture_map in maps:
self.log.info(f"Exported texture: {texture_map}")
# We'll insert the color space data for each image instance that we
# added into this texture set. The collector couldn't do so because

View file

@ -30,11 +30,16 @@ class ValidateOutputMaps(pyblish.api.InstancePlugin):
# it will generate without actually exporting the files. So we try to
# generate the smallest size / fastest export as possible
config = copy.deepcopy(config)
invalid_channels = self.get_invalid_channels(instance, config)
if invalid_channels:
raise PublishValidationError(
"Invalid Channel(s): {} found in texture set {}".format(
invalid_channels, instance.name
))
parameters = config["exportParameters"][0]["parameters"]
parameters["sizeLog2"] = [1, 1] # output 2x2 images (smallest)
parameters["paddingAlgorithm"] = "passthrough" # no dilation (faster)
parameters["dithering"] = False # no dithering (faster)
result = substance_painter.export.export_project_textures(config)
if result.status != substance_painter.export.ExportStatus.Success:
raise PublishValidationError(
@ -108,3 +113,41 @@ class ValidateOutputMaps(pyblish.api.InstancePlugin):
message=message,
title="Missing output maps"
)
def get_invalid_channels(self, instance, config):
"""Function to get invalid channel(s) from export channel
filtering
Args:
instance (pyblish.api.Instance): Instance
config (dict): export config
Raises:
PublishValidationError: raise Publish Validation
Error if any invalid channel(s) found
Returns:
list: invalid channel(s)
"""
creator_attrs = instance.data["creator_attributes"]
export_channel = creator_attrs.get("exportChannel", [])
tmp_export_channel = copy.deepcopy(export_channel)
invalid_channel = []
if export_channel:
for export_preset in config.get("exportPresets", {}):
if not export_preset.get("maps", {}):
raise PublishValidationError(
"No Texture Map Exported with texture set: {}.".format(
instance.name)
)
map_names = [channel_map["fileName"] for channel_map
in export_preset["maps"]]
for channel in tmp_export_channel:
# Check if channel is found in at least one map
for map_name in map_names:
if channel in map_name:
break
else:
invalid_channel.append(channel)
return invalid_channel

View file

@ -681,7 +681,7 @@ class PublishAttributeValues(AttributeValues):
@property
def parent(self):
self.publish_attributes.parent
return self.publish_attributes.parent
class PublishAttributes:

View file

@ -212,7 +212,13 @@ class ApplicationsAddonSettings(BaseSettingsModel):
scope=["studio"]
)
only_available: bool = SettingsField(
True, title="Show only available applications")
True,
title="Show only available applications",
description="Enable to show only applications in AYON Launcher"
" for which the executable paths are found on the running machine."
" This applies as an additional filter to the applications defined in a "
" project's anatomy settings to ignore unavailable applications."
)
@validator("tool_groups")
def validate_unique_name(cls, value):

View file

@ -572,8 +572,11 @@ class ExporterReview(object):
self.fhead = self.fhead.replace("#", "")[:-1]
def get_representation_data(
self, tags=None, range=False,
custom_tags=None, colorspace=None
self,
tags=None,
range=False,
custom_tags=None,
colorspace=None,
):
""" Add representation data to self.data
@ -584,6 +587,8 @@ class ExporterReview(object):
Defaults to False.
custom_tags (list[str], optional): user inputted custom tags.
Defaults to None.
colorspace (str, optional): colorspace name.
Defaults to None.
"""
add_tags = tags or []
repre = {
@ -591,7 +596,13 @@ class ExporterReview(object):
"ext": self.ext,
"files": self.file,
"stagingDir": self.staging_dir,
"tags": [self.name.replace("_", "-")] + add_tags
"tags": [self.name.replace("_", "-")] + add_tags,
"data": {
# making sure that once intermediate file is published
# as representation, we will be able to then identify it
# from representation.data.isIntermediate
"isIntermediate": True
},
}
if custom_tags:
@ -837,7 +848,8 @@ class ExporterReviewMov(ExporterReview):
def generate_mov(self, farm=False, delete=True, **kwargs):
# colorspace data
colorspace = None
colorspace = self.write_colorspace
# get colorspace settings
# get colorspace data from context
config_data, _ = get_colorspace_settings_from_publish_context(
@ -999,7 +1011,7 @@ class ExporterReviewMov(ExporterReview):
tags=tags + add_tags,
custom_tags=add_custom_tags,
range=True,
colorspace=colorspace
colorspace=colorspace,
)
self.log.debug("Representation... `{}`".format(self.data))

View file

@ -63,7 +63,8 @@ class LoadClip(plugin.NukeLoader):
# option gui
options_defaults = {
"start_at_workfile": True,
"add_retime": True
"add_retime": True,
"deep_exr": False
}
node_name_template = "{class_name}_{ext}"
@ -80,6 +81,11 @@ class LoadClip(plugin.NukeLoader):
"add_retime",
help="Load with retime",
default=cls.options_defaults["add_retime"]
),
qargparse.Boolean(
"deep_exr",
help="Read with deep exr",
default=cls.options_defaults["deep_exr"]
)
]
@ -115,6 +121,9 @@ class LoadClip(plugin.NukeLoader):
add_retime = options.get(
"add_retime", self.options_defaults["add_retime"])
deep_exr = options.get(
"deep_exr", self.options_defaults["deep_exr"])
repre_id = repre_entity["id"]
self.log.debug(
@ -155,13 +164,21 @@ class LoadClip(plugin.NukeLoader):
return
read_name = self._get_node_name(context)
# Create the Loader with the filename path set
read_node = nuke.createNode(
"Read",
"name {}".format(read_name),
inpanel=False
)
read_node = None
if deep_exr:
# Create the Loader with the filename path set
read_node = nuke.createNode(
"DeepRead",
"name {}".format(read_name),
inpanel=False
)
else:
# Create the Loader with the filename path set
read_node = nuke.createNode(
"Read",
"name {}".format(read_name),
inpanel=False
)
# get colorspace
colorspace = (
@ -173,14 +190,14 @@ class LoadClip(plugin.NukeLoader):
# we will switch off undo-ing
with viewer_update_and_undo_stop():
read_node["file"].setValue(filepath)
self.set_colorspace_to_node(
read_node,
filepath,
project_name,
version_entity,
repre_entity
)
if read_node.Class() == "Read":
self.set_colorspace_to_node(
read_node,
filepath,
project_name,
version_entity,
repre_entity
)
self._set_range_to_node(
read_node, first, last, start_at_workfile, slate_frames
@ -330,13 +347,14 @@ class LoadClip(plugin.NukeLoader):
# to avoid multiple undo steps for rest of process
# we will switch off undo-ing
with viewer_update_and_undo_stop():
self.set_colorspace_to_node(
read_node,
filepath,
project_name,
version_entity,
repre_entity
)
if read_node.Class() == "Read":
self.set_colorspace_to_node(
read_node,
filepath,
project_name,
version_entity,
repre_entity
)
self._set_range_to_node(read_node, first, last, start_at_workfile)

View file

@ -138,7 +138,6 @@ class ExtractReviewIntermediates(publish.Extractor):
self, instance, o_name, o_data["extension"],
multiple_presets)
o_data["add_custom_tags"].append("intermediate")
delete = not o_data.get("publish", False)
if instance.data.get("farm"):

View file

@ -1,6 +1,6 @@
name = "nuke"
title = "Nuke"
version = "0.2.0"
version = "0.2.1"
client_dir = "ayon_nuke"

View file

@ -22,7 +22,9 @@ class LoadClipOptionsModel(BaseSettingsModel):
add_retime: bool = SettingsField(
title="Add retime"
)
deep_exr: bool = SettingsField(
title="Deep Exr Read Node"
)
class LoadClipModel(BaseSettingsModel):
enabled: bool = SettingsField(
@ -65,7 +67,8 @@ DEFAULT_LOADER_PLUGINS_SETTINGS = {
"node_name_template": "{class_name}_{ext}",
"options_defaults": {
"start_at_workfile": True,
"add_retime": True
"add_retime": True,
"deep_exr": False
}
}
}

View file

@ -1,3 +1,3 @@
name = "substancepainter"
title = "Substance Painter"
version = "0.1.1"
version = "0.1.2"

View file

@ -0,0 +1,59 @@
from ayon_server.settings import BaseSettingsModel, SettingsField
class ChannelMappingItemModel(BaseSettingsModel):
_layout = "compact"
name: str = SettingsField(title="Channel Type")
value: str = SettingsField(title="Channel Map")
class CreateTextureModel(BaseSettingsModel):
channel_mapping: list[ChannelMappingItemModel] = SettingsField(
default_factory=list, title="Channel Mapping")
class CreatorsModel(BaseSettingsModel):
CreateTextures: CreateTextureModel = SettingsField(
default_factory=CreateTextureModel,
title="Create Textures"
)
DEFAULT_CREATOR_SETTINGS = {
"CreateTextures": {
"channel_mapping": [
{"name": "Base Color", "value": "BaseColor"},
{"name": "Metallic", "value": "Metallic"},
{"name": "Roughness", "value": "Roughness"},
{"name": "Normal", "value": "Normal"},
{"name": "Height", "value": "Height"},
{"name": "Specular Edge Color",
"value": "SpecularEdgeColor"},
{"name": "Opacity", "value": "Opacity"},
{"name": "Displacement", "value": "Displacement"},
{"name": "Glossiness", "value": "Glossiness"},
{"name": "Anisotropy Level",
"value": "Anisotropylevel"},
{"name": "Ambient Occulsion", "value": "AO"},
{"name": "Anisotropy Angle",
"value": "Anisotropyangle"},
{"name": "Transmissive", "value": "Transmissive"},
{"name": "Reflection", "value": "Reflection"},
{"name": "Diffuse", "value": "Diffuse"},
{"name": "Index of Refraction", "value": "Ior"},
{"name": "Specular Level", "value": "Specularlevel"},
{"name": "Blending Mask", "value": "BlendingMask"},
{"name": "Translucency", "value": "Translucency"},
{"name": "Scattering", "value": "Scattering"},
{"name": "Scatter Color", "value": "ScatterColor"},
{"name": "Sheen Opacity", "value": "SheenOpacity"},
{"name": "Sheen Color", "value": "SheenColor"},
{"name": "Coat Opacity", "value": "CoatOpacity"},
{"name": "Coat Color", "value": "CoatColor"},
{"name": "Coat Roughness", "value": "CoatRoughness"},
{"name": "CoatSpecularLevel",
"value": "Coat Specular Level"},
{"name": "CoatNormal", "value": "Coat Normal"}
],
}
}

View file

@ -1,5 +1,6 @@
from ayon_server.settings import BaseSettingsModel, SettingsField
from .imageio import ImageIOSettings, DEFAULT_IMAGEIO_SETTINGS
from .creator_plugins import CreatorsModel, DEFAULT_CREATOR_SETTINGS
from .load_plugins import LoadersModel, DEFAULT_LOADER_SETTINGS
@ -18,6 +19,8 @@ class SubstancePainterSettings(BaseSettingsModel):
default_factory=list,
title="Shelves"
)
create: CreatorsModel = SettingsField(
default_factory=DEFAULT_CREATOR_SETTINGS, title="Creators")
load: LoadersModel = SettingsField(
default_factory=DEFAULT_LOADER_SETTINGS, title="Loaders")
@ -25,5 +28,7 @@ class SubstancePainterSettings(BaseSettingsModel):
DEFAULT_SPAINTER_SETTINGS = {
"imageio": DEFAULT_IMAGEIO_SETTINGS,
"shelves": [],
"create": DEFAULT_CREATOR_SETTINGS,
"load": DEFAULT_LOADER_SETTINGS,
}