Thursday, July 19, 2018

Transfer Shader Tool

Hey it's been a while since I've posted any new tools, I've been a bit busy with work. I've polished up some knowledge I have and it's given me time to create new tools used in a small production. One problem that usually happens is when you want to transfer shaders from a model to a rig, a cache or another model, obviously of the same topology. I decided to create an easy to use tool that I have used a lot, however it contains some bugs. I know it can still be polished a lot.
It is quite easy to use and the buttons are arranged consecutively.



  • First we must have the model with the final shader to be able to pass it to the other model. Where it says "Node Type" we will have to put the name of the shader node that we want to obtain. Example: aiStandardSurface, redShiftMaterial, miaMaterialX, VRayMtl, etc. We can join more than two shader types by separating them with commas. As follows: "aiStandardSurface, redShiftMaterial", without commas.
  • Once this is done we press the "Get Faces" button, and then "Export Shader". This will write a couple of files (json. and ma.) in temporary.
  • Then we will open the other file we want to transfer the shaders to. Select the meshes and press "lambert1/Unused". This will apply lambert1 to the meshes and delete the shaders it has.
  • We will import the shaders that we had exported before, pressing the button "Import Shaders".
  • Finally we press "Assing Shader", our model should be with its correct shader. The tool will skip the meshes that do not exist in the target model.
Note: For some reason when in "Node Type" we set it to "lambert", you will get all the default Maya materials (lambert ,blinn, anisotropic, phong, phongE).

The code:

"""
########################################################################################################################
AUTHOR:
Steffano Richi (steffano.richi@gmail.com)
https://encci20.blogspot.pe/
20.03.2018
DESCRIPTION:
Trasfer shader from previous file. Guided by vertex ID.
USAGE:
*Just drag and drop into the script editor
*Run it
########################################################################################################################
"""
import maya.cmds as mc
import json
import tempfile
import maya.mel as mel
import sys
temp = tempfile.gettempdir()
def getSelectionFaces(self):
master = str(mc.textField('textType',q=1,tx=1))
masterNode = master.split(', ')
nodes = mc.ls(typ=masterNode)
jsonLib = {}
for node in nodes:
mc.hyperShade(o=node)
faces = mc.ls(sl=1)
jsonLib.update({node:faces})
print 'The shader ' + str(node) + ' has been assigned to ' + str(faces)
with open( temp + '\jsonSel.json', 'w') as outfile:
json.dump(jsonLib,outfile)
mc.select(cl=1)
sys.stdout.write('These objects has been obtained ' + str(jsonLib) + ' in ' + temp)
def assingSelectionFaces(self):
master = str(mc.textField('textType',q=1,tx=1))
masterNode = master.split(', ')
nodes = mc.ls(typ=masterNode)
with open(temp + '\jsonSel.json', 'r') as outfile:
data = json.load(outfile)
for node in nodes:
mc.select(cl=1)
for sel in data[node]:
try:
mc.select(sel,add=1)
mc.hyperShade(a=node)
print '############# Selection ' + str(sel) + ' has been assinged to ' + str(node)
except:
print '############# Selection' + str(sel) + ' does not exist'
mc.select(cl=1)
def exportShaders(self):
master = str(mc.textField('textType',q=1,tx=1))
masterNode = master.split(', ')
nodes = mc.ls(typ=masterNode)
mc.select(nodes,r=1)
mc.file(temp + '/currentShader.ma', pr=1, typ="mayaAscii", es=1, op="v=0;",f=1)
mc.select(cl=1)
sys.stdout.write('These shaders has been exported ' + str(nodes))
def importShaders(self):
mc.file(temp + '/currentShader.ma', pr=1, typ="mayaAscii", i=1, op="v=0;")
sys.stdout.write('Imported Shaders')
def unusedLamb(self):
mc.sets(forceElement='initialShadingGroup', e=1)
mel.eval('MLdeleteUnused')
"""//UI//"""
w = 250
h = 280
if mc.window('ExAsShaders',ex=True):
mc.deleteUI('ExAsShaders')
mc.window('ExAsShaders',t='Export and Assing Shaders',minimizeButton=False,maximizeButton=False,sizeable=0)
mc.window('ExAsShaders',e=1,w=w,h=h)
mc.columnLayout(rs=10,w=w,h=h)
mc.rowLayout(nc=2)
mc.text(l='Node Type ',ann='Separated by commas')
mc.textField('textType',tx='lambert',w=w-70,sf=1)
mc.setParent('..')
mc.button(w=w,h=h/8,l='Get Faces',bgc=[0.1,0.6,0.2],c=getSelectionFaces)
mc.button(w=w,h=h/8,l='Export Shaders',bgc=[0.1,0.6,0.8],c=exportShaders)
mc.separator(h=5,w=w,st='in')
mc.button(w=w,h=h/8,l='lambert1 / Unused',c=unusedLamb)
mc.button(w=w,h=h/8,l='Import Shaders',bgc=[0.1,0.8,0.6],c=importShaders)
mc.button(w=w,h=h/8,l='Assing Shaders',bgc=[0.4,0.4,0.8],c=assingSelectionFaces)
mc.showWindow()
view raw Transfer Shader hosted with ❤ by GitHub

Hope you find this useful. Thanks

No comments:

Post a Comment