Skip to content

extract_maya_usd_layer

ExtractMayaUsdLayer

Bases: Extractor

Extractor for Maya USD Layer from mayaUsdProxyShape

Exports a single Sdf.Layer from a mayaUsdPlugin mayaUsdProxyShape. These layers are the same managed via Maya's Windows > USD Layer Editor.

Source code in client/ayon_maya/plugins/publish/extract_maya_usd_layer.py
 7
 8
 9
10
11
12
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
class ExtractMayaUsdLayer(publish.Extractor):
    """Extractor for Maya USD Layer from `mayaUsdProxyShape`

    Exports a single Sdf.Layer from a mayaUsdPlugin `mayaUsdProxyShape`.
    These layers are the same managed via Maya's Windows > USD Layer Editor.

    """

    label = "Extract Maya USD Layer"
    hosts = ["maya"]
    families = ["mayaUsdLayer"]

    def process(self, instance):

        import mayaUsd

        # Load plugin first
        cmds.loadPlugin("mayaUsdPlugin", quiet=True)

        data = instance.data["stageLayerIdentifier"]
        self.log.debug(f"Using proxy layer: {data}")

        proxy, layer_identifier = data.split(">", 1)
        # TODO: The stage and layer should actually be retrieved during
        #  Collecting so that they can be validated upon and potentially that
        #  any 'child layers' can potentially be recursively exported along
        stage = mayaUsd.ufe.getStage('|world' + proxy)
        layers = stage.GetLayerStack(includeSessionLayers=False)
        layer = next(
            layer for layer in layers if layer.identifier == layer_identifier
        )

        # Define output file path
        staging_dir = self.staging_dir(instance)
        file_name = "{0}.usd".format(instance.name)
        file_path = os.path.join(staging_dir, file_name)
        file_path = file_path.replace('\\', '/')

        self.log.debug("Exporting USD layer to: {}".format(file_path))
        layer.Export(file_path, args={
            "format": instance.data.get("defaultUSDFormat", "usdc")
        })

        # TODO: We might want to remap certain paths - to do so we could take
        #  the SdfLayer and transfer its contents into a anonymous SdfLayer
        #  then we can use the copy to alter it in memory to our like before
        #  writing out

        representation = {
            'name': "usd",
            'ext': "usd",
            'files': file_name,
            'stagingDir': staging_dir
        }
        instance.data.setdefault("representations", []).append(representation)
        self.log.debug(
            "Extracted instance {} to {}".format(instance.name, file_path)
        )