Support 'Export Mask to File' functionality to Python API for scenes with UDIMs
# you can leverage the Javascript API to easily export masks to files
# but the code below ONLY works for scenes that are NOT using UDIMs
# it would be nice if it just worked for scenes with UDIMs as well
# there does not seem to be an easy way to do this through
# the Python API export module unless I am missing something
import substance_painter as sp
import posixpath
default_export_path = sp.export.get_default_export_path()
image_format = "png"
current_stack = sp.textureset.get_active_stack()
texture_set_name = current_stack.material().name
selected_layers = sp.layerstack.get_selected_nodes(current_stack)
for layer in selected_layers:
layer_name = layer.get_name()
export_filename = f"{texture_set_name}_{layer_name}_mask.{image_format}"
export_path = posixpath.join(default_export_path, export_filename)
export_path_str = f'\"{export_path}\"'
mask = "mask"
data_path_str = f'[\"{texture_set_name}\", "", \"{layer_name}\", \"{mask}\"]'
javascript_code = "alg.mapexport.save("+data_path_str+","+export_path_str+",{padding:false});"
json_result = sp.js.evaluate(javascript_code)
