Python in Maya


Must import maya.cmds as something, anything works but cmds is good for clarity. 

Maya specific commands must be preceded with cmds, for example cmds.object().

You can create a cube with cmds.polyCube

Can parent with cmds.parent, and group with cmds.group. 


cmds.rotate, cmds.move, and cmds.scale can all be used when an object is selected.


Python/Maya code for a simple interface (so I don't forget):


import maya.cmds as cmds


if cmds.window ("my_Window exists = True):

    cmds.deleteUI("my_Window")

window = cmds.window("my_Window", title="Long Name", iconName="Short Name"), widthHeight = (200, 55))

cmds.columnLayout(adjustableColumn=True)

cmds.button(label = "Do Nothing")

cmds.button(label = "Close"), commands("cmds.deleteUI(/"'+ window + '/", window=True)'))

cmds.setParent ('..')

cmds.aboveWindow (window)



Code for creating a 3d pyramid. Took a while to figure this one out, which maybe it shouldn't have done, and the way that I did do it was a way I wasn't supposed to do it anyway, but here it is if I need it.


class layer_creator:

    def __init__(self, name, width, depth, height):

        self.name = name

        self.width = width

        self.depth = depth

        self.height = height 

   

    def createLayer(self):

        cmds.polyCube(n = self.name, w = self.width, d = self,depth, h = self.height)

        cmds.move(x/2, z = true)

#this second part of the createLayer moves the cubes across the z axis so that they are not mirrored across the bottom, which was a roundabout way of solving the problem that the program wasn't the most straightforward way of creating a pyramid in the first place.


x = 1


layer_1 = layer_creator("layer1", x*5, x*5, x*1)

layer_2 = layer_creator("layer2, x*4, x*4, x*2)

layer_3 = layer_creator("layer3, x*3, x*3, x*3)

layer_4 = layer_creator("layer4", x*2, x*2, x*2)

layer_5 = layer_creator("layer5, x*1, x*1, x*5)

Leave a comment

Log in with itch.io to leave a comment.