一半君的总结纸

听话只听一半君

依然是中古时代的maya python写UI的方法总结纸

这个页面没什么意义,就是直接把 中古时代的用MEL写ui的方法简介 的方法翻译成Maya Python,毫无意义…

在此列出mel版本,以示对比

global proc fm_delSelected(string $list){
    string $sel[]=`textScrollList -q -si $list`;
    for($s in $sel){
	    textScrollList -e -ri $s $list;
	    delete $s;
	}
}

global proc fm_loadSelected(string $list){
    string $sel[]=`ls -sl`;
    for($s in $sel){
	    textScrollList -e -a $s $list;    
	}
}

global proc fuckme(){
    if (`window -ex fuckMeWnd`)
        deleteUI fuckMeWnd;
    window -t "This is a fuckme tool ver 0.1 " fuckMeWnd;
        string $form=`formLayout`;
            string $list=`textScrollList`;
            string $makeCubeBtn = `button -l "Make Cube" -c "polyCube"`;
            string $makeSphereBtn=`button -l "Make Sphere" -c "polySphere"`;
            string $loadSelectedBtn=`button -l "Load Selected" -c ("fm_loadSelected(\""+$list+"\")")`;
            string $clearBtn=`button -l "Clear" -c ("textScrollList -e -ra "+$list)`;
            string $delSelectedBtn=`button -l "Delete Selected Items" -c ("fm_delSelected(\""+$list+"\")")`;
            
        formLayout -e
            // 让 list 吸附到form的顶部/左边/右边
            -af $list "left" 0
            -af $list "top" 0
            -ap $list "right" 0 75
        
            // 让 makeCubeBtn的左边 吸附到 list
            -ac $makeCubeBtn "left" 2 $list
            -af $makeCubeBtn "right" 2
            -ap $makeCubeBtn "top" 0 20
            
            // 让 makeSphereBtn的左边 吸附到 list,顶部吸附到makeCubeBtn
            -ac $makeSphereBtn "left" 2 $list
            -ac $makeSphereBtn "top" 10 $makeCubeBtn
            -af $makeSphereBtn "right" 2
            
            // 让 loadSelectedBtn的左边 吸附到 list,顶部吸附到makeSphereBtn
            -ac $loadSelectedBtn "left" 2 $list
            -ac $loadSelectedBtn "top" 10 $makeSphereBtn
            -af $loadSelectedBtn "right" 2
           
            // 让 clearBtn的左边 吸附到 list,顶部吸附到loadSelectedBtn
            -ac $clearBtn "left" 2 $list
            -ac $clearBtn "top" 10 $loadSelectedBtn
            -af $clearBtn "right" 2
 
            // delSelectedBtn 的左右侧和底部都吸附到form
            -af $delSelectedBtn "left" 2
            -af $delSelectedBtn "right" 2
            -af $delSelectedBtn "bottom" 2
            
            // list的底边吸附到 delSelectedBtn
            -ac $list "bottom" 2 $delSelectedBtn
                                                           
            $form;
            
    showWindow;
}

翻译成maya python后稍有不同(存成fuckme.py):

import maya.cmds as cmds

def fm_delSelected(scrollList):
    sel=cmds.textScrollList(scrollList, q=True, si=True)
    for s in sel:
	    cmds.textScrollList(scrollList, e=True, ri=s)
	    cmds.delete(s)


def fm_loadSelected(scrollList):
    sel=cmds.ls(sl=True)
    for s in sel:
	    cmds.textScrollList(scrollList, e=True, a=s)

def run():
    if cmds.window('fuckMeWnd',ex=True):
        cmds.deleteUI('fuckMeWnd')
    cmds.window('fuckMeWnd',t='This is a fuckme tool ver 0.1')
    form=cmds.formLayout()
    scrollList=cmds.textScrollList()
    makeCubeBtn=cmds.button(l='Make Cube',c='cmds.polyCube()')
    makeSphereBtn=cmds.button(l='Make Sphere',c='cmds.polySphere()')
    loadSelectedBtn=cmds.button(l='Load Selected',c=lambda *args:fm_loadSelected(scrollList))
    clearBtn=cmds.button(l='Clear',c=lambda *args: cmds.textScrollList(scrollList,e=True,ra=True))
    delSelectedBtn=cmds.button(l='Delete Selected Items',c=lambda *args:fm_delSelected(scrollList))
    
    cmds.formLayout(form,e=True,
            af=[
            (scrollList, 'left', 0),
            (scrollList, 'top', 0),
            
            (makeCubeBtn, 'right', 2),
            
            (makeSphereBtn, 'right', 2),
            
            (loadSelectedBtn, 'right', 2),
            
            (clearBtn, 'right', 2),
            
            (delSelectedBtn, 'left', 2),
            (delSelectedBtn, 'right', 2),
            (delSelectedBtn, 'bottom', 2)
            ],
            ap=[(scrollList,'right',0,75),(makeCubeBtn,'top',0,20)],
            ac=[
            (makeCubeBtn,'left',2,scrollList),
            
            (makeSphereBtn,'left',2,scrollList),
            (makeSphereBtn,'top',10,makeCubeBtn),
            
            (loadSelectedBtn,'left',2,scrollList),
            (loadSelectedBtn,'top',10,makeSphereBtn),
            
            (clearBtn,'left',2,scrollList),
            (clearBtn,'top',10,loadSelectedBtn),
            
            (scrollList,'bottom',2,delSelectedBtn)
            ]
            )
            
    cmds.showWindow()

主要不同在于,-c 后面跟的是个lambda的匿名function,至于为什么要这样,而不是直接用c=’fm_loadSelected()’,可以参见pymel的Building User Interfaces解说

用的时候要像下面这样:

import maya.cmds as cmds
import fuckme
reload(fuckme)
fuckme.run()

最终效果:
python_ui

Advertisements

One response to “依然是中古时代的maya python写UI的方法总结纸

  1. ringing in the ears remedies 六月 28, 2014 @ 7:51 上午

    Useful information. Fortunate me I found your site by accident, and I’m surprised why this coincidence did not came about earlier!

    I bookmarked it.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: