Feb 2008
1 / 5
Feb 2008
Jul 2018

How can I get all of the shaders/materials from a single mesh?

We have 2 scenarios:
1. mesh can have a single shader/material
2. mesh can have multiple shaders/materials (due to combining nodes).

This is what we do to get a single shader from a mesh

CODE
#========================================================================
def GetShaderFromMesh (mesh):
    future = mayaCmd.listHistory(str(mesh), f=1, pdo=1)

    if future is None:
        return None

    # Search for the connecting shading group
    for n in future:
        if mayaCmd.attributeQuery("surfaceShader",node=n, exists=True):
            # Get shader from incoming connection of the shading group
            shader = mayaCmd.listConnections("%s.surfaceShader" % (str(n)), source=True)

            if shader is None:
                continue

            shader = str(shader[0])

    return None

Thanks in advance!

  • created

    Feb '08
  • last reply

    Jul '18
  • 4

    replies

  • 11.2k

    views

  • 1

    user

oh python.... this is actually my special subject but in python??!!? ... well lets have a look.

here a mel example:

CODE
global proc string[] getShaders(string $input[])
{
    string $shaders[];

    // shaders can be attached to shapes even if not on a face!
    // so get only the faces
    string $faces[] = polyListComponentConversion("-tf",$input);
    if (!size($faces)) // break if there are no faces
        return $shaders;

    // now list all shadinggroups that these faces are members of
    string $connectedSGs[] = listSets -type 1 -object $faces;
    if (!size($connectedSGs)) // break if there are no shadingGroups
        return $shaders;

    // type lambert would not return shaders that
    // aren't derived from lambert1, ls -materials works better
    $shaders = ls("-materials",(listConnections -s 1 -d 0 $connectedSGs));

    return $shaders;
}

now listHistory works well. but for example: If you select all faces of a poly object and then assign a different shader, you'd get the old shadingGroup too! Even if its not connected to any surface.
As well as the surfaceShader plug is good. Thats the common case.. but what about a volumeShader? ls -materials would get any shaders and you can parse an array! Thats the key I guess :]

Oh wow, thanks! Does this work for the scenario when a material is added to the whole object?

QUOTE(Overdrive D @ 02/14/08, 01:25 AM) [snapback]281057[/snapback]
Oh wow, thanks! Does this work for the scenario when a material is added to the whole object?

Yes! absolutely :]

10 years later

Hi to all, I finded this but i need of a reverse script,select all mesh with same material without use "hyperShade -objects " command.
Can you help me?