Week 8


///1.Cube Class

 Create a Python class called Cube that can be used as a template to be able to create, move, rotate, freeze, modify and select Cube objects in Maya

import maya.cmds as cmds

class Cube(): #creates a class named Cube
    def __init__(self,name):  
       self.name = name #initializes the name of the Cube
       
    def moveCube(self,x,y,z):
        cmds.select(self.name) 
        cmds.move(x,y,z) #sets the position of the Cube 
       
    def createCube(self):
        cmds.polyCube(n = self.name) #creates the Cube 
   
    def rotateCube(self, x,y,z):
        cmds.polyCube(n = self.name)
        cmds.rotate(x,y,z) #rotates the Cube
   
   def freezeCube(self):
        cmds.polyCube(n= self.name)
        cmds.makeIdentity( apply=True, t=1, r=1, s=1, n=0 ) #freezes the translations of the Cube

       

These are examples I used for the function 

cube1 = Cube("Hello")
cube2 = Cube("Bye")

cube1.createCube()
cube2.createCube()
cube1.moveCube(10,0,0)
cube1.rotateCube(0, 45, 0)
cube2.rotateCube(0, -45, 0)
cube1.freezeCube()


///Creating a pyramid

#Function that creates a pyramid

def pyramid():  

    sc=4  #we set a variable to 4, meaning that's how many cubes we will have

    for x in range(1,5):  #for each array, the scale will be reduced

        cmds.polyCube(w=1,h=1,d=1) creates a cube with width=1, height=1 and depth=1

        cmds.move(x,moveY=True) #moves the cube

        cmds.scale(sc,sc,scaleXZ=True) #scales the cube

        sc=sc-1     

pyramid()  #calls the pyramid

#Another way to create the same pyramid but without a for loop

import maya.cmds

def pyramid(): #Creates a function named pyramid 

#For each cube, the function will follow the same pattern as the function used above

    cmds.polyCube(w=1,h=1,d=1) #this creates a cube

    cmds.move(1,moveY=True) #moves the cube

    cmds.scale(4,4,scaleXZ=True) #scales the cube

    cmds.polyCube(w=1,h=1,d=1)

    cmds.move(2,moveY=True)

    cmds.scale(3,3,scaleXZ=True)

    cmds.polyCube(w=1,h=1,d=1)

    cmds.move(3,moveY=True)

    cmds.scale(2,2,scaleXZ=True) 

    cmds.polyCube(w=1,h=1,d=1)

    cmds.move(4,moveY=True)  

pyramid() #calls the function and creates a pyramid 


///4.Creating UI's       

# Makes a new window that will ask from the user to input a number to create a pyramid

window = cmds.window( title="Long Name", iconName='Short Name', widthHeight=(200, 55) ) #creates a window with x=200 and y=55
cmds.columnLayout( adjustableColumn=True )

Inputnumber = cmds.intSliderGrp( label="Input a number:", field=True, min=3, max=50, value=1) #asks from the user to Input a number #between 50 and 3
cmds.button( label='pyramid', command="pyramid01(Inputnumber)") #creates a button labeled as 'pyramid'

cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') ) #creates a button labeled as 'Close'
cmds.setParent( '..' )
cmds.showWindow( window ) #shows the window we just created

Leave a comment

Log in with itch.io to leave a comment.