将透明度连接到关键帧动画Maya MelPython

时间:2015-09-23 12:23:33

标签: python maya material mel

我有一个解决方法将Metrial的透明度连接到Maya中的关键帧栏。我创建了一个新的Material press“s”,关键帧到达关键帧10,将透明度设置为0并再次按“s”。

因此,您可以淡化1到10个关键帧之间的透明度。

我想在python中编写脚本,我不知道如何做到这一点。

2 个答案:

答案 0 :(得分:0)

这就是我所做的。我使用基本的Maya材质对其进行了测试,但它适用于任何具有transparency属性的材质(请注意,由于透明度属性称为outTransparency,因此它不适用于表面着色器,但是您可以通过更改函数来修复它,以便您也可以将属性名称传递给它。

我在this topic中获得了getShaderFrom (obj)函数,并在Python中将其转换(它在MEL中)。

我尽可能地简单和详细,但它可能会被优化。无论如何,希望这能回答你的问题!

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)

答案 1 :(得分:0)

这就是我所做的。

paste

lambert1行的setKeyFrame只是为了使脚本在之前没有Key存在的情况下运行。

我调整当前时间,因为如果您第一次应用它,您的素材会显示为透明。

PS:Blau在德语中是蓝色的