Basic Python assistance


#1

I’m currently making a tool in my lunch that gets a value of a selected object then moves it by 5 (Simple but trying to get fundamentals down…

This is my code section I need help with

def By5() :
cmds.ls(selection=True)
mc.move( 0, translate_x_value + 5, 0 )

selected = cmds.ls(selection=True)

for item in selected:
translate_x_value = cmds.getAttr("%s.translateX" % item)
# do something with the value. egs:
print translate_x_value

The issue I’m having that once its been ran once (With GUI and a button to run the command, it’ll move by 5 once and no more…

I don’t know much about python but do I need to run a loop to request the position of cube constantly?

Thanks and apologies for the noob question


#2

I did just realize I don’t seem to need the code below

def By5() :
cmds.ls(selection=True)
mc.move( 0, translate_x_value + 5, 0 )

I’ve tried using getattr but still having issues.


#3

By default, the move command moves the object in worldspace. So by moving it ‘5’, you’re moving it to the coordinate 5 on X (which is default axis on move command if you don’t specify).

You can add the ‘relative/r’ flag to move in a direction relative to it’s current position. You should also assign a variable to your selection unless you’re using cmds.ls in the move command
So you can either do:

def By5():
    sel=cmds.ls(selection=True)
    mc.move(sel, 5, relative=True)

or

def By5():
    mc.move(cmds.ls(selection=True), 5, relative=True)

Hope that helps you understand!


#4

Thankyou for this! Its exactly what I needed!