mia2vray material converter

This small script will swap selected materials mia shaders into vraymtrl shaders. This script was inspired by Paul Driesen’s script for Maya 

#mia2vray converter
#author: gareth bell
#date: 15-10-2012

#This script will break the back of converting mia shaders in selected materials into vrayMtls

#Use: Select materials. Run

import win32com.client
import siutils

from siutils import si
from siutils import C

xsi = Application
coll = win32com.client.Dispatch( "XSI.Collection" )
vrayMaterials = win32com.client.Dispatch( "XSI.Collection" )


#############################################
def traverse( s, coll ):
    for p in s.Parameters:
        if p.Source and p.Source.IsClassOf( C.siShaderParameterID ):
            coll.Add( p.Source.Parent )
            traverse( p.Source.Parent, coll )

    for l in s.TextureLayers:
        for y in l.Parameters:
            if y.Source and y.Source.IsClassOf( C.siShaderParameterID ):
                coll.Add( y.Source.Parent ) 
                traverse( y.Source.Parent, coll )
		  
############################################

def createVrayShader (eachShader):
			vrayMaterial = xsi.CreateShaderFromProgID("Softimage.VRayMtl.1.0", mat, "VRayMtl")
			vrayMaterials.AddItems (vrayMaterial) 
			miaName = eachShader.name
			vrayName = vrayMaterial.FullName
			
			#match diffuse colour
			xsi.SetValue( (str(vrayName) + ".diffuse_color.red"), eachShader.diffuse.red.value, "")
			xsi.SetValue( (str(vrayName) + ".diffuse_color.green"), eachShader.diffuse.green.value, "")
			xsi.SetValue( (str(vrayName) + ".diffuse_color.blue"), eachShader.diffuse.blue.value, "")
			
			#match reflection colour
			xsi.SetValue( (str(vrayName) + ".reflection_color.red"), eachShader.refl_color.red.value, "")
			xsi.SetValue( (str(vrayName) + ".reflection_color.green"), eachShader.refl_color.green.value, "")
			xsi.SetValue( (str(vrayName) + ".reflection_color.blue"), eachShader.refl_color.blue.value, "")
			
			#match refraction colour
			xsi.SetValue( (str(vrayName) + ".refr_color.red"), eachShader.refr_color.red.value, "")
			xsi.SetValue( (str(vrayName) + ".refr_color.green"), eachShader.refr_color.green.value, "")
			xsi.SetValue( (str(vrayName) + ".refr_color.blue"), eachShader.refr_color.blue.value, "")
			
			#match roughness
			xsi.SetValue( (str(vrayName) + ".roughness"), eachShader.diffuse_roughness.value, "")
			
			#match gloss
			xsi.SetValue( (str(vrayName) + ".refr_glossiness"), eachShader.refr_gloss.value, "")
			
			#match IOR
			xsi.SetValue( (str(vrayName) + ".refr_ior"), eachShader.refr_ior.value, "")
			
			#match anisotropy
			a = eachShader.anisotropy.value
			#equation for getting anisotropy values in range 0 to 10 (mia_shader) converted to -1 to 1 (vray_shader) is (a/5.0)-1)
			xsi.SetValue( (str(vrayName) + ".brdf_anisotropy"), (a/5.0)-1.0)
			
			#match rotation
			xsi.SetValue( (str(vrayName) + ".brdf_rotation"), eachShader.anisotropy_rotation.value, "")
			
			#match fresnel
			fres = eachShader.brdf_fresnel.value
			if fres == True:
				xsi.SetValue( (str(vrayName) + ".reflection_enable_fresnel"), False, "")
			else:
				xsi.SetValue( (str(vrayName) + ".reflection_enable_fresnel"), True, "")
			
			inputCnx = win32com.client.Dispatch( "XSI.Collection" )
			cnxPoint = win32com.client.Dispatch( "XSI.Collection" )
			
			for p in eachShader.Parameters:
				if p.Source and p.Source.IsClassOf( C.siShaderParameterID ):
					inputCnx.Add (p.Source.Parent)
					cnxPoint.Add (p)

			for i in range (0, inputCnx.Count):
				#print inputCnx(i)
				#print cnxPoint(i).Name
				
				if cnxPoint(i).Name == "diffuse":
					vrayCnxPoint = "diffuse_color"
				elif cnxPoint(i).Name == "refl_color":
					vrayCnxPoint = "reflection_color"
				elif cnxPoint(i).Name == "refr_color":
					vrayCnxPoint == "refr_color"
				elif cnxPoint(i).Name == "roughness":
					vrayCnxPoint == "diffuse_roughness"
				elif cnxPoint(i).Name == "refr_glossiness":
					vrayCnxPoint == "refr_gloss"
				elif cnxPoint(i).Name == "refr_ior":
					vrayCnxPoint == "refr_ior"
				elif cnxPoint(i).Name == "anisotropy":
					vrayCnxPoint == "brdf_anisotropy"
				elif cnxPoint(i).Name == "brdf_rotation":
					vrayCnxPoint == "anisotropy_rotation"
				
				xsi.SIConnectShaderToCnxPoint( (inputCnx(i).out) , str(vrayName) + "." + str(vrayCnxPoint))
				
			#match name to mia_name
			xsi.SetValue((str(vrayName) + ".Name"), "VRay" + str(eachShader.name)[3:])


#############################################################
#############################################################


prefs = xsi.Preferences
#get commandlog prefs
origPrefs = prefs.GetPreferenceValue ("scripting.cmdlog")
#turn log off
prefs.SetPreferenceValue ("scripting.cmdlog", False)

# get material
for mat in xsi.Selection:
	traverse( mat, coll )
	coll.Unique = True
	
	for eachShader in coll:
		if "Softimage.mia_material_phen.1.0" in eachShader.ProgID:
			print ""          
		elif "mentalray.mia_material_x.1.0" in eachShader.ProgID:
			print ""
		else:
			coll.Remove( eachShader )

	for eachShader in coll:
		createVrayShader (eachShader)
	#get mia shaders output connections and plug them into the newly created vRay shader
	for s in range (0, coll.count):
		targets = coll(s).GetShaderParameterTargets("")	
		for eachTarget in targets:
			xsi.SIConnectShaderToCnxPoint(str(vrayMaterials(s)) + ".out" , eachTarget, False)
			
		#delete mia shader
		xsi.DeleteObj (coll(s))
		
	#empty collections
	vrayMaterials.RemoveAll()
	coll.RemoveAll()
		
#restore original log prefs	
prefs.SetPreferenceValue ("scripting.cmdlog", origPrefs)