美文网首页狮猿社Max
批量渲染场景

批量渲染场景

作者: 锦囊喵 | 来源:发表于2019-12-07 21:47 被阅读0次

    此脚本可以设置批量渲染场景物体。

    大致思路如下:

    1. 设置渲染输出路径
    2. 批量渲染
    • 设置渲染器(VRay 或Scanline)
                if chk_Render.state==true then
                (
    --              varScan = "*mental_ray*"
                    varScan = "*Scanline*"
                    checkRender varScan
                )
    
      • checkRender :
        fn checkRender nSearch = -- function check if render installed 
        (
            theRenderer = for obj in RendererClass.classes where \
            (matchPattern (obj as string) pattern:nSearch)
            collect obj
            if theRenderer.count ==1
            then renderers.current = theRenderer[1]()
            else 
            (messageBox "render not installed")
        )
    
        • 下面的matchPattern 非常关键,用法比较特别,可以看成一种简单的正则表达式

    matchPattern (obj as string) pattern:nSearch

    • 初始化Viewport
                viewport.ResetAllViews()
                viewport.setType #view_iso_user
    
    • 初始化路径
                strApppath = GetAppPath()   
    
    • 初始化其他相关参数
                obj = #()
                cnt=geometry.count
                i=0             
                for j in geometry  where (canConvertTo j Editable_Mesh) do 
                (
                    append obj j 
                    i = i+1
                    Export_Prog.value = 100.*(i+1)/cnt
                )
                
                Export_Prog.value = 0               
                cnt=obj.count
                i=0             
                
                patharr=filterString thepath "'\'"
                newpath=""
                for pathitem in patharr do
                (
                    newpath=newpath+pathitem+"\\"+"\\"
                )
                
                strFilenames=""
    
    • 紧跟着是一个while
                while obj.count>0 do
                (
                    gc()
                    freescenebitmaps() 
                    p=1
                    clearselection()
                    unhide objects
                    select obj[1]
    
                    if obj[1].parent == undefined then
                    (
                        transformation = obj[1].transform
                        obj[1].transform = (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
                    )
                    else
                    (
                        try
                        (
                            transformation = obj[1].parent.transform
                            obj[1].parent.transform = (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
                        )
                        catch()
                    )
    
                    
                    for m=obj.count to 2 by -1 where(obj[1].name == obj[m].name and obj[1].material == obj[m].material) do
                    (                           
                        p=p+1                       
                        deleteitem obj m
                    )
                    
                    
    --              IsolateSelection.EnterIsolateSelectionMode() --适用于max2013+
                    
                    
                    actionMan.executeAction 0 "281"  -- Tools: 隐藏未选定对象  适用于max2012-             
                    max zoomext sel--适用于max2012-
    --              max quick render--测试用                   
                    strFilename=(obj[1].name as string) +"."+( p as string) + "." +( obj[1].material.name as string)+ ".png"
                    outputfilepath=newpath + strFilename
                    bm=render outputwidth:renderWidth outputheight:renderHeight outputFile:outputfilepath quiet:true -- progressbar:true 
                    close bm                
                    
                    Export_Prog.value =100. - 100.*(obj.count)/cnt
    
                    if obj[1].parent == undefined then
                    (
                        obj[1].transform = transformation
                    )
                    else
                    (                       
                        try(obj[1].parent.transform = transformation)--(matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0]))
                        catch()
                    )
                    deleteitem obj 1
                    strFilenames=strFilenames+strFilename+";"
                )
    
                Export_Prog.value = 0       
                
                dotnet.loadAssembly (strApppath+"PNGFactory.dll")--as string)
                PNGFactory=dotNetObject "PNGFactory.PNGWorkbook" strApppath thepath strFilenames
                PNGFactory.PNGTreating()
            )
    
      • 以下两句用于内存释放
                    gc()
                    freescenebitmaps() 
    
      • 判断物体是否在group内部
                    if obj[1].parent == undefined then
    
      • 为防止改变数组内容造成的变化,以下的for采用逆序
                    for m=obj.count to 2 by -1 where(obj[1].name == obj[m].name and obj[1].material == obj[m].material) do
                    (                           
                        p=p+1                       
                        deleteitem obj m
                    )
    
      • 以下为maxscript调用.net dll 写法
                dotnet.loadAssembly (strApppath+"PNGFactory.dll")--as string)
                PNGFactory=dotNetObject "PNGFactory.PNGWorkbook" strApppath thepath strFilenames
                PNGFactory.PNGTreating()
    

    完整代码如下:

    try(destroyDialog Rollout_BOM)catch()
    
    rollout Rollout_BOM "BOM Rollout"
    (       
        global thepath
        group "BOM Export"
        (   
            progressbar Export_Prog color:blue
            
            checkbox chk_Render "Default Render" checked:false align:#left width:120
    
            label label_item_IN "No Directory Selected!" align:#center style_sunkenedge:true height:50 width:135 
            button DIR_Output "Output" across:2 align:#left width:64 --width:64--across:2 align:#left
    
            button   Exporting "Export"  align:#right enabled:false width:64
        )
        
        fn checkRender nSearch = -- function check if render installed 
        (
            theRenderer = for obj in RendererClass.classes where \
            (matchPattern (obj as string) pattern:nSearch)
            collect obj
            if theRenderer.count ==1
            then renderers.current = theRenderer[1]()
            else 
            (messageBox "render not installed")
        )
        
        fn GetAppPath =
        (
            strpath=getThisScriptFilename()--????script??
            patharr=filterString strpath "\\"
            strpath=""--patharr[1]
        
    
            if (patharr.count > 1) then
            (
                for i=1 to patharr.count-1 do
                (
                    strpath=strpath+patharr[i]+"\\"--+"\\"
                )
            )
            return strpath
        )
    
        on Exporting pressed do
        (   
            start = timeStamp()
            format "http://shop.zbj.com/22705164/\n" 
            format "Code by LeoZ\n" 
            format "            \n" 
            s=getUniversalTime()
            
            if (blimit s) then 
            (
                with redraw off 
                undo off 
                
                if chk_Render.state==true then
                (
    --              varScan = "*mental_ray*"
                    varScan = "*Scanline*"
                    checkRender varScan
                )
    
                viewport.ResetAllViews()
                viewport.setType #view_iso_user
                
                strApppath = GetAppPath()   
    
                
                obj = #()
                cnt=geometry.count
                i=0             
                for j in geometry  where (canConvertTo j Editable_Mesh) do 
                (
                    append obj j 
                    i = i+1
                    Export_Prog.value = 100.*(i+1)/cnt
                )
                
                Export_Prog.value = 0               
                cnt=obj.count
                i=0             
                
                patharr=filterString thepath "'\'"
                newpath=""
                for pathitem in patharr do
                (
                    newpath=newpath+pathitem+"\\"+"\\"
                )
                
                strFilenames=""
                
                while obj.count>0 do
                (
                    gc()
                    freescenebitmaps() 
                    p=1
                    clearselection()
                    unhide objects
                    select obj[1]
    
                    if obj[1].parent == undefined then
                    (
                        transformation = obj[1].transform
                        obj[1].transform = (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
                    )
                    else
                    (
                        try
                        (
                            transformation = obj[1].parent.transform
                            obj[1].parent.transform = (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
                        )
                        catch()
                    )
    
                    
                    for m=obj.count to 2 by -1 where(obj[1].name == obj[m].name and obj[1].material == obj[m].material) do
                    (                           
                        p=p+1                       
                        deleteitem obj m
                    )
                    
                    
    --              IsolateSelection.EnterIsolateSelectionMode() --适用于max2013+
                    
                    
                    actionMan.executeAction 0 "281"  -- Tools: 隐藏未选定对象  适用于max2012-             
                    max zoomext sel--适用于max2012-
    --              max quick render--测试用                   
                    strFilename=(obj[1].name as string) +"."+( p as string) + "." +( obj[1].material.name as string)+ ".png"
                    outputfilepath=newpath + strFilename
                    bm=render outputwidth:renderWidth outputheight:renderHeight outputFile:outputfilepath quiet:true -- progressbar:true 
                    close bm                
                    
                    Export_Prog.value =100. - 100.*(obj.count)/cnt
    
                    if obj[1].parent == undefined then
                    (
                        obj[1].transform = transformation
                    )
                    else
                    (                       
                        try(obj[1].parent.transform = transformation)--(matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0]))
                        catch()
                    )
                    deleteitem obj 1
                    strFilenames=strFilenames+strFilename+";"
                )
    
                Export_Prog.value = 0       
                
                dotnet.loadAssembly (strApppath+"PNGFactory.dll")--as string)
                PNGFactory=dotNetObject "PNGFactory.PNGWorkbook" strApppath thepath strFilenames
                PNGFactory.PNGTreating()
            )
            end = timeStamp()
            format " % seconds!\n" ((end - start) / 1000.0)
            clearselection()
            unhide objects
            max zoomext sel
        )
    
    
        on DIR_Output pressed do
        (   
            thepath=getSavePath caption:"Select the OutPut Directory!" initialDir:(getDir #maxroot)
            if not thepath==undefined  do
            (
                label_item_IN.caption="OutPut Directory:" + thepath--+"\n being Monitored!"
                Exporting.enabled=true
            )
        )
    
    )
    createdialog Rollout_BOM pos:[100,100]
    

    相关文章

      网友评论

        本文标题:批量渲染场景

        本文链接:https://www.haomeiwen.com/subject/dxcpgctx.html