Skip to main content
brooks_0502
Participant
February 24, 2026
Open for Voting

Support 'Export Mask to File' functionality to Python API for scenes with UDIMs

  • February 24, 2026
  • 0 replies
  • 12 views

# 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)