Created
March 17, 2023 03:08
-
-
Save thomashollier/a56e68ca9d18a4ab9b12cd6d6fb5d5ff to your computer and use it in GitHub Desktop.
Create USD Unreal material binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pxr import Usd, UsdGeom, Sdf, UsdShade | |
import os | |
############## | |
# Assuming a single mesh usd file | |
# Create a material and a binding to an Unreal material location so it gets automagically assined on load | |
# Remove the existing material assignment if one is found in the file | |
# | |
############## | |
def findNodes(stage): | |
look = None | |
for x in stage.Traverse(): | |
if x.GetTypeName() == 'Mesh': | |
geo = x | |
if x.GetTypeName() == 'Material': | |
if x.GetParent().GetTypeName() == "Scope": | |
look = x.GetParent() | |
return geo, look | |
def addUnrealMtlAttrs(stage, prim, mtl): | |
primPath = prim.GetPath() | |
_material = stage.DefinePrim("%s/UnrealMaterial"% primPath, "Material") | |
_shader = stage.DefinePrim("%s/UnrealMaterial/UnrealShader"% primPath, "Shader") | |
sourceAsset= _shader.CreateAttribute('info:unreal:sourceAsset', Sdf.ValueTypeNames.Asset, variability = Sdf.VariabilityUniform) | |
sourceAsset.Set(mtl) | |
implementationSource = _shader.CreateAttribute('info:implementationSource', Sdf.ValueTypeNames.Token, variability = Sdf.VariabilityUniform) | |
implementationSource.Set('sourceAsset') | |
outputsToken = _shader.CreateAttribute('outputs:out', Sdf.ValueTypeNames.Token) | |
connect = _material.CreateAttribute("outputs:unreal:surface", Sdf.ValueTypeNames.Token) | |
connect.SetConnections([outputsToken.GetPath()]) | |
UsdShade.MaterialBindingAPI(prim).GetDirectBindingRel().SetTargets([_material.GetPath()]) | |
def processFile(srcFile, unrealMtlPath, dryRun = True): | |
print("Processing %s" % srcFile) | |
dstDir = "%s_processed" % os.path.dirname(srcFile) | |
dstFile = "%s/%s" % (dstDir, os.path.basename(srcFile)) | |
stage = Usd.Stage.Open(srcFile) | |
primMesh, primLooks = findNodes(stage) | |
if primLooks: | |
print("\tRemoving Looks at %s" % primLooks.GetPath()) | |
stage.RemovePrim(primLooks.GetPath()) | |
else: | |
primMesh.ApplyAPI(UsdShade.MaterialBindingAPI) | |
print("\tAssigning unreal mtl to %s" % primMesh.GetPath()) | |
addUnrealMtlAttrs(stage, primMesh, unrealMtlPath) | |
print("\tOutput to: %s"% dstFile) | |
if not dryRun: | |
print("\t\tExporting %s\n" % dstFile) | |
os.makedirs(dstDir, exist_ok = True) | |
stage.Export(dstFile) | |
else: | |
print("\t\tNot saving: dryRun is True\n") | |
for root, dirs, files in os.walk("./usd/cmpt/walls_v20"): | |
unrealMtlPath = "/Game/Set/mtl/mtl_env_houses.mtl_env_houses" | |
for f in files: | |
if f.find("usd")>0: | |
srcFile = os.path.join(root, f) | |
processFile(srcFile, unrealMtlPath, dryRun = 1) | |
exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LogPython: PRIM_MESH_PATH:/abcd/baseCussion_grp/baseCussion01__leather_leather_geo
LogPython: MATSG_PATH:/Looks/M_chair_body_mtlSG
LogPython: MTL_PATH:/Looks/M_chair_body_mtlSG/M_chair_body_mtl
LogPython: UNREAL MATERIALS PATH:/Game/bpll/M_chair_body_mtlSG.M_chair_body_mtlSG
line 27 sourceAsset.Set(mtl) == > i got an error
LogPython: Error: File "", line 27, in addUnrealMtlAttrs
LogPython: Error: pxr.Tf.ErrorException:
LogPython: Error: Error in 'pxrInternal_v0_21__pxrReserved__::UsdStage::_SetValueImpl' at line 5981 in file C:\Users\MattJohnson\Developer\USD\pxr\usd\usd\stage.cpp : 'Type mismatch for </Looks/M_chair_body_mtlSG/M_chair_body_mtl.info:unreal:sourceAsset>: expected 'SdfAssetPath', got 'TfPyObjWrapper''