My first use of classes with Python. Very simple code which I feel may be further simplified. Please break my code or give me input on where it could have been better/efficient.
import maya.cmds as mc
from functools import partial
class MoveSelTool(object):
'''Tool for moving selected object or objects by a
value specified by user on the X, Y, and Z axis.'''
def __init__(self, *args):
pass
if(mc.window("ak_move_sel_tool_window", query=True, exists=True)):
mc.deleteUI("ak_move_sel_tool_window")
def build_window_UI(self):
'''Builds the window for GUI'''
self.window = mc.window("ak_move_sel_tool_window",
title="Move Selection Tool")
self.columnLayout = mc.columnLayout()
self.txt_directions = mc.text(align="left",
label="Directions: Input translation increment.\n")
self.rowColumn = mc.rowColumnLayout(numberOfColumns=8)
self.txt_x = mc.text(label=" X: ",
align="right")
self.txtfld_x = mc.textField("mst_txtfld_x_value",
text="0",
ann='input units to move X',
width=50)
self.txt_y = mc.text(label=" Y: ",
align="right")
self.txtfld_y = mc.textField("mst_txtfld_y_value",
text="0",
ann='input units to move Y',
width=50)
self.txt_z = mc.text(label=" Z: ",
align="right")
self.txtfld_z = mc.textField("mst_txtfld_z_value",
text="0",
ann='input units to move Z',
width=50)
self.txt_space = mc.text(label=" ")
self.move_btn = mc.button(label="Move")
#ui commands
mc.button(self.move_btn,
edit=True,
command=partial(self.move_selection))
mc.textField("mst_txtfld_x_value",
edit=True,
enterCommand=partial(self.move_selection))
mc.textField("mst_txtfld_y_value",
edit=True,
enterCommand=partial(self.move_selection))
mc.textField("mst_txtfld_z_value",
edit=True,
enterCommand=partial(self.move_selection))
#show ui
mc.showWindow(self.window)
def query_mst_user_input(self):
'''Queries all 3 textfields and returns
values of X, Y, and Z in a single list'''
self.x_value = mc.textField("mst_txtfld_x_value",
query=True,
text=True)
self.y_value = mc.textField("mst_txtfld_y_value",
query=True,
text=True)
self.z_value = mc.textField("mst_txtfld_z_value",
query=True,
text=True)
return (self.x_value, self.y_value, self.z_value)
def move_selection(self, *args):
'''Convert input or lack thereof into floats
and plug them into Maya's move command'''
self.mst_user_selection = mc.ls(selection=True)
self.mst_user_inputs = self.query_mst_user_input()
user_x = self.mst_user_inputs[0]
user_y = self.mst_user_inputs[1]
user_z = self.mst_user_inputs[2]
print len(user_x)
if len(user_x) == 0:
user_x = 0
if len(user_y) == 0:
user_y = 0
if len(user_z) == 0:
user_z = 0
print "Your X is {0}".format(float(user_x))
print "Your Y is {0}".format(float(user_y))
print "Your Z is {0}".format(float(user_z))
mc.move(float(user_x),
float(user_y),
float(user_z),
self.mst_user_selection,
relative=True)
def show(self):
'''Display UI'''
self.build_window_UI()
mst=MoveSelTool()
mst.show()
-
\$\begingroup\$ Indentation of "if(mc.window..." seems a bit awkward... \$\endgroup\$SylvainD– SylvainD2015年06月23日 07:42:08 +00:00Commented Jun 23, 2015 at 7:42
1 Answer 1
Alright, your code looks around but it could be improved slightly:
In the lines
def __init__(self, *args): pass if(mc.window("ak_move_sel_tool_window", query=True, exists=True)):
You should add whitespace between the two lines, and also between if
and (
.
PEP8, Python's official style guide suggests naming your variables like this x_value
, rather than self.txtfld_y
, and also using text_field_y
rather than an abbreviation.
I would suggest turning:
user_x = self.mst_user_inputs[0] user_y = self.mst_user_inputs[1] user_z = self.mst_user_inputs[2] print len(user_x) if len(user_x) == 0: user_x = 0 if len(user_y) == 0: user_y = 0 if len(user_z) == 0: user_z = 0
into an array.
I would suggest turning ann='input units to move X'
into a string elsewhere like: string_elsewhere = 'input units to move ' ... ann = string_elsewhere + 'X'
-
\$\begingroup\$ Can you give a reason for your suggestions? why should i turn those lines into an array? also, why should ann be put into a variable? does it make much of a difference? \$\endgroup\$Asaph Kim– Asaph Kim2015年06月24日 03:22:17 +00:00Commented Jun 24, 2015 at 3:22