VrayMtl to StandardMaterial

Hey guys,

We need to tranfer a model to another software package and would like to convert our vraymtls into standardmaterials. I have tried to piece a script together based on a vew examples I have look at. I have not had any luck. The last one script is based on the build in vray scene converter by Chaos. My version is below. I think it’s close but I am noob to maxscript. Can someone give me a hand with this script?

Thanks!

-------------------------------------------------------------------------------
fn sqr x = x*x

fn convertFrom_Vray origMtl = (
    r=standardMaterial()
    r.name=origMtl.name

-- diffuse
    r.diffuse=origMtl.diffuse
    r.diffuseMap=origMtl.texmap_diffuse
    r.diffuseMapEnable=origMtl.texmap_diffuse_on
    r.diffuseMapAmount=origMtl.texmap_diffuse_multiplier

-- specular
    r.specular*(r.specularLevel*0.01)=origMtl.reflection
    r.glossiness=origMtl.hilight_glossiness

-- opacity
    r.opacityMap=origMtl.texmap_opacity
    r.opacityMapEnable=origMtl.texmap_opacity_on
    r.opacityMapAmount=origMtl.texmap_opacity_multiplier

--bump map
    r.bumpMap=origMtl.texmap_bump
    r.bumpMapEnable=origMtl.texmap_bump_on
    r.bumpMapAmount=origMtl.texmap_bump_multiplier

-- displacement
    r.displacementMap=origMtl.texmap_displacement
    r.displacementMapEnable=origMtl.texmap_displacement_on
    r.displacementMapAmount=origMtl.texmap_displacement_multiplier

return r
)
fn createstandardMaterial origMtl = (
    cls=classof origMtl
    if (cls==Multimaterial) then (
        for i=1 to origMtl.count do origMtl=createstandardMaterial origMtl
        return origMtl
    )

if (cls==Standardmaterial) then return origMtl
    if (cls==VRay) then return convertFromVray origMtl

-- unrecognized material, leave as it is
    format " Unrecognized material
"
    return origMtl
    -- r=standardMaterial()
    -- r.name=origMtl.name
    -- return r
)
fn replaceMtlNode node origMtl newMtl = (
    if ((isValidNode node) and (node.material==origMtl)) then (
        node.material=newMtl
        format " node: %
" node.name
    )
    for i=1 to node.children.count do (
        replaceMtlNode node.children origMtl newMtl
    )
)

fn replaceMtl origMtl newMtl = (
    if (origMtl!=newMtl) then replaceMtlNode rootNode origMtl newMtl
)

-- convert all scene materials to Standard materials
fn mtlsTostandardMaterial = (
    format "Converting Vray scene materials…
"
    for i=1 to sceneMaterials.count do (
        origMtl=sceneMaterials
*        format " material: %*
" origMtl
        -- create a new Standardmaterial out of VrayMtl
        vrMtl=createstandardMaterial origMtl
        -- put the material back to the scene
        replaceMtl origMtl vrMtl
    )
    format "Done
"

macroScript VRaySceneConverter category:“VRay” buttontext:“VRay Standard Material converter” tooltip:“Convert all Vray materials to Standard Materials” (
    local res=queryBox “All Vray materials will be converted to Standard materials. Are you sure?” title:“VRay Standard Material converter”
    if res then standardmaterial()
)

fn setupVRayUI = (
    registerMenuItems()
)

setupVrayUI()