Wednesday, November 23, 2011

I Don't Just Watch Movies All Day, and brief lesson on Python Operations


When I was little I wanted to be an engineer cause I liked building things. At some point I felt that that would be too difficult (just wait for the irony), and I fell in love with animation dream got derailed. I went to school to be an animator, and all my friends were jealous that my homework involved watching movies and almost no papers. Three years passed, and I discovered I actually like the technical aspects about the animation pipeline versus the actual animating (but don't get me wrong I do still enjoy animating..I just like the other thing better). It's been six months since I graduated from college with a degree an animation, but its day one of my serious journey to figure out this technical animation nonsense.

PYTHON LESSON, YES PLEASE! (Variables, Printing, Strings)

Ok, I don't know where everyone might be in their programming knowledge. If you're brand new to programming, and remotely trust my knowledge feel free to email me at tleong127@gmail.com. Put in the subject line what you want claifications on.

I have been reading this book: http://www.amazon.com/Maya-Python-Games-Film-Reference/dp/0123785782 (Maya Python for Games and Film) in an attempt to teach myself Python Scripting.

I'm at chapter two, and it's the first programming language I've ever attempted to learn.

Python inside of Maya can call upon certain commands related to Maya. Before you run any python scripts in maya you must type:

import maya.cmds

now say you wanted to make a cube by scripting:

maya.cmds.polyCube()

#if you're in the script editor hit Ctrl+A (To select everything) Shift+Enter (on the numpad) to run the script and keep the script present in the script editor. However if you're ok with the script being erased, and just run in the computer go ahead and just hit Enter (on the numpad).

#The number sign is how you comment in Python

So far the coolest thing about Python is how easy it is to assign variables. You can assign almost anything to a variable:

x can equal any of the following things:

x = 5
#This is an interger
x = 5.0
#This is known as a float
x = 'Python is fantastic!'
# This is a string
#A string is just a group of words enclosed by either '' or "". You can use quotes interchangeably, just make sure you're consistent!
#However you can't mix numbers and strings together. If you want to do that you'll have to let python know that you're converting one to the other.
#Here's how you'd do that:

#Say you want to print out the sentence "there are 3 kittens in the park."
#and you have:
x = 3

#if you go:
print('there are ' + x + 'kittens in the park.'
#an error with pop up: Error: typeError: file ,maya console/ line 1: Cannot concatenate 'str' and "int' objects
#However if you change it to:
print('there are ' +str(x)+' kittens in the park.')
#you should get
there are 3 kittens in the park
# Now you can also do that vice versa with declaring something as an int() versus a str() or a float()

SKIPPING AHEAD TO OPERATIONS!

You can do math in python too! I'll start off with just basic math operations (multiplication, divison, subtraction, and addition). Though remember that Python follows the same of order of operations you learned in middle school alongside with Algebra. Time to remember: Please Excuse My Dear Aunt Sally. Of course that is the mnemonic device to remember: Parentheses, Exponents, Multiplication, Division, Addition, Subtraction (in that order!!)

Quick Reference:
x + y The sum of x and y
x - y The difference of x and y
x*y The product of x and y
x/y The quotient of x and y, if x and y are integers the answer is rounded down
x//y Floored quotient of x and y, if x and y are float numbers the result is a float number
x%y quotient of x and y with a remainder
pow(x,y) x to the power of y
x ** y also x to the power of y

With that it's pretty fun to go find some word problems and try to solve them using python
here's a site with Word Problems: http://www.mathplayground.com/mathhoops_Z1.html


Sample:
Matthew has a lot of baseball cards in his collection.
He began his collection when his dad gave him 45 cards for his eighth birthday. Matthew won 15 cards during a game.
He bought 20 more cards at the Sport Court.

How many baseball cards are in Matthew's collection now?

cards = 45

cards += 15

cards += 20

print(cards)

80


#There's two things I forgot to mention. One Python is a dynamic programming language, meaning you can always update/change the variable on the next line. But be careful and make sure you WANT to change the variable before you do so. Also if you type an operation before the equals sign when you want to assign a new number to a variable python will just do that operation to the original variable to give you the new variable.

I hope you enjoyed the lesson and it was somewhat useful. Again as I keep learning I'll keep posting. See you next time!