Fix #107633: USD import: duplicate shader nodes #107945

Merged
Michael Kowalski merged 4 commits from makowalski/blender:usd-import-duplicate-shaders into main 2023-05-16 17:46:32 +02:00

This pull request addresses bug #107633

Added logic to avoid creating duplicate Blender shader nodes when
converting USD Preview Surface shaders that are connected to more
than one input.

This change adds a NodePlacementContext::node_cache map to record
Blender nodes converted for given USD shaders. The node creation
logic has been updated to query this cache to check if a node was
previously created for a given USD shader, before creating a
new node.

This pull request addresses bug https://projects.blender.org/blender/blender/issues/107633 Added logic to avoid creating duplicate Blender shader nodes when converting USD Preview Surface shaders that are connected to more than one input. This change adds a `NodePlacementContext::node_cache` map to record Blender nodes converted for given USD shaders. The node creation logic has been updated to query this cache to check if a node was previously created for a given USD shader, before creating a new node.
Michael Kowalski added the
Interest
Pipeline, Assets & IO
Interest
USD
labels 2023-05-15 18:55:58 +02:00
Michael Kowalski added 1 commit 2023-05-15 18:56:08 +02:00
4e1f5cd701 USD import: Fix duplicate shader nodes
Added logic to avoid creating duplicate Blender shader nodes when
converting USD Preview Surface shaders that are connected to more
than one input.

This change adds a `NodePlacementContext::node_cache` map to record
Blender nodes converted for given USD shaders.  The node creation
logic has been updated to query this cache to check if a node was
previously created for a given USD shader, before creating a
new node.
Michael Kowalski added this to the USD project 2023-05-15 18:56:33 +02:00
Michael Kowalski requested review from Matt McLin 2023-05-15 18:56:51 +02:00
Michael Kowalski requested review from Jesse Yurkovich 2023-05-15 18:57:21 +02:00
Author
Member

To test, import the attached test_usd_preview_alpha.usdz and verify that the Image Texture and UV Map nodes in the imported shader network are not duplicated.

To test, import the attached `test_usd_preview_alpha.usdz` and verify that the `Image Texture` and `UV Map` nodes in the imported shader network are not duplicated.
Hans Goudey reviewed 2023-05-15 19:03:19 +02:00
@ -15,6 +15,8 @@ struct bNodeTree;
namespace blender::io::usd {
typedef std::map<pxr::SdfPath, bNode *> ShaderToNodeMap;
Member

For Blender code it's preferred to use our own container types, Map in this case (https://wiki.blender.org/wiki/Style_Guide/C_Cpp#C.2B.2B_Containers). Also, using is preferred over typedef

For Blender code it's preferred to use our own container types, `Map` in this case (https://wiki.blender.org/wiki/Style_Guide/C_Cpp#C.2B.2B_Containers). Also, `using` is preferred over `typedef`
Author
Member

Thanks for the helpful suggestions, @HooglyBoogly. I made the requested changes. As part of the change to use blender::Map, I'm now using std::string, rather than pxr::SdfPath, for the map key, to avoid a compiler error because pxr::SdfPath doesn't have a hash function defined.

Thanks for the helpful suggestions, @HooglyBoogly. I made the requested changes. As part of the change to use `blender::Map`, I'm now using `std::string`, rather than `pxr::SdfPath`, for the map key, to avoid a compiler error because `pxr::SdfPath` doesn't have a hash function defined.
Member

Just so you know, the key type doesn't have to have a .hash() member function, it can also be provided manually outside of the class. This is documented in BLI_hash.hh, in the section after There are three main ways to provide a hash table implementation with a custom hash function

Just so you know, the key type doesn't have to have a `.hash()` member function, it can also be provided manually outside of the class. This is documented in `BLI_hash.hh`, in the section after `There are three main ways to provide a hash table implementation with a custom hash function`
HooglyBoogly marked this conversation as resolved
Iliya Katushenock added the
Interest
Nodes & Physics
label 2023-05-15 19:04:57 +02:00
Michael Kowalski added 1 commit 2023-05-15 20:57:06 +02:00
9135003c96 USD material import: Use blender::Map.
Changed code to use blender::Map instead of
std::map, to conform with current coding guidelines.
Author
Member

@HooglyBoogly I'd like to merge this for 3.6 by the deadline today, if possible. Would you be available to approve this pull request, by any chance? No worries if you can't.

@HooglyBoogly I'd like to merge this for 3.6 by the deadline today, if possible. Would you be available to approve this pull request, by any chance? No worries if you can't.
Hans Goudey approved these changes 2023-05-16 15:56:17 +02:00
Hans Goudey left a comment
Member

The code looks reasonable to me. I haven't tested it though.

The code looks reasonable to me. I haven't tested it though.
@ -87,0 +93,4 @@
const pxr::UsdShadeShader &usd_shader)
{
const std::string path_str = usd_shader.GetPath().GetAsString();
if (node_cache.contains(path_str)) {
Member

Better to use lookup_ptr here, to avoid the double lookup

Better to use `lookup_ptr` here, to avoid the double lookup
Author
Member

I just updated the code to use lookup_ptr. Thanks for pointing this out.

I just updated the code to use `lookup_ptr`. Thanks for pointing this out.
makowalski marked this conversation as resolved
Michael Kowalski added 2 commits 2023-05-16 17:13:00 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
6d8f7ce7fe
USD import: call Map::lookup_ptr().
Now calling Map::lookup_ptr() to avoid double
lookup.
Author
Member

@blender-bot build

@blender-bot build
Michael Kowalski merged commit 74b5e62d2a into main 2023-05-16 17:46:32 +02:00
Michael Kowalski deleted branch usd-import-duplicate-shaders 2023-05-16 17:46:35 +02:00
Member

Sorry I was late in my review, but fyi, I did test this yesterday with a more complicated shader graph and it worked well.

Sorry I was late in my review, but fyi, I did test this yesterday with a more complicated shader graph and it worked well.
Author
Member

Sorry I was late in my review, but fyi, I did test this yesterday with a more complicated shader graph and it worked well.

No worries at all about any delay, and thanks for testing!

> Sorry I was late in my review, but fyi, I did test this yesterday with a more complicated shader graph and it worked well. No worries at all about any delay, and thanks for testing!
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:39 +02:00
Bastien Montagne removed this from the USD project 2023-08-25 23:55:07 +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
3 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#107945
No description provided.