能够准确测量Maya软件中模型的尺寸大小。通过打开脚本编辑器,加入python脚本代码,点击模型运行即可。
编辑
编辑
import maya.cmds as cmds from collections import Counter from functools import partial JobSelected=cmds.scriptJob(event=["SelectionChanged", "AutoUpdateInfo()"]) def KillJob(): cmds.scriptJob( kill=JobSelected, force=True) def AutoUpdateInfo(): ObjectSelect=cmds.ls(sl=True,fl=True) transforms = cmds.listRelatives(ObjectSelect,typ="mesh") yep=cmds.listRelatives(transforms, p=True,pa=True) try: for i in yep: queryNameObject=cmds.text("TitleText",e=True,l=str(i)+" Size") List =cmds.listRelatives(i,f=True) transforms1 = cmds.listRelatives(i,typ="mesh") for j in List: if j.find('MesuresGRP_') != -1 or j.find('_BoundingBox') != -1: sizeX=cmds.getAttr("Distance_X_"+str(transforms1[0])+".distance") sizeY=cmds.getAttr("Distance_Y_"+str(transforms1[0])+".distance") sizeZ=cmds.getAttr("Distance_Z_"+str(transforms1[0])+".distance") round(sizeX,3),round(sizeY,3),round(sizeZ,3) sizeX,sizeY,sizeZ=round(sizeX,3),round(sizeY,3),round(sizeZ,3) cmds.textField("sizeXTextField",e=True,tx=sizeX) cmds.textField("sizeYTextField",e=True,tx=sizeY) cmds.textField("sizeZTextField",e=True,tx=sizeZ) cmds.textField("NewSizeX",e=True,tx=sizeX) cmds.textField("NewSizeY",e=True,tx=sizeY) cmds.textField("NewSizeZ",e=True,tx=sizeZ) else: cmds.textField("sizeXTextField",e=True,tx=0) cmds.textField("sizeYTextField",e=True,tx=0) cmds.textField("sizeZTextField",e=True,tx=0) cmds.textField("NewSizeX",e=True,tx=0) cmds.textField("NewSizeY",e=True,tx=0) cmds.textField("NewSizeZ",e=True,tx=0) except: pass def QueryUnit(): axes=("X","Y","Z") Unit=cmds.currentUnit( query=True, linear=True ) for i in axes: cmds.text("Unit"+str(i),e=True,l=Unit) cmds.text("Unit"+str(i)+"2",e=True,l=Unit) return Unit def QueryUpAxis(): UpAxis=cmds.upAxis( q=True, axis=True ) return UpAxis def GetBoudingBoxInfo(selected): bbox = cmds.exactWorldBoundingBox(selected ,ce=True,ii=True) return bbox def SizeObject(selected): infos=GetBoudingBoxInfo(selected) sizeX=infos[3]-infos[0] sizeY=infos[4]-infos[1] sizeZ=infos[5]-infos[2] return round(sizeX,3),round(sizeY,3),round(sizeZ,3) def homothetieCheck(info,*args): listTextField=["NewSizeX","NewSizeY","NewSizeZ"] QueryHomothetie=cmds.checkBox("Homothetie",q=True,v=True) listTextField.remove(info) def RoutineResizeObject(NewSize,*args): HomthetySize=0 NewSizeX=cmds.textField("NewSizeX",q=True,tx=True) NewSizeY=cmds.textField("NewSizeY",q=True,tx=True) NewSizeZ=cmds.textField("NewSizeZ",q=True,tx=True) if NewSize=="NewSizeX": HomthetySize=NewSizeX if NewSize=="NewSizeY": HomthetySize=NewSizeY if NewSize=="NewSizeZ": HomthetySize=NewSizeZ selected=cmds.ls(sl=True,fl=True) try: for Sel in selected: List =cmds.listRelatives(Sel,f=True) transforms = cmds.listRelatives(Sel,typ="mesh") print transforms sizeX=cmds.getAttr("Distance_X_"+str(transforms[0])+".distance") sizeY=cmds.getAttr("Distance_Y_"+str(transforms[0])+".distance") sizeZ=cmds.getAttr("Distance_Z_"+str(transforms[0])+".distance") for j in List: if j.find('MesuresGRP_') != -1: cmds.delete(j) resizeObject(Sel,sizeX,sizeY,sizeZ,NewSizeX,NewSizeY,NewSizeZ,NewSize,HomthetySize) except: pass cmds.select(selected) RoutineGetObjectSize() def resizeObject(object,sizeX,sizeY,sizeZ,NewSizeX,NewSizeY,NewSizeZ,NewSize,HomthetySize): ListResults=[] QueryHomothetie=cmds.checkBox("Homothetie",q=True,v=True) Valide=False print sizeX,sizeY,sizeZ if NewSize=="NewSizeX": NewSizeX=HomthetySize else: NewSizeX=sizeX if NewSize=="NewSizeY": NewSizeY=HomthetySize else: NewSizeY=sizeY if NewSize=="NewSizeZ": NewSizeZ=HomthetySize else: NewSizeZ=sizeZ if NewSizeX =="1.0": resultX=1.0 ListResults.append(resultX) else: resultX=(float(NewSizeX) / float(sizeX)) ListResults.append(resultX) if NewSizeY =="1.0": resultY=1.0 ListResults.append(resultY) else: resultY=(float(NewSizeY) / float(sizeY)) ListResults.append(resultY) if NewSizeZ =="1.0": resultZ=1.0 ListResults.append(resultZ) else: resultZ=(float(NewSizeZ) / float(sizeZ)) ListResults.append(resultZ) CounterList=Counter(ListResults) if QueryHomothetie==True: MultiplyResult=resultX*resultY*resultZ cmds.scale(MultiplyResult,MultiplyResult,MultiplyResult,object,r=True) else: cmds.scale(resultX,resultY,resultZ,object,r=True) def SetInfoBoungingBox(selected): sizeX,sizeY,sizeZ=SizeObject(selected) cmds.textField("sizeXTextField",e=True,tx=sizeX) cmds.textField("sizeYTextField",e=True,tx=sizeY) cmds.textField("sizeZTextField",e=True,tx=sizeZ) def SetInfoBoungingBoxNewSize(selected): sizeX,sizeY,sizeZ=SizeObject(selected) cmds.textField("NewSizeX",e=True,tx=sizeX) cmds.textField("NewSizeY",e=True,tx=sizeY) cmds.textField("NewSizeZ",e=True,tx=sizeZ) def DistanceDimension(ObjectSelect): ObjectSelected=ObjectSelect Bbox=cmds.geomToBBox(name=ObjectSelect, nameSuffix='_BoundingBox', shaderColor=[0,1,0.043],ko=True) cmds.setAttr(str(Bbox[0])+".template",1) infos=GetBoudingBoxInfo(ObjectSelect) print infos DistanceX=cmds.distanceDimension(startPoint=[infos[0], infos[1], infos[2]],endPoint=[infos[3], infos[1], infos[2]]) Loc =cmds.listConnections(DistanceX) DistanceOrigine=cmds.rename(Loc[0],"Loc_Origine_"+str(ObjectSelect)) cmds.setAttr(DistanceOrigine+".visibility",0) DistanceX=cmds.rename(Loc[1],"Loc_X_"+str(ObjectSelect)) cmds.setAttr(DistanceX+".visibility",0) sel=cmds.ls(sl=True) LocX=cmds.rename(sel[1],"Distance_X_"+str(ObjectSelect)) X_GRP=cmds.group(DistanceX,LocX,n="X_"+str(ObjectSelect)) DistanceY=cmds.distanceDimension(startPoint=[infos[0], infos[1], infos[2]],endPoint=[infos[0], infos[4], infos[2]]) Loc =cmds.listConnections(DistanceY) DistanceOrigine=cmds.rename(Loc[0],"Loc_Origine_"+str(ObjectSelect)) DistanceY=cmds.rename(Loc[1],"Loc_Y_"+str(ObjectSelect)) cmds.setAttr(DistanceY+".visibility",0) sel=cmds.ls(sl=True) LocY=cmds.rename(sel[1],"Distance_Y_"+str(ObjectSelect)) Y_GRP=cmds.group(DistanceY,LocY,n="Y_"+str(ObjectSelect)) DistanceZ=cmds.distanceDimension(startPoint=[infos[0], infos[1], infos[2]],endPoint=[infos[0], infos[1], infos[5]]) Loc = cmds.listConnections(DistanceZ) DistanceOrigine=cmds.rename(Loc[0],"Loc_Origine_"+str(ObjectSelect)) DistanceZ=cmds.rename(Loc[1],"Loc_Z_"+str(ObjectSelect)) cmds.setAttr(DistanceZ+".visibility",0) sel=cmds.ls(sl=True) LocZ=cmds.rename(sel[1],"Distance_Z_"+str(ObjectSelect)) Z_GRP=cmds.group(DistanceZ,LocZ,n="Z_"+str(ObjectSelect)) cmds.parent((DistanceOrigine,X_GRP,Y_GRP,Z_GRP,Bbox[0]),ObjectSelect) Mesures_GRP=cmds.group(DistanceOrigine,X_GRP,Y_GRP,Z_GRP,n="MesuresGRP_"+str(ObjectSelect)) def RoutineGetObjectSize(): ObjectSelect=cmds.ls(sl=True,fl=True) print ObjectSelect transforms = cmds.listRelatives(ObjectSelect,typ="mesh") yep=cmds.listRelatives(transforms, p=True,pa=True) try: for i in yep: queryNameObject=cmds.text("TitleText",e=True,l=str(i)+" Size") List =cmds.listRelatives(i,f=True) for j in List: if j.find('MesuresGRP_') != -1 or j.find('_BoundingBox') != -1: cmds.delete(j) else: pass cmds.select(i) SetInfoBoungingBox(i) SetInfoBoungingBoxNewSize(i) DistanceDimension(i) cmds.select(ObjectSelect) except: pass def ShowHideMesure(): ObjectSelect=cmds.ls(sl=True,fl=True) try: for i in ObjectSelect: visibility=cmds.getAttr("MesuresGRP_"+str(i)+".visibility") if visibility==True: cmds.setAttr("MesuresGRP_"+str(i)+".visibility",0) if visibility==False: cmds.setAttr("MesuresGRP_"+str(i)+".visibility",1) except: pass def ShowHideBbox(): ObjectSelect=cmds.ls(sl=True,fl=True) try: for i in ObjectSelect: visibility=cmds.getAttr(str(i)+"_BoundingBox.visibility") if visibility==True: cmds.setAttr(str(i)+"_BoundingBox.visibility",0) if visibility==False: cmds.setAttr(str(i)+"_BoundingBox.visibility",1) except: pass def clearMesh(): ObjectSelect=cmds.ls(sl=True,fl=True) print ObjectSelect try: for i in ObjectSelect: List =cmds.listRelatives(i,f=True) for j in List: if j.find('MesuresGRP_') != -1 or j.find('_BoundingBox') != -1: cmds.delete(j) else: pass except: pass def MovePivot(Vector,*args): ObjectSelect=cmds.ls(sl=True,fl=True) for Object in ObjectSelect: clearMesh() infos=GetBoudingBoxInfo(Object) Xmin=infos[0] Xmidd=infos[3]-((infos[3]-infos[0])/2) Xmax=infos[3] Ymin=infos[1] Ymidd=infos[4]-((infos[4]-infos[1])/2) Ymax=infos[4] Zmin=infos[2] Zmidd=infos[5]-((infos[5]-infos[2])/2) Zmax=infos[5] if Vector == "Xmin": cmds.move(Xmin,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, x=True) if Vector == "Xmidd": cmds.move(Xmidd,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, x=True) if Vector == "Xmax": cmds.move(Xmax,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, x=True) if Vector == "Ymin": cmds.move(Ymin,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, y=True) if Vector == "Ymidd": cmds.move(Ymidd,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, y=True) if Vector == "Ymax": cmds.move(Ymax,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, y=True) if Vector == "Zmin": cmds.move(Zmin,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, z=True) if Vector == "Zmidd": cmds.move(Zmidd,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, z=True) if Vector == "Zmax": cmds.move(Zmax,str(Object)+".scalePivot", str(Object)+".rotatePivot", a=True, z=True) cmds.select(ObjectSelect) RoutineGetObjectSize() def CloseWindow(): cmds.deleteUI('ResizeObject',window=True ) def UI(): if (cmds.window('ResizeObject', exists=True)): print "window already open!!" cmds.deleteUI('ResizeObject') win= cmds.window("ResizeObject",title="Mesh Resizer",cc="KillJob()",s=False, mnb=True,mxb=False, rtf = True, w=500) cmds.columnLayout(adj=True) cmds.text("TitleText",l="Mesh Resizer",h=25, fn = "boldLabelFont") cmds.rowColumnLayout(numberOfColumns=3,columnWidth=[(1, 133), (2, 133), (3, 133)]) cmds.text(l="Size X",h=25, fn = "boldLabelFont") cmds.text(l="Size Y",h=25, fn = "boldLabelFont") cmds.text(l="Size Z",h=25, fn = "boldLabelFont") cmds.setParent('..') cmds.rowColumnLayout(numberOfColumns=6,columnWidth=[(1, 100), (2, 33), (3, 100),(4, 33), (5, 100), (6, 33)]) cmds.textField("sizeXTextField",ed=False,bgc=(0.2,0.2,0.2)) cmds.text("UnitX2") cmds.textField("sizeYTextField",ed=False,bgc=(0.2,0.2,0.2)) cmds.text("UnitY2") cmds.textField("sizeZTextField",ed=False,bgc=(0.2,0.2,0.2)) cmds.text("UnitZ2") cmds.setParent('..') cmds.separator(style='in' ) cmds.button(l="Get Object Size" ,c="RoutineGetObjectSize()",h=40) cmds.separator(style='in' ) cmds.rowColumnLayout(numberOfColumns=2,columnWidth=[(1,200), (2,200)]) cmds.button(l="show/Hide Mesures" ,c="ShowHideMesure()",h=40) cmds.button(l="show/Hide Bounding Box" ,c="ShowHideBbox()",h=40) cmds.setParent('..') cmds.separator(style='in' ) cmds.button(l="Clear Mesh" ,c="clearMesh()") cmds.separator(style='in' ) #----------------------------------------------------------------------------------------------------------------------- cmds.frameLayout(l="Move Pivot",cll=False,bgc=(0.15,0.15,0.15),li=160) cmds.rowColumnLayout(numberOfColumns=3,columnWidth=[(1, 133), (2,133 ), (3, 133)]) ButtonH=40 cmds.button(l="X min" ,bgc=(0.5,0.0,0.0),c=partial(MovePivot,'Xmin'),h=ButtonH) cmds.button(l="X midd" ,bgc=(0.5,0.0,0.0),c=partial(MovePivot,'Xmidd'),h=ButtonH) cmds.button(l="X max" ,bgc=(0.5,0.0,0.0),c=partial(MovePivot,'Xmax'),h=ButtonH) cmds.button(l="Y min" ,bgc=(0.0,0.5,0.0),c=partial(MovePivot,'Ymin'),h=ButtonH) cmds.button(l="Y midd" ,bgc=(0.0,0.5,0.0),c=partial(MovePivot,'Ymidd'),h=ButtonH) cmds.button(l="Y max" ,bgc=(0.0,0.5,0.0),c=partial(MovePivot,'Ymax'),h=ButtonH) cmds.button(l="Z min" ,bgc=(0.0,0.0,0.5),c=partial(MovePivot,'Zmin'),h=ButtonH) cmds.button(l="Z midd" ,bgc=(0.0,0.0,0.5),c=partial(MovePivot,'Zmidd'),h=ButtonH) cmds.button(l="Z max" ,bgc=(0.0,0.0,0.5),c=partial(MovePivot,'Zmax'),h=ButtonH) cmds.setParent('..') cmds.setParent('..') cmds.separator(style='in' ) #----------------------------------------------------------------------------------------------------------------------- cmds.frameLayout(l="Resize Objects",cll=False,bgc=(0.15,0.15,0.15),li=160) cmds.rowColumnLayout(numberOfColumns=3,columnWidth=[(1, 133), (2, 133), (3, 133)]) cmds.text(l="New Size X",h=25, fn = "boldLabelFont") cmds.text(l="New Size Y",h=25, fn = "boldLabelFont") cmds.text(l="New Size Z",h=25, fn = "boldLabelFont") cmds.setParent('..') cmds.rowColumnLayout(numberOfColumns=6,columnWidth=[(1, 100), (2, 33), (3, 100),(4, 33), (5, 100), (6, 33)]) cmds.textField("NewSizeX",cc=partial(RoutineResizeObject,'NewSizeX')) cmds.text("UnitX") cmds.textField("NewSizeY",cc=partial(RoutineResizeObject,'NewSizeY')) cmds.text("UnitY") cmds.textField("NewSizeZ",cc=partial(RoutineResizeObject,'NewSizeZ')) cmds.text("UnitZ") cmds.setParent('..') cmds.checkBox("Homothetie",l="homothety",onc="CheckBoxHomothetie()") cmds.setParent('..') #----------------------------------------------------------------------------------------------------------------------- cmds.separator(style='in' ) cmds.button(l="close",c="CloseWindow()",h=40) cmds.setParent('..') cmds.showWindow(win) cmds.window("ResizeObject",edit = True, wh = (400,485)) UI() QueryUnit()
代码来源: