Is it possible to override the use default material in Maya

Is it possible to override the use default material in Maya

malcolm_341
Collaborator Collaborator
2,332 Views
16 Replies
Message 1 of 17

Is it possible to override the use default material in Maya

malcolm_341
Collaborator
Collaborator

Hi, I'm looking for a way to override the material that is drawn when you click the use default material button in Maya. Is that possible through mel? I've tried plugging my custom material into the initialShadingGroup, and that works for newly created objects, but when you press the use default material button it overrides it when lambert1 again which makes no sense. Looking for a clue on how this system works.

0 Likes
2,333 Views
16 Replies
Replies (16)
Message 2 of 17

mcw0
Advisor
Advisor

I've not tried this but maybe delete "lambert1" and rename your node as "lambert1".

0 Likes
Message 3 of 17

malcolm_341
Collaborator
Collaborator

lambert1 is undeletable, and when you press the use default material button and then edit lambert one only some of the changes show up, for example diffuse colours shows up, but plugging a texture in doesn't work?

0 Likes
Message 4 of 17

mcw0
Advisor
Advisor

I was afraid "lambert1" was undeletable.  Wasn't aware that it would disconnect any connections.  It's almost as if it remakes the shader...but you say the color stays.  Can you just rename "lambert1" to  something else.  Then name your node as "lambert1"?  But then it would disconnect any textures...so that won't work.  You might look into a scriptJob that reconnects your textures.  I don't know what event or condition would be appropriate for this.

0 Likes
Message 5 of 17

mcw0
Advisor
Advisor

Another thought...lock all attributes so they can't be disconnected.  

0 Likes
Message 6 of 17

malcolm_341
Collaborator
Collaborator

The actual issue is when you press the use default material even if you edit lambert1 the changes don't show in the viewport, except colour sliders. It does feel like it remakes the shader from some hard coded place, or perhaps hard codes the shading model. I looked through the history and couldn't come up with any useful info in scripts others folder either.

0 Likes
Message 7 of 17

mcw0
Advisor
Advisor

Then the only other alternative is to create your own "use my material" button.  Create a switch to all the shadingEngine nodes.

0 Likes
Message 8 of 17

MrEddie
Enthusiast
Enthusiast

Encountered this annoying issue, I had wanted to flip to checkered texture (UV editor checker, not exactly what I want.)  In other scripts I have utilised the separate pipes if using Arnold.

In Shading Group

Surface material = Viewport material.

Arnold Surface Shader = Render material.

 

so you could script putting a default material into the surface material slot, and then toggling off, just delete the material. This means you don't have to mess with the intact rendering material on each object.

 

 

0 Likes
Message 9 of 17

malcolm_341
Collaborator
Collaborator

@MrEddie  Do you mean plug it into the surface shader of the initialShadingGroup? If so that's the route I took, but people didn't like that and they refused to click a button on the shelf to activate it. They wanted to use the standard UI and wanted to click the use default material button, that and overriding the surface shader only applies to newly created objects and objects that still have the default material applied. I did create another tool as well which overrides the whole scene and can swap back and forth, but it's brittle because if you override the whole scene and the artist then opens the hypershade and changes names or deletes materials there's no way for the tool to swap back to the original material for those modified materials.

0 Likes
Message 10 of 17

MrEddie
Enthusiast
Enthusiast

Hey there, I noticed my post was really not written well! 

Yes I have noticed all those limitations, the easiest way would have been if the UI 'Use default material' was connected to a shader of choice. I use a script to setup a default material on starting a scene but it's not respected by the above menu button. 

 

So what I was suggesting above: 

*Caveat, only works if you are using Arnold as it exposes a property in the SG for an arnold shader for rendering vs what you see in the viewport. Also the assumption is you are rendering in Arnold and ok with swapping out shaders for just what you see in the viewport.

MrEddie_0-1739787449485.png

 

For each material in the scene, you can rewire (1) to jump to (2) meaning it will keep the original material for rendering. They you can pop in your default shader/checkered etc into slot (1).  This would give everything a default view in the viewport but would retain the real material when rendering in Arnold. To turn it off, on all materials wire (2) back to (1). A bit brittle too, but should be fine for a personal workflow. 

 

 

 

 

 

0 Likes
Message 11 of 17

MrEddie
Enthusiast
Enthusiast

From hopping around the internet, another solution could be a new render layer with a material override applied. Switching on and off would be switching layers. That might be cleaner. Haven't tried it yet 

0 Likes
Message 12 of 17

malcolm_341
Collaborator
Collaborator

@MrEddie Thanks for your reply. Yes this is what I ended up doing a while back, making a tool which rewires the initialShadingGroup to whatever you want, but as the customer told me it's not good enough because they want to press the button in the Maya UI. As well, they want it to override the whole scene rather than just objects that have the default material applied. I still released the tool because I thought it was useful.

 

