一半君的总结纸

听话只听一半君

中古时代后期的用pymel写UI的方法总结纸

同样,没有什么意义,如果你只是用pymel来做个ui的话,那他在这方面没体现出什么优势. 下面是直接把 依然是中古时代的maya python写ui的方法简介 的方法翻译成pymel,仅此而已… 在此列出python版本,以示对比

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

而转换成pymel版本的话: 其实你如果把第一行换成

import pymel.core as pm

然后搜索替换所有cmds.为pm. ,就变成pymel版本了,但是那样没什么意义,所以下面我把某些地方改写的更能体现出pymel的优势一点,用术语说就是,某些地方可以更pythonic一点(其实并没有…)

import pymel.core as pm

def fm_delSelected(scrollList):
    for s in scrollList.getSelectItem():
        scrollList.removeItem(s)
        pm.delete(s)

def fm_loadSelected(scrollList):
    for s in pm.selected():
        scrollList.append(s)

def run():
    if 'fuckMeWnd' in pm.lsUI(type='window'):
        pm.deleteUI('fuckMeWnd')

    with pm.window('fuckMeWnd') as win:
        win.setTitle('This is a fuckme tool ver 0.1')
        with pm.formLayout() as form:
            scrollList=pm.textScrollList()
            # makeCubeBtn=pm.button(l='Make Cube',c='pm.polyCube()')
            makeCubeBtn=pm.button().setLabel('Make Cube').setCommand('pm.polyCube()')
            makeSphereBtn=pm.button().setLabel('Make Sphere').setCommand('pm.polySphere()')
            loadSelectedBtn=pm.button().setLabel('Load Selected').setCommand(pm.Callback(fm_loadSelected,scrollList))
            clearBtn=pm.button().setLabel('Clear').setCommand(lambda *args: scrollList.removeAll())
            delSelectedBtn=pm.button().setLabel('Delete Selected Items').setCommand(pm.Callback(fm_delSelected,scrollList))

            pm.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)
                    ]
                    )

        pm.showWindow()
  • 上面的例子里,我尽量没有使用maya python的格式,而是按照pymel的往使用object的方向上靠,formLayout -e 的这些因为太长了懒得改了,想改成form.attachForm(…)这样的格式也是可以的
  • with … as … 这个叫做context manager的写法是python2.6以后新加的,当使用多个layout嵌套的时候,可以避免使用setParent,使结构更清晰,用法见pymel doc

最终效果:

python_ui

Advertisements

发表评论

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 博主赞过: