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

从Shotgun获取帧率后在Maya中设置无变化的问题求助

问题分析与修复方案

1. 核心问题:覆盖了Shotgun获取的帧率值

你的代码里,刚调用query_frame_rate()拿到帧率数据,立刻用frame_per_second = 16.0覆盖了该值,等于白从Shotgun取数据;同时调用playbackOptions时硬编码了fps=16.0,还注释掉了正确使用变量的行,这是设置无效的直接原因。

2. 次要问题:格式兼容与容错性不足

Shotgun返回的sg_frame_rate可能是字符串格式(比如"16fps"),直接传给Maya会不兼容;另外原函数没处理Shot和Project都未设置帧率的极端情况,容易报错崩溃。


修复后的完整代码

import maya.cmds as cmds

def query_frame_rate():
    """
    Shot level : return frame_rate value for shot if it's present
    Hierarchy level : If frame_rate is not present in shot it goes for corresponding project 
                      and then extract the frame_rate value from the Project
    """
    sht_entity = db_query.query_context("entity")
    # 一次查询获取Shot的帧率和关联Project
    shot_result = sg.find_one("Shot", [["id", "is", sht_entity["id"]]], ["sg_frame_rate", "project"])
    
    frame_rate = shot_result.get("sg_frame_rate")
    
    if not frame_rate:
        project_link = shot_result.get("project")
        if not project_link:
            raise ValueError("当前Shot未关联Project,无法获取帧率")
        
        project_result = sg.find_one("Project", [["id", "is", project_link["id"]]], ["sg_frame_rate"])
        frame_rate = project_result.get("sg_frame_rate")
        
        if not frame_rate:
            raise ValueError("Shot和对应Project都未设置sg_frame_rate")
    
    # 处理Shotgun返回的字符串格式帧率(如"16fps"转成数值16.0)
    if isinstance(frame_rate, str):
        frame_rate = float(frame_rate.replace("fps", "").strip())
    
    return frame_rate 

if __name__ == "__main__":
    try:
        frame_per_second = query_frame_rate()
        # 使用从Shotgun获取的帧率设置Maya
        cmds.playbackOptions(
            minTime=cmds.playbackOptions(q=True, minTime=True),
            maxTime=cmds.playbackOptions(q=True, maxTime=True),
            fps=frame_per_second
        )
        print(f"Done frame rate has been set to {frame_per_second}")
    except Exception as e:
        print(f"设置帧率失败: {str(e)}")

关键修复点说明

  • 移除了硬编码的frame_per_second = 16.0,保留Shotgun获取的真实值
  • 调用playbackOptions时使用变量frame_per_second而非固定数值
  • 优化查询逻辑:一次获取Shot的帧率和Project关联,避免重复查询Shot
  • 添加格式转换:自动处理Shotgun返回的字符串格式帧率
  • 增加异常捕获,处理无帧率设置的情况,避免程序崩溃
  • 将import maya.cmds移到代码开头,符合Python编码规范

内容的提问来源于stack exchange,提问作者Kartikey Sinha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 00:04:02