Skip to content

Commit

Permalink
HYDRA-1291 : Fix default maya material is not picked up correctly (#201)
Browse files Browse the repository at this point in the history
* HYDRA-1291 : Fix default maya material is not picked up correctly

* Use MMaterial::defaultMaterial()
  • Loading branch information
lanierd-adsk authored Nov 14, 2024
1 parent 520c8bd commit c2f4764
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 151 deletions.
4 changes: 0 additions & 4 deletions lib/mayaHydra/hydraExtensions/adapters/renderItemAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@
#include <pxr/usdImaging/usdImaging/tokens.h>

#include <maya/MAnimControl.h>
#include <maya/MDGContext.h>
#include <maya/MDGContextGuard.h>
#include <maya/MHWGeometry.h>
#include <maya/MShaderManager.h>
#include <maya/MViewport2Renderer.h>

#include <functional>

Expand Down
79 changes: 1 addition & 78 deletions lib/mayaHydra/hydraExtensions/mayaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,11 @@

#include <maya/MDagPath.h>
#include <maya/MFnDagNode.h>
#include <maya/MGlobal.h>
#include <maya/MMatrix.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MSelectionList.h>
#include <maya/MObjectArray.h>
#include <maya/MFnAttribute.h>

namespace
{
//To compute the size of an array automatically
template<typename T, std::size_t N>
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 <maya/MStringArray.h>

namespace MAYAHYDRA_NS_DEF {

Expand Down Expand Up @@ -143,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");
Expand All @@ -193,6 +117,5 @@ bool IsDagPathAnArnoldSkyDomeLight(const MDagPath& dagPath)
shapeDagPath.extendToShape();
return _aiSkyDomeLight == MFnDependencyNode(shapeDagPath.node()).typeName();
}


} // namespace MAYAHYDRA_NS_DEF
34 changes: 0 additions & 34 deletions lib/mayaHydra/hydraExtensions/mayaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
40 changes: 16 additions & 24 deletions lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@
#include <maya/MDagPath.h>
#include <maya/MDagPathArray.h>
#include <maya/MFnComponent.h>
#include <maya/MFnMesh.h>
#include <maya/MItDag.h>
#include <maya/MMatrixArray.h>
#include <maya/MObjectHandle.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MProfiler.h>
#include <maya/MObjectArray.h>
#include <maya/MMaterial.h>
#include <maya/MSelectionList.h>
#include <maya/MShaderManager.h>
#include <maya/MString.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
Expand Down Expand Up @@ -163,7 +160,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

Expand Down Expand Up @@ -773,26 +770,21 @@ 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;

// Get the shading group of the default material
MObject defaultMaterialShadingGroupObj = MMaterial::defaultMaterial().shadingEngine();
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <maya/MFrameContext.h>
#include <maya/MObject.h>
#include <maya/MSelectionList.h>
#include <maya/MViewport2Renderer.h>
#include <maya/MDrawContext.h>

#include <mayaHydraLib/api.h>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions lib/mayaHydra/mayaPlugin/renderOverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@
#include <pxr/pxr.h>

#include <maya/MCallbackIdArray.h>
#include <maya/MMessage.h>
#include <maya/MObjectHandle.h>
#include <maya/MString.h>
#include <maya/MViewport2Renderer.h>

#include <atomic>
#include <chrono>
Expand Down
2 changes: 0 additions & 2 deletions lib/mayaHydra/mayaPlugin/renderOverrideUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

#include <pxr/pxr.h>

#include <maya/MViewport2Renderer.h>

PXR_NAMESPACE_OPEN_SCOPE

class MayaHydraPreRender : public MHWRender::MSceneRender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

#include <pxr/imaging/hd/tokens.h>

#include <maya/MViewport2Renderer.h>
#include <maya/MDagPath.h>
#include <maya/MMatrix.h>

#include <gtest/gtest.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#include <pxr/imaging/hd/tokens.h>

#include <maya/MViewport2Renderer.h>

#include <gtest/gtest.h>

PXR_NAMESPACE_USING_DIRECTIVE
Expand Down

0 comments on commit c2f4764

Please sign in to comment.