code clean up & rename the contextmanager function to set_layer_stack_opacity

This commit is contained in:
Kayla Man 2024-05-22 19:24:43 +08:00
parent f3b1d34545
commit 597f4a24a3

View file

@ -4,7 +4,7 @@ import json
from collections import defaultdict
import contextlib
import substance_painter as sp
import substance_painter
import substance_painter.project
import substance_painter.resource
import substance_painter.js
@ -645,47 +645,77 @@ def prompt_new_file_with_mesh(mesh_filepath):
def get_export_presets_by_filtering(export_preset_name, channel_type_list):
"""Function to get 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
"""
new_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_presets[export_preset_name])), None)
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 n in channel_type_list:
if not channel_map.get("fileName"):
continue
if preset is not None:
maps = preset.list_output_maps()
for channel_map in maps:
for n in channel_type_list:
if n in channel_map["fileName"]:
new_maps.append(channel_map)
# Create a new preset
return {
"exportPresets": [
{
"name": export_preset_name,
"maps": new_maps
}
],
}
return {}
if n in channel_map["fileName"]:
new_maps.append(channel_map)
# Create a new preset
return {
"exportPresets": [
{
"name": export_preset_name,
"maps": new_maps
}
],
}
@contextlib.contextmanager
def supsend_publish_layer_stack(node_ids, channel_type):
def set_layer_stack_opacity(node_ids, channel_type):
"""Function to set the opacity of the layer stack during
context
Args:
node_ids (list): A list of substance painter node ids
channel_type (list): A list of channel types
"""
all_selected_nodes = []
opacity_set_list = []
stack = sp.textureset.get_active_stack()
stack_root_layers = sp.layerstack.get_root_layer_nodes(stack)
if node_ids and channel_type:
for node_id in node_ids:
node = sp.layerstack.get_node_by_uid(int(node_id))
all_selected_nodes.append(node)
filtered_nodes = [node for node in stack_root_layers
if node not in all_selected_nodes]
stack = substance_painter.textureset.get_active_stack()
stack_root_layers = (
substance_painter.layerstack.get_root_layer_nodes(stack)
)
# Do nothing
if not node_ids or not channel_type:
yield
return
for node_id in node_ids:
node = substance_painter.layerstack.get_node_by_uid(int(node_id))
all_selected_nodes.append(node)
filtered_nodes = {node for node in stack_root_layers
if node not in all_selected_nodes}
for node in filtered_nodes:
for channel in channel_type:
chan = getattr(sp.textureset.ChannelType, channel)
chan = getattr(substance_painter.textureset.ChannelType, channel)
opacity_set_list.append((chan, node.get_opacity(chan)))
try:
for node in filtered_nodes: