Scripting for Animation and VFX: Week3
Scripting for Animation and VFX: Week3
Golden Angle
In geometry the golden angle is the smaller angle of two angles created by dividing the circumference of a circle using the golden ratio.
The golden angle is a pattern that appears in nature. One example being the pattern that sunflower seeds form. The seeds grow in lines in a spiral formation. The amount each seed turns allows there to be no gaps in the pattern. The pattern uses the golden ratio for each turn. The golden ratio is an irrational number, a number that cannot be written as a simple fraction. Using a fraction would mean the lines would eventually stack up making gaps in the pattern.
Golden Angle Maya Script
For this week we were given a script to experiment with. This script creates spheres that follow the golden angle pattern.
The script:
import maya.cmds as cmds
import math
for i in range (0,2000):
obj = cmds.polySphere()
goldenAngle = i * math.radians(137.51)
cmds.setAttr (obj[0]+'.tx', math.sin(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.tz', math.cos(goldenAngle)*math.sqrt(i))
The first line of code, import maya.cmds as cmds, imports the maya cmds scripts and allows you to write only cmds when calling for a function from maya cmds. Otherwise you would have to write out maya cmds each time.
The second line of code imports the math library. This library is filled with useful math functions that are used in this script to calculate the golden angle. The function from the math library in this script are sin(), cos(), and sqrt(). sqrt() takes the square root of the value written in the parameter.
The next lines of code are a for in loop. This loop uses the range function, to determine how many times the loop will execute. This loop will execute 2000 times. i is an integer number that will increment each loop, and the range function determines the start and end numbers.
obj = cmds.polySphere() creates an object.
goldenAngle = i*math.radians(137.51) this creates a variable that holds the value of the golden variable. math.radians(137.51) is multiplied by i which increases each time the loop executes meaning this line allows the next sphere to keep rotating using the angle.
cmds.setAttr (obj[0]+'.tx', math.sin(goldenAngles)*math.sqrt(i)) this moves the sphere that is created each loop, this changes the translate x attribute. math.sqrt() is used to get the square root of i. obj[0] changes the attribute of the fist index of the object.
cmds.setAttr (obj[0]+'.tz', math.cos(goldenAngle)*math.sqrt(i)) is similar to the previous line of code, except this time is affects the translate z attribute.
This script calculates the golden angle and the amount of rotation and what translation values each new sphere needs to create the golden angle pattern seen in sunflower seeds.
Experimenting with the golden angle script
While experimenting with the script, I first tried using the golden ration on the translate y attribute. I copied the line of code for the translate x. This created a shape that was on and angle and more oval.
cmds.setAttr (obj[0]+'.ty', math.sin(goldenAngle)*math.sqrt(i))
Next I wanted to mess with the scale of the spheres.
cmds.setAttr (obj[0]+'.sy', math.cos(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.sx', math.cos(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.sz', math.cos(goldenAngle)*math.sqrt(i))
import maya.cmds as cmds
import math
for i in range (0,2000):
obj = cmds.polySphere()
goldenAngle = i * math.radians(137.51)
cmds.setAttr (obj[0]+'.tx', math.sin(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.tz', math.cos(goldenAngle)*math.sqrt(i))
if i % 2 == 0:
cmds.setAttr (obj[0]+'.sx', math.sin(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.sz', math.cos(goldenAngle)*math.sqrt(i))
I added code that would change the x and z scale of every other sphere that was made. This was acomplised with the condition if i % 2 == 0. If the number used in i equals 0 when divided by 2 the scale of x and z would be changed by the following lines. This means that every even number i will have it's scale changed.
Next I wanted to try to mess with the translate y of the spheres.
import maya.cmds as cmds
import math
for i in range (0,2000):
obj = cmds.polySphere()
goldenAngle = i * math.radians(137.51)
cmds.setAttr (obj[0]+'.tx', math.sin(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.tz', math.cos(goldenAngle)*math.sqrt(i))
cmds.polySphere(n= "sphere_" + str(i))
cmds.setAttr (obj[0]+'.ty', i)
I added the line of code:
cmds.setAttr (obj[0]+'.ty', i)
This made it so every sphere that was made would be translated positively on the y axis by i.
To make the height of the resulting shape less drastic, I changed the code so that i was divided by 16 for the translate y value.
import maya.cmds as cmds
import math
for i in range (0,2000):
obj = cmds.polySphere()
goldenAngle = i * math.radians(137.51)
cmds.setAttr (obj[0]+'.tx', math.sin(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.tz', math.cos(goldenAngle)*math.sqrt(i))
cmds.polySphere(n= "sphere_" + str(i + 1))
cmds.setAttr (obj[0]+'.ty', i / 16)
I added a sin() function to the translate y line of code.
import maya.cmds as cmds
import math
for i in range (0,2000):
obj = cmds.polySphere()
goldenAngle = i * math.radians(137.51)
cmds.setAttr (obj[0]+'.tx', math.sin(goldenAngle)*math.sqrt(i))
cmds.setAttr (obj[0]+'.tz', math.cos(goldenAngle)*math.sqrt(i))
cmds.polySphere(n= "sphere_" + str(i + 1))
cmds.setAttr (obj[0]+'.ty', math.sin(i))
This created a wave pattern.
Comments
Post a Comment