美文网首页狮猿社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]

相关文章

  • 批量渲染场景

    此脚本可以设置批量渲染场景物体。 大致思路如下: 设置渲染输出路径 批量渲染 设置渲染器(VRay 或Scanli...

  • Shader笔记——渲染流水线

    1.0 3D图形渲染流水线 一个通用渲染流水线的渲染流程,如下: 建立场景:在真正开始渲染之前,需要对整个场景惊喜...

  • 三维技术探索-Three.js开发指南

    一、场景搭建 1、使用three.js搭建三维场景需要的基本要素 渲染器与场景 (1、)渲染器(renderer)...

  • Cocos 3.x 由浅到浅入门批量渲染

    参考由浅到浅入门批量渲染(一)[https://gameinstitute.qq.com/community/de...

  • 学习并实现react (2)

    组件列表渲染场景 DOM复用 react 的重点在于首次渲染和更新渲染,现在考虑更新渲染如何复用DOM 让渲染更有...

  • WebGL 基础

    webgl 场景创建 渲染render (scene, camera)setViewport() 场景 相机cam...

  • 动态合批和静态合批的区别

    参考文章:图形渲染及优化—Unity合批技术实践图形渲染及优化—Batch 批是啥?对某对象进行批量处理叫批处理 ...

  • 场景起步

    引入three.js 新建场景 新建相机 添加renderer 渲染到html 执行渲染

  • python批量请求注册接口&爬虫相关问题记录

    背景批量生成账号,依次循环给每个帐号模拟真实注册场景,添加appname和deviceid,然后登录 批量生成账号...

  • OC实时查看器设置0002

    1-发送场景并开始新的渲染:使用此命令将场景发送到Octane。每次该命令都会收集场景数据,并从头开始渲染过程。使...

网友评论

    本文标题:批量渲染场景

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