Week 9


///1.Making a class

Classes always have a function named  __init__(). It is executed when the class is being initiated.

We use the __init__() function to assign values to object properties or other operators.

I generally have difficulty grasping the concept of classes and methods so the following is just experimentation.

Example:

class Tutor():

      def __init__ (self, *args, **kwargs): *kwargs uses keywords and returns the values in the form of a dictionary whereas args is non-keyworded arguements

            self.first_name = kwargs.setdefault("first")

            self.last_name = kwargs.setdefault("last")

print(dir(Tutor))

 There are 26 items in the list above. The 24 in-between as generated by default while the first and last component comes from the class Tutor.

///5. Making shapes

import maya.cmds as cmds

class Shape: #creates a class named Shape

    def __init__(self, name): #creates a function with these parameters

        self.name = name

    def printMyself(self): #creates a function named printMyself

        print("I am a shape named %s."  %self.name) #prints the following message and the name of the shape

shape1 = Shape(name = "myFirstShape") #creates an object that has the name "MyFirstShape")

shape2 = Shape(name = "mySecondShape")  #creates an object that has the name "MySecondShape")

 

shape1.printMyself() #prints the name of shape1

shape2.printMyself() #prints the name of shape2


///6. Adding Functions

Like the example above create one rectangle called “rectangle1” with length 30 and width 50:

import maya.cmds as cmds

class myRectangle: #creates a class named myRectangle

    def __init__(self,name,length,width): #creates a function with these parameters

        self.name = name

        self.length = length

        self.width = width

    def printMyself(self):

        print("I am a rectangle named %s."  %self.name) #prints the message within the quotation marks and the name of the rectangle

shape1 = myRectangle("Rectangle1", 30,50)   #calls the function and sets the rectangles attributes

shape1.printMyself()

Leave a comment

Log in with itch.io to leave a comment.