3DMax导出STL插件

源代码
-- STL_Export_Diagnostic.ms-- 诊断式STL导出脚本:先检测可用方法,再用最稳的方式导出global gAvailableExportMethods = #() -- 存储检测到的可用方法-- 函数1:诊断所有可能的STL导出方法fndiagnoseSTLExport = (print "====== 开始诊断STL导出功能 ======"local availableMethods = #()-- 准备一个临时测试用的方块local testBox = Box lengthsegs:1 widthsegs:1 heightsegs:1testBox.name = "__Diagnostic_Test_Box__"select testBoxlocal tempFilePath = "$temp/_diagnostic_test.stl"-- 方法A:检测标准的 STL_Exporter 类(这是之前失败的方法)print "检测方法A: STL_Exporter类..."try (if (classof STL_Exporter) != UndefinedClass then (local exporter = STL_Exporter()exporter.Binary = trueexportFile tempFilePath #noPrompt using:exporterif doesFileExist tempFilePath then (append availableMethods #("STL_Exporter类", "A")print " --> 可用") else (print" --> 类存在但导出失败")) else (print" --> 类未定义")) catch (print (" --> 错误: " + getCurrentException()) )-- 方法B:尝试使用旧版/通用的导出函数,指定格式IDprint "检测方法B: 通用ExportFile函数..."try (-- 尝试使用3ds Max的内部格式ID来导出STLexportFile tempFilePath #noPrompt using:4607142 -- 常见STL格式IDif doesFileExist tempFilePath then (append availableMethods #("通用导出函数", "B")print " --> 可用") else (print" --> 导出无报错但文件未生成")) catch (print (" --> 错误: " + getCurrentException()) )-- 方法C:调用系统菜单命令(模拟用户操作,最底层)print "检测方法C: 系统菜单命令..."try (-- 这是最底层的方法,直接调用3ds Max内部的导出命令menuMan.executeAction 0 "592" -- 文件导出命令的内部IDappend availableMethods #("系统菜单命令", "C")print " --> 可用 (将弹出系统对话框)") catch (print (" --> 错误: " + getCurrentException()) )-- 清理测试文件try ( deleteFile tempFilePath ) catch ()delete testBoxprint "====== 诊断结束 ======"return availableMethods)-- 函数2:根据诊断结果,使用最佳可用方法进行导出fn exportUsingBestMethod exportPath exportSelected:false = (if gAvailableExportMethods.count == 0 do (messageBox "未找到任何可用的STL导出方法。诊断失败。"return false)local finalMethod = gAvailableExportMethods[1] -- 使用第一个检测到的方法local methodName = finalMethod[1]local methodCode = finalMethod[2]print ("使用导出方法: [" + methodCode + "] " + methodName)-- 根据不同的方法执行导出case methodCode of ("A": ( -- 使用标准 STL_Exporter 类try (local exporter = STL_Exporter()exporter.Chord_Height = 0.0exporter.Angle_Control = 1.0exporter.Binary = trueexporter.Selected_Only = exportSelectedexportFile exportPath #noPrompt using:exporterreturn true) catch (messageBox ("方法A失败:\n" + getCurrentException())return false))"B": ( -- 使用通用导出函数try (-- 这里的数字是STL格式的内部ID,是最稳定的方式exportFile exportPath #noPrompt using:4607142return true) catch (messageBox ("方法B失败:\n" + getCurrentException())return false))"C": ( -- 系统菜单命令(会弹出对话框,需要用户操作)messageBox "将打开3ds Max标准导出对话框。\n\n请手动选择保存路径,并确保文件类型为STL(*.stl),然后点击保存。" title:"请完成导出操作"try (menuMan.executeAction 0 "592" -- 触发导出菜单return "dialog_opened" -- 特殊返回值,表示已弹出对话框) catch (messageBox ("无法打开导出对话框:\n" + getCurrentException())return false))default: (messageBox "未知的导出方法。"return false)))-- 创建用户界面try ( DestroyDialog rolSTLDiagnostic ) catch()rollout rolSTLDiagnostic "STL导出诊断工具" width:400 height:320 (local isDiagnosed = falseGroupBox grpDiagnose "第一步:诊断" pos:[10,10] width:380 height:100button btnDiagnose "点击诊断可用导出方法" pos:[20,40] width:360 height:30label lblDiagnoseResult "状态:未诊断" pos:[20,80]GroupBox grpExport "第二步:导出" pos:[10,120] width:380 height:150label lblPath "保存路径:" pos:[20,150]editText edtPath "" pos:[90,147] width:220 text:""button btnBrowse "浏览" pos:[315,147] width:65checkbox chkSelected "仅导出选中对象" pos:[20,180] checked:false enabled:falselabel lblMethod "将使用的方法:" pos:[20,210]editText edtMethod "" pos:[20,230] width:360 height:40 readOnly:truebutton btnExport "开始导出" pos:[100,280] width:100 height:30 enabled:falsebutton btnCancel "关闭" pos:[220,280] width:100 height:30-- 诊断按钮on btnDiagnose pressed do (btnDiagnose.enabled = falsebtnDiagnose.caption = "诊断中..."gAvailableExportMethods = diagnoseSTLExport()if gAvailableExportMethods.count > 0 then (local methodText = ""for m in gAvailableExportMethods do (methodText += m[1] + " [" + m[2] + "]\n")lblDiagnoseResult.text = "诊断完成:找到 " + (gAvailableExportMethods.count asstring) + " 种方法"edtMethod.text = "最佳方法: " + gAvailableExportMethods[1][1] + "\n" + methodTextbtnExport.enabled = truechkSelected.enabled = trueisDiagnosed = true) else (lblDiagnoseResult.text = "诊断完成:未找到可用方法"edtMethod.text = "未找到任何STL导出方法。\n可能原因:\n1. 3ds Max安装不完整\n2. STL导出插件已损坏\n3. 需要重新安装或修复3ds Max")btnDiagnose.caption = "重新诊断"btnDiagnose.enabled = true)-- 浏览按钮on btnBrowse pressed do (local stlPath = getSaveFileName \caption:"保存STL文件" \filename:"model.stl" \types:"STL文件(*.stl)|*.stl|所有文件(*.*)|*.*|"if stlPath != undefined do edtPath.text = stlPath)-- 导出按钮on btnExport pressed do (if not isDiagnosed do (messageBox "请先进行诊断!"return undefined)if edtPath.text == "" do (messageBox "请选择保存路径!"return undefined)local result = exportUsingBestMethod edtPath.text exportSelected:chkSelected.checkedif result == true then (messageBox "导出成功!" title:"完成") else if result == "dialog_opened" then (messageBox "系统导出对话框已打开。\n\n完成手动导出后,您可以关闭此脚本窗口。" title:"请完成保存操作") else (messageBox "导出失败。请尝试其他方法或查看错误信息。" title:"失败"))-- 取消按钮on btnCancel pressed do DestroyDialog rolSTLDiagnostic)-- 显示界面createDialog rolSTLDiagnosticprint "=================================================================="print "STL导出诊断工具已加载"print "请务必按顺序操作:"print "1. 点击'诊断'按钮,查看您的3ds Max支持哪些导出方式"print "2. 选择保存路径"print "3. 点击'导出'按钮"print "=================================================================="
夜雨聆风
