Skip to content

alembic

Tools for loading looks to vray proxies.

get_alembic_ids_cache(path)

Build a id to node mapping in Alembic file.

Nodes without IDs are ignored.

Returns:

Name Type Description
dict dict

Mapping of id to nodes in the Alembic.

Source code in client/ayon_maya/tools/mayalookassigner/alembic.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def get_alembic_ids_cache(path: str) -> dict:
    """Build a id to node mapping in Alembic file.

    Nodes without IDs are ignored.

    Returns:
        dict: Mapping of id to nodes in the Alembic.

    """
    node_ids = get_alembic_paths_by_property(path, attr="cbId")
    id_nodes = defaultdict(list)
    for node, _id in node_ids.items():
        id_nodes[_id].append(node)

    return dict(id_nodes.items())

get_alembic_paths_by_property(filename, attr, verbose=False)

Return attribute value per objects in the Alembic file.

Reads an Alembic archive hierarchy and retrieves the value from the attr properties on the objects.

Parameters:

Name Type Description Default
filename str

Full path to Alembic archive to read.

required
attr str

Id attribute.

required
verbose bool

Whether to verbosely log missing attributes.

False

Returns:

Name Type Description
dict

Mapping of node full path with its id

Source code in client/ayon_maya/tools/mayalookassigner/alembic.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def get_alembic_paths_by_property(filename, attr, verbose=False):
    # type: (str, str, bool) -> dict
    """Return attribute value per objects in the Alembic file.

    Reads an Alembic archive hierarchy and retrieves the
    value from the `attr` properties on the objects.

    Args:
        filename (str): Full path to Alembic archive to read.
        attr (str): Id attribute.
        verbose (bool): Whether to verbosely log missing attributes.

    Returns:
        dict: Mapping of node full path with its id

    """
    # Normalize alembic path
    filename = os.path.normpath(filename)
    filename = filename.replace("\\", "/")
    filename = str(filename)  # path must be string

    try:
        archive = alembic.Abc.IArchive(filename)
    except RuntimeError:
        # invalid alembic file - probably vrmesh
        log.warning("{} is not an alembic file".format(filename))
        return {}
    root = archive.getTop()

    iterator = list(root.children)
    obj_ids = {}

    for obj in iterator:
        name = obj.getFullName()

        # include children for coming iterations
        iterator.extend(obj.children)

        props = obj.getProperties()
        if props.getNumProperties() == 0:
            # Skip those without properties, e.g. '/materials' in a gpuCache
            continue

        # THe custom attribute is under the properties' first container under
        # the ".arbGeomParams"
        prop = props.getProperty(0)  # get base property

        _property = None
        try:
            geo_params = prop.getProperty('.arbGeomParams')
            _property = geo_params.getProperty(attr)
        except KeyError:
            if verbose:
                log.debug("Missing attr on: {0}".format(name))
            continue

        if not _property.isConstant():
            log.warning("Id not constant on: {0}".format(name))

        # Maya alembic export seems to export `cbId` string as array property
        # whereas Houdini primitive `cbId` attribute comes through as a
        # scalar property (which still holds the string?)
        if isinstance(_property, alembic.Abc.IArrayProperty):
            # Get first value sample
            value = _property.getValue()[0]
        else:
            value = _property.getValue()

        obj_ids[name] = value

    return obj_ids