Maya测量尺寸python脚本Mesh Resizer

简介: 能够准确测量Maya软件中模型的尺寸大小。通过打开脚本编辑器,加入python脚本代码,点击模型运行即可。import maya.cmds as cmdsfrom collections import Counterfrom functools import partialJobSelected=cmds.scriptJob(event=["SelectionChanged", "AutoUpdateInfo()"])def KillJob(): cmds.scrip..

 能够准确测量Maya软件中模型的尺寸大小。通过打开脚本编辑器,加入python脚本代码,点击模型运行即可。

 

image.gif编辑

image.gif编辑

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()

image.gif

代码来源:

卢伟国的个人网站 | Mesh Resizer

相关文章
|
27天前
|
数据采集 监控 数据挖掘
Python自动化脚本:高效办公新助手###
本文将带你走进Python自动化脚本的奇妙世界,探索其在提升办公效率中的强大潜力。随着信息技术的飞速发展,重复性工作逐渐被自动化工具取代。Python作为一门简洁而强大的编程语言,凭借其丰富的库支持和易学易用的特点,成为编写自动化脚本的首选。无论是数据处理、文件管理还是网页爬虫,Python都能游刃有余地完成任务,极大地减轻了人工操作的负担。接下来,让我们一起领略Python自动化脚本的魅力,开启高效办公的新篇章。 ###
|
12天前
|
数据采集 存储 监控
21个Python脚本自动执行日常任务(2)
21个Python脚本自动执行日常任务(2)
54 7
21个Python脚本自动执行日常任务(2)
|
2天前
|
数据挖掘 vr&ar C++
让UE自动运行Python脚本:实现与实例解析
本文介绍如何配置Unreal Engine(UE)以自动运行Python脚本,提高开发效率。通过安装Python、配置UE环境及使用第三方插件,实现Python与UE的集成。结合蓝图和C++示例,展示自动化任务处理、关卡生成及数据分析等应用场景。
17 5
|
20天前
|
Android开发 开发者 Python
通过标签清理微信好友:Python自动化脚本解析
微信已成为日常生活中的重要社交工具,但随着使用时间增长,好友列表可能变得臃肿。本文介绍了一个基于 Python 的自动化脚本,利用 `uiautomator2` 库,通过模拟用户操作实现根据标签批量清理微信好友的功能。脚本包括环境准备、类定义、方法实现等部分,详细解析了如何通过标签筛选并删除好友,适合需要批量管理微信好友的用户。
26 7
|
24天前
|
监控 数据挖掘 数据安全/隐私保护
Python脚本:自动化下载视频的日志记录
Python脚本:自动化下载视频的日志记录
|
1月前
|
运维 监控 网络安全
自动化运维的崛起:如何利用Python脚本简化日常任务
【10月更文挑战第43天】在数字化时代的浪潮中,运维工作已从繁琐的手工操作转变为高效的自动化流程。本文将引导您了解如何运用Python编写脚本,以实现日常运维任务的自动化,从而提升工作效率和准确性。我们将通过一个实际案例,展示如何使用Python来自动部署应用、监控服务器状态并生成报告。文章不仅适合运维新手入门,也能为有经验的运维工程师提供新的视角和灵感。
|
1月前
|
存储 Python
Python自动化脚本编写指南
【10月更文挑战第38天】本文旨在为初学者提供一条清晰的路径,通过Python实现日常任务的自动化。我们将从基础语法讲起,逐步引导读者理解如何将代码块组合成有效脚本,并探讨常见错误及调试技巧。文章不仅涉及理论知识,还包括实际案例分析,帮助读者快速入门并提升编程能力。
66 2
|
1月前
|
运维 监控 Python
自动化运维:使用Python脚本简化日常任务
【10月更文挑战第36天】在数字化时代,运维工作的效率和准确性成为企业竞争力的关键。本文将介绍如何通过编写Python脚本来自动化日常的运维任务,不仅提高工作效率,还能降低人为错误的风险。从基础的文件操作到进阶的网络管理,我们将一步步展示Python在自动化运维中的应用,并分享实用的代码示例,帮助读者快速掌握自动化运维的核心技能。
82 3
|
1月前
|
缓存 运维 NoSQL
python常见运维脚本_Python运维常用脚本
python常见运维脚本_Python运维常用脚本
31 3
|
1月前
|
数据采集 JSON 数据安全/隐私保护
Python常用脚本集锦
Python常用脚本集锦
29 2
下一篇
DataWorks