Changing the Blender USD Export Settings

Changing the Blender USD Export Settings#

This example plugin changes the default USD export settings in Blender.

See the section for Single File Plugins on how to load this example.

# BlenderUsdExporter.py

name = "BlenderUsdExporter"
classname = "BlenderUsdExporter"


import os
from qtpy.QtWidgets import *


class BlenderUsdExporter:
    def __init__(self, core):
        self.core = core
        self.version = "v1.0.0"

        if self.core.appPlugin.pluginName == "Blender":
            # replace default exportUsd function of the Blender plugin
            self.core.plugins.monkeyPatch(self.core.appPlugin.exportUsd, self.exportUsd, self, force=True)

    def exportUsd(self, outputName, origin, startFrame, endFrame, expNodes, catchError=True, additionalSettings=None):
        # this function was copied from the Blender plugin and some small modifications were made to add custom settings for the USD export.
        import bpy
        from _bpy import ops as _ops_module
        additionalSettings = additionalSettings or {}
        try:
            _ops_module.as_string("WM_OT_usd_export")
        except:
            ext = os.path.splitext(outputName)[1]
            msg = "Format \"%s\" is not supported in this Blender version. Exporting USD requires at least Blender 2.82" % ext
            self.core.popup(msg)
            return False

        self.core.appPlugin.setFrameRange(origin, startFrame, endFrame)
        additionalSettings["export_curves"] = False
        additionalSettings["root_prim_path"] = "/asset"
        try:
            if bpy.app.version < (4, 0, 0):
                bpy.ops.wm.usd_export(
                    self.core.appPlugin.getOverrideContext(origin),
                    filepath=outputName,
                    export_animation=startFrame != endFrame,
                    selected_objects_only=(not origin.chb_wholeScene.isChecked()),
                    **additionalSettings,
                )
            else:
                with bpy.context.temp_override(**self.core.appPlugin.getOverrideContext(origin)):
                    bpy.ops.wm.usd_export(
                        filepath=outputName,
                        export_animation=startFrame != endFrame,
                        selected_objects_only=(not origin.chb_wholeScene.isChecked()),
                        **additionalSettings,
                    )
        except:
            if catchError:
                return False
            else:
                raise

        return outputName