How to create Maya sliders to move objects in interface

我只是一个虾纸丫 提交于 2019-12-31 05:20:09

问题


I am trying to use sliders to move objects along the x,y and z axis. This is my code so far:

import maya.cmds as cmds

cmds.columnLayout( adjustableColumn=True )                                                

cmds.intSlider(min=-100, max=100, value=0, step=1, dc = cmds.move(x=True))

cmds.showWindow()

I keep getting this error

# Error: line 1: TypeError: file <maya console> line 10: Invalid arguments for flag 'dc'.  Expected string or function, got NoneType # 

I am very new to Python so I am not sure what it means. Many Thanks, Martyn


回答1:


you have to create a function containing cmds.move()

import maya.cmds as cmds
from functools import partial

# Create the function to move
def moveXYZ(slider, *args, **kwargs):
    # slider is the name of the controller
    # *args is to avoid a maya default argument passed trhought functions
    # **kwargs is used to pass maya flags to the function in order to chose X, Y or Z axis

    # the slider value is queried each time in order to refresh the value
    value = getSliderValue(slider)
    cmds.move(value, **kwargs)

# slider get value, can fit any slider if you provide the name
def getSliderValue(ctrlName):
    value = cmds.intSlider(ctrlName, q=True, value=True)
    return value

#basic window
cmds.window()    
cmds.columnLayout( adjustableColumn=True )                                                
# create the slider with a placeholder function
mySlider = cmds.intSlider(min=-100, max=100, value=0, step=1, dc = 'empty')
# now that the var mySlider is created, we can pass it as argument for our function
# let's edit the function with functools.partial that allow us to pass some arguments to a function
# Here we pass moveXYZ function then mySlider, the name of the X slider then x=1 to pass the flag to cmds.move(x=1)
cmds.intSlider(mySlider, e=True, dc = partial(moveXYZ, mySlider, x=1))

#redo it for all sliders
# mySlider should be a dictionnary or at least a global, i.e :
# uiDic = {}
# uiDic['xslider'] = cmds.intSlider(min=-100, max=100, value=0, step=1, dc = 'empty')
# cmds.intSlider(uiDic['xslider'], e=True, dc = partial(moveXYZ, uiDic['xslider'], x=1))

cmds.showWindow()

I can explain any part you would not understand



来源:https://stackoverflow.com/questions/43255426/how-to-create-maya-sliders-to-move-objects-in-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!