Maya V-Ray to MaxScript material converter (MEL)

Since Max doesn’t have a human readable mat format, I made a MEL script that spits out a MaxScript to use in Max for any selected material. It was originally a Unix shell script I wrote and you can see the results here:

The newer code writes out to a text file and, on OS X the results are sent to the clipboard automatically (this isn’t doable on Windows, from what I can tell). The script doesn’t do subtextures, since that’s the easy part, with all the other stuff like subdivs and Fresnel, trace depth, etc. being taken care of by the script. The script is part of my V-Ray Tuner 2 script but here’s the code if you’d rather have it on it’s own:

http://www.creativecrash.com/maya/downloads/scripts-plugins/rendering/c/v-ray-tuner-for-maya

This is for V-Ray 1.5 materials but I’ll be doing another for V-Ray 2 format soon:

global proc vray15MatConverter()
{
string $mySelection[] = `ls -sl`;
float $myDiffuse[] = `getAttr ($mySelection[0] + ".diffuseColor")`;
float $myDiffuseMax[] = {($myDiffuse[0] * 255), ($myDiffuse[1] * 255), ($myDiffuse[2] * 255)};

float $myRoughness = `getAttr ($mySelection[0] + ".roughnessAmount")`;
float $myreflectionColor[] = `getAttr ($mySelection[0] + ".reflectionColor")`;
float $myreflectionColorMax[] = {($myreflectionColor[0] * 255), ($myreflectionColor[1] * 255), ($myreflectionColor[2] * 255)};

float $myfresnelIOR = `getAttr ($mySelection[0] + ".fresnelIOR")`;
float $myrefractionIOR = `getAttr ($mySelection[0] + ".refractionIOR")`;

float $myreflectionGlossiness = `getAttr ($mySelection[0] + ".reflectionGlossiness")`;
int $myreflectionSubdivs = `getAttr ($mySelection[0] + ".reflectionSubdivs")`;
int $myuseFresnel = `getAttr ($mySelection[0] + ".useFresnel")`;
string $booluseFresnel = ($myuseFresnel == 0) ? "false" : "true";

int $myreflectionsMaxDepth = `getAttr ($mySelection[0] + ".reflectionsMaxDepth")`;
float $myreflectionExitColor[] = `getAttr ($mySelection[0] + ".reflectionExitColor")`;
float $myreflectionExitColorMax[] = {($myreflectionExitColor[0] * 255), ($myreflectionExitColor[1] * 255), ($myreflectionExitColor[2] * 255)};

int $myreflInterpolation = `getAttr ($mySelection[0] + ".reflInterpolation")`;
string $boolmyreflInterpolation = ($myreflInterpolation == 0) ? "false" : "true";
int $myrefrInterpolation = `getAttr ($mySelection[0] + ".refrInterpolation")`;
string $boolmyrefrInterpolation = ($myrefrInterpolation == 0) ? "false" : "true";
int $myhilightGlossinessLock = `getAttr ($mySelection[0] + ".hilightGlossinessLock")`;
string $boolhilightGlossinessLock = ($myhilightGlossinessLock == 0) ? "false" : "true";

int $mylockFresnelIORToRefractionIOR = `getAttr ($mySelection[0] + ".lockFresnelIORToRefractionIOR")`;
string $boolmylockFresnelIORToRefractionIOR = ($mylockFresnelIORToRefractionIOR == 0) ? "false" : "true";

float $myrefractionColor[] = `getAttr ($mySelection[0] + ".refractionColor")`;
float $myrefractionColorMax[] = {($myrefractionColor[0] * 255), ($myrefractionColor[1] * 255), ($myrefractionColor[2] * 255)};

float $myrefractionGlossiness = `getAttr ($mySelection[0] + ".refractionGlossiness")`;
int $myrefractionSubdivs = `getAttr ($mySelection[0] + ".refractionSubdivs")`;
float $myfogColor[] = `getAttr ($mySelection[0] + ".fogColor")`;
float $myfogColorMax[] = {($myfogColor[0] * 255), ($myfogColor[1] * 255), ($myfogColor[2] * 255)};

float $myfogMult = `getAttr ($mySelection[0] + ".fogMult")`;
float $myfogBias = `getAttr ($mySelection[0] + ".fogBias")`;
int $myaffectShadows = `getAttr ($mySelection[0] + ".affectShadows")`;
string $boolmyaffectShadows = ($myaffectShadows == 0) ? "false" : "true";

int $myaffectAlpha = `getAttr ($mySelection[0] + ".affectAlpha")`;
string $boolmyaffectAlpha = ($myaffectAlpha == 0) ? "false" : "true";

int $myrefractionsMaxDepth = `getAttr ($mySelection[0] + ".refractionsMaxDepth")`;
float $myrefractionExitColor[] = `getAttr ($mySelection[0] + ".refractionExitColor")`;
float $myrefractionExitColorMax[] = {($myrefractionExitColor[0] * 255), ($myrefractionExitColor[1] * 255), ($myrefractionExitColor[2] * 255)};

int $myrefractionExitColorOn = `getAttr ($mySelection[0] + ".refractionExitColorOn")`;
string $boolmyrefractionExitColorOn = ($myrefractionExitColorOn == 0) ? "false" : "true";

float $myscatterCoeff = `getAttr ($mySelection[0] + ".scatterCoeff")`;
float $myscatterDir = `getAttr ($mySelection[0] + ".scatterDir")`;
float $mytranslucencyColor[] = `getAttr ($mySelection[0] + ".translucencyColor")`;
float $mytranslucencyColorMax[] = {($mytranslucencyColor[0] * 255), ($mytranslucencyColor[1] * 255), ($mytranslucencyColor[2] * 255)};

int $mybrdfType = `getAttr ($mySelection[0] + ".brdfType")`;
float $myanisotropy = `getAttr ($mySelection[0] + ".anisotropy")`;
float $myanisotropyRotation = `getAttr ($mySelection[0] + ".anisotropyRotation")`;
float $mysoftenEdge = `getAttr ($mySelection[0] + ".softenEdge")`;

int $mytraceReflections = `getAttr ($mySelection[0] + ".traceReflections")`;
string $boolmytraceReflections = ($mytraceReflections == 0) ? "false" : "true";

int $mytraceRefractions = `getAttr ($mySelection[0] + ".traceRefractions")`;
string $boolmytraceRefractions = ($mytraceRefractions == 0) ? "false" : "true";

int $myreflectOnBackSide = `getAttr ($mySelection[0] + ".reflectOnBackSide")`;
string $boolmyreflectOnBackSide = ($myreflectOnBackSide == 0) ? "false" : "true";

int $myuseIrradianceMap = `getAttr ($mySelection[0] + ".useIrradianceMap")`;
string $boolmyuseIrradianceMap = ($myuseIrradianceMap == 0) ? "false" : "true";

float $mybumpMult = `getAttr ($mySelection[0] + ".bumpMult")`;

string $maxScriptTemplate = "objs = selectByName title:\"Select the objects you want to create a VRay material for\"\n\
for obj in objs do \(\n\
newMat = VRayMtl\(\)\n\
newMat.diffuse = [" + $myDiffuseMax[0] + "," + $myDiffuseMax[1] + "," + $myDiffuseMax[2] + "]\n\
newMat.diffuse_roughness = " + $myRoughness + "\n\
newMat.reflection = [" + $myreflectionColorMax[0] + "," + $myreflectionColorMax[1] + "," + $myreflectionColorMax[2] + "]\n\
newMat.reflection_glossiness = " + $myreflectionGlossiness + "\n\
newMat.reflection_subdivs = " + $myreflectionSubdivs + "\n\
newMat.reflection_fresnel = " + $booluseFresnel + " \n\
newMat.reflection_maxDepth = " + $myreflectionsMaxDepth + "\n\
newMat.reflection_exitColor = [" + $myreflectionExitColorMax[0] + "," + $myreflectionExitColorMax[1] + "," + $myreflectionExitColorMax[2] + "]\n\
newMat.reflection_useInterpolation = " + $boolmyreflInterpolation + "\n\
newMat.reflection_lockGlossiness = " + $boolhilightGlossinessLock + "\n\
newMat.hilight_glossiness = " + $myreflectionGlossiness + "\n\
newMat.reflection_ior = " + $myfresnelIOR + "\n\
newMat.reflection_lockIOR = " + $boolmylockFresnelIORToRefractionIOR + "\n\
newMat.refraction = [" + $myrefractionColorMax[0] + "," + $myrefractionColorMax[1] + "," + $myrefractionColorMax[2] + "]\n\
newMat.refraction_glossiness = " + $myrefractionGlossiness + "\n\
newMat.refraction_subdivs = " + $myrefractionSubdivs + "\n\
newMat.refraction_ior = " + $myrefractionIOR + "\n\
newMat.refraction_fogColor = [" + $myfogColorMax[0] + "," + $myfogColorMax[1] + "," + $myfogColorMax[2] + "]\n\
newMat.refraction_fogMult = " + $myfogMult + "\n\
newMat.refraction_fogBias = " + $myfogBias + "\n\
newMat.refraction_affectShadows = " + $boolmyaffectShadows + "\n\
newMat.refraction_affectAlpha = " + $myaffectAlpha + "\n\
newMat.refraction_maxDepth = " + $myrefractionsMaxDepth + "\n\
newMat.refraction_exitColor = [" + $myrefractionExitColorMax[0] + "," + $myrefractionExitColorMax[1] + "," + $myrefractionExitColorMax[2] + "]\n\
newMat.refraction_useExitColor = " + $boolmyrefractionExitColorOn + "\n\
newMat.refraction_useInterpolation = " + $boolmyrefrInterpolation + "\n\
newMat.translucency_scatterCoeff = " + $myscatterCoeff + "\n\
newMat.translucency_fbCoeff = " + $myscatterDir + "\n\
newMat.translucency_color = [" + $mytranslucencyColorMax[0] + "," + $mytranslucencyColorMax[1] + "," + $mytranslucencyColorMax[2] + "]\n\
newMat.brdf_type = " + $mybrdfType + "\n\
newMat.anisotropy = " + $myanisotropy + "\n\
newMat.anisotropy_rotation = " + $myanisotropyRotation + "\n\
newMat.soften = " + $mysoftenEdge + "\n\
newMat.option_traceReflection = " + $boolmytraceReflections + "\n\
newMat.option_traceRefraction = " + $boolmytraceRefractions + "\n\
newMat.option_reflectOnBack = " + $boolmyreflectOnBackSide + "\n\
newMat.option_useIrradMap = " + $boolmyuseIrradianceMap + "\n\
newMat.texmap_bump_multiplier = " + $mybumpMult + "\n\
obj.material = newMat  \n\
\)\n";

string $singleFilter = "All Files (*.*)";
string $result[] = `fileDialog2 -fileFilter $singleFilter -dialogStyle 2`;
$exampleFileName = $result[0];
$fileId=`fopen $exampleFileName "w"`;
fprint $fileId $maxScriptTemplate;
fclose $fileId;

if( `about -win`)
	{
	system $result[0]; 
	}
else if ( `about -mac`)
	{
	system ("open -a /Applications/TextEdit.app " + $result[0]);
	//Dave's personal setting
	system ("cat " + $result[0] + " | pbcopy");
	}

}

