USD IO Customization Proposal #101283

Open
opened 2022-09-23 01:38:15 +02:00 by Michael Kowalski · 7 comments

The following description was provided by @charlesfleche on the pipeline-assets-io-module Blender chat channel on 22 Sept 2022.


We want to support our custom metadata and attributes for a given prim, import and export. A few use cases:

  • ID to bind a Blender object to our game engine, so we can import in blender / edit / export to our database.
  • In games, skeletons are usually made of several smaller skeletons: one for the main body, but others for the weapon, clothes, accessories, etc... Those are stored as different objects in our game database, so you mix and match (think about those character creators where you choose the hair, the body, the eyes, the hat of your character...). We need to tag in USD the owner of each bone (with our custom schemas), store this info in Blender, and reserialize it to USD on the way back to the engine.
  • USD doesn't have a definite schema to define an object transform and its offset transform. We express those as two transforms ops in USD, but Blender can't make sense of those at the moment, so we should be able to post process an object to handle our custom schemas and apply those transforms.
  • Imagine a new object is created in Blender, then published to the game engine (we have a network API for that purpose). The response we get contains the new IDs of the objects in the Engine, and we need to assign those IDs to existing blender objects in the Blender scene.

So far we've seen two approaches to customize USD I/O in DCCs:

  • The Maya one, based on Schema translators. Here you register a schema and call some code when it is found while walking the Prims at import time.
  • Our own, based on hooks in the import / export loops:
def import_to_dcc(path):

    def recursively_create_node(prim, parent_dcc_node=None):
        if not prim.IsPseudoRoot():
            node = hook.create_node(prim=prim, parent_dcc_node=parent_dcc_node)            
            if node:
                hook.post_create_node(prim=prim, node=node)
                parent_dcc_node=node

        for child_prim in prim.GetChildren():
            recursively_create_nodes(child_prim, parent_dcc_node)

    # Import loop
    stage = hook.open_import_stage(path=path)
    hook.pre_stage_import(stage=stage)
    recursively_create_nodes(stage.GetPseudoRoot())
    hook.post_stage_import(stage=stage)

This loop does not do anything but to define hooks to be called at certain time during the lifecycle of an import:

  • open a stage
  • allow importers to initialize themselves after a stage is imported
  • recursively walk the USD tree to create prim
  • post process a prim (handle custom metadata, apply materials)
  • cleanup after the stage is imported

An implementation of a hook could look like this, for camera for example:

@hookimpl
def create_node(prim, parent_dcc_node):
"""Camera USD -> DCC importer hook implementation"""
    usd_camera = UsdGeom.Camera(prim)

    - Leave if the prim cannot be handled by this importer
    - Another importer might handle this prim, or it won't
    # be imported at all

    if not usd_camera:
        return None

    dcc_camera = build_dcc_camera()
    set_dcc_camera_from_usd_camera(usd_camera, dcc_camera)
  • Is the current prim to be imported a camera?
  • No ? I return None and let another importer deal with this prim.
  • Yes ? I create a new camera in my DCC and transfer the USD attributes to it. Other importers won't be called.

By the way, this stuff is based on pluggy .

I personally don't have a preference for the API style (hook based vs schema based). I've the feeling that being hook based allows for more flexibility, while Schema based could be more performant as it'd be faster to dispatch import code based on the presence of an Api (rather than the "is my UsdGeomCamera valid ?" of my hooks).

I've also seen in other DCCs that the creation of the prims happens at the Sdf level, not the Usd level, for performance reasons:

  • a first hook receives a layer, not a stage, to create a prim
  • another hook receives an UsdPrim so it can leverage the USD SchemaAPI system

That's quite neat, I think.

Another point to note is that for the DCCs I've seen (Maya and 3ds Max), those customizations are not DCC plugins, but USD plugins. The custom code is actually registered through the USD plugin system, not the DCC's.

The following description was provided by @charlesfleche on the `pipeline-assets-io-module` Blender chat channel on 22 Sept 2022. --- We want to support our custom metadata and attributes for a given prim, import and export. A few use cases: - ID to bind a Blender object to our game engine, so we can import in blender / edit / export to our database. - In games, skeletons are usually made of several smaller skeletons: one for the main body, but others for the weapon, clothes, accessories, etc... Those are stored as different objects in our game database, so you mix and match (think about those character creators where you choose the hair, the body, the eyes, the hat of your character...). We need to tag in USD the owner of each bone (with our custom schemas), store this info in Blender, and reserialize it to USD on the way back to the engine. - USD doesn't have a definite schema to define an object transform and its offset transform. We express those as two transforms ops in USD, but Blender can't make sense of those at the moment, so we should be able to post process an object to handle our custom schemas and apply those transforms. - Imagine a new object is created in Blender, then published to the game engine (we have a network API for that purpose). The response we get contains the new IDs of the objects in the Engine, and we need to assign those IDs to existing blender objects in the Blender scene. So far we've seen two approaches to customize USD I/O in DCCs: - The Maya one, based on [Schema translators](https://github.com/Autodesk/maya-usd/blob/dev/lib/mayaUsd/fileio/doc/SchemaAPI_Import_Export_in_Python.md). Here you register a schema and call some code when it is found while walking the Prims at import time. - Our own, based on hooks in the import / export loops: ``` def import_to_dcc(path): def recursively_create_node(prim, parent_dcc_node=None): if not prim.IsPseudoRoot(): node = hook.create_node(prim=prim, parent_dcc_node=parent_dcc_node) if node: hook.post_create_node(prim=prim, node=node) parent_dcc_node=node for child_prim in prim.GetChildren(): recursively_create_nodes(child_prim, parent_dcc_node) # Import loop stage = hook.open_import_stage(path=path) hook.pre_stage_import(stage=stage) recursively_create_nodes(stage.GetPseudoRoot()) hook.post_stage_import(stage=stage) ``` This loop does not do anything but to define hooks to be called at certain time during the lifecycle of an import: - open a stage - allow importers to initialize themselves after a stage is imported - recursively walk the USD tree to create prim - post process a prim (handle custom metadata, apply materials) - cleanup after the stage is imported An implementation of a hook could look like this, for camera for example: ``` @hookimpl def create_node(prim, parent_dcc_node): """Camera USD -> DCC importer hook implementation""" usd_camera = UsdGeom.Camera(prim) - Leave if the prim cannot be handled by this importer - Another importer might handle this prim, or it won't # be imported at all if not usd_camera: return None dcc_camera = build_dcc_camera() set_dcc_camera_from_usd_camera(usd_camera, dcc_camera) ``` - Is the current prim to be imported a camera? - No ? I return None and let another importer deal with this prim. - Yes ? I create a new camera in my DCC and transfer the USD attributes to it. Other importers won't be called. By the way, this stuff is based on [pluggy ](https://pluggy.readthedocs.io/en/stable/). I personally don't have a preference for the API style (hook based vs schema based). I've the feeling that being hook based allows for more flexibility, while Schema based could be more performant as it'd be faster to dispatch import code based on the presence of an Api (rather than the "is my `UsdGeomCamera` valid ?" of my hooks). I've also seen in other DCCs that the creation of the prims happens at the `Sdf` level, not the `Usd` level, for performance reasons: - a first hook receives a layer, not a stage, to create a prim - another hook receives an `UsdPrim` so it can leverage the `USD SchemaAPI` system That's quite neat, I think. Another point to note is that for the DCCs I've seen (Maya and 3ds Max), those customizations **are not DCC plugins**, but **USD plugins**. The custom code is actually registered through the USD plugin system, not the DCC's.
Author
Member

Added subscribers: @charlesfleche, @makowalski

Added subscribers: @charlesfleche, @makowalski
Author
Member

Added subscribers: @dr.sybren, @SonnyCampbell_Unity

Added subscribers: @dr.sybren, @SonnyCampbell_Unity
Author
Member

Added subscriber: @BrianSavery

Added subscriber: @BrianSavery

Added subscriber: @Zhen-Dai

Added subscriber: @Zhen-Dai
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk

Added subscriber: @GeorgiaPacific

Added subscriber: @GeorgiaPacific
Author
Member

Added subscriber: @mont29

Added subscriber: @mont29
Bastien Montagne added this to the Pipeline, Assets & IO project 2023-02-09 15:39:30 +01:00
Bastien Montagne modified the project from Pipeline, Assets & IO to USD 2023-02-10 11:08:11 +01:00
Iliya Katushenock removed the
Status
Needs Triage
label 2023-08-24 17:33:27 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#101283
No description provided.