Week 4
In this week I did some function exercises and applied the knowledge to maya.
The function exaples I completed can be found at w3schools.com
Function Practice in MAYA
Here are the functions I made. On the last line I call the function 'create_window()' which takes 2 arguments. These arguments are then called with a variety of uses such as; naming windows, deleting existing windows, and generating functions.
import maya.cmds
# Function to create a poly cube
def create_cube(cube_depth = '5', cube_width = '5', cube_height = '5'):
cmds.polyCube(h = cube_height, w = cube_width, d = cube_depth)
# Function to delete existing window
def delete_existing_window(window_name):
if cmds.window(window_name, exists = True): cmds.deleteUI(window_name)
# Function to create window
def create_window(name, cmd):
delete_existing_window(name) # Calls above function
window = cmds.window(name, title='Test Window', iconName='Short Name', widthHeight=(300, 55) )
cmds.columnLayout( adjustableColumn=True )
cmds.button( label=name, command=(cmd) )
cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )
cmds.setParent( '..' )
cmds.showWindow( window )
create_window("Creates a cube", create_cube)

Here is the output. It uses the name passed in the 'create_window()' function.
More MAYA Function Practice
Here is another example of using functions for maya.
import maya.cmds
# Function to create a shape and animate based on later variables
def animate_shape():
global shape
if shape == "sphere": shape = cmds.polySphere()
if shape == "cube": shape = cmds.polyCube()
for i in range(animation_range):
cmds.currentTime( (i+1)*10, edit=True )
cmds.setKeyframe(shape, v=i, at="translateX")
# Function to create a window
# Contains a button that calls the 'animate_shape()' function
def create_window():
if cmds.window("my_window", exists = True): cmds.deleteUI("my_window")
window = cmds.window("my_window", title="Pop-up", iconName='Short Name', widthHeight=(300, 55) )
cmds.columnLayout( adjustableColumn=True )
cmds.button( label='Animate', command='animate_shape()' )
cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )
cmds.setParent( '..' )
cmds.showWindow( window )
shape = "cube" # Name can be 'sphere' or 'cube' animation_range = 10 # This is the loop range for the animation create_window()
In this example, I can set the shape and animation range at the bottom. When I press the 'Animate' window button, the function 'animate_shape()' is called. This function takes the value of the 'shape' variable and evaluates it before overwriting the variable with its respective command e.g. 'cmds.polySphere()'.
Week 4 Activities
Activity 3: While-Loops
Write a script that uses a while-loop so that it counts forward from 1 to 10 in threes, i.e. 1, 4, 7,10.
count = 1
while count <= 10:
print(count)
count += 3
Output:
1 4 7 10
Activity 4: Functions
Write a Python script to convert temperatures from C to F degrees.
def c_to_f(c):
return (float(c)*1.8+32)
temp_c = input("Enter the temperature in degrees C: ")
print(f"The temperature in fahrenheit is: {c_to_f(temp_c)}")
Activity 5: More Functions
Create a while-loop that runs indefinitely, asking the user for a temperature in C, and converting it to F.
def c_to_f(c):
return (float(c)*1.8+32)
while True:
temp_c = input("Enter the temperature in degrees C: ")
print(f"The temperature in fahrenheit is: {c_to_f(temp_c)}\n")
Activity 6: Testing Again
Make the program respond accordingly even if output is a string.
def c_to_f(c):
return (float(c)*1.8+32)
while True:
try:
temp_c = float(input("Enter the temperature in degrees C: "))
print(f"The temperature in fahrenheit is: {c_to_f(temp_c)}\n")
except:
print("Please enter a number.")
Here I use try, except to catch when an input is taken that cannot be cast to float and ask the user for a new number.
Activity 7: Multiple Functions
compositing_mark, programming_mark, research_mark, animation_mark = 75, 97, 78, 81 # Here I set the variables
def module_sum(): # Sum of all marks for all modules
return compositing_mark + programming_mark + research_mark + animation_mark
print(module_sum())
def quarter(): # 25% of module 2 and module 3
return (programming_mark + research_mark)/4
print(quarter())
def average(): # Overall avarage mark of all modules
return module_sum()/4
print(average())
def overall_mark(): # The weighted average of all modules
return compositing_mark/5 + programming_mark/5 + research_mark/5 + animation_mark*2/5
print(overall_mark())
Output:
331 43.75 82.75 82.4
Programming for Animation Blog
| Status | In development |
| Category | Other |
| Author | up2115462 |
Leave a comment
Log in with itch.io to leave a comment.