Skip to content

Commit d4ca6a6

Browse files
committed
Added the Introduction scripts
1 parent 0c59a6a commit d4ca6a6

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.idea/
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# PythonForMayaSamples
22
Code samples for people who take part in my Python for Maya course
3+
4+
The course is not currently live, but this code repo will represent the projects being taught by the course.
5+
The focus of the course will be to teach Python to prospective students using a few useful projects as end goals
6+

introduction/__init__.py

Whitespace-only changes.

introduction/helloCube.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# In this excercise we will create a simple cube!
2+
# Just a reminder that anything after a # is just a comment and will not be run
3+
4+
# We need to import the commands (cmds for short) library from maya to be able to give Maya commands to run
5+
# The import statement lets us bring in other python libraries, called modules from python packages.
6+
# In this case we are bringing in the cmds module from the maya package
7+
from maya import cmds
8+
9+
# We create a cube by giving maya the polyCube command
10+
# Maya will then give us back the cube's transform and its' shape.
11+
# We will store both in a variable called cube.
12+
# Variables are like nicknames we can give to objects in python, so that we don't need to know how to actually call it.
13+
# Sort of like that guy at work who always forgets my name and calls me Dave.
14+
cube = cmds.polyCube()
15+
16+
# OKAY I LIED!
17+
# We've created the cube, but that's not exciting is it? Let's go further and make it ready to animate with a control.
18+
19+
# If we print out the contents of the cube variable we see that it will contain a transform and a shape
20+
# You should see something like [u'pCube1', u'polyCube1']
21+
print cube
22+
23+
# This is called a list, so we can say the type of cube is a list.
24+
# If you don't believe me, you can run this and it will say: <type 'list'>
25+
print type(cube)
26+
27+
# Lists are , well, a list of objects. They can contain anything, even other lists.
28+
# In this case, the list contains the names of the transform and the shape of the cube.
29+
# We need to get just the transform, which is the first member of the list
30+
transform = cube[0]
31+
32+
# While humans count lists from 1, computers count lists from 0.
33+
# It takes a while to get used to it, but you'll learn it quickly enough
34+
# So we've taken the first object in the list, with index of 0.
35+
# If you want the shape instead, you can get the second item in the list, at index 1
36+
# As you can see the [] notation is used to get the item at that index
37+
shape = cube[1]
38+
39+
# Now lets create a nurbs circle controller to parent the cube under
40+
# But I don't remember the command to create a circle!
41+
# If you create a nurbs circle manually, it will show you the circle command in the script editor
42+
# So we can deduce it will be the following
43+
circle = cmds.circle()
44+
45+
# Similar to the cube, we've created a circle and got back a list of the circle transform and its' shape
46+
# We can see this by printing out the contents of circle
47+
print circle
48+
49+
# We only need the transform so lets take just the transform like we did above
50+
# As you can see here, you can always repurpose a variable and it will now refer to the new thing we point it to.
51+
# Just like my coworker calls a few different people Dave, I can call a few different things circle
52+
circle = circle[0]
53+
54+
# Okay so we have the circle transform (circle) and the cube's transform (transform)
55+
# Let's parent the cube under the circle
56+
# What you see here is that we can give commands more details on how to run.
57+
# In this case, we're telling the parent command to take the transform we found earlier from the cube
58+
# and then put it under the circle transform we found aboove
59+
# These are called positional arguments as their order is important
60+
cmds.parent(transform, circle)
61+
62+
# Now that we can controle the cube with the circle, lets lock the cube's controls
63+
# We're going to set the attributes of the cube to locked
64+
# Maya uses the period symbol to show that attributes belong to an object. e.g. pCube1.transform
65+
# transform is a string and we can use the plus symbol to add another string to it
66+
# As you can see, strings can use either single or double quotes as long as you end them with the same
67+
# lock is an example of a keyword argument. Its' order is not important as you refer to the argument name directly.
68+
cmds.setAttr(transform+'.translate', lock=True)
69+
cmds.setAttr(transform+".rotate", lock=True)
70+
cmds.setAttr(transform+'.scale', lock=True)
71+
72+
# Finally lets select the circle
73+
cmds.select(circle)
74+
75+
# And there you have it, you've quickly learned how to prop things!

introduction/helloWorld.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
This is a very simple example of a python script.
3+
The print statement will display any thing after the word print.
4+
In this case, we are creating a "string" that says "Hello, World!"
5+
6+
A string is a alphabets, numbers and/or symbols.
7+
8+
This is a simple but important test, because it's one of the simplest ways to see if something is working.
9+
10+
P.S. If you're wondering, everything inside these triple quotes will not be run, and is just my commentary
11+
"""
12+
# P.P.S Anything after a # is also a comment and will not be run
13+
14+
print "Hello, World!"

0 commit comments

Comments
 (0)