Skip to content

Instantly share code, notes, and snippets.

@TSCG
Created September 22, 2015 12:59
Show Gist options
  • Save TSCG/f58a1948c69d1792dd56 to your computer and use it in GitHub Desktop.
Save TSCG/f58a1948c69d1792dd56 to your computer and use it in GitHub Desktop.
MaterialConvert / Maya
#マテリアルのコンバート
#変換するマテリアルのアトリビュート接続先を辞書型で指定して使用
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
def TsMatConvert(MatType,AttList):
selMat = cmds.ls( selection=True, materials=True )
for tmpMat in selMat:
TgtShader = cmds.shadingNode(MatType, asShader=True, n=tmpMat+"_Conv")
print "[" + tmpMat + " -> " + TgtShader + "]"
for tmpAtt in AttList:
if cmds.attributeQuery( tmpAtt, node=tmpMat, exists=1 ):
att = cmds.connectionInfo( tmpMat+"."+tmpAtt, sfd=True, )
if cmds.attributeQuery( AttList[tmpAtt], node=TgtShader, exists=1 ):
if att == "":
attVals = cmds.getAttr(tmpMat+"."+tmpAtt)
if cmds.attributeQuery( tmpAtt, node=tmpMat, usedAsColor=1 ):
attVal = attVals[0]
cmds.setAttr( TgtShader + "." + AttList[tmpAtt], attVal[0],attVal[1],attVal[2], type="double3" )
print " Conect:" + str(attVal) + " > " + TgtShader + "." + AttList[tmpAtt]
else:
cmds.setAttr( TgtShader + "." + AttList[tmpAtt], attVals )
print " Conect:" + str(attVals) + " > " + TgtShader + "." + AttList[tmpAtt]
else:
cmds.connectAttr( att, TgtShader + "." + AttList[tmpAtt] )
print " Conect:" + att + " > " + TgtShader + "." + AttList[tmpAtt]
else:
OpenMaya.MGlobal.displayWarning(u" 接続先の " + TgtShader + "." + AttList[tmpAtt] + u" がありません")
else:
OpenMaya.MGlobal.displayWarning(u" 接続元の " + tmpMat + "." + tmpAtt + u" がありません")
# usage
##作成するマテリアルの種類
# MatType = "VRayMtl"
#
##アトリビュートの接続リスト -> 変換元のアトリビュート名:変換後のマテリアルへ接続するアトリビュート名
##"diffuse":"diffuseColorAmount"なら変換前マテリアルのdiffuseを変換後マテリアルのdiffuseColorAmountに接続する
# VrayAttList = {"color":"color", "diffuse":"diffuseColorAmount", "specularColor":"reflectionColor", "incandescence":"illumColor"}
#
##変換するマテリアルを選択して実行
# TsMatConvert(MatType,VrayAttList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment