Skip to main content
 首页 » 编程设计

python之使用 python 脚本在 maya 中创建 UI

2024年09月03日33findumars

我正在使用 python 在 maya 中创建一个自定义 UI,但我被困在这条线上发生的这个错误上:

parts = button2.split(",") # NameError: global name 'button2' is not defined  

这是我的脚本:

import maya.cmds as cmds 
 
def createMyLayout(): 
    window = cmds.window(widthHeight=(1000, 600), title="lalala",   resizeToFitChildren=1) 
    cmds.rowLayout("button1, button2, button3", numberOfColumns=5) 
 
    cmds.columnLayout(adjustableColumn=True, columnAlign="center", rowSpacing=10) 
 
    button2 = cmds.textFieldButtonGrp(label="LocatorCurve", 
                                    text="Please key in your coordinates", 
                                    changeCommand=edit_curve, 
                                    buttonLabel="Execute", 
                                    buttonCommand=locator_curve) 
    cmds.setParent(menu=True) 
 
    cmds.showWindow(window) 
 
def locator_curve(*args): 
    # Coordinates of the locator-shaped curve. 
    crv = cmds.curve(degree=1, 
                 point=[(1, 0, 0), 
                        (-1, 0, 0), 
                        (0, 0, 0), 
                        (0, 1, 0), 
                        (0, -1, 0), 
                        (0, 0, 0), 
                        (0, 0, 1), 
                        (0, 0, -1), 
                        (0, 0, 0)]) 
 
 
    return crv 
 
def edit_curve(*args): 
    parts = button2.split(",") 
    print parts 
 
 
createMyLayout()     

基本上,我的脚本试图创建一个 UI,其中包含用于执行操作的按钮。在这种情况下,我试图创建一个文本字段按钮,用户在该按钮中输入一组坐标,并根据给定的一组坐标创建基于定位器的曲线。但是,我只能设法创建一个创建默认曲线的按钮。有人可以告诉我如何着手创建一个按钮,该按钮考虑到一个人给出的坐标并输出特定曲线吗?

请您参考如下方法:

您可以使用全局变量,但更优雅的方法是实现一个类。

下面是一个如何用类解决这个问题的例子。请注意我们如何使用 self.button2 而不仅仅是 button2。这使它成为该类的实例变量并可在其他函数中访问,而 button2 是一个局部变量并且在您离开函数时不再可访问。

import maya.cmds as cmds 
 
class createMyLayoutCls(object): 
    def __init__(self, *args): 
        pass 
    def show(self): 
        self.createMyLayout() 
    def createMyLayout(self): 
        self.window = cmds.window(widthHeight=(1000, 600), title="lalala",   resizeToFitChildren=1) 
        cmds.rowLayout("button1, button2, button3", numberOfColumns=5) 
 
        cmds.columnLayout(adjustableColumn=True, columnAlign="center", rowSpacing=10) 
 
        self.button2 = cmds.textFieldButtonGrp(label="LocatorCurve", 
                                        text="Please key in your coordinates", 
                                        changeCommand=self.edit_curve, 
                                        buttonLabel="Execute", 
                                        buttonCommand=self.locator_curve) 
        cmds.setParent(menu=True) 
 
        cmds.showWindow(self.window) 
 
    def locator_curve(self,*args): 
        # Coordinates of the locator-shaped curve. 
        crv = cmds.curve(degree=1, 
                     point=[(1, 0, 0), 
                            (-1, 0, 0), 
                            (0, 0, 0), 
                            (0, 1, 0), 
                            (0, -1, 0), 
                            (0, 0, 0), 
                            (0, 0, 1), 
                            (0, 0, -1), 
                            (0, 0, 0)]) 
        return crv 
 
    def edit_curve(self,*args): 
        parts = self.button2.split(",") 
        print parts 
        txt_val = cmds.textFieldButtonGrp(self.button2, q=True,text=True) 
        print txt_val 
 
 
b_cls = createMyLayoutCls()   
b_cls.show() 

我知道现在回复太晚了,你现在肯定会成为 Python 的高手 ;)

希望对其他人有所帮助:)