Search code examples
pythonblendermaya

Write blendshape weights for object per frame in maya to text file


I'm a blender novice and have been using the following script to dump all the blendshape weights per frame for an object into a text file - each new line bring a frame in the animation sequence.

import bpy
sce = bpy.context.scene
ob = bpy.context.object

filepath =  "blendshape_tracks.txt"

file = open(filepath, "w")

for f in range(sce.frame_start, sce.frame_end+1):
    sce.frame_set(f)
    vals = ""

    for shapeKey in bpy.context.object.data.shape_keys.key_blocks:

        if shapeKey.name != 'Basis':

            v = str(round(shapeKey.value, 8)) + " "
            vals += v        

    vals = vals[0:-2]    
    file.write(vals + "\n");

As you can see this is super easy in Blender but now I'm trying to do the same thing in Maya. Beforehand I attempted to just bring the 3d model over in different formats; DAE and FBX (tried both ascii and bin and different year versions) but Blender just won't import them (receive numerous errors each time).

So basically what I am asking is how to do this same thing in maya via python or MEL? I checked out the motion builder api but haven't a clue where to get started.

Cheers in advance.

Edit: Okay I figured it out. Surprisingly just as easy once you get to grips with the cmds lib.

import maya.cmds as cmds

filepath =  "blendshape_tracks.txt"
file = open(filepath, "w")
startFrame = cmds.playbackOptions(query=True,ast=True)
endFrame = cmds.playbackOptions(query=True,aet=True) 

for i in range(int(startFrame), int(endFrame)):
    vals = ""
    cmds.currentTime(int(i), update=True)

    weights = cmds.blendShape('blendshapeName',query=True,w=True)

    vals = ""
    for w in weights:
        v = str(round(w, 8)) + " "
        vals += v    
    vals = vals[0:-2]
    file.write(vals + "\n")

Solution

  • Answering own question.

    import maya.cmds as cmds
    
    filepath =  "blendshape_tracks.txt"
    file = open(filepath, "w")
    startFrame = cmds.playbackOptions(query=True,ast=True)
    endFrame = cmds.playbackOptions(query=True,aet=True) 
    
    for i in range(int(startFrame), int(endFrame)):
        vals = ""
        cmds.currentTime(int(i), update=True)
    
        weights = cmds.blendShape('blendshapeName',query=True,w=True)
    
        vals = ""
        for w in weights:
            v = str(round(w, 8)) + " "
            vals += v    
        vals = vals[0:-2]
        file.write(vals + "\n")