Tuesday, January 27, 2015

Ruudy's Spring

Here is a comparison between Maya's Default PolyHelix and Ruudy's Spring.

I had taken a class on Particles and Dynamics, which was being taught by Ruudy Liu. Ruudy was teaching us about creating rigid body constraints in Autodesk Maya, and showed us how to create a spring. He first demonstrated modeling a spring using the helix polygon, and explained that although this was easy to do, it was not ideal. He then showed us how to make a spring using locators, the CV Curve tool, and the polyDisk, which proved to be a better way to create the spring, but the process was so long that I decided to build a tool to make this process both easy and ideal.

The current module I wrote looks like this:

"""
    RuudysSpring.py
    Author:: Carl Gustafson
    Date:: 1/23/2015
    Language:: Python and MEL
    Description:: This creates a model spring that can be used with the Spring Constraint
        eliminating the problems of the Poly Helix.
    Version:: 1.5.1.
    Copyright:: (c) 2015 Carl Gustafson
"""

#Import all Necessary Libraries and Classes
from math import *
import maya.cmds as cmds
import maya.mel as mel

#Functions
def makeSpiral(numSpan,rad,dis,name,useGrad,grad):
    #This function makes the spiral
 
    #Error check
    if useGrad>1 or useGrad<0:
        print("useGrad=%s"%useGrad)
        cmds.error("VALUE ERROR: useGrad needs to be either 1 or 0! Line 16")
    if rad<0:
        cmds.error("VALUE ERROR: the radius can not be less than 0! Line 16")
    #Get the initial X, Y, and Z positions
    posX=math.cos(0)*math.exp(useGrad*grad)
    posY=rad*dis
    posZ=math.sin(0)*math.exp(useGrad*grad)
    #Get the curve name
    curveName=cmds.curve(d=3,p=[posX,posY,posZ])
    #add more curve points to the spiral
    for idx in range(numSpan-1):
        posX=math.cos(idx+1)*math.exp((idx+1)*useGrad*grad)
        posY=(idx+1)*rad*dis
        posZ=math.sin((idx+1))*math.exp((idx+1)*useGrad*grad)
        cmds.curve(curveName,os=True,a=True,p=[posX,posY,posZ])
    #End
    #Rename the curve
    cmds.rename(curveName,name)
#End

def makeSpringMesh():
    #This Creates the Spring
    #Variables
    numSpan=50
    radius=1
    dis=0.1
    name="RuddysSpring_Curve"
    useGrad=0
    grad=0.1
    #Make the Spiral
    makeSpiral(numSpan,radius,dis,name,useGrad,grad)
    #Create the spring
    mel.eval('''polyDisk;
    scale -r 0.1 0.1 0.1 ;
    move -r -os -wd 1 0.1 0 ;
    setAttr "pCircle1.rotateX" 90;
    setAttr "pCircle1.rotateZ" 180;
    select -tgl RuddysSpring_Curve ;
    hilite pCircle1 RuddysSpring_Curve ;
    selectMode -component ;
    select -r pCircle1.f[0:76] ;
    polyExtrudeFacet -constructionHistory 1 -keepFacesTogether 1 -pvx 1 -pvy 0.1 -pvz 0\
     -divisions 1000 -twist 0 -taper 1 -off 0 -thickness 0 -smoothingAngle 30\
      -inputCurve RuddysSpring_Curve  pCircle1.f[0:76];
    select -r pCircle1 ;
    rename |pCircle1 "RuudysSpring" ;
    DeleteHistory;
    select -r RuddysSpring_Curve ;
    doDelete;
    ''')
#End

#Test Harness
if __name__=='__main__':
    makeSpringMesh()
#End

This module is the start of the new tool I am developing for Maya that creates the spring that Ruudy taught our class. Although Ruudy may not have been the first person to have modeled a spring like this, I still felt it was appropriate to name this tool and method after him. Once I build an actual GUI, the user will have the option to edit the radius of the spring, the number of coils, the distance between the coils, and the option to make the the spring's radius gradually increase or decrease. There are already tools online that made the spiral curves, but I wanted to go beyond them and create a tool that actually creates the spring mesh. To do this I had to create the curves, and then a polyDisk which was scaled down and adjusted so that it aligned with the bottom of the spiral curve. With the polyDisk and the curve selected, the disk was then extruded with only 1000 divisions, then the curve is deselected, the mesh is renamed, the history is deleted and finally the spiral curve is then deleted.

Although Ruudy's Spring is not perfected yet, it is the start to improving the process of modeling spiral objects in Maya.