Sorry, wthat does this do? Convert Maya to max materials?

preety handy if your switching pakages

Hi Dave,
and the question is , can you do the opposite, from Max to Maya ?
Thanks,
Alex.

__________________________________________________ _____________
Alex Grau
www.furia-digital.com
phone: +34 93 4856327
fax: +34 93 300 9909

Hi alexitoland,

You can do this partially by exporting a vrscene from Max and then in Maya you can import the material by “Create → VRay-> Import V-Ray Material from file”

/Teodor

Check this out if you also export the Max scene as FBX:

##############################################################
# Author: Zachary Cole
# Website: http://www.nzaneproductions.com
##############################################################
#
# Copyright: Creative Commons
# Attribution-ShareAlike 3.0 Unported License
#
# This work is licensed under the
# Creative Commons Attribution-ShareAlike 3.0 Unported License.
# To view a copy of this license,
# visit Deed - Attribution-ShareAlike 3.0 Unported - Creative Commons
# or send a letter to
# Creative Commons
# 444 Castro Street, Suite 900,
# Mountain View, California, 94041, USA.
#
# what does the above copyright mean?
# give me credit, and share any modifications you make! thanks!
#
##############################################################
#
# what does this code snippet do?
#
# it relinks materials imported from a .vrscene to
# geometry imported from a .fbx file when you wish
# to use a scene created for vray for 3dsmax in
# a scene in vray for maya
#
##############################################################
# caveats? please save backup copies of your work.
# I’m not responsible if it finds the
# higgs boson or anything unexpected.
# the steps below assume you know how to do basic
# actions in both 3ds max and maya
# support? are you paying for this? no.
# if you want to pay for it,
# then I’ll try and support it.
# testing? minimal testing in 3dsmax 2012 w/ vray 2
# and maya 2011 w/ vray 1.5
# feedback? post comments on my blog!!
#
# assumptions? I’m assuming that the named order is the
# same in both formats.
# i.e. the first brdf corresponds
# to the first phong. (seems to work)

##############################################################
# IMPORTANT steps:
# in 3ds max:
# select all the geometry you want to transfer
# save selected as new scene
# open scene you just saved
# export fbx
# export .vrscene
# in maya:
# change renderer to vray
# import fbx
# import .vrscene
# fix texture paths in hypershade
# run this script in the python window of the script editor
#
# wow i think these header comments are longer than my code. geeze.
##############################################################

import maya.cmds as cmds

def relinkVrayMaterials():

# get em while they’re hot
    mats = cmds.ls(materials=True)

phongs = []
    materials = []
    submaterials = []

print “#### searching through materials…”
    # cycle through all materials
    for mat in mats:

# look for shaders that match the naming
        # when you bring them in from .fbx
        if cmds.nodeType(mat) == “phong”:
            phongs.append(mat)

# look for shaders that match the naming
        # when you bring them in from .vrscene
        elif mat.find(“brdf”) == 0 or mat.find(“material”) == 0:
            if mat not in submaterials:
                materials.append(mat)

# layered shaders are special cases
            # the connected shaders will not match up to the
            # phong shaders from the fbx
            if cmds.nodeType(mat) == “layeredShader”:
                # get the connected attrs
                arr = cmds.listAttr(mat + “.inputs”, m=True)
                for a in arr:
                    # we only care about the color inputs
                    if a[-6:] == “.color”:
                        # get the connected nodes
                        conn = cmds.listConnections(mat + “.” + a)
                        # add them to the ignored submaterials list
                        for c in conn:
                            submaterials.append(c)

print “#### replacing material assigments…”
    # cycle through all phong materials
    for i in range(0, len(phongs)):
        pho = phongs
        mat = materials
*        # these vray materials don’t have shader groups when*
        # they are imported from .vrscenes, so let’s make one for each
        matSG = cmds.sets( renderable=True,
                           noSurfaceShader=True,
                           empty=True,
                           name=mat+‘SG’ );
        cmds.connectAttr( mat+“.outColor”,
                          mat+“SG.surfaceShader”,
                          force=True)
        # get the objects assigned to the phong
        objs = cmds.hyperShade(objects=pho)
        # assign new material to selected objects
        cmds.hyperShade(assign=mat)
        print “#### assigned %s to %s assigned objects” % (mat, pho)

print “#### done”

relinkVrayMaterials()[/QUOTE]

Thanks guys, we will give it a try.
Best,
Alex.