About the render layers, I know I tried this because it would be the optimal solution, but I can't remember why I couldn't get it working. Maybe I couldn't find any Mel commands to create and it render layers? I'm not sure, I'll look into it again.

0 Likes
Message 13 of 17

MrEddie
Enthusiast
Enthusiast

Render layers is now Python only but it's easy enough to script, even calling the python commands within a mel script. There are good examples in the main Maya docs. Think I'll set it up myself as have a lot of UV stuff to do at the moment. I bought and used your scripts btw, very useful. I'll share back here when done.

Message 14 of 17

malcolm_341
Collaborator
Collaborator

@MrEddie Thanks a lot for buying my scripts and supporting me. I don't know any python only Mel so you'll be able to come up with something better than me I think. 

0 Likes
Message 15 of 17

MrEddie
Enthusiast
Enthusiast

Hey there, I am not a python coder either but this script below works. You could do the checking for existing materials and renderlayers in mel first and use this to create the layer. The material and SG are hardcoded in here as "Mat_Checker_SG" and "Mat_Checker".

 

This might not be a tidy python function but it works, the python stuff is undocumented, but this helps :

https://fredrikaverpil.github.io/blog/2017/05/07/querying-render-setup-in-maya-2017/?h=render+layers...

 

 

def aed_createUVRenderLayer():
    import maya.app.renderSetup.model.override as override
    import maya.app.renderSetup.model.selector as selector
    import maya.app.renderSetup.model.collection as collection
    import maya.app.renderSetup.model.renderLayer as renderLayer
    import maya.app.renderSetup.model.renderSetup as renderSetup
    import maya.cmds as cmds 
    import maya.api.OpenMaya as OpenMaya
    cmds.undoInfo(openChunk=True)  # Start undo chunk
    try:
        # Create dummy obj to get attributes.
        rs = renderSetup.instance() 
        
     

        # Create render layer and collection
        rl = rs.createRenderLayer("UV_Layer") 
        col = rl.createCollection("allMeshes") 
        
        # Use a pattern selector to include all meshes
        col.getSelector().setPattern('*')
        
        # Set collection type to "Custom" (fixing your self-reference issue)
        col.getSelector().setFilterType(selector.Filters.kCustom)
        #  Set Custom Type field to "mesh"
        col.getSelector().setCustomFilterValue("mesh")
       # Create a material override for the collection
       # Create shading override OpenMaya.MTypeId(0x58000387)
        material_override = col.createOverride('overrideAllMaterials', OpenMaya.MTypeId(0x58000387))
       # Set the shader for the material override
        cmds.connectAttr("Mat_CheckerShader_SG.surfaceShader","overrideAllMaterials.attrValue");
        # Set render layer as renderable and switch to it
        rl.setRenderable(1)
        rs.switchToLayer(rl)
    
    except Exception as e:
        print(f"Error: {e}")
    
    finally:
        cmds.undoInfo(closeChunk=True)  # Close undo chunk

aed_createUVRenderLayer()

 

 

 

 

0 Likes
Message 16 of 17

MrEddie
Enthusiast
Enthusiast
0 Likes
Message 17 of 17

vasily.shark
Community Visitor
Community Visitor

 

This script will change the default material to Arnold (or any other)

 

"C:\Users\<you>\Documents\maya\2026\scripts\userSetup.py"

#-----------------------------------------------------------
# userSetup.py
# On new scene: assign standardSurface1 to initialShadingGroup

import maya.cmds as cmds

def assign_standard_surface():
    # ensure Arnold (and standardSurface) is available
    if not cmds.pluginInfo('mtoa', query=True, loaded=True):
        cmds.loadPlugin('mtoa')

    # if standardSurface1 exists, wire it up; otherwise create it
    if cmds.objExists('standardSurface1'):
        shader_name = 'standardSurface1'
    else:
        shader_name = cmds.shadingNode('aiStandardSurface',
                                       asShader=True,
                                       name='standardSurface1')

    # force-connect it into Maya’s built-in initialShadingGroup
    cmds.connectAttr(f'{shader_name}.outColor',
                     'initialShadingGroup.surfaceShader',
                     force=True)

# 1) Run once after Maya’s own startup (so the very first scene is handled)
cmds.evalDeferred('assign_standard_surface()', lp=True)

# 2) Also register a scriptJob so every New Scene fires the same routine
#    - "protected=True" keeps it alive even if you undo/redo, switch layouts, etc.
cmds.scriptJob(event=['NewSceneOpened', assign_standard_surface],
               protected=True)
#-----------------------------------------------------------

 

0 Likes