I have somewhat search through the office database and I chanced upon this code that my senior did and it is very much similar to what I am trying to achieve.
As it is a proprietary shader, the texture file will be located in <proprietary shader>.Input_Texture[0].inputTextures, example mtlShader.Input_Texture[0].inputTextures
I did some amendments to it, here and there, but it seems that my senior is using json file to extract the information out of the surfaceShader in the scene before applying it and have it convert in the mtlShader.
And he uses txmake
to do the conversion of .tga to .tif then to .tex
While it works for me in a sense, are there any ways that I can bypass the json file generation (I do not have any ideas how does the json file comes about)? Meaning to say, have everything done in one shot without it creating any other files etc?
import os
import maya.cmds as mc
import maya.mel as mm
import pymel.core as pm
import json
from PIL import Image
lsSurfShdr = mc.ls(type = 'surfaceShader')
def makeJsonDir():
tmpJson = "/user_data/.tmp/tmpJson/"
if not os.path.exists(tmpJson):
print "Tmp folder does not exists, creating one.
"
os.makedirs(tmpJson)
def lsCon(shaders):
conDict = {}
for i in shaders:
con = mc.listConnections('%s.outColor' % i)
try:
msh = pm.listConnections(con[1], type = "mesh")[0].longName()
text = mc.getAttr("%s.fileTextureName" % con[0])
currentDict = { "twoD" : con, "mesh" : msh, "txtPath" : text}
conDict[i] = currentDict
except IndexError:
pass
return conDict
def writeJson():
makeJsonDir()
path = "/user_data/.tmp/tmpJson/connectionData.con"
conData = lsCon(lsSurfShdr)
toBeSaved = json.dumps(conData, sort_keys = True, ensure_ascii = True, indent = 4)
f = open(path, 'w')
f.write(toBeSaved)
f.close()
def getMtlShader():
# Command to create the proprietary shader
createMtl = mm.eval('mtlShader("/hosts/krateh/tools/mtlshaders/mtlGeneric.slo")')
def convertToTexture(inImg, outImg, texImg):
if not os.path.exists(texImg):
Image.open(inImg).save(outImg)
os.system('/apps/Linux64/prman/bin/txmake ' + outImg + ' ' + texImg)
os.remove(outImg)
print "done converting " + inImg
else:
pass
def propagate(trans):
shapes = mc.listRelatives(trans, fullPath = True, shapes = True)
if len(shapes) == 1:
mc.rename(shapes, mc.listRelatives(shapes, p = True)[0] + "_Shape")
else:
count = 0
for s in shapes:
dup = mc.duplicate(trans)
ls = mc.listRelatives(dup, fullPath = True, shapes = True)
i = 0
for i in xrange(0, len(ls)):
if i == count:
mc.rename(ls[i], mc.listRelatives(ls[i], p = True)[0] + "_Shape")
else:
mc.delete(ls[i])
i += 1
count += 1
mc.delete(trans)
def fixTrans():
sel = mm.eval("SelectAllGeometry;")
s = mc.ls(sl = True)
for i in s:
propagate(i)
def main():
fixTrans()
writeJson()
f1 = open("/user_data/.tmp/tmpJson/connectionData.con"
sort = json.load(f1)
getMtlShader()
for key, value in sort.iteritems():
msh = value["mesh"]
text = value["txtPath"]s
path = os.path.dirname(text)
f = text.split("/")[-1]
tifName = os.path.splitext(f)[0] + ".tif"
texName = os.path.splitext(f)[0] + ".tex"
tifPath = os.path.join(path, tifName)
texPath = os.path.join(path, texName)
convertText(text, tifPath, texPath)
newTmp = mc.duplicate("mtlGeneric", ic = True)
mm.eval("setAttr -type \"string\" " + newTmp[0] + ".Input_Texture[0].inputTextures" "\"" + texPath + "\";")
mc.rexAssign(msh, p = "BEAUTY", a = newTmp[0])
mc.rename(newTmp[0], msh.replace("|", "") + "_GenShdr")
Contents in the Json file:
{
"AreaA_0010_1SE": {
"mesh": "|pansasCity_model_null|area1|Ref3D_2001_2",
"twoD": [
"AreaA_0010_1File",
"AreaA_0010_1SG"
],
"txtPath": "/hosts/gangel/lidar/maya/data/texture.0463.tga"
},
"AreaB_0110_1SE": {
"mesh": "|pansasCity_model_null|area1|Ref3D_2001_3",
"twoD": [
"AreaB_0110_1File",
"AreaB_0110_1SG"
],
"txtPath": "/hosts/gangel/lidar/maya/data/texture.0464.tga"
}
}