Help with Translate into Maya Tool

208 views
Skip to first unread message

Daniel LeBreton

unread,
Jun 1, 2022, 3:54:32 PM6/1/22
to Python Programming for Autodesk Maya
Hi everyone! I'm trying to make a general layout tool for maya scripting course, I have the interface generally down, but I am having trouble adding a translate function to the floatFieldGrp for location. If anyone could help me sort it out, it would be greatly appreciated! Thanks!

Here is the script so far: 

import maya.cmds as cmds
import random
   #Creates the board to layout buildings, might put into UI later if I have time.
cmds.polyPlane(width=100, height = 100, name = 'Ground')
   #Define class, put DL to avoid confusion between other potential window items.
class DL_Window (object):
    #creates method to construct window, call function "self".
    def __init__(self):
       
        #Creating some attributes, like name, title, and size of the window as it will initially appear on the screen.
        self.window = 'DL_Window'
        self.title = "City Layout Creator"
        self.size = (400,400)
        # will close old window if it's still open
        if cmds.window(self.window, exists = True):
            cmds.deleteUI (self.window, window= True)
        #creates the new window    
        self.window = cmds.window (self.window, title=self.title, widthHeight=self.size)
       
        #adjusts all UI to fit in the column
        cmds.columnLayout(adjustableColumn = True)
       
        #Create a title, and seperator from the name of the UI, and the function of the UI
        cmds.text(self.title)
        cmds.separator(height = 20, width = 100)
       
        #Some customizable widgets that can adjust name, location of building on map, subdivisions for extruding windows or doors, and lastly the button.
        self.cubeName = cmds.textFieldGrp( label = 'Building Name:')
        self.cubeSize = cmds.floatFieldGrp ( numberOfFields = 3, label = 'size:', value1=1, value2=1,value3=1 )
        self.cubeLocation = cmds.floatFieldGrp (numberOfFields = 3, label = 'location:', value1=1,value2=1,value3=1)
        self.cubeSubdivs =cmds.intSliderGrp(field=True, label = 'subdivs', minValue=1,maxValue=20, value=1)
        self.cubeCreateButton = cmds.button(label= 'Create Building', command=self.createBuilding)
       
        #Repeat Steps for Trees
        cmds.separator(height = 20, width = 100)
        cmds.text("Tree Generator")
        cmds.separator(height = 20, width = 100)
       
        self.treeName = cmds.textFieldGrp( label = 'Tree Name:')
        self.treeSize = cmds.floatFieldGrp ( numberOfFields = 3, label = 'size:', value1=1, value2=1,value3=1 )
        self.treeLocation = cmds.floatFieldGrp (numberOfFields = 3, label = 'location:', value1=1,value2=1,value3=1)
        self.treeSubdivs =cmds.intSliderGrp(field=True, label = 'subdivs', minValue=1,maxValue=20, value=1)
        self.cubeCreateButton = cmds.button(label= 'Create Tree', command=self.createBuilding)          
       
       
        # Displays the window in Maya
        cmds.showWindow()
       
    def createBuilding (self, *args):
        print ("A button has been pressed")
       
        name = cmds.textFieldGrp(self.cubeName, query = True, text=True)
       
        width = cmds.floatFieldGrp (self.cubeSize, query=True, value1=True)
        height = cmds.floatFieldGrp (self.cubeSize, query=True, value2=True)
        depth = cmds.floatFieldGrp (self.cubeSize, query=True, value3=True)
       
       
        translateX = cmds.floatFieldGrp (self.cubeLocation, query = True, value1=True)
        translateY = cmds.floatFieldGrp (self.cubeLocation, query = True, value2=True)
        translateZ = cmds.floatFieldGrp (self.cubeLocation, query = True, value3=True)
       
        subdivs = cmds.intSliderGrp (self.cubeSubdivs, query=True, value=True)
       
        cmds.polyCube(name = name, width = width, height = height, depth = depth, translateX=translateX, translateY=translateY, translateZ=translateZ,  subdivisionsWidth = subdivs,subdivisionsHeight = subdivs,subdivisionsDepth = subdivs)
       
           
 #Calling the class here      
myWindow = DL_Window()

Justin Israel

unread,
Jun 2, 2022, 5:55:29 PM6/2/22
to python_in...@googlegroups.com
Hi Daniel,

Thanks for providing the example code. Maybe it would help to also explain what the outcome was in your current implementation, and what you expected? 

Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/f634ecdb-043d-4a16-9693-6aca59018d78n%40googlegroups.com.

Pacifique Nzitonda

unread,
Jul 8, 2022, 5:27:25 PM7/8/22
to Python Programming for Autodesk Maya
Hi Daniel, 
the error you're getting is from the  polyCube command. This commands does not have translation key words argument that you are trying to assign to it.

Solution:

# get returned values of polyCube command 
cube = cmds.polyCube(name = name, width = width, height = height, depth = depth, subdivisionsWidth = subdivs, subdivisionsHeight = subdivs, subdivisionsDepth = subdivs)

# set the 1st returned transform node.  With the move command it is faster as you can set value relatively or absolutely
cmds.move(translateX, translateY, translateZ, cube[0],  r=True)

# or you can also use the setAttr command
cmds.setAttr("{0}.translate".format(cube[0]),  translateX, translateY,  translateZ, type="double3")


And.... yeah, here is a check to avoid creating the ground each time. 

# create it once until you delete it:
None if cmds.objExists('Ground') else cmds.polyPlane(width=100, height = 100, name = 'Ground')

or you can write:
if cmds.objExists('Ground'):
    pass
else:

    cmds.polyPlane(width=100, height = 100, name = 'Ground')


Cheers!
Reply all
Reply to author
Forward
Message has been deleted
0 new messages