Wednesday, 7 November 2012

Sine Wave Maya Python Script


Ok so not sure that this is the most exciting thing in the world, but this is my first year scripting assignment. It was a basic python script that when in Maya created a GUI window which in turn would create a lot of spheres and then depending on what options you chose it would move up and down in a wave like pattern. You could make it gentle or choppy, and it worked on the basis of manipulating a sine wave.

So here is some code for those that are interested:
(there is some boring Maya GUI stuff missing and applying the shader to it)

#define procedure to move a named object up and down on a sine wave.
def moveparticle(speed,Name,depth):
      #for loop to establish values on the sine wave – hence 0,180   
      for y in range(0,180):
               #make j the radian equivalent of the current y degrees eg. 180 = pi (3.14)
                j = math.radians(y)
               #using the j radians calculate the sine of that multiplied by the speed to increase the 
                 frequency of the wave within the range 180 degrees, and assign to x
               x = math.sin(speed*j)
               #assign a keyframe to the object’s translate y values by value x and key it at the time value
                  y / 2 + the depth value to determine the delay so all the objects don’t move together
               cmds.setKeyframe(Name, at='translateY', v=x, t=((y/2)+depth))
               #select the current object
               cmds.select(Name)
               #set the current object selected pre and post infinity to cycle so the wave will continue
               cmds.setInfinity(pri='cycle', poi='cycle')



#define procedure to make and arrange the objects made
def makegrid(*args):
    # check to see it the GUI window has appeared otherwise do else statment
    if cmds.window("myWindow", exists=True):
               #if the GUI does exist then take the value of the width slider and assign to variable width
               width = cmds.intSlider("width", query=True, value=True)
               #take the value of the depth slider and assign to width
               depth = cmds.intSlider("depth", query=True, value=True)
               #take the value of the speed slider and assign to speed
               speed = cmds.intSlider("speed", query=True, value=True)
               #take the name from the string typed into the string text field and assign to Name
               Name = cmds.textField("Name", query=True, text=True)
     #else statement for when the GUI doesn’t exist
     else:
              #print out “Query failed” so that it is obvious when programming has broken
              print "Query failed"
              #start to create a grid using the width and depth values
              #for loop to create the x values between 0, width value
              for x in range(0,width):
                   #nested for loop to create the y values between 0, depth value
                   for y in range(0,depth):
                        #create a sphere named using the Name variable taken from the GUI and add the
                        current x and y values to achieve a suitable naming system in numerical order, radius 1
                        cmds.polySphere(n=Name+str(x)+str(y), r=1)
                        # use the setLevelMaterial procedure to assign a blue blinn material to the current 
                        object and name the material after the name of the object
                        setLevelMaterial(Name+str(x)+str(y),str(Name)+str(x)+str(y)+'material', 'blinn', (0,125.00,255.0))
                         #translate the current object by value x in the x axis and y in the z axis to form grid
                         cmds.xform(Name+str(x)+str(y), t=(x,0,y))
                         #use the moveparticle procedure to add appropriate key frames to the object
                         moveparticle(speed,Name+str(x)+str(y),(x+y))
    

No comments:

Post a Comment