Maya Python:当在另一个函数中创建该对象时,如何在一个函数中调用和编辑对象的属性?

时间:2015-04-24 06:02:28

标签: python maya

计划:

我一直在研究一个脚本,用于测量用户选择的2个顶点之间的距离,并根据这些顶点之间的所需长度来放大或缩小对象。

问题:

错误表明它无法找到我在其中一个函数中创建的textFieldButtonGrp对象。

我基本上把我的窗口格式的东西放到一个函数中:

def window_presets():
    '''
    presets for UI window
    '''
    if mc.window("Distance Scale Tool", exists=True):
        mc.deleteUI("Distance Scale Tool")
    mc.window("Distance Scale Tool", t="Distance Based Scale Tool")
    mc.rowColumnLayout(numberOfColumns=2, 
                      columnAttach=(1, 'left', 0), 
                      columnWidth=[(1,100), (2,300)])
    mc.text(l="Current Length")
    current_length = mc.textFieldButtonGrp("Current Length", 
                                          editable=False, 
                                          text="{0}".format(refresh_current_dist()), 
                                          buttonLabel="Refresh", 
                                          buttonCommand=refresh_current_dist)

    mc.text(l="Desired Length")
    desired_length = mc.textFieldButtonGrp("Desired Length", 
                                          buttonLabel="Scale",  
                                          buttonCommand=scale_dist, 
                                          tcc=refresh_scale_factor)

    mc.showWindow()

我希望刷新按钮调用另一个编辑我创建的textFieldButtonGrp的函数:

def refresh_textfield(distance):
    if mc.textFieldButtonGrp("Current Length", exists=True):
        mc.textFieldButtonGrp("Current Length", 
                              edit=True, 
                              text="{0}".format(distance))   
    else:
        print "Current Length dont exist"

但是"当前长度" ....它似乎不存在......

与"期望长度相同" ....

以下是完整的剧本:

##  ((Ax - Bx)**2 + (Ay - By)**2 + (Az - Bz)**2)**0.5

import maya.cmds as mc
import math

def window_presets():
    '''
    presets for UI window
    '''
    if mc.window("Distance Scale Tool", exists=True):
        mc.deleteUI("Distance Scale Tool")
    mc.window("Distance Scale Tool", t="Distance Based Scale Tool")
    mc.rowColumnLayout(numberOfColumns=2, 
                      columnAttach=(1, 'left', 0), 
                      columnWidth=[(1,100), (2,300)])
    mc.text(l="Current Length")
    current_length = mc.textFieldButtonGrp("Current Length", 
                                          editable=False, 
                                          text="{0}".format(refresh_current_dist()), 
                                          buttonLabel="Refresh",                          buttonCommand=refresh_current_dist)

    mc.text(l="Desired Length")
    desired_length = mc.textFieldButtonGrp("Desired Length", 
                                          buttonLabel="Scale",  
                                          buttonCommand=scale_dist, 
                                          tcc=refresh_scale_factor)

    mc.showWindow()


def get_object_name():
    selPoints = mc.ls(sl=True)
    obj_name = selPoints[0].split('.')[0]
    return obj_name

def get_coordinates():
    '''
    Gets coordinates of selected points and gets distance between them
    '''
    selPoints = mc.ls(sl=True)
    obj_name = get_object_name()
    print obj_name
    vtxCoordList = mc.xform(selPoints, 
                            query=True, 
                            translation=True, 
                            ws=True)
    Ax, Ay, Az = vtxCoordList[:-3]
    Bx, By, Bz = vtxCoordList[3:]

    return (Ax, Bx, Ay, By, Az, Bz)

def calculate_distance(Ax, Bx, Ay, By, Az, Bz):
    '''
    Determines distance between 2 coordinates on single mesh.

    Below are formulas for distance based on single axis:

    dx = ((Ax - Bx)**2)**0.5
    print "Distance on X axis is: {0}".format(dx) #distance on X axis
    dy = ((Ay - By)**2)**0.5
    print "Distance on Y axis is: {0}".format(dy) #distance on Y axis
    dz = ((Az - Bz)**2)**0.5
    print "Distance on Z axis is: {0}".format(dz) #distance on Z axis

    '''
    distance = math.sqrt((Ax - Bx)**2 + (Ay - By)**2 + (Az - Bz)**2)

    print "the distance between points is {0}".format(distance)

    return distance

def refresh_textfield(distance):
    if mc.textFieldButtonGrp("Current Length", exists=True):
        mc.textFieldButtonGrp("Current Length", 
                              edit=True, 
                              text="{0}".format(distance))   
    else:
        print "Current Length dont exist"
def refresh_current_dist():
    '''
    returns current distance
    '''

    current_coordinates = get_coordinates()
    current_distance = calculate_distance(*current_coordinates)

    refresh_textfield(current_distance)

    return current_distance



def refresh_scale_factor(sf):
    '''
    returns factor by which object will be scaled 
    ''' 

    current_distance = refresh_current_dist()

    scale_factor = (float(sf))/(float(current_distance))
    print "dist btwn pnts is d: {0}".format(current_distance)
    print "sf is {0}".format(sf)
    print "user input is {0}".format(sf)
    print "scale factor is {0}".format(scale_factor)
    print "-"*10
    return scale_factor


def scale_dist():
    '''
    scale object to match measurement
    '''
    user_input = float(mc.textFieldButtonGrp("Desired Length",
                                             query=True, 
                                             text=True))
    scale_factor = refreshScaleFactor(user_input)                                                                                          
    mc.makeIdentity(get_object_name(), 
                    apply=True, 
                    translate=1, 
                    rotate=1, 
                    scale=1, 
                    normal=0, 
                    preserveNormals=1)#freeze transformations
    mc.DeleteAllHistory()     
    mc.scale(scale_factor, scale_factor, scale_factor, get_object_name())
    print "you scaled by {0}".format(scale_factor)
    mc.makeIdentity(get_object_name(), 
                    apply = True, 
                    translate=1, 
                    rotate=1, 
                    scale=1, 
                    normal=0, 
                    preserveNormals=1)#freeze transformations



if __name__ == '__main__':
    window_presets()

3 个答案:

答案 0 :(得分:1)

<强>解决方案:

删除"Current Length"中的空格,这样可以解决您的错误。

命名说明:

考虑应用与您在函数上使用的标签相同的标签。我通常用这种方式命名:

"<mine or company initials>_<ToolName>_<WidgetName>"

在你的情况下,这将是"ak_VertexDistance_InputCurrentLength"

为何命名? 几个月前,我正在编写一个脚本来保存网络上某处的Maya场景。我试图将一些项目添加到optionMenu到我的窗口,但无论我尝试什么,optionMenu都是空的。经过两个小时的不成功研究后,我意识到该项目已添加到我的其他工具中的其他optionMenu。小部件具有相同的通用名称。

您的首字母是可选的,但如果您想要区分不同工具的小部件,我认为必须添加<ToolName>

答案 1 :(得分:0)

我可以通过两种方式解决这个问题:

第一个是创建一个围绕所有函数的CLASS,并将textFieldButtonGrp保存在类变量self.textField = textFieldButtonGrp(...)中。 这意味着创建一个封装所有函数的类,将__init__(self,*args)函数与其余函数一起添加,并可能添加self。它失败的地方(函数调用,调用变量......)。而且,作为一个加号,您将能够在类变量中保存UI中的每个项目,并使用self.variableName在任何函数中使用它

第二个 aproach是创建textField,将其保存在变量中,然后使用buttonCommand=partial(functionname,variable)

将其作为参数传递

部分来自:

from functools import partial

但是这样做的方法是先创建没有buttonCommand的文本字段,然后使用编辑模式(不是创建模式)Short explanation about execution modes

执行textField的创建。

textFieldButtonGrp(variableName, edit=True, buttonCommand=partial(function,variableName)

所以你编辑文本字段后再添加这个按钮命令。

我还没有测试过这个,但我确定它们中的任何一个都能正常工作,尤其是第一个。 希望这会有所帮助。

答案 2 :(得分:0)

@DrHaze是正确的,您的UI元素名称中不会有空格:创建时

一般而言。使用类是确保您的函数可以访问所需的UI元素的最简单方法:请参阅herehere以及扩展讨论here

如果您不想使用类,则需要确保函数获取UI元素的真实名称,而不是您期望的名称。