Friday
Jun042010

Stretching Your Legs

What I'll cover:

 - Writing a for loop

 - Setting up a stretchy bone system manually

 - Automate the stretchy bone setup process

For loop

A for loop is a way for the computer to repeat a task until it's done. There's three things the computer needs to know in order to perform a for loop.

 - Where does it start?

 - Where does it end?

 - Where is it now?

Like this:

for i = 1 to 10 do
(
   print i
)


This tells the computer that it will loop from 1 to 10. The "i" is a variable which stores where we are now. So the result of this code is therefore 1 2 3 4 5 6 7 8 9 10.

Stretchy Bone Chain

Next we're going to set up a stretchy bone system.
First set up your chain. We'll be using only two bones to simplify this example.

1: Create a bone chain consisting of two bones
2: Create two point helpers and align them to the two bones
3: Position constrain each bone to their respective point helper
4: Add a look-at constraint to the first bone where the target is the second point helper

This can be done with longer chains of bones but it would require some time set it all up. Fortunately this is a very repetitive task and as such it's ideal for a for loop which is basicly just a task repeated until it's done.

Stretchy Bones Script

 First we'll create all of the point helpers, place them correctly and add them into an array. An array is a kind of collection or stack of things if you will. The first thing you put in the stack is number one, the second is number two and so on.

pts = #() --this is an empty array. We'll use it for storing the point helpers
for i = 1 to selection.count do -- loops through the selected amount and counts with i
(
  --creates a point helper
  pt = point size:20 centerMarker:false axisTripod:false cross:false box:true
  --aligns the point helper witht he bone
  pt.transform = selection[i].transform
  --adds the point helper to the arrray pts
  append pts pt
)


Copy this code into max then read through this code and try and understand what it does.
Next we'll add the position and lookAt constraints.

for x = 1 to selection.count do -- loops throughthe selecterd amount and counts with x
(
  --stores a position constraint in the variable posCon
  posCon = position_constraint()
  --targets the x point in the pts array ands sets the weight to 100
  posCon.appendTarget pts[x] 100
  --sets posCon as the position controller for selection x
  selection[x].pos.controller = posCon
)

 
This will add all the position constraints we need. Now inside the same pair of parenthesis we'll set up the lookAt. The main point here is that we dont want the last bone in the chain to have a lookAt. We do this by using an if expression. It works like this:

if a == b do c  --if a is equal to b do c
if a != b do c  --if a is not equal to b do c
if a  > b do c  --if a is greater then b do c
if a  < b do c  --if a is less then b do c
if a >= b do c  --if a is greater or equal to b do c
if a <= b do c  --if a is less or equal to b do c


And since we want to add the lookAt on everything except the last bone we can use this statement:

if x != selection.count do --where x is the current bone
(
  --add lookAt_constraint
)


Adding this into what we already have we get this:


pts = #() --this is an empty array. We'll use it for storing the point helpers
for i = 1 to selection.count do -- loops through the selected amount and counts with i
(
  --creates a point helper
  pt = point size:20 centerMarker:false axisTripod:false cross:false box:true
  --aligns the point helper witht he bone
  pt.transform = selection[i].transform
  --adds the point helper to the array pts
  append pts pt
)

for x = 1 to selection.count do -- loops through the selected amount and counts with x
(
  --stores a position constraint in the variable posCon
  posCon = position_constraint()

  --targets the x point in the pts array
  posCon.appendTarget pts[x] 100
  --sets posCon as the position controllor for selection x
  selection[x].pos.controller = posCon
  if x != selection.count do
  (
    --variable containing a new lookAt constraint
    lookCon = lookAt_Constraint()

    --adjusting the lookAt viewline length

    lookCon.lookat_vector_length = 0
    --the next point in the array is the target
    lookCon.appendTarget pts[x+1] 100
    --the current bone's rotation controller is now lookCon

    selection[x].rotation.controller = lookCon
  )
)


Last tip:
Try and drag the code from your maxScript editor to a toolbar and Max will make a button for you.

Monday
Nov232009

Hemisphere Control

More for the lectures in Oslo

Tuesday
Nov172009

Wheel Roll

This is not so much a full on tutorial as a quick walk through on how to very quickly set up a simple wheel rig that works on one axis. It's part of a 3 day lecture I had at Noroff in Oslo.

Monday
Sep212009

Loops and What Not

The first tutorial is up. It's not much yet, but I'm getting there. The next step will be going through a script for setting up stretcy bones as seen in face rigs and in extra helping bones.