Skip to content

extract_usd

ExtractModelUSD

Bases: ExtractUSD

Extract model as USD.

Source code in client/ayon_blender/plugins/publish/extract_usd.py
182
183
184
185
186
187
188
189
190
class ExtractModelUSD(ExtractUSD):
    """Extract model as USD."""

    label = "Extract USD (Model)"
    hosts = ["blender"]
    families = ["model"]

    # Driven by settings
    optional = True

ExtractUSD

Bases: BlenderExtractor, OptionalPyblishPluginMixin

Extract as USD.

Source code in client/ayon_blender/plugins/publish/extract_usd.py
 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
 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class ExtractUSD(plugin.BlenderExtractor,
                 OptionalPyblishPluginMixin):
    """Extract as USD."""

    label = "Extract USD"
    hosts = ["blender"]
    families = ["usd"]

    convert_orientation = False

    def process(self, instance):
        if not self.is_active(instance.data):
            return

        # Ignore runtime instances (e.g. USD layers)
        # TODO: This is better done via more specific `families`
        if not instance.data.get("transientData", {}).get("instance_node"):
            return

        # Define extract output file path
        stagingdir = self.staging_dir(instance)
        filename = f"{instance.name}.usd"
        filepath = os.path.join(stagingdir, filename)

        # Perform extraction
        self.log.debug("Performing extraction..")

        # Select all members to "export selected"
        plugin.deselect_all()

        selected = []
        for obj in instance:
            if isinstance(obj, bpy.types.Object):
                obj.select_set(True)
                selected.append(obj)

        root = lib.get_highest_root(objects=instance[:])
        if not root:
            instance_node = instance.data["transientData"]["instance_node"]
            raise KnownPublishError(
                f"No root object found in instance: {instance_node.name}"
            )
        self.log.debug(f"Exporting using active root: {root.name}")

        context = plugin.create_blender_context(
            active=root, selected=selected)

        attribute_values = self.get_attr_values_from_data(instance.data)
        convert_orientation = attribute_values.get(
            "convert_orientation",
            self.convert_orientation
        )
        kwargs = {
            "convert_orientation": convert_orientation,
            "export_global_forward_selection": attribute_values.get("forward_axis", "Z"),
            "export_global_up_selection": attribute_values.get("up_axis", "Y"),
        }
        if lib.get_blender_version() < (4, 2, 1):
            kwargs = {}
            if convert_orientation:
                self.log.warning(
                    "Convert orientation was enabled for USD export but is not "
                    "supported in Blender < \"4.2.1\". Please update to at least Blender "
                    "4.2.1 to support it."
                )

        # Export USD
        with bpy.context.temp_override(**context):
            bpy.ops.wm.usd_export(
                # Override the `/root` default value. If left as an empty 
                # string, Blender will use the top-level object as the root prim.
                filepath=filepath,
                root_prim_path="",  
                selected_objects_only=True,
                export_textures=False,
                relative_paths=False,
                export_animation=False,
                export_hair=False,
                export_uvmaps=True,
                # TODO: add for new version of Blender (4+?)
                # export_mesh_colors=True,
                export_normals=True,
                export_materials=True,
                use_instancing=True,
                # Convert Orientation
                **kwargs
            )

        plugin.deselect_all()

        # Add representation
        representation = {
            'name': 'usd',
            'ext': 'usd',
            'files': filename,
            "stagingDir": stagingdir,
        }
        instance.data.setdefault("representations", []).append(representation)
        self.log.debug("Extracted instance '%s' to: %s",
                       instance.name, representation)

    @classmethod
    def get_attr_defs_for_instance(cls, create_context, instance):
        # Filtering of instance, if needed, can be customized
        if not cls.instance_matches_plugin_families(instance):
            return []

        # Attributes logic
        publish_attributes = cls.get_attr_values_from_data_for_plugin(
            cls, instance
        )

        visible = publish_attributes.get("convert_orientation", cls.convert_orientation)

        orientation_axes = {
            "X": "X",
            "Y": "Y",  
            "Z": "Z",
            "NEGATIVE_X": "-X",
            "NEGATIVE_Y": "-Y",
            "NEGATIVE_Z": "-Z",
        }

        return [
            BoolDef("convert_orientation",
                    label="Convert Orientation",
                    tooltip="Convert orientation axis to a different"
                    " convention to match other applications.",
                    default=cls.convert_orientation),
            EnumDef("forward_axis",
                    label="Forward Axis",
                    items=orientation_axes,
                    default="Z",
                    visible=visible),
            EnumDef("up_axis",
                    label="Up Axis",
                    items=orientation_axes,
                    default="Y",
                    visible=visible)
        ]

    @classmethod
    def register_create_context_callbacks(cls, create_context):
        create_context.add_value_changed_callback(cls.on_values_changed)

    @classmethod
    def on_values_changed(cls, event):
        """Update instance attribute definitions on attribute changes."""

        # Update attributes if any of the following plug-in attributes
        # change:
        keys = ["convert_orientation"]

        for instance_change in event["changes"]:
            instance = instance_change["instance"]
            if not cls.instance_matches_plugin_families(instance):
                continue
            value_changes = instance_change["changes"]
            plugin_attribute_changes = cls.get_attr_values_from_data_for_plugin(
                cls, value_changes
            )

            if not any(key in plugin_attribute_changes for key in keys):
                continue

            # Update the attribute definitions
            new_attrs = cls.get_attr_defs_for_instance(
                event["create_context"], instance
            )
            instance.set_publish_plugin_attr_defs(cls.__name__, new_attrs)

on_values_changed(event) classmethod

Update instance attribute definitions on attribute changes.

Source code in client/ayon_blender/plugins/publish/extract_usd.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@classmethod
def on_values_changed(cls, event):
    """Update instance attribute definitions on attribute changes."""

    # Update attributes if any of the following plug-in attributes
    # change:
    keys = ["convert_orientation"]

    for instance_change in event["changes"]:
        instance = instance_change["instance"]
        if not cls.instance_matches_plugin_families(instance):
            continue
        value_changes = instance_change["changes"]
        plugin_attribute_changes = cls.get_attr_values_from_data_for_plugin(
            cls, value_changes
        )

        if not any(key in plugin_attribute_changes for key in keys):
            continue

        # Update the attribute definitions
        new_attrs = cls.get_attr_defs_for_instance(
            event["create_context"], instance
        )
        instance.set_publish_plugin_attr_defs(cls.__name__, new_attrs)