Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show bound surface materials as extra AE tabs #2470

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mayaUsd/resources/ae/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if (CMAKE_UFE_V2_FEATURES_AVAILABLE)
# We need an empty __init__.py file in each subfolder so it is considered a python module.
install(FILES __init__.py DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python/ufe_ae)
install(FILES __init__.py DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python/ufe_ae/usd)
install(FILES ae_utils.py DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python/ufe_ae/usd)
install(FILES __init__.py DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python/ufe_ae/usd/nodes)

# Maya Attribute Editor python template - just uses a single schema base template file.
Expand Down
79 changes: 79 additions & 0 deletions lib/mayaUsd/resources/ae/ae_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2022 Autodesk
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import ufe
import collections
import mayaUsd.ufe as mayaUsdUfe

from pxr import UsdShade

def getConnectedShaders(port):
"""Dig down across NodeGraph boundaries until a surface Shader is found"""
shaders = []
for sourceInfo in port.GetConnectedSources()[0]:
if sourceInfo.source.GetPrim().GetTypeName() != "Shader":
if sourceInfo.sourceType == UsdShade.AttributeType.Output:
shaders = shaders + getConnectedShaders(sourceInfo.source.GetOutput(sourceInfo.sourceName))
else:
shaders = shaders + getConnectedShaders(sourceInfo.source.GetInput(sourceInfo.sourceName))
else:
shaders.append(sourceInfo.source.GetPath())
return shaders

def getAEBoundMaterials(pathStr):
"""Return the bound materials."""
ret = []
try:
ufePath = ufe.PathString.path(pathStr)
prim = mayaUsdUfe.ufePathToPrim(pathStr)
frontPath = ufePath.popSegment()
matAPI = UsdShade.MaterialBindingAPI(prim)

if matAPI is None:
return ret

# Preserve order of discovery:
shaders = collections.OrderedDict()

for purpose in matAPI.GetMaterialPurposes():
boundInfo = matAPI.ComputeBoundMaterial(materialPurpose=purpose)
if not boundInfo or not boundInfo[0]:
continue
boundMaterial = boundInfo[0]
for output in boundMaterial.GetSurfaceOutputs():
for shader in getConnectedShaders(output):
usdSeg = ufe.PathSegment(str(shader), mayaUsdUfe.getUsdRunTimeId(), '/')
newPath = (frontPath + usdSeg)
newPathStr = ufe.PathString.string(newPath)
if newPathStr not in shaders:
shaders[newPathStr] = 1

ret = list(shaders.keys())
except Exception as exp:
print("Error getting bound materials: {exp}".format(exp=exp))

return ret

def getAERelatedNodes(pathStr):
"""Return a string array of paths that are related to pathStr and should be shown in extra AE
tabs."""
ret = []

# Add bound materials.
ret = ret + getAEBoundMaterials(pathStr)

# Add other related nodes, as required (relationships might be of interest).

return ret