学习用MaxScipt批处理Max文件

学习用MaxScipt批处理Max文件

需求

对几百个.max文件中的指定指定名称的骨骼进行重命名。

解决

考虑到是一次性需求,花了两个钟用maxscript实现了功能,基本逻辑把改名规则做成配置文本,然后一个个加载文件夹中的max档更加配置给节点改名。

为了方便以后使用,又用winform写了个带详细使用说明的界面,这个界面可以编辑配置文件,指定批处理文件夹,最后通过cmd来启动3dsmax执行这个脚本:

MaxScript:

-- by Tongyun Liu
-- 2017-4-27
----------------- define variables -------------------------------
logpath = @"D:\BoneRename.txt"
inipath = @"D:\RenameLog.ini"
batchpath = @"D:\batch_Directories.ini"

outputLogs = #()
inifile = openfile inipath

----------------- define functions -------------------------------
fn fn_RenameAllMax =
(
    batchfile = openfile batchpath
    while not eof inifile do
    (
        iniLine = #()
        iniLine = filterString (readline inifile) "="

        if(iniLine!= undefined) then
        (
            len = iniLine.count
            if(len == 2) then
            (
                oldName = trimRight iniLine[1] "\t"
                newName = trimLeft iniLine[2] "\t"
                if(oldName !=undefined) and (oldName !=undefined)and(oldName !=undefined) then
                (
                    bobj = execute("$'"+oldName+"'")
                    for o in objects
                        where (classof(o) == biped_object) or (classof(0) == BoneGeometry) do
                    (
                        if((bobj != undefined) and (o.name == bobj.name)) then
                        (
                            append outputLogs ("\t["+oldName+"]\t\tRenameTo:\t\t["+newName+"]")
                            bobj.name = newName
                        )
                    )
                )
            )
        )
    )
    return 1
)

--------------------------------- batch rename -------------------------------------
while not eof batchfile do
(
    maxpathline = readline batchfile
    if(maxpathline != "")and((findstring maxpathline ".max") != undefined) then
    (
        loadmaxfile maxpathline
        append outputLogs ("MaxFileName: "+maxpathline + "\t\t"+localtime)
        func_rename = fn_RenameAllMax()
        savemaxfile maxpathline clearNeedSaveFlag:true quiet:true
    )
)

close inifile
close batchfile

---------------------- output logs --------------------------------------------
append outputLogs ("\n-------------------------------------------------------")
logfile = openfile logpath mode:"a+"
print outputLogs to: logfile
close logfile
shelllaunch "notepad.exe" logpath

quitmax #noPrompt

命令行启动和操作3dsmax

在winform中启动max并执行maxScript的方法:

private void RunCmd()
{
    Process process =  new Process();
    string maxExePath = "3dsmax.exe所在的文件夹路径";
    string maxScriptPath = "需要执行maxscript脚本的路径";
    process.StartInfo.WorkingDirectory = maxExePath;
    process.StartInfo.FileName = "3dsmax.exe"
    process.StartInfo.Arguments = " -silent -mip -u MAXScript " + maxScriptPath;

    process.Start();
    process.WaitForExit();

    if(process.HasExited)
    {
        process.Close();
    }
}

猜你喜欢

转载自blog.csdn.net/ltycloud/article/details/79365523
今日推荐