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

Maya Python脚本问题:批量导出选中对象及生成缩略图异常

Maya Python脚本修复:批量导出对象并生成缩略图

需求

将选中的每个对象导出到指定路径的单独文件中,同时为每个对象执行:

  • 隔离显示
  • 视图适配
  • 生成可用于内容浏览器的Maya缩略图

现有问题

  1. 无法正确隔离对象
  2. 仅为最后一个选中对象生成缩略图
  3. cmds.thumbnailCaptureComponent弹出弹窗,无法自动生成缩略图

原脚本代码

import maya.cmds as cmds
import os

# Get a list of selected objects
selected_objects = cmds.ls(selection=True)

if len(selected_objects) == 0:
    cmds.warning("Please select and object to export")

else:
    # Prompt the user for the location to export the files
    export_location = cmds.fileDialog2(fm=3, dir=os.path.expanduser('~'))[0]
    
    # Iterate over the selected objects
    for object in selected_objects:

        # Select object
        cmds.select(object)
        
        # Isolate the current object
        cmds.isolateSelect('modelPanel1', loadSelected=True)
        
        # Take a thumbnail of the current object
        cmds.viewFit(object)
        
        # Create thumbnail capture
        cmds.thumbnailCaptureComponent(capture=True, save=export_location + "/" + object + ".ma")
           
        # Use the file command to export the object
        cmds.file( export_location + "/" + object + ".ma", type='mayaAscii', exportSelected=True, force=True )
        
        # Reset the isolation
        cmds.isolateSelect('modelPanel1', state=False)

修复后的脚本代码

import maya.cmds as cmds
import os

# 获取选中对象列表
selected_objects = cmds.ls(selection=True, long=False)  # 使用短名称避免路径冲突

if not selected_objects:
    cmds.warning("请选择要导出的对象")
else:
    # 选择导出路径
    export_location = cmds.fileDialog2(fm=3, dir=os.path.expanduser('~'))
    if not export_location:  # 处理用户取消选择的情况
        cmds.warning("已取消导出操作")
    else:
        export_location = export_location[0]
        # 确保目标目录存在
        if not os.path.exists(export_location):
            os.makedirs(export_location)
        
        # 获取当前激活的模型面板(避免硬编码面板名导致的兼容性问题)
        model_panel = cmds.getPanel(withFocus=True)
        if cmds.getPanel(typeOf=model_panel) != 'modelPanel':
            model_panel = 'modelPanel1'  #  fallback到默认面板
        
        for obj in selected_objects:
            # 选中当前对象
            cmds.select(obj, replace=True)
            
            # 开启隔离显示并加载当前选中对象
            cmds.isolateSelect(model_panel, state=True)
            cmds.isolateSelect(model_panel, loadSelected=True)
            
            # 适配视图到对象并强制刷新,确保视图更新完成
            cmds.viewFit(obj, all=False)
            cmds.refresh(full=True)
            
            # 生成缩略图(替换为thumbnailCapture,关闭弹窗)
            thumbnail_path = os.path.join(export_location, f"{obj}.ma.jpg")
            cmds.thumbnailCapture(
                capture=True,
                save=thumbnail_path,
                showDialog=False,
                width=256,
                height=256
            )
            
            # 导出对象并关联缩略图
            export_path = os.path.join(export_location, f"{obj}.ma")
            cmds.file(
                export_path,
                type='mayaAscii',
                exportSelected=True,
                force=True,
                saveThumbnail=True  # 让Maya自动关联缩略图到导出文件
            )
            
            # 关闭隔离显示
            cmds.isolateSelect(model_panel, state=False)
        
        cmds.select(selected_objects)  # 恢复初始选中状态

修复说明

  1. 隔离显示修复:
    • 先通过state=True开启隔离模式,再用loadSelected=True加载当前对象,确保隔离规则生效
    • 自动获取当前激活的模型面板,避免硬编码modelPanel1导致的面板不匹配问题
  2. 缩略图生成修复:
    • 替换thumbnailCaptureComponent为thumbnailCapture(后者专门用于捕获视图缩略图,更适配需求)
    • 添加showDialog=False参数关闭弹窗,实现自动生成
    • 在viewFit后调用cmds.refresh(full=True),确保视图更新完成后再捕获,解决仅最后一个对象生成缩略图的问题
  3. 其他优化:
    • 处理用户取消路径选择的边界情况
    • 自动创建不存在的导出目录
    • 导出后恢复初始选中状态
    • 使用os.path.join拼接路径,避免跨平台路径分隔符错误
    • 添加saveThumbnail=True让Maya自动关联缩略图与导出文件,确保内容浏览器能正确识别

内容的提问来源于stack exchange,提问作者Harry Latham

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 00:05:27