You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发从Maya导出单行格式关键帧至Nuke的Python脚本需求

Maya to Nuke: Python Script for Single-Line Keyframe Export

Got it, let's solve this problem of getting Maya translation keyframes into that single-line format Nuke understands. I've put together a Python script tailored for Maya that grabs your keyframe data, formats it exactly like the example you shared, and even copies it to your clipboard for easy pasting into Nuke.

The Full Python Script

import maya.cmds as cmds

def export_maya_translate_to_nuke():
    # Get selected object in Maya
    selected_objects = cmds.ls(selection=True, long=True)
    if not selected_objects:
        cmds.warning("Please select an object with translate keyframes first!")
        return
    
    target_obj = selected_objects[0]
    translate_attrs = ["translateX", "translateY", "translateZ"]
    curve_blocks = []
    
    # Map each axis to Nuke's curve identifier (x1, y1, z1)
    axis_curve_ids = ["x1", "y1", "z1"]
    
    for attr, curve_id in zip(translate_attrs, axis_curve_ids):
        # Get all keyframe times for the attribute
        key_times = cmds.keyframe(target_obj, attribute=attr, query=True, timeChange=True)
        if not key_times:
            cmds.warning(f"No keyframes found for {attr} on {target_obj}")
            continue
        
        # Get corresponding keyframe values, formatted to 8 decimal places (matches your example)
        key_values = []
        for time in key_times:
            value = cmds.getAttr(f"{target_obj}.{attr}", time=time)
            key_values.append(f"{value:.8f}")
        
        # Combine times and values into a single list (time1, value1, time2, value2...)
        key_data = []
        for t, v in zip(key_times, key_values):
            key_data.append(str(t))
            key_data.append(v)
        
        # Build the Nuke curve block
        curve_str = f"{{curve R {curve_id} {' '.join(key_data)}}}"
        curve_blocks.append(curve_str)
    
    # Assemble the final single-line translate string
    final_output = f"translate {{ {' '.join(curve_blocks)} }}"
    
    # Print to Script Editor and copy to clipboard
    print("Nuke-compatible keyframe string:")
    print(final_output)
    cmds.copy(final_output)
    cmds.confirmDialog(title="Success", message="Keyframe string copied to clipboard!")

# Run the function
export_maya_translate_to_nuke()

How This Script Works

Let's break down the key parts so you know what's happening:

  • Object Selection Check: First, it makes sure you've selected an object in Maya—no point running it on nothing!
  • Keyframe Extraction: For each translate axis (X/Y/Z), it pulls the keyframe times and their corresponding values using Maya's keyframe and getAttr commands.
  • Format Matching: It formats the values to 8 decimal places to match the precision in your example, then pairs each time with its value in the order Nuke expects.
  • Curve Block Building: Each axis gets wrapped into a {curve R x1/y1/z1 ...} block, just like your sample format.
  • Clipboard Convenience: It copies the final single-line string to your clipboard so you don't have to manually copy-paste from the Script Editor.

Using the Script

Here's how to put this into action:

  1. Open Maya's Script Editor and switch to the Python tab.
  2. Paste the entire script into the editor.
  3. Select the object in your scene that has translate keyframes you want to export.
  4. Click the Run button (the play icon) in the Script Editor.
  5. A dialog will pop up confirming the string is copied—head over to Nuke!
  6. In Nuke, select the node you want to apply the keyframes to, find the translate attribute, right-click it, and choose Expression.... Paste the copied string into the expression field and click OK.

That's it—your keyframes should now work perfectly in Nuke, formatted exactly as you needed.

内容的提问来源于stack exchange,提问作者William Eguienta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:11:13