Using int slider to move object along XYZ axis eror


#1

Hi there I am trying to move an object around the XYZ axis using the int sliders. I am doing it for a school assignment and I have to make a window that allows you to move objects around. My code so far:

import maya.cmds as cmds  

cmds.window()
cmds.columnLayout( adjustableColumn=True )                                                 
cmds.intSlider(min=-360, max=360, value=0, step=1, dc = cmds.move(x=True))
cmds.showWindow()

I keep getting this error message

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

Any idea what’s causing this? or how to fix it?


#2

These UI commands that take functions as arguments have some limitations with maya.cmds. You need to either pass the function as a string:

dc = 'cmds.move(x=True)'

or, the most robust way as I understand it, use the “partial” function from functools:

from functools import partial
# ...
dc = partial(cmds.move,x=True)

#3

Hi Spire 2
Thanks for the feedback but when I run the code, the slider comes up but it doesn’t do anything. Just moves and doesn’t move a cube inside maya.


#4

You have to tell it where you want it to move :slight_smile: If you select your cube and just on the mel command line type “move -x”, nothing happens either, right? But if you type “move 1 -x” the cube will move 1 unit on the +x axis.

You’ll have to figure out how you want to get the value from the slider. What you really need is a wrapper function, for example:

import maya.cmds as cmds
from functools import partial

def moveIt(*args):
    cmds.move(args[-1], x=True)
    
win = 'IntSliderTest'
if cmds.window(win, exists=True):
    cmds.deleteUI(win, window=True)
cmds.window(win)
cmds.columnLayout( adjustableColumn=True )                                                 
cmds.intSlider(min=-10, max=10, value=0, step=1, dc = partial(moveIt))
cmds.showWindow()

This works because intSlider automatically sends its value in the arg list. As it is now, you could just use a function pointer instead of partial, since you don’t need any args of your own (i.e. just say “dc = moveIt”). But partial is more flexible because you can add args if necessary.

If you really wanted to use a string for some reason, you would have to get the value of the slider yourself by querying its -v flag. That means the wrapper function would have to know the name of the slider, either by passing it in or some other construct.