Wednesday, January 2, 2019

Exporter & Importer for ngSkinTools v1

Hi how are you, it's been a while since I've posted anything. After researching and asking a colleague about how to automate the task of importing and exporting layers from ngSkinTools. I managed to import the .json files and place them in the layers. A somewhat easy task, but complicated and tedious if there are several meshes to import.

TO DO:
  • Import Mode: Closest, ID, UV
The code:
import maya.cmds as mc
import os
import json
from ngSkinTools.importExport import LayerData,JsonExporter,JsonImporter
#Func
def getCurrentRoot():
filepath = mc.file(q=True, sn=True)
filename = os.path.basename(filepath)
path = filepath.replace(filename,'')
return path
def progressBarWindow(name,elems=None,single=None,step=0,open=False,close=False,textProg='Progress'):
if open == True:
if len(elems) >= 1:
mc.window(name,t=name + ' Progress')
mc.window(name,e=True,w=300,h=50)
mc.columnLayout(rs=10,adj=1,cat=['both',10])
mc.text(name + 'text',l='Waiting input')
mc.progressBar(name + 'ProgBar',maxValue=len(elems)-1)
mc.showWindow(name)
if step >= 1:
mc.progressBar(name + 'ProgBar', edit=True, step=step)
mc.text(name + 'text',e=True,l=textProg + single)
if close == True:
try:
mc.deleteUI(name)
except:
pass
#Def
def exportLayers(self):
path = getCurrentRoot()
meshes = mc.ls('*_mesh')
progressBarWindow('ngExport',meshes,open=True)
for mesh in meshes:
layerData = LayerData()
try:
layerData.loadFrom(mesh)
exporter = JsonExporter()
jsonContents = exporter.process(layerData)
jj = json.loads(jsonContents)
with open(path + mesh.replace('_mesh','_skin') + '.json', 'w') as outfile:
json.dump(jj,outfile,sort_keys=True,indent=4,separators=(',', ': '))
progressBarWindow('ngExport',step=1,single=mesh,textProg='Exporting... ')
except:
continue
progressBarWindow('ngExport',close=True)
def importLayers(self):
path = getCurrentRoot()
meshes = mc.ls('*_mesh')
progressBarWindow('ngImport',meshes,open=True)
for mesh in meshes:
try:
with open(path + mesh.replace('_mesh','_skin') + '.json', 'r') as outfile:
data = json.load(outfile)
data = json.dumps(data)
importer = JsonImporter()
layerData = importer.process(data)
layerData.saveTo(mesh)
progressBarWindow('ngImport',step=1,single=mesh,textProg='Importing... ')
except:
continue
progressBarWindow('ngImport',close=True)
#UI
width = 250
height = 50
if mc.window('ngExportImportWin',ex=True):
mc.deleteUI('ngExportImportWin')
mc.window('ngExportImportWin',t='ngSkinTools Export/Import',minimizeButton=False,maximizeButton=False,sizeable=0)
mc.window('ngExportImportWin',e=1,w=width,h=height)
mc.frameLayout(lv=0)
mc.separator(h=1,w=width)
mc.text('It will export all the layers contained\nin skinCluster and then import them')
mc.separator(h=2,style='in',w=width)
mc.radioButtonGrp(label='Mode',cw4=[width/4,width/4,width/4,width/4],labelArray3=['Closest','ID','UV'],nrb=3,h=15,sl=1,en=0)
mc.rowLayout(nc=2)
mc.button(l='Export Layers',h=30,bgc=[1,0.701,0.023],w=width/2,c=exportLayers)
mc.button(l='Import Layers',h=30,bgc=[0.051,0.604,0.558],w=width/2,c=importLayers)
mc.showWindow()

Hope you find this useful. Thanks

No comments:

Post a Comment