Converting Mental Ray materials to VRay materials

This is some code I wrote a while ago to semi-automatically convert Mental Ray materials to VRay materials. It should work for the most part but, of course, some of the attributes don’t have exact equivalents.

It appears that WordPress has a hard time displaying indentation properly. This code should look correct when copy pasted elsewhere (and thus, should still work properly).

'''Convert Mental Ray to VRay'''
from pymel.core import *
 
        
selected = ls(sl=True)
for g in selected:
    
    
    '''Check if material is mia_material_x or mie_material_x_passes'''
    
    if(nodeType(g) == "mia_material_x_passes") or (nodeType(g) == "mia_material_x"):
    
		n = shadingNode('VRayMtl', asShader=True)
		
		#Make array with all the attributes that need to be compared, mia material attribute first, VRay material attribute second
		
		attrList = \
		[("diffuse", "color"),\
		("diffuse_weight", "diffuseColorAmount"),\
		("diffuse_roughness","roughnessAmount"),\
		("refl_color","reflectionColor"),\
		("reflectivity","reflectionColorAmount"),\
		("refl_gloss","reflectionGlossiness"),\
		("refl_gloss_samples","reflectionSubdivs"),\
		("refl_falloff_on","reflectionDimDistanceOn"),\
		("refl_falloff_dist","reflectionDimDistance"),\
		("refl_depth","reflectionsMaxDepth"),\
		("refr_ior","refractionIOR"),\
		("refr_color","refractionColor"),\
		("transparency","refractionColorAmount"),\
		("refr_gloss","refractionGlossiness"),\
		("refr_gloss_samples","refractionSubdivs"),\
		("thin_walled","refractionIOR")]


		#Connect maps if available, otherwise adjust values/colours

		for a in attrList:
			if connectionInfo(g + "." + a[0], id=True):
				outAttr = listConnections(g + "." + a[0], p=True)[0]
				inAttr = (n + "." + a[1])
				connectAttr(outAttr, inAttr)
			else:
				outAttr = getAttr(g + "." + a[0])
				inAttr = (n + "." + a[1])
				setAttr(inAttr, outAttr)
		if getAttr(g + ".thin_walled") == 1:
			setAttr(n + ".refractionIOR", 1)
		
		
		#If there is a bump map, connect it and adjust the multiplier
			
		if connectionInfo(g + ".standard_bump", id=True):
			outAttr = listConnections(g + ".standard_bump")[0]
			outValue = listConnections(outAttr + ".bumpValue", p=True)[0]
			inValue = (n + "." + 'bumpMap')
			connectAttr(outValue, inValue+"R")
			connectAttr(outValue, inValue+"G")
			connectAttr(outValue, inValue+"B")
		
			outDepth = getAttr(outAttr + ".bumpDepth")
			inDepth = (n + "." + 'bumpMult')
			setAttr(inDepth, outDepth)

This all started with an earlier and less elegent script for converting Maya phong materials to Mental Ray materials.

  # import python
from pymel.core import *


selected = ls(sl=True)
for g in selected: 
        
    # get releventt atributes from original
    col = getAttr((g)+'.color')
    transp = getAttr((g)+'.transparencyR')
    refl = getAttr((g)+'.specularColorR')
    specCol = getAttr((g)+'.specularColor')
    cosPow = getAttr((g)+'.cosinePower')
    cosPow = (cosPow)/30
    
    
    # create new material nodes
    newn = (g)+'_02'
    gamn = 'gamma'+(g)+'_02'
    shadingNode('mia_material_x_passes', name=(newn), asShader=True)
    shadingNode('mip_gamma_gain', name=(gamn), asUtility=True)
    
    # check if there are incoming connections, if so, update them.
    ## diffuse:
    cFile = listConnections((g)+'.color', d=False, s=True )
    l = len(cFile)      
    if (l > 0):
        print 'connecting file'
        connectAttr((cFile[0])+'.outColor', (gamn)+'.input')
    else:
        setAttr((gamn)+'.input', (col))
        
    ### connect gamma node
    setAttr((gamn)+'.gamma', 0.454)
    connectAttr((gamn)+'.outValue', (newn)+'.diffuse', force=True)
        
    ## transparency:
    cFile = listConnections((g)+'.transparency', d=False, s=True )
    l = len(cFile)      
    if (l > 0):
        setAttr((cFile[0])+'.alphaIsLuminance', 1)
        print 'connecting file'
        connectAttr((cFile[0])+'.outAlpha', (newn)+'.cutout_opacity')
    else:
        setAttr((newn)+'.transparency', (transp))
        
    ## bump
    cFile = listConnections((g)+'.normalCamera', d=False, s=True )
    l = len(cFile)      
    if (l > 0):
        print 'connecting file'
        connectAttr((cFile[0])+'.outNormal', (newn)+'.standard_bump')
    
    #set relevent attributes
    setAttr((newn)+'.reflectivity', (refl))
    setAttr((newn)+'.refl_color', (specCol))
    setAttr((newn)+'.refl_gloss', (cosPow))
    setAttr((newn)+'.thin_walled', 1)
    
    select(g)
    # select objects with current shader
    shaded = hyperShade(objects="")
    # assign new mia shader
    hyperShade(assign=(newn))
    
    # connect old shader to 'surface' in shading group for viewport preview
    newSG = (newn)+'SG'
    g.message >> (newSG)+'.surfaceShader'
    disconnectAttr((g)+'.message', (newSG)+'.surfaceShader')
    g.message >> (newSG)+'.surfaceShader'

Leave a Reply

Your email address will not be published. Required fields are marked *