From efdcd482f0fff37595ae79576db058a5a4d5288b Mon Sep 17 00:00:00 2001 From: "ADS\\lanierd" Date: Thu, 14 Nov 2024 11:59:10 +0100 Subject: [PATCH 1/2] HYDRA-1291 : Fix default maya material is not picked up correctly --- .../adapters/renderItemAdapter.cpp | 4 -- lib/mayaHydra/hydraExtensions/mayaUtils.cpp | 34 ++++++++++++++++- lib/mayaHydra/hydraExtensions/mayaUtils.h | 8 ++++ .../sceneIndex/mayaHydraSceneIndex.cpp | 38 +++++++------------ .../sceneIndex/mayaHydraSceneIndex.h | 5 +-- lib/mayaHydra/mayaPlugin/renderOverride.h | 3 -- .../mayaPlugin/renderOverrideUtils.h | 2 - .../cpp/testMayaSceneFlattening.cpp | 3 +- .../mayaToHydra/cpp/testMayaUsdUfeItems.cpp | 2 - 9 files changed, 59 insertions(+), 40 deletions(-) diff --git a/lib/mayaHydra/hydraExtensions/adapters/renderItemAdapter.cpp b/lib/mayaHydra/hydraExtensions/adapters/renderItemAdapter.cpp index c51b49a90d..f0c578d3a7 100644 --- a/lib/mayaHydra/hydraExtensions/adapters/renderItemAdapter.cpp +++ b/lib/mayaHydra/hydraExtensions/adapters/renderItemAdapter.cpp @@ -33,11 +33,7 @@ #include #include -#include #include -#include -#include -#include #include diff --git a/lib/mayaHydra/hydraExtensions/mayaUtils.cpp b/lib/mayaHydra/hydraExtensions/mayaUtils.cpp index 50eb0469b2..314f8318bb 100644 --- a/lib/mayaHydra/hydraExtensions/mayaUtils.cpp +++ b/lib/mayaHydra/hydraExtensions/mayaUtils.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace { @@ -193,6 +194,37 @@ bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath) shapeDagPath.extendToShape(); return _aiSkyDomeLight == MFnDependencyNode(shapeDagPath.node()).typeName(); } - + + +MObject getDefaultMaterialShadingGroupNode() +{ + MStatus status; + + // Iterate over all shading engines + MItDependencyNodes itDep(MFn::kShadingEngine, &status); + if (status != MStatus::kSuccess) { + MGlobal::displayError("Failed to create dependency node iterator."); + return MObject::kNullObj; + } + + while (!itDep.isDone()) { + MObject shadingEngine = itDep.item(); + MFnDependencyNode shadingEngineFn(shadingEngine, &status); + if (status != MStatus::kSuccess) { + MGlobal::displayError("Failed to create dependency node function set."); + return MObject::kNullObj; + } + + // Is this shading engine the default material ? + if (shadingEngineFn.name() == "initialShadingGroup") { + return shadingEngine; + } + + itDep.next(); + } + + MGlobal::displayError("Failed to find the default material shader."); + return MObject::kNullObj; +} } // namespace MAYAHYDRA_NS_DEF diff --git a/lib/mayaHydra/hydraExtensions/mayaUtils.h b/lib/mayaHydra/hydraExtensions/mayaUtils.h index 6c9ac5ede2..cf1d55af31 100644 --- a/lib/mayaHydra/hydraExtensions/mayaUtils.h +++ b/lib/mayaHydra/hydraExtensions/mayaUtils.h @@ -185,6 +185,14 @@ MObject GetShadingGroupFromShader(const MObject& shader); */ bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath); +/** + * @brief Get the default material shading group node using the shadingEngine named "initialShadingGroup". + * + * @return a valid MObject if we succeeded finding the default material shading group or MObject::kNullObj otherwise + */ + +MObject getDefaultMaterialShadingGroupNode(); + } // namespace MAYAHYDRA_NS_DEF #endif // MAYAHYDRALIB_MAYA_UTILS_H diff --git a/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp b/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp index af577d15f2..72424aef52 100644 --- a/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp +++ b/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp @@ -24,15 +24,11 @@ #include #include #include -#include #include -#include #include #include -#include -#include +#include #include -#include #include #include #include @@ -163,7 +159,7 @@ TF_DEFINE_PRIVATE_TOKENS( SdfPath MayaHydraSceneIndex::_fallbackMaterial; SdfPath MayaHydraSceneIndex::_mayaDefaultMaterialPath; // Common to all scene indexes -VtValue MayaHydraSceneIndex::_mayaDefaultMaterialFallback;//Used only if we cannot find the default material named standardSurface1 +VtValue MayaHydraSceneIndex::_mayaDefaultMaterialFallback;//Used only if we cannot find the maya default material SdfPath MayaHydraSceneIndex::_mayaDefaultLightPath; // Common to all scene indexes SdfPath MayaHydraSceneIndex::_mayaFacesSelectionMaterialPath; // Common to all scene indexes @@ -773,26 +769,20 @@ VtValue MayaHydraSceneIndex::GetMaterialResource(const SdfPath& id) return ret.IsEmpty() ? MayaHydraMaterialAdapter::GetPreviewMaterialResource(id) : ret; } -//Create the default material from the "standardSurface1" maya material or create a fallback material if it cannot be found +//Create the default maya material or create a fallback material if it cannot be found void MayaHydraSceneIndex::CreateMayaDefaultMaterialData() { - // Try to get the standardSurface1 material - MObject defaultShaderObj; - GetDependNodeFromNodeName("standardSurface1", defaultShaderObj); // From mayautils.cpp - bool defaultMaterialSuccessfullyCreated = false; - if (MObjectHandle(defaultShaderObj).isValid()) { - //Get its shading group as it is what we use to create a material adapter - MObject defaultMaterialShadingGroupObj - = GetShadingGroupFromShader(defaultShaderObj); // From mayautils.cpp - if (MObjectHandle(defaultMaterialShadingGroupObj).isValid()) { - defaultMaterialSuccessfullyCreated = _CreateMaterial(MayaHydraSceneIndex::_mayaDefaultMaterialPath, defaultMaterialShadingGroupObj); - } - } - - if (! defaultMaterialSuccessfullyCreated){ - TF_CODING_WARNING("standardSurface1 material and its shading group could not be retrieved, using a fallback material"); - // In case we could not create the default material from the standardSurface1 material, we - // create a fallback material + bool defaultMaterialCreatedSuccessfully = false; + + MObject defaultMaterialShadingGroupObj = getDefaultMaterialShadingGroupNode(); + if (defaultMaterialShadingGroupObj != MObject::kNullObj) { + defaultMaterialCreatedSuccessfully = _CreateMaterial( + MayaHydraSceneIndex::_mayaDefaultMaterialPath, defaultMaterialShadingGroupObj); + } + + if (! defaultMaterialCreatedSuccessfully){ + TF_CODING_WARNING("maya default material and its shading group could not be retrieved, using a fallback material"); + _mayaDefaultMaterialFallback = MayaHydraSceneIndex::_CreateDefaultMaterialFallback(); auto mayaHydraDefaultMaterialDataSource = MayaHydraMaterialDataSource::New( diff --git a/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.h b/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.h index bea85a75ff..a731f9a1b2 100644 --- a/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.h +++ b/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -257,7 +256,7 @@ class MAYAHYDRALIB_API MayaHydraSceneIndex : public HdRetainedSceneIndex, public /// Is using an environment variable to tell if we should pass normals to Hydra when using the render item and mesh adapters static bool passNormalsToHydra(); - ///Create the default material from the "standardSurface1" maya material or create a fallback material if it cannot be found + ///Create the default hydra material from maya default material or create a fallback material if it cannot be found void CreateMayaDefaultMaterialData(); /// Get the maya default light path to be used in filtering scene indices to recognize the default light in primitives path @@ -342,7 +341,7 @@ class MAYAHYDRALIB_API MayaHydraSceneIndex : public HdRetainedSceneIndex, public static SdfPath _fallbackMaterial; /// _mayaDefaultMaterialPath is common to all scene indexes static SdfPath _mayaDefaultMaterialPath; - static VtValue _mayaDefaultMaterialFallback;//Used only if we cannot find the default material named standardSurface1 + static VtValue _mayaDefaultMaterialFallback;//Used only if we cannot find the maya default material /// _mayaFacesSelectionMaterialPath is a path to a Hydra material used to display the faces selection on nodes when being in components selection mode static SdfPath _mayaFacesSelectionMaterialPath; diff --git a/lib/mayaHydra/mayaPlugin/renderOverride.h b/lib/mayaHydra/mayaPlugin/renderOverride.h index cfcc5215f1..79a7140ade 100644 --- a/lib/mayaHydra/mayaPlugin/renderOverride.h +++ b/lib/mayaHydra/mayaPlugin/renderOverride.h @@ -66,10 +66,7 @@ #include #include -#include -#include #include -#include #include #include diff --git a/lib/mayaHydra/mayaPlugin/renderOverrideUtils.h b/lib/mayaHydra/mayaPlugin/renderOverrideUtils.h index 731dff012f..63f38daad7 100644 --- a/lib/mayaHydra/mayaPlugin/renderOverrideUtils.h +++ b/lib/mayaHydra/mayaPlugin/renderOverrideUtils.h @@ -20,8 +20,6 @@ #include -#include - PXR_NAMESPACE_OPEN_SCOPE class MayaHydraPreRender : public MHWRender::MSceneRender diff --git a/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaSceneFlattening.cpp b/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaSceneFlattening.cpp index 7c66d63d35..088d881bf9 100644 --- a/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaSceneFlattening.cpp +++ b/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaSceneFlattening.cpp @@ -20,7 +20,8 @@ #include -#include +#include +#include #include diff --git a/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaUsdUfeItems.cpp b/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaUsdUfeItems.cpp index 1b6a673ca4..7d595bdc44 100644 --- a/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaUsdUfeItems.cpp +++ b/test/lib/mayaUsd/render/mayaToHydra/cpp/testMayaUsdUfeItems.cpp @@ -19,8 +19,6 @@ #include -#include - #include PXR_NAMESPACE_USING_DIRECTIVE From 6938682a1f5ab725ad9a3fed310d5c328948873f Mon Sep 17 00:00:00 2001 From: "ADS\\lanierd" Date: Thu, 14 Nov 2024 17:05:58 +0100 Subject: [PATCH 2/2] Use MMaterial::defaultMaterial() --- lib/mayaHydra/hydraExtensions/mayaUtils.cpp | 111 +----------------- lib/mayaHydra/hydraExtensions/mayaUtils.h | 42 ------- .../sceneIndex/mayaHydraSceneIndex.cpp | 4 +- 3 files changed, 4 insertions(+), 153 deletions(-) diff --git a/lib/mayaHydra/hydraExtensions/mayaUtils.cpp b/lib/mayaHydra/hydraExtensions/mayaUtils.cpp index 314f8318bb..8e3521f247 100644 --- a/lib/mayaHydra/hydraExtensions/mayaUtils.cpp +++ b/lib/mayaHydra/hydraExtensions/mayaUtils.cpp @@ -18,47 +18,11 @@ #include #include -#include #include #include -#include #include #include -#include -#include - -namespace -{ - //To compute the size of an array automatically - template - constexpr std::size_t arraySize(T(&)[N]) noexcept - { - return N; - } - - ///Is an array of strings that are all maya transform attributes names - const char* kMayaTransformAttributesStrings[] = {"translateX", "translateY", "translateZ", - "rotatePivotTranslateX", "rotatePivotTranslateY", "rotatePivotTranslateZ", - "rotatePivotX", "rotatePivotY", "rotatePivotZ", - "rotateX", "rotateY","rotateZ", - "rotateAxisX", "rotateAxisY", "rotateAxisZ", - "scalePivotTranslateX", "scalePivotTranslateY", "scalePivotTranslateZ", - "scalePivotX", "scalePivotY", "scalePivotZ", - "shearXY", "shearXZ", "shearYZ", - "scaleX", "scaleY", "scaleZ", - "worldMatrix", - "localPositionX", "localPositionY", "localPosition", - "translate", "rotate", "scale" - }; - //Convert from const char* [] to MStringArray - const MStringArray transformAttrNames(kMayaTransformAttributesStrings, arraySize(kMayaTransformAttributesStrings)); - - //For visibility attributes - const char* visibilityNames[] = {"visibility"}; - - //For visibility attributes - const MStringArray visibilityAttrNames = MStringArray(visibilityNames, arraySize(visibilityNames)); -} +#include namespace MAYAHYDRA_NS_DEF { @@ -144,47 +108,6 @@ MStatus GetObjectsFromNodeNames(const MStringArray& nodeNames, MObjectArray & ou return MS::kSuccess; } -bool IsAMayaTransformAttributeName(const MString& attrName) -{ - return (-1 != transformAttrNames.indexOf(attrName)); -} - -bool IsAMayaVisibilityAttribute(const MPlug& plug, bool& outVal) -{ - //Get the visibility value from MPlug - MFnAttribute attr (plug.attribute()); - bool isVisibility = -1 != visibilityAttrNames.indexOf(attr.name()); - if (isVisibility){ - plug.getValue(outVal); - } - return isVisibility; -} - -MObject GetShadingGroupFromShader(const MObject& shader) -{ - MObject shadingGroup; - MFnDependencyNode fn(shader); - - // Get the "outColor" plug of the shader - MPlug outColorPlug = fn.findPlug("outColor", true); - - // Get the connected plugs - MPlugArray connectedPlugs; - outColorPlug.connectedTo(connectedPlugs, false, true); - - // Loop over the connected plugs - for (unsigned int i = 0; i < connectedPlugs.length(); ++i) { - MObject node = connectedPlugs[i].node(); - if (node.apiType() == MFn::kShadingEngine) // Check if the node is a shading group - { - shadingGroup = node; - break; - } - } - - return shadingGroup; -} - bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath) { static const MString _aiSkyDomeLight("aiSkyDomeLight"); @@ -195,36 +118,4 @@ bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath) return _aiSkyDomeLight == MFnDependencyNode(shapeDagPath.node()).typeName(); } - -MObject getDefaultMaterialShadingGroupNode() -{ - MStatus status; - - // Iterate over all shading engines - MItDependencyNodes itDep(MFn::kShadingEngine, &status); - if (status != MStatus::kSuccess) { - MGlobal::displayError("Failed to create dependency node iterator."); - return MObject::kNullObj; - } - - while (!itDep.isDone()) { - MObject shadingEngine = itDep.item(); - MFnDependencyNode shadingEngineFn(shadingEngine, &status); - if (status != MStatus::kSuccess) { - MGlobal::displayError("Failed to create dependency node function set."); - return MObject::kNullObj; - } - - // Is this shading engine the default material ? - if (shadingEngineFn.name() == "initialShadingGroup") { - return shadingEngine; - } - - itDep.next(); - } - - MGlobal::displayError("Failed to find the default material shader."); - return MObject::kNullObj; -} - } // namespace MAYAHYDRA_NS_DEF diff --git a/lib/mayaHydra/hydraExtensions/mayaUtils.h b/lib/mayaHydra/hydraExtensions/mayaUtils.h index cf1d55af31..df157a8caf 100644 --- a/lib/mayaHydra/hydraExtensions/mayaUtils.h +++ b/lib/mayaHydra/hydraExtensions/mayaUtils.h @@ -29,7 +29,6 @@ namespace MAYAHYDRA_NS_DEF { // Names of color tables for indexed colors const std::string kActiveColorTableName = "active"; -const std::string kDormantColorTableName = "dormant"; // Color names const std::string kLeadColorName = "lead"; @@ -118,30 +117,6 @@ bool IsUfeItemFromMayaUsd(const MDagPath& dagPath, MStatus* returnStatus = nullp MAYAHYDRALIB_API bool IsUfeItemFromMayaUsd(const MObject& obj, MStatus* returnStatus = nullptr); -/** - * @brief Is it a maya node transform attribute ? - * - * @param[in] attrName is an attribute's name. - * - * @return true if this is a maya transform attribute name, false otherwise. - */ -MAYAHYDRALIB_API -bool IsAMayaTransformAttributeName(const MString& attrName); - -/** - * @brief Is it a maya node visibility attribute ? - * - * If so then we fill the outVal with the visibility value - * - * @param[in] plug is a MPlug from an attribute. - * @param[out] outVal will contain true or false if that was a maya visibility attribute - * - * @return true if this is a maya visibility attribute name, false otherwise. - */ - -//Is it a maya node visibility attribute ? -bool IsAMayaVisibilityAttribute(const MPlug& plug, bool& outVal); - /** * @brief Set the value of a DG node attribute. * @@ -167,15 +142,6 @@ bool SetNodeAttribute(MObject node, std::string attrName, AttrType newValue) return plug.setValue(newValue); } -/** - * @brief Get the shading group MObject from a shader MObject. - * - * @param[in] shader is the MObject of the shader - * - * @return the MObject of the shading group - */ -MObject GetShadingGroupFromShader(const MObject& shader); - /** * @brief Get if this MDagPath is an Arnold sky dome light. * @@ -185,14 +151,6 @@ MObject GetShadingGroupFromShader(const MObject& shader); */ bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath); -/** - * @brief Get the default material shading group node using the shadingEngine named "initialShadingGroup". - * - * @return a valid MObject if we succeeded finding the default material shading group or MObject::kNullObj otherwise - */ - -MObject getDefaultMaterialShadingGroupNode(); - } // namespace MAYAHYDRA_NS_DEF #endif // MAYAHYDRALIB_MAYA_UTILS_H diff --git a/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp b/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp index 72424aef52..74919446de 100644 --- a/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp +++ b/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -774,7 +775,8 @@ void MayaHydraSceneIndex::CreateMayaDefaultMaterialData() { bool defaultMaterialCreatedSuccessfully = false; - MObject defaultMaterialShadingGroupObj = getDefaultMaterialShadingGroupNode(); + // Get the shading group of the default material + MObject defaultMaterialShadingGroupObj = MMaterial::defaultMaterial().shadingEngine(); if (defaultMaterialShadingGroupObj != MObject::kNullObj) { defaultMaterialCreatedSuccessfully = _CreateMaterial( MayaHydraSceneIndex::_mayaDefaultMaterialPath, defaultMaterialShadingGroupObj);