Skip to content

extract_apex_usd

ExtractAPEXUSD

Bases: HoudiniInstancePlugin

Extract APEX USD Rig Layer

Initialize the Rig USD layer and populate the 'ayon:apex_rig' attribute to point to the expected APEX Rig bgeo representation path in publish context.

Source code in client/ayon_houdini/plugins/publish/extract_apex_usd.py
 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
class ExtractAPEXUSD(plugin.HoudiniInstancePlugin):
    """Extract APEX USD Rig Layer

    Initialize the Rig USD layer and populate the 'ayon:apex_rig' attribute
    to point to the expected APEX Rig bgeo representation path in publish
    context.
    """

    order = pyblish.api.ExtractorOrder
    label = "Extract APEX To USD"

    families = ["rig"]

    def process(self, instance):
        """Inject the current working file"""

        staging_dir = instance.data.get("stagingDir")
        layer_path = os.path.join(staging_dir, f"{instance.name}.usd")

        sdf_layer = Sdf.Layer.CreateNew(layer_path, args={"format": "usda"})
        self.log.debug(f"Creating USD rig layer: {sdf_layer}")

        folder_path = instance.data["folderPath"]
        default_prim = get_standard_default_prim_name(folder_path)
        set_layer_defaults(sdf_layer, default_prim=default_prim)

        # Add rig attribute to the default prim.
        default_prim_path = Sdf.Path(f"/{default_prim.strip('/')}")
        asset_prim = get_or_define_prim_spec(
            sdf_layer,
            default_prim_path,
            "Xform"
        )
        asset_prim.specifier = Sdf.SpecifierOver
        rig_attr = Sdf.AttributeSpec(
            asset_prim,
            "ayon:apex_rig",
            Sdf.ValueTypeNames.Asset,
            variability=Sdf.VariabilityUniform
        )
        rig_path = self.get_representation_path_in_publish_context(
            instance
        )
        self.log.debug(f"Setting 'ayon:apex_rig' attr to '{rig_path}'")
        rig_attr.default = Sdf.AssetPath(rig_path)

        # Save the file
        sdf_layer.Save()

        representations = instance.data.setdefault("representations", [])
        representations.append({
            "name": "usd",
            "ext": "usd",
            "files": os.path.basename(layer_path),
            "stagingDir": os.path.dirname(layer_path),
        })

    def get_representation_path_in_publish_context(self, instance):
        files = instance.data["frames"]
        first_file = files[0] if isinstance(files, (list, tuple)) else files
        _, ext = splitext(
            first_file, allowed_multidot_extensions=[
                ".ass.gz", ".bgeo.sc", ".bgeo.gz",
                ".bgeo.lzma", ".bgeo.bz2"]
        )
        ext = ext.lstrip(".")

        version_name = instance.data["version"]
        specific_version = isinstance(version_name, int)
        path = get_instance_expected_output_path(
            instance,
            representation_name="bgeo",
            ext=ext,
            version=version_name if specific_version else None
        )
        if path:
            return path

        raise RuntimeError(
            "Unable to resolve publish path for representation \"bgeo\"."
        )

process(instance)

Inject the current working file

Source code in client/ayon_houdini/plugins/publish/extract_apex_usd.py
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
def process(self, instance):
    """Inject the current working file"""

    staging_dir = instance.data.get("stagingDir")
    layer_path = os.path.join(staging_dir, f"{instance.name}.usd")

    sdf_layer = Sdf.Layer.CreateNew(layer_path, args={"format": "usda"})
    self.log.debug(f"Creating USD rig layer: {sdf_layer}")

    folder_path = instance.data["folderPath"]
    default_prim = get_standard_default_prim_name(folder_path)
    set_layer_defaults(sdf_layer, default_prim=default_prim)

    # Add rig attribute to the default prim.
    default_prim_path = Sdf.Path(f"/{default_prim.strip('/')}")
    asset_prim = get_or_define_prim_spec(
        sdf_layer,
        default_prim_path,
        "Xform"
    )
    asset_prim.specifier = Sdf.SpecifierOver
    rig_attr = Sdf.AttributeSpec(
        asset_prim,
        "ayon:apex_rig",
        Sdf.ValueTypeNames.Asset,
        variability=Sdf.VariabilityUniform
    )
    rig_path = self.get_representation_path_in_publish_context(
        instance
    )
    self.log.debug(f"Setting 'ayon:apex_rig' attr to '{rig_path}'")
    rig_attr.default = Sdf.AssetPath(rig_path)

    # Save the file
    sdf_layer.Save()

    representations = instance.data.setdefault("representations", [])
    representations.append({
        "name": "usd",
        "ext": "usd",
        "files": os.path.basename(layer_path),
        "stagingDir": os.path.dirname(layer_path),
    })