Skip to content

lib

export_outputs_by_sd_graph(instance_name, target_graph, output_dir, extension, selected_map_identifiers)

Modified and referenced from exportSDGraphOutputs in Substance Designer Python API. Export the textures from the output nodes of the specified SD Graphs

Parameters:

Name Type Description Default
instance_name Instance

instance name

required
target_graph SDGraph

target SD Graph

required
output_dir str

output directory

required
extension str

extension

required
selected_map_identifiers set

list of maps targeted to be exported

required

Returns:

Name Type Description
bool

Shows if the maps are successfully exported

Source code in client/ayon_substancedesigner/api/lib.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def export_outputs_by_sd_graph(instance_name, target_graph, output_dir,
                               extension, selected_map_identifiers):
    """
    Modified and referenced from exportSDGraphOutputs in Substance Designer
    Python API.
    Export the textures from the output nodes of the specified SD Graphs

    Args:
        instance_name (Instance): instance name
        target_graph (sd.api.sdgraph.SDGraph): target SD Graph
        output_dir (str): output directory
        extension (str): extension
        selected_map_identifiers (set): list of maps targeted to be exported

    Returns:
        bool: Shows if the maps are successfully exported
    """
    if not target_graph:
        return False

    if not issubclass(type(target_graph), sdsbscompgraph.SDSBSCompGraph):
        return False

    # Compute the SDSBSCompGraph so that all node's textures are computed
    target_graph.compute()

    # Get some information on the graph
    graph_name = target_graph.getIdentifier()

    for sd_node in target_graph.getOutputNodes():
        node_definition = sd_node.getDefinition()
        for output in sd_node.getProperties(
            sdproperty.SDPropertyCategory.Output):
                map_identifier = output.getId()
                if map_identifier not in selected_map_identifiers:
                    continue
                output_properties = node_definition.getProperties(
                    sdproperty.SDPropertyCategory.Output
                )
                for output_property in output_properties:
                    # Get the property value
                    property_value = sd_node.getPropertyValue(output_property)

                    # Get the property value as texture
                    property_texture = property_value.get()
                    if not property_texture:
                        continue

                    filename = (
                        f"{instance_name}_{graph_name}_{map_identifier}.{extension}"
                    )
                    texture_filename = os.path.abspath(
                        os.path.join(output_dir, filename)
                    )

                    try:
                        property_texture.save(texture_filename)
                    except APIException:
                        print('Fail to save texture %s' % texture_filename)

    return True

get_colorspace_data(raw_colorspace=False)

Get Colorspace data of the output map Args: raw_colorspace (bool, optional): Use raw colorspace data. Defaults to False.

Returns:

Name Type Description
str

colorspace name

Source code in client/ayon_substancedesigner/api/lib.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def get_colorspace_data(raw_colorspace=False):
    """Get Colorspace data of the output map
    Args:
        raw_colorspace (bool, optional): Use raw colorspace data.
                                         Defaults to False.

    Returns:
        str: colorspace name
    """
    ctx = sd.getContext()
    app = ctx.getSDApplication()

    # Access the color management engine.
    color_management = app.getColorManagementEngine()
    if raw_colorspace:
        return color_management.getRawColorSpaceName()
    else:
        return color_management.getWorkingColorSpaceName()

get_current_graph_name()

Get the name of the current SD graph

Returns:

Name Type Description
str

current SD graph name

Source code in client/ayon_substancedesigner/api/lib.py
49
50
51
52
53
54
55
56
57
58
59
60
def get_current_graph_name():
    """Get the name of the current SD graph

    Returns:
        str: current SD graph name
    """
    qt_ui = qt_ui_manager()
    current_graph = qt_ui.getCurrentGraph()
    if not current_graph:
        return None

    return current_graph.getIdentifier()

get_map_identifiers_by_graph(target_graph_name)

Get map identifiers of the target SD graph

Parameters:

Name Type Description Default
target_graph_name str

target SD graph name

required

Returns:

Name Type Description
set

all map identifiers

Source code in client/ayon_substancedesigner/api/lib.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def get_map_identifiers_by_graph(target_graph_name):
    """Get map identifiers of the target SD graph

    Args:
        target_graph_name (str): target SD graph name

    Returns:
        set: all map identifiers
    """
    all_map_identifiers = set()
    target_graph = get_sd_graph_by_name(target_graph_name)
    if target_graph:
        for output_node in target_graph.getOutputNodes():
            for output in output_node.getProperties(
                sd.api.sdproperty.SDPropertyCategory.Output):
                    all_map_identifiers.add(output.getId())

    return all_map_identifiers

get_output_maps_from_graphs()

Get Output Maps from Substance Designer graphs by package

Returns:

Name Type Description
set

name of the output maps from substance designer graphs

Source code in client/ayon_substancedesigner/api/lib.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
def get_output_maps_from_graphs():
    """Get Output Maps from Substance Designer graphs by package

    Returns:
        set: name of the output maps from substance designer graphs
    """
    all_output_maps = set()
    current_package = get_package_from_current_graph()
    for resource in current_package.getChildrenResources(True):
        if resource.getClassName() == "SDSBSCompGraph":
            for output_node in resource.getOutputNodes():
                for output in set(output_node.getProperties(
                    sd.api.sdproperty.SDPropertyCategory.Output)):
                        all_output_maps.add(output.getId())

    return all_output_maps

get_package_from_current_graph()

Get Package from the current graph.

Returns:

Type Description

sd.api.sdpackage.SDPackage: A package with SDResource

Source code in client/ayon_substancedesigner/api/lib.py
36
37
38
39
40
41
42
43
44
45
46
def get_package_from_current_graph():
    """Get Package from the current graph.

    Returns:
        sd.api.sdpackage.SDPackage: A package with SDResource
    """
    qt_ui = qt_ui_manager()
    current_graph = qt_ui.getCurrentGraph()
    if not current_graph:
        return None
    return current_graph.getPackage()

get_sd_graph_by_name(graph_name)

Get SD graph base on its name

Parameters:

Name Type Description Default
graph_name str

SD graph name

required

Returns:

Type Description

sd.api.sdgraph.SDGraph: SD Graph

Source code in client/ayon_substancedesigner/api/lib.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def get_sd_graph_by_name(graph_name):
    """Get SD graph base on its name

    Args:
        graph_name (str): SD graph name

    Returns:
        sd.api.sdgraph.SDGraph: SD Graph
    """
    pkg_mgr = package_manager()
    for package in pkg_mgr.getUserPackages():
        for resource in package.getChildrenResources(True):
            if (
                resource.getClassName() == "SDSBSCompGraph"
                and resource.getIdentifier() == graph_name
            ):
                return resource

get_sd_graphs_by_package()

Get Substance Designer Graphs by package

Returns:

Name Type Description
list

name of Substance Designer Graphs

Source code in client/ayon_substancedesigner/api/lib.py
63
64
65
66
67
68
69
70
71
72
73
74
75
def get_sd_graphs_by_package():
    """Get Substance Designer Graphs by package

    Returns:
        list: name of Substance Designer Graphs
    """
    current_package = get_package_from_current_graph()
    return [
        resource.getIdentifier()
        for resource in
        current_package.getChildrenResources(True)
        if resource.getClassName() == "SDSBSCompGraph"
    ]

package_manager()

Get Package Manager of Substance Designer

Returns:

Type Description

sd.api.sdpackagemgr.SDPackageMgr: Package Manager

Source code in client/ayon_substancedesigner/api/lib.py
12
13
14
15
16
17
18
19
20
def package_manager():
    """Get Package Manager of Substance Designer

    Returns:
        sd.api.sdpackagemgr.SDPackageMgr: Package Manager
    """
    app = sd.getContext().getSDApplication()
    pkg_mgr = app.getPackageMgr()
    return pkg_mgr

parsing_sd_data(target_package, metadata_type, is_dictionary=True)

Parse and convert Subsatnce Designer SDValue data to dictionary

Parameters:

Name Type Description Default
target_package SDPackage

target SD Package

required
metadata_type str

Ayon metadata type

required

Returns:

Type Description

dict/list: metadata dict

Source code in client/ayon_substancedesigner/api/lib.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def parsing_sd_data(target_package, metadata_type: str, is_dictionary=True):
    """Parse and convert Subsatnce Designer SDValue data to dictionary

    Args:
        target_package (sd.api.sdpackage.SDPackage): target SD Package
        metadata_type (str): Ayon metadata type

    Returns:
        dict/list: metadata dict
    """
    metadata = {} if is_dictionary else []
    package_metadata_dict = target_package.getMetadataDict()
    with contextlib.suppress(APIException):
        metadata_value = package_metadata_dict.getPropertyValueFromId(
            metadata_type).get()
        metadata = json.loads(metadata_value)

    return metadata

qt_ui_manager()

Get Qt Python UI Manager of Substance Designer

Returns:

Type Description

sd.api.qtforpythonuimgrwrapper.QtForPythonUIMgrWrapper: Qt Python UI Manager

Source code in client/ayon_substancedesigner/api/lib.py
23
24
25
26
27
28
29
30
31
32
33
def qt_ui_manager():
    """Get Qt Python UI Manager of Substance Designer

    Returns:
        sd.api.qtforpythonuimgrwrapper.QtForPythonUIMgrWrapper: Qt Python
            UI Manager
    """
    ctx = sd.getContext()
    sd_app = ctx.getSDApplication()
    qt_ui_mgr = sd_app.getQtForPythonUIMgr()
    return qt_ui_mgr

set_sd_metadata(metadata_type, metadata)

Set AYON-related metadata in Substance Painter

Parameters:

Name Type Description Default
metadata_type str

AYON metadata key

required
metadata dict / list

AYON-related metadata

required
Source code in client/ayon_substancedesigner/api/lib.py
117
118
119
120
121
122
123
124
125
126
127
128
129
def set_sd_metadata(metadata_type: str, metadata):
    """Set AYON-related metadata in Substance Painter

    Args:
        metadata_type (str): AYON metadata key
        metadata (dict/list): AYON-related metadata
    """
    # Need to convert dict to string first
    target_package = get_package_from_current_graph()
    metadata_to_str = f"{json.dumps(metadata)}"
    metadata_value = sd.api.sdvaluestring.SDValueString.sNew(metadata_to_str)
    package_metadata_dict = target_package.getMetadataDict()
    package_metadata_dict.setPropertyValueFromId(metadata_type, metadata_value)