Skip to content

pipeline

Pipeline tools for OpenPype Substance Painter integration.

get_instances()

Return all instances stored in the project instances as a list

Source code in client/ayon_substancepainter/api/pipeline.py
424
425
426
def get_instances():
    """Return all instances stored in the project instances as a list"""
    return list(get_instances_by_id().values())

get_instances_by_id()

Return all instances stored in the project instances metadata

Source code in client/ayon_substancepainter/api/pipeline.py
415
416
417
418
419
420
421
def get_instances_by_id():
    """Return all instances stored in the project instances metadata"""
    if not substance_painter.project.is_open():
        return {}

    metadata = substance_painter.project.Metadata(OPENPYPE_METADATA_KEY)
    return metadata.get(OPENPYPE_METADATA_INSTANCES_KEY) or {}

imprint_container(container, name, namespace, context, loader)

Imprint a loaded container with metadata.

Containerisation enables a tracking of version, author and origin for loaded assets.

Parameters:

Name Type Description Default
container dict

The (substance metadata) dictionary to imprint into.

required
name str

Name of resulting assembly

required
namespace str

Namespace under which to host container

required
context dict

Asset information

required
loader LoaderPlugin

loader instance used to produce container.

required

Returns:

Type Description

None

Source code in client/ayon_substancepainter/api/pipeline.py
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
def imprint_container(container,
                      name,
                      namespace,
                      context,
                      loader):
    """Imprint a loaded container with metadata.

    Containerisation enables a tracking of version, author and origin
    for loaded assets.

    Arguments:
        container (dict): The (substance metadata) dictionary to imprint into.
        name (str): Name of resulting assembly
        namespace (str): Namespace under which to host container
        context (dict): Asset information
        loader (load.LoaderPlugin): loader instance used to produce container.

    Returns:
        None

    """

    data = [
        ("schema", "openpype:container-2.0"),
        ("id", AVALON_CONTAINER_ID),
        ("name", str(name)),
        ("namespace", str(namespace) if namespace else None),
        ("loader", str(loader.__class__.__name__)),
        ("representation", context["representation"]["id"]),
        ("project_name", context["project"]["name"]),
    ]
    for key, value in data:
        container[key] = value

remove_container_metadata(object_name)

Helper method to remove the data for a specific container

Source code in client/ayon_substancepainter/api/pipeline.py
369
370
371
372
373
374
375
def remove_container_metadata(object_name):
    """Helper method to remove the data for a specific container"""
    metadata = substance_painter.project.Metadata(OPENPYPE_METADATA_KEY)
    containers = metadata.get(OPENPYPE_METADATA_CONTAINERS_KEY)
    if containers:
        containers.pop(object_name, None)
        metadata.set("containers", containers)

remove_instance(instance_id)

Helper method to remove the data for a specific container

Source code in client/ayon_substancepainter/api/pipeline.py
407
408
409
410
411
412
def remove_instance(instance_id):
    """Helper method to remove the data for a specific container"""
    metadata = substance_painter.project.Metadata(OPENPYPE_METADATA_KEY)
    instances = metadata.get(OPENPYPE_METADATA_INSTANCES_KEY) or {}
    instances.pop(instance_id, None)
    metadata.set("instances", instances)

set_container_metadata(object_name, container_data, update=False)

Helper method to directly set the data for a specific container

Parameters:

Name Type Description Default
object_name str

The unique object name identifier for the container

required
container_data dict

The data for the container. Note 'objectName' data is derived from object_name and key in container_data will be ignored.

required
update bool

Whether to only update the dict data.

False
Source code in client/ayon_substancepainter/api/pipeline.py
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
def set_container_metadata(object_name, container_data, update=False):
    """Helper method to directly set the data for a specific container

    Args:
        object_name (str): The unique object name identifier for the container
        container_data (dict): The data for the container.
            Note 'objectName' data is derived from `object_name` and key in
            `container_data` will be ignored.
        update (bool): Whether to only update the dict data.

    """
    # The objectName is derived from the key in the metadata so won't be stored
    # in the metadata in the container's data.
    container_data.pop("objectName", None)

    metadata = substance_painter.project.Metadata(OPENPYPE_METADATA_KEY)
    containers = metadata.get(OPENPYPE_METADATA_CONTAINERS_KEY) or {}
    if update:
        existing_data = containers.setdefault(object_name, {})
        existing_data.update(container_data)  # mutable dict, in-place update
    else:
        containers[object_name] = container_data
    metadata.set("containers", containers)

set_instance(instance_id, instance_data, update=False)

Helper method to directly set the data for a specific container

Parameters:

Name Type Description Default
instance_id str

Unique identifier for the instance

required
instance_data dict

The instance data to store in the metaadata.

required
Source code in client/ayon_substancepainter/api/pipeline.py
378
379
380
381
382
383
384
385
def set_instance(instance_id, instance_data, update=False):
    """Helper method to directly set the data for a specific container

    Args:
        instance_id (str): Unique identifier for the instance
        instance_data (dict): The instance data to store in the metaadata.
    """
    set_instances({instance_id: instance_data}, update=update)

set_instances(instance_data_by_id, update=False)

Store data for multiple instances at the same time.

This is more optimal than querying and setting them in the metadata one by one.

Source code in client/ayon_substancepainter/api/pipeline.py
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
def set_instances(instance_data_by_id, update=False):
    """Store data for multiple instances at the same time.

    This is more optimal than querying and setting them in the metadata one
    by one.
    """
    metadata = substance_painter.project.Metadata(OPENPYPE_METADATA_KEY)
    instances = metadata.get(OPENPYPE_METADATA_INSTANCES_KEY) or {}

    for instance_id, instance_data in instance_data_by_id.items():
        if update:
            existing_data = instances.get(instance_id, {})
            existing_data.update(instance_data)
        else:
            instances[instance_id] = instance_data

    metadata.set("instances", instances)