1. import maya.cmds as cmds
  2. import maya.mel as mel
  3. import maya.api.OpenMaya as om2
  4. PM_startTime = cmds.timerX()
  5. PM_timeAdder = 0
  6. MAYA_version = cmds.about(v=1)
  7. MAYA_version_float = float(MAYA_version.split(' ')[0])
  8. ## math ##
  9. def round(number,pre):
  10. if number > 0:
  11. return float(int(number*10**pre+0.5))/10**pre
  12. else:
  13. return float(int(number*10**pre-0.5))/10**pre
  14. def distance(pointA,pointB):
  15. return ((pointB[0]-pointA[0])**2+(pointB[1]-pointA[1])**2+(pointB[2]-pointA[2])**2)**(0.5)
  16. ## Plugins ##
  17. def existsCmd(command):
  18. if not mel.eval('exists "'+command+'"'):
  19. return 0
  20. else:
  21. return 1
  22. ## Hotkeys ##
  23. def getOrCreateCustomHotkeySet():
  24. allHotkey= cmds.hotkeySet( q=1, hsa=1 )
  25. currentHotkey = cmds.hotkeySet( q=1, current=1 )
  26. if currentHotkey == 'Maya_Default' and len(allHotkey)==1:
  27. cmds.hotkeySet( "MyHotkeySet", current=1 )
  28. elif currentHotkey == 'Maya_Default':
  29. for i in allHotkey:
  30. if i != "Maya_Default":
  31. cmds.hotkeySet(i,e=1,current=1)
  32. currentHotkey = cmds.hotkeySet( q=1, current=1 )
  33. return currentHotkey
  34. ## Tranforms ##
  35. def freeze(sel):
  36. cmds.makeIdentity(sel,apply=True, t=1, r=1, s=1, n=0)
  37. def movePivotToCenter(sel):
  38. selBB = cmds.polyEvaluate(sel,b=1)
  39. selBB_diff_x = selBB[0][1]-selBB[0][0]
  40. selBB_diff_y = selBB[1][1]-selBB[1][0]
  41. selBB_diff_z = selBB[2][1]-selBB[2][0]
  42. selBB_center_x = (selBB[0][1]+selBB[0][0])/2
  43. selBB_center_y = (selBB[1][1]+selBB[1][0])/2
  44. selBB_center_z = (selBB[2][1]+selBB[2][0])/2
  45. cmds.move(selBB_center_x, selBB_center_y, selBB_center_z, sel+".scalePivot", sel+".rotatePivot", absolute=True)
  46. def moveObjToZero(sel):
  47. cmds.move(0,0,0,sel,rpr=1)
  48. ## Select ##
  49. def ls( index="all", type = "transform" ):
  50. # if index is str and not indicating index, then we think this is a type and return all index
  51. if index != "all" and index != 0:
  52. type = index
  53. # specify each type
  54. if type == "transform":
  55. sel = cmds.ls( sl=1, type = type )
  56. elif type in ["poly","polygon","polymesh","mesh","obj"]:
  57. sel = cmds.ls (sl=1, type ="transform")
  58. sel = cmds.filterExpand(sel,sm=12)
  59. elif type in ["c","cr","curve","nurbsCurve"]:
  60. sel = cmds.ls (sl=1, type ="transform")
  61. sel = cmds.filterExpand(sel,sm=9)
  62. # specify index
  63. if index == "all" or isinstance(index,(str)):
  64. if sel == None:
  65. msg("Nothing Selected")
  66. return None
  67. return sel
  68. elif isinstance(index,(int)):
  69. if sel == None:
  70. msg("Nothing Selected")
  71. return None
  72. return sel[index]
  73. ## Warning ##
  74. def msg(msg="Heads Up!"):
  75. global PM_startTime
  76. global PM_timeAdder
  77. PM_offset = 0
  78. PM_time = cmds.timerX(startTime = PM_startTime)
  79. PM_startTime = cmds.timerX()
  80. if PM_time<1.5:
  81. PM_offset = 10+20*(PM_timeAdder)
  82. PM_timeAdder = PM_timeAdder+1
  83. else:
  84. PM_offset = 10
  85. PM_timeAdder = 0
  86. if MAYA_version_float >= 2014:
  87. cmds.inViewMessage(
  88. amg = "<span style=\"color:#ffffff\">"+ msg +"</span>",
  89. fade = 1, fit = 150, fst = 800, fot = 150, fof = PM_offset, bkc = 0x2288ff,
  90. pos = "topCenter", fontSize = 10, a = 0, ta = 0.68)
  91. # Get Mesh Info
  92. def get(sel,what):
  93. if what == "uvid_uv":
  94. selMesh = MFnMesh(sel)
  95. uvid_uv = []
  96. uvArray = selMesh.getUVs()
  97. for i in xrange(len(uvArray[0])):
  98. uvid_uv.append([uvArray[0][i],uvArray[1][i]])
  99. return uvid_uv
  100. elif what == "vtxid_uvid":
  101. vtxid_uvid = []
  102. selVtxIter = MItMeshVertex(sel)
  103. while not selVtxIter.isDone():
  104. vtxid_uvid.append(list(set(selVtxIter.getUVIndices()))[0])
  105. selVtxIter.next()
  106. return vtxid_uvid
  107. elif what == "bb":
  108. return cmds.polyEvaluate(sel,b=1)
  109. #API
  110. def getDagPath(sel):
  111. selList = om2.MSelectionList()
  112. selList.add(sel)
  113. selPath = selList.getDagPath(0)
  114. return selPath
  115. def MFnMesh(sel):
  116. selPath = getDagPath(sel)
  117. selMesh = om2.MFnMesh(selPath)
  118. return selMesh
  119. def MItMeshVertex(sel):
  120. selPath = getDagPath(sel)
  121. selVtxIter = om2.MItMeshVertex(selPath)
  122. return selVtxIter
  123. def MItMeshEdge(sel):
  124. selPath = getDagPath(sel)
  125. selEdgeIter = om2.MItMeshEdge(selPath)
  126. return selEdgeIter
  127. def MItMeshPolygon(sel):
  128. selPath = getDagPath(sel)
  129. selPolyIter = om2.MItMeshPolygon(selPath)
  130. return selPolyIter
  131. def MItMeshFaceVertex(sel):
  132. selPath = getDagPath(sel)
  133. selFaceIter = om2.MItMeshFaceVertex(selPath)
  134. return selFaceIter
  135. #UVSet
  136. def switchUVSet(sel):
  137. allUVSets = cmds.polyUVSet(sel,q=1,auv=1)
  138. firstUVSetName = allUVSets[0]
  139. secondUVSetName = allUVSets[1]
  140. currentUVSetName = cmds.polyUVSet(q=1,cuv=1)
  141. try:
  142. cmds.polyUVSet(sel,uvs = firstUVSetName, nuv=secondUVSetName, reorder=1)
  143. except:
  144. newUVSet = cmds.polyUVSet(sel,cr=1)
  145. newUVSet = newUVSet[0]
  146. cmds.polyUVSet(sel, uvs = firstUVSetName ,nuv= newUVSet, cp=1)
  147. cmds.polyUVSet(sel, uvs = secondUVSetName ,nuv= firstUVSetName, cp=1)
  148. cmds.polyUVSet(sel, uvs = newUVSet ,nuv= secondUVSetName, cp=1)
  149. cmds.polyUVSet(sel, uvs = newUVSet, d=1)
  150. if currentUVSetName == firstUVSetName:
  151. cmds.polyUVSet(sel,uvs = secondUVSetName , e=1,cuv=1)
  152. else:
  153. cmds.polyUVSet(sel,uvs = firstUVSetName , e=1,cuv=1)
  154. #UI
  155. def ifExistsWindow(nameOfWindow):
  156. if cmds.window(nameOfWindow,ex=1):
  157. cmds.deleteUI(nameOfWindow)
  158. def toggle(displayName):
  159. if displayName == "hyperShade":
  160. if cmds.window('hyperShadePanel1Window',ex=1):
  161. cmds.deleteUI('hyperShadePanel1Window')
  162. else:
  163. cmds.scriptedPanel("hyperShadePanel1",e=1,to=1)
文档更新时间: 2018-06-19 00:42   作者:刘电