Skip to content

Instantly share code, notes, and snippets.

@TSCG
Last active August 16, 2018 07:12
Show Gist options
  • Save TSCG/960e3097d6f8b7ac19676782389af9b0 to your computer and use it in GitHub Desktop.
Save TSCG/960e3097d6f8b7ac19676782389af9b0 to your computer and use it in GitHub Desktop.
import maya.cmds as cmds
import maya.mel as mel
import os.path
def TsRename():
sel = cmds.ls( selection=True )
for obj in sel:
type = cmds.ls( obj,showType=True )[1]
if type == "VRayMesh":
name = os.path.splitext( os.path.basename( cmds.getAttr('%s.fileName2' % obj) ) )[0]
cmds.rename( obj, '%s_vraymesh' % name )
elif type == "VRayMeshMaterial":
name = os.path.splitext( os.path.basename( cmds.getAttr('%s.fileName' % obj) ) )[0]
cmds.rename( obj, '%s_m' % name )
elif type == "VRayBlendMtl":
name = cmds.listConnections('%s.base_material' % obj)[0]
cmds.rename( obj, '%s_BM_m' % name.replace("_m","") )
elif type == "VRayMtl2Sided":
name = cmds.listConnections('%s.frontMaterial' % obj)[0]
cmds.rename( obj, '%s_2S_m' % name.replace("_m","") )
elif type == "file":
cmds.select( obj, r=True )
Ts_RenameNode( 2, obj )
elif type == "shadingEngine":
cmds.select( obj, r=True, ne=True )
Ts_RenameNode( 1, obj )
else:
list = cmds.ls(cmds.listHistory(obj), type='file')
if list == None:
cmds.rename( obj, '%s_m' % "None" )
else:
name = os.path.commonprefix(list)
if len(name) == 0:
n_arr = []
for str in list:
n_arr.append(str.lower())
name = os.path.commonprefix(n_arr)
if name[-1:] == "_":
name = name[:-1]
cmds.rename( obj, '%s_m' % name )
def Ts_RenameNode( type, item ):
if type == 1: #ShadingGroup
matName = cmds.listConnections( item, scn=True, s=True, d=False )
if item != "%sSG" % matName[0]:
cmds.rename( item, "%sSG" % matName[0] )
print u"%s を %sSG にリネーム" % ( item, matName[0])
if type == 2: #File
path = cmds.getAttr( "%s.fileTextureName" % item )
texname = os.path.splitext( os.path.basename( path ) )[0]
if item != "%sTex" % texname:
cmds.rename( item, "%sTex" % texname)
print u"%s を %sTex にリネーム" % ( item, texname)
if type == 4: #Shape
transform = cmds.listRelatives( item, fullPath=True, p=True )
name = transform[0].split("|")[-1]
if item != "%sShape" % name:
cmds.rename( item, "%sShape" % name)
print u"%s を %sShape にリネーム" % ( item, name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment