Skip to content

load_multiverse_usd

MultiverseUsdLoader

Bases: Loader

Read USD data in a Multiverse Compound

Source code in client/ayon_maya/plugins/load/load_multiverse_usd.py
 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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class MultiverseUsdLoader(plugin.Loader):
    """Read USD data in a Multiverse Compound"""

    product_base_types = {
        "model",
        "usd",
        "mvUsdComposition",
        "mvUsdOverride",
        "pointcache",
        "animation",
    }
    product_types = product_base_types
    representations = {"*"}
    extensions = {"usd", "usda", "usdc", "usdz", "abc"}

    label = "Load USD to Multiverse"
    order = -10
    icon = "code-fork"
    color = "orange"

    def load(self, context, name=None, namespace=None, options=None):
        folder_name = context["folder"]["name"]
        namespace = namespace or unique_namespace(
            folder_name + "_",
            prefix="_" if folder_name[0].isdigit() else "",
            suffix="_",
        )

        path = self.filepath_from_context(context)

        # Make sure we can load the plugin
        cmds.loadPlugin("MultiverseForMaya", quiet=True)
        import multiverse

        # Create the shape
        with maintained_selection():
            cmds.namespace(addNamespace=namespace)
            with namespaced(namespace, new=False):
                shape = multiverse.CreateUsdCompound(path)
                transform = cmds.listRelatives(
                    shape, parent=True, fullPath=True)[0]

        nodes = [transform, shape]
        self[:] = nodes

        return containerise(
            name=name,
            namespace=namespace,
            nodes=nodes,
            context=context,
            loader=self.__class__.__name__)

    def update(self, container, context):
        # type: (dict, dict) -> None
        """Update container with specified representation."""
        node = container['objectName']
        assert cmds.objExists(node), "Missing container"

        members = cmds.sets(node, query=True) or []
        shapes = cmds.ls(members, type="mvUsdCompoundShape")
        assert shapes, "Cannot find mvUsdCompoundShape in container"

        previous_project_name = container.get(
            "project_name", get_current_project_name()
        )
        repre_entity = context["representation"]
        path = self.filepath_from_context(context)
        prev_representation_id = cmds.getAttr("{}.representation".format(node))
        prev_representation = get_representation_by_id(
            previous_project_name, prev_representation_id
        )
        prev_path = os.path.normpath(prev_representation["attrib"]["path"])

        # Make sure we can load the plugin
        cmds.loadPlugin("MultiverseForMaya", quiet=True)
        import multiverse

        for shape in shapes:

            asset_paths = multiverse.GetUsdCompoundAssetPaths(shape)
            asset_paths = [os.path.normpath(p) for p in asset_paths]

            assert asset_paths.count(prev_path) == 1, \
                "Couldn't find matching path (or too many)"
            prev_path_idx = asset_paths.index(prev_path)

            asset_paths[prev_path_idx] = path

            multiverse.SetUsdCompoundAssetPaths(shape, asset_paths)

        for key, value in [
            ("representation", repre_entity["id"]),
            ("project_name", context["project"]["name"]),
        ]:
            set_attribute(key, value, node)
        mel.eval('refreshEditorTemplates;')

    def switch(self, container, context):
        self.update(container, context)

    def remove(self, container):
        # type: (dict) -> None
        """Remove loaded container."""
        # Delete container and its contents
        if cmds.objExists(container['objectName']):
            members = cmds.sets(container['objectName'], query=True) or []
            cmds.delete([container['objectName']] + members)

        # Remove the namespace, if empty
        namespace = container['namespace']
        if cmds.namespace(exists=namespace):
            members = cmds.namespaceInfo(namespace, listNamespace=True)
            if not members:
                cmds.namespace(removeNamespace=namespace)
            else:
                self.log.warning("Namespace not deleted because it "
                                 "still has members: %s", namespace)

remove(container)

Remove loaded container.

Source code in client/ayon_maya/plugins/load/load_multiverse_usd.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def remove(self, container):
    # type: (dict) -> None
    """Remove loaded container."""
    # Delete container and its contents
    if cmds.objExists(container['objectName']):
        members = cmds.sets(container['objectName'], query=True) or []
        cmds.delete([container['objectName']] + members)

    # Remove the namespace, if empty
    namespace = container['namespace']
    if cmds.namespace(exists=namespace):
        members = cmds.namespaceInfo(namespace, listNamespace=True)
        if not members:
            cmds.namespace(removeNamespace=namespace)
        else:
            self.log.warning("Namespace not deleted because it "
                             "still has members: %s", namespace)

update(container, context)

Update container with specified representation.

Source code in client/ayon_maya/plugins/load/load_multiverse_usd.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def update(self, container, context):
    # type: (dict, dict) -> None
    """Update container with specified representation."""
    node = container['objectName']
    assert cmds.objExists(node), "Missing container"

    members = cmds.sets(node, query=True) or []
    shapes = cmds.ls(members, type="mvUsdCompoundShape")
    assert shapes, "Cannot find mvUsdCompoundShape in container"

    previous_project_name = container.get(
        "project_name", get_current_project_name()
    )
    repre_entity = context["representation"]
    path = self.filepath_from_context(context)
    prev_representation_id = cmds.getAttr("{}.representation".format(node))
    prev_representation = get_representation_by_id(
        previous_project_name, prev_representation_id
    )
    prev_path = os.path.normpath(prev_representation["attrib"]["path"])

    # Make sure we can load the plugin
    cmds.loadPlugin("MultiverseForMaya", quiet=True)
    import multiverse

    for shape in shapes:

        asset_paths = multiverse.GetUsdCompoundAssetPaths(shape)
        asset_paths = [os.path.normpath(p) for p in asset_paths]

        assert asset_paths.count(prev_path) == 1, \
            "Couldn't find matching path (or too many)"
        prev_path_idx = asset_paths.index(prev_path)

        asset_paths[prev_path_idx] = path

        multiverse.SetUsdCompoundAssetPaths(shape, asset_paths)

    for key, value in [
        ("representation", repre_entity["id"]),
        ("project_name", context["project"]["name"]),
    ]:
        set_attribute(key, value, node)
    mel.eval('refreshEditorTemplates;')