PDA

View Full Version : Coding in Python Maya, problems with Textfield and button


Rantaz
01-09-2013, 03:32 PM
Hey I am totally new to coding in python for Maya. I have got the bare basics but I have come to a wall.

Basically I am creating a tool for animation where you can create control shapes for a skeleton and snap them to a joint and set the tranlations and rotations to 0. However it will name the nurb circle, group the nurb circle and then name the group in it's process.

What I am stuck on is connecting the Textfield to the button. I want the user to write in the joint they want the Nurb Circle to snap to and using the text they have written will rename the Nurb circle and the group the Nurb circle while naming it at the click of a button.

Here is what I want my button to do:

import maya.cmds

#Create circle

circle_names = maya.cmds.circle( c=(0, 0, 0), nr=(0, 1, 0), sw=360, r=1, d=3, ut=0, tol=0.01, s=8, ch=1);

#Rename Circle

maya.cmds.rename(circle_names[0], “ctrl_L_Wrist”);

#Select the circle

maya.cmds.select(“ctrl_L_Wrist”)

#Group the NURB circle

group_name = maya.cmds.group();

#Name the group

maya.cmds.rename(group_name, “grp_L_Wrist_offset”);

#Select the group and joint

maya.cmds.select(“grp_L_Wrist_offset”, r=1);

maya.cmds.selectKey(clear=1);

maya.cmds.select(“jnt_L_forearmtwist”, tgl=1);

#Parent the group and joint

maya.cmds.parent();

maya.cmds.select(“grp_L_Wrist_offset”, r=1);

#Set the translate and rotation to the value of 0

maya.cmds.setAttr(“grp_L_Wrist_offset.rotate”, 0, 0, 0);

maya.cmds.setAttr(“grp_L_Wrist_offset.translate”, 0, 0, 0);

#Select the group and unparent it from the selected joint.

maya.cmds.select(“grp_L_Wrist_offset”, r=1);

maya.cmds.parent(w=1);


And here is the UI for the tool:

import maya.cmds as cmds

def UI():

#Check to see if our window exists
if cmds.window("exampleUI", exists = True):
cmds.deleteUI("exampleUI")

#Create a window
window = cmds.window ("exampleUI", title = "Adjusting Your CTRL Shape", w = 500, h =300)

#Giving the button an action
def CreateCtrl(*args):

#Create circle
circle_names = cmds.circle( c=(0, 0, 0), nr=(0, 1, 0), sw=360, r=1, d=3, ut=0, tol=0.01, s=8, ch=1);


#Linking the text field with button
global
buttoncre = cmds.ls




#Create a main layout
cmds.rowColumnLayout(numberOfRows=2, rowHeight=[(1,100), (2, 300)] )

#Banner image
imagePath = cmds.internalVar(upd = True) + "icons/adjustingyourctrlshape.jpg"
cmds.image(w = 300, h = 100, image = imagePath)

#making a tab system

tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)


#creating columns and the creating control shape ctrl menu


child1 = cmds.rowColumnLayout( numberOfColumns=3, columnWidth=[(1, 100), (2, 120), (3, 90)] )

cmds.text(label="Joint Name: ")
cmds.textField("jointN", edit=True, query=True, command=buttoncre )
cmds.button(label="Create", command=CreateCtrl)
cmds.setParent( ".." )

#Creating the colour menu

child2 = cmds.rowColumnLayout( numberOfRows=2, rowHeight=[(1,50), (2, 100)] )

cmds.text( label="Colour:")
cmds.colorSliderGrp( label='Colour Of Ctrl Shape: ', rgb=(0, 0, 1) )
cmds.setParent( ".." )


child3 = cmds.rowColumnLayout("tabThree", numberOfRows=2, rowHeight=[(1,50), (2, 100)] )
cmds.text( label="Scale:")
cmds.floatSlider()
cmds.setParent( ".." )

#labelling tabs
cmds.tabLayout( tabs, edit=True, tabLabel=((child1, 'Creating Control Shape'), (child2, "Colour"), (child3, "Scaling")) )



#Show Window
cmds.showWindow(window)

bman6200
01-12-2013, 07:53 PM
I think your main question was connecting the textField object to the process of clicking the button. Without getting too complicated, here is a quick example, hope it helps...


import maya.cmds as cmds

window = cmds.window()
cmds.columnLayout()
textField = cmds.textField( 'textField_action' )
button = cmds.button( label='click', c=action )
cmds.showWindow( window )

def action( *args ):
"""
@synopsis: Function to do stuff

"""

# Establish 'textField_obj' as the text from "textField_action"
textField_obj = cmds.textField( 'textField_action',
query=True,
text=True )

# If 'textField_obj' is valid and obj exists..
if textField_obj and cmds.objExists( textField_obj ):

# Validate nodeType() is a joint
if cmds.nodeType( textField_obj ) == 'joint':
jointName = textField_obj


So after you get the jointName, you can continue to do the command calls to rename the control and group and position it.

-Jason

CGTalk Moderation
01-12-2013, 07:53 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.