0

I have a workaround to connect the transparency of a Metrial to the keyframe bar in Maya. I create a new Material press "s" for a keyframe got to the keyframe 10, set the transparency to 0 and press again "s".

So you are able to fade the transparency beteween the 1 and 10 keyframe.

I want to script this in python and I have no idea how to do this.

Edric
  • 24,639
  • 13
  • 81
  • 91
Denis H.
  • 29
  • 1
  • 3

2 Answers2

0

here is what I've made. I tested it with basic Maya materials, but it should work for any material wich has a transparency attribute (note that it won't work for surface shaders as the transparency attribute is called outTransparency, but you could fix it by changing the function so you could also pass it the attribute name).

I got the getShaderFrom (obj) function in this topic and just converted it in Python (it's in MEL).

I made it as simple and detailled as possible but it could probably be optimized. Anyway, hope this answered your question!

from maya import cmds

def getShaderFrom (obj):
    '''
    List all materials for the given object
    '''
    # List the shapes of the given object
    shapes = cmds.ls (obj, objectsOnly = 1, dagObjects = 1, shapes = 1)
    # List the shading engines connected to the shapes
    shadingEngines = cmds.listConnections (shapes, type = "shadingEngine")
    # List the materails connected to the shading engines
    rawMaterials = cmds.ls ((cmds.listConnections (shadingEngines)), materials = 1)
    # Remove duplicated occurences of materials
    materials = []
    for mat in rawMaterials:
        if mat not in materials:
            materials.append(mat)

    return materials


def keyTransparency (startFrame, endFrame, material):
    '''
    Will key the transparency of
    a given materials between
    selected frames
    '''
    # Set transparency value at 1
    cmds.setAttr (material + ".transparency", 1,1,1, type = "double3")
    # Add a keyframe at last frame
    cmds.setKeyframe (material, attribute = "transparency", time = endFrame, inTangentType = "linear", outTangentType = "linear")
    # Add a keyframe at first frame
    cmds.setKeyframe (material, attribute = "transparency", time = startFrame, inTangentType = "linear", outTangentType = "linear")
    # Set transparency value at 0
    cmds.setAttr (material + ".transparency", 0,0,0, type = "double3")


def doKeyTransparencyForMaterials (startTime, endTime):
    '''
    Main function
    Call functions: 'getShaderFrom', 'keyTransparency'
    '''
    if ((not isinstance(startTime, int)) or (not isinstance(endTime, int))):
        cmds.error ("You must provide start and end frame numbers (int).")
    # List selected objects
    selection = cmds.ls (selection = 1)
    if len(selection) == 0:
        cmds.error ("Nothing is selected.")
    # List all materials
    for obj in selection:
        materials = getShaderFrom (obj)
        # Key transparency
        for mat in materials:
            keyTransparency (startTime, endTime, mat)

    print "Transparency has been successfully keyed for: " + (", ".join(materials)) + "\n",


doKeyTransparencyForMaterials (1, 10)
UKDP
  • 226
  • 5
  • 21
0

This is what I did.

import maya.cmds as cmds

######### Delete all existing Shader ############## 
cmds.delete (cmds.ls(type='shadingDependNode'))
cmds.delete (cmds.ls(type='shadingEngine'))
######### Delete all existing Shader ##############

######### Delete all Keyframes between 0 and 20 ##############
cmds.setKeyframe( 'lambert1', t='1', at='transparency'  )
cmds.cutKey ( t=(0,20) )
######### Delete all Keyframes ##############

#create Blau shader
Blau = cmds.shadingNode('phong', asShader=True, n='Blau', )
cmds.setAttr(Blau + '.color', 0.163,0.284,0.5)


#create KeyFrame Transparency ###Blau####
cmds.setKeyframe( 'Blau', t='1', at='transparency'  )
cmds.setAttr ( 'Blau.transparency', 1,1,1, type = "double3")
cmds.setKeyframe( 'Blau', t='15', at='transparency' )

######### update Time to 2 and back to 0  ##############
cmds.currentTime ( 2, update=True )
cmds.currentTime ( 0, update=True )
######### update Time to 2 and back to 0  ##############

The setKeyFrame for lambert1 line is just to make the script running if no Key exists before.

I adjust the current time because your material is shown transparent if you apply it the first time.

PS: Blau is Blue in German

Denis H.
  • 29
  • 1
  • 3