31st May2009

Unit circle

by Johan Borgström

In mathematics, a unit circle is a circle with a unit radius, i.e., a circle whose radius is 1. Frequently, especially in trigonometry, the unit circle is the circle of radius 1 centered at the origin (0, 0) in the Cartesian coordinate system in the Euclidean plane. The unit circle is often denoted S1; the generalization to higher dimensions is the unit sphere.

Why is the unit circle interesting to a motion designer?
To me it is interseting to get a better understanding of how you can describe rotation in space whether it be 2 or 3 D. Another reason would be  to get a better understanding how Maya stores the rotation values . The rotation of an object is displayed through the user interface as three rotation values: rotate X, Y and Z. Internally Maya stores the rotaion in the transform matrix as values ranging from -1 to 1. The flash movie below shows how a rotation value from 0 to 360 degrees can be visualized in both radians and as a two dimensional coordinate values.

Get Adobe Flash player

I saw a video on youtube discussing the unit circle and I got this nice trick to remeber the sin, cos and tan functions.
Soh Cah Toa - that reads :

Sin(angle) = Opposite / Hypotenuse.
Cos(angle) = Adjacent / Hypotenuse.
Tan(angle) = Opposite / Adjacent.

24th May2009

Trigonometry Wars 3.14

by Johan Borgström

There is a game called “Geometry wars – evolved” that has some really nice and simple graphics. I really like the “background image” of the game. It consists of a grid with springs that you dynamically interact with when you fire your weapon. It gives a nicy wavy motion. So my aim with this project is to create a piece inspired by that. My idea is that I create some object based in a grid pattern and these objects are connected with lines. When you press the mouse the distance from the mouse to the objects determine the magnitude of the force that affects them. First I need to figure out how to get the magnitude of the force. Like in my previous post Pythagorean and olives I will use the Pythagorean formula to get the distance from the mouse to the object. Than I inverse and clamp that value so that the range is from 0 to a maximum distance. When I inverse the value it will be at maximum force closest to the object and then drop off linear to o when reaching the max distance value.

Important Formulas:

Sine

The sine of an angle is the ratio of the length of the opposite side to the length of the hypotenuse.

Cosine

The cosine of an angle is the ratio of the length of the adjacent side to the length of the hypotenuse. In our case

Using the Pythagorean formula I get the length of  “h” and the angle “A”. To invert and normalize the hypotenuse I use the max function

var force = Math.max(0,(max_dist - dist));

Using Sine formula I get the opposite “a” (y coordinate) using: y coord = sin(A) * force) + clip_mc.y;

To get the adjacent “b” I use: x coord =  cos(A) * force) + clip_mc.x;

Get Adobe Flash player

I created a grid of objects and drew some lines connecting them. When I did this I wanted to create a smooth drop off between the objects affected by the force. In the case above the object gets affected by the inverse of the hypotenuse starting as soon as the object is closer than max_distance. When i played around with the numbers I got this effect. Is it familiar ? :)

Get Adobe Flash player

In this swf I´ve added the function to explode the grid by clicking !

Get Adobe Flash player

21st May2009

Pythagorean theorem and olives

by Johan Borgström

When I was studying at the design school I used to do some really random interactive-graphics-thingies. I was playing around a bit with flash and actionscript 3 the other day and I ended up with this really random thing. You can play around with the mouse and click. Thats about it : ) was fun making though. I think i will keep on doing theese simple interactive graphics cause first of all it´s fun and second it´s a good way to learn how AS 3 works ! In this clip I tested some basic trigonometric functions. Here is a little trivia for you ! Pythagoras was born between 580 and 572 BC BC on Samos, a Greek island in the eastern Aegean. He was an Ionian Greek mathematician and founder of the religious movement called Pythagoreanism. Pythagoras once said that “number is the ruler of forms and ideas and the cause of gods and daemons.”

Pythagorean theorem. The sum of the areas of the two squares on the legs (a and b) equals the area of the square on the hypotenuse (c).

a^2 + b^2 = c^2\,

or, solved for c:

 c = \sqrt{a^2 + b^2}. \,

To use it in flash we use the formula below.

Distance formula in Cartesian coordinates is derived from the Pythagorean theorem. If (x0, y0) and (x1, y1) are points in the plane, then the distance between them, also called the Euclidean distance, is given by

 \sqrt{(x_1-x_0)^2 + (y_1-y_0)^2}.

To get the distance in 3D we just add the Z dimension to the formula above.

Get Adobe Flash player

04th May2009

Words that speaks to me

by Johan Borgström

I am currently reading Extremely loud and incredibly close by Jonathan Safran Foer. I fell in love with his words. So I wanted to start to collect on phrases that makes me feel.

“…and if something made me want to laugh, I´d write “ha ha ha!” and instead of singing in the shower I would write out the lyrics of my favourite songs, the ink would turn the water blue or red or green, and the music would run down my legs, at the end of each day I would take the book to bed with me and read through the pages of my life:…”

“so I ripped the page from the book – ” I don´t speak, I´m sorry.” – and used it to dry her cheeks, my explanation and apology ran down her face like mascara, she took my pen from me and wrote on the next blank page of my daybook, the final one:

Please marry me”

” I could have released myself from the marble of myself.”

I am currently reading Extremely loud and incredibly close by Jonathan Safran Foer. I fell in love with his words. So I wanted to start to collect on phrases that makes me feel.

“…and if something made me want to laugh, I´d write “ha ha ha!” and instead of singing in the shower I would write out the lyrics of my favourite songs, the ink would turn the water blue or red or green, and the music would run down my legs, at the end of each day I would take the book to bed with me and read through the pages of my life:…”

“so I ripped the page from the book – ” I don´t speak, I´m sorry.” – and used it to dry her cheeks, my explanation and apology ran down her face like mascara, she took my pen from me and wrote on the next blank page of my daybook, the final one:

Please marry me”

” I could have released myself from the marble of myself.”

01st May2009

MEL – Introduction

by Johan Borgström

In this post I will give you a brief introduction to how you can make use of MEL in your Maya workflow. Since I am a designer and my approach to maya has always been artistic rather than technical I will try to explain it in less technical terms.

What is MEL ?

MEL is an abbreviation for Maya Embedded Language. Mel is a scripting launguage. This means that you can type a Mel command and execute that command as soon as you hit enter. This differs from a compiled language, a compiled language would require you convert your code into commands the computer understands. Since Mel is a scripting language, it makes Mel extremely fast to use. It is deeply integrated with Maya, and allows you to do anything from open a window or perform a simple action with a command, to total customization of the Maya interface, to writing an entirely new application on top of Maya. Practically everything that Maya can do can be accomplished through MEL (and what can’t can be done in another language with the Maya API). The great benefit of using MEL is that it helps you to perform tedious and or complex tasks. And it is REUSABLE. It will be of great help to you, that is a promise !

Mel Command, Procedure  and Script

A Mel command performs one action. The mel command sphere creates a nurbs sphere. A MEL procedure is a group of MEL commands that can be executed by a single command. A MEL script can contain one or more MEL procedures. Procedures can be local or global. Local procedures are visible only within their script file. Global procedures are visible outside their file. Typically, you create procedures in a script with a text editor. You can also enter procedures in the Script Editor.

Where are  MEL scripts stored ?

When you install Maya a folder called maya is created in the my documents folder. In that folder you will find a folder called 2009, if that is the version of Maya you are running. In that folder there is a folder called scripts, i.e  C:\Documents and Settings\user\My documents\maya\2009\scripts. This is the default folder were you can put MEL scripts you downloaded or created yourself. There are ways to use other folders but for know we will use the default folder.

How do I use MEL ?

When maya is launched it searches all the defined script folders and read the scripts in to memory. Before you execute a procedure, you must declare it. Declaring a procedure loads it into Maya’s memory so it can be executed. In the command line or script editor you type  source followed by the name of the script and hit enter. When the script is in memory you simple type the name of the procedure you want to execute.

  • Note if you added a script to the script folder after you launched maya you won´t be able to source the script. To make Maya re-read the scripts folder you use the command rehash.
  • If a global procedure has the same name as the script and is in a Maya scripts folder it will be sourced when maya launches.

How to write a MEL script !

When you learn to speak a new language you must learn the grammar to be able to express yourself correctly. The same is true when you learn how to write scripts. When you write something that the computer doesn´t understand you will get a syntax error or it simply won´t work. So lets have a look at how a Mel script is constructed. First of all if the “debugging” featutres of mel is really bad. If you´ve got an error in your script, the help you get from maya to try to understand what is causing the error is sometimes not to helpful, to say the least. When I write scripts I have an itterative process and test my script in Maya while I am writing it, to make sure it works. Sometimes it can be hard to figure out what´s causing an error. So my tip is that when you´ve written a bit of code, try it out and see if it works, write some more and check again. This way you narrow down the potential sources of errors. So lets start out really simple. The example below is a pretty useless script in terms of production but it helps to illustrate the construction of a mel script. All the script below does is to print out the text Hello You !. You will see the text in the command line as well in the script editor. Open the script editor and write the following lines in the input area.

print “Hello You!\n”;

Select all the text you´ve written and hit ctrl+enter on your keyboard. The text Hello You! is printed out in the command line and the script editor. The print command is really helpful when you write or debug a script and  is also useful when you want to give feedback to a user of your script. The script editor lets you write scripts and or simple commands. If you for instance write sphere a nurbs sphere is created. Altough you can create new tabs in the script editor and keep scripts written there it is a good idea to save your scripts as an external script file.

Global procedure

Lets create an external  script file with the same simple function as above. You can write scripts in a simple text editor but I would reccomend that you get a proper script editor. It will be easier. But for know we will use notepad. Create a new file and save it in your scipts folder as jb_testing_mel.mel. Make sure that the file suffix is .mel. Copy the text below and paste it in your document. Save the file. Open Maya.

global proc jb_testing_mel()
{
print “Hello You!\n”;
}

Note: If you had maya open before you placed the script in the scripts folder type rehash in the command line and execute the command.

To execute our script we first need to source it. Type source jb_testing_mel; in the command line and hit enter. Now the script is ready to use. So lets use our amazing script ! Type jb_testing_mel; in the command line and hit enter. That is how you execute a mel script.

The use of a global procedure makes it possible to execute a number of MEL command at one time. The script above is a simple example of a global procedure. After the word global proc is the name of the procedure. Inbetween the two parenthesis we would declare some argument to be used by the proc if we have any. The text between the two curly braces is what is going to be executed when we run the script.

Argument & variables

So know that we know how to execute a script lets make the script a little more flexible. To do this we will  introduce the use of arguments. An argument is a way to pass information to be used by the script. It makes the script more reusable and flexible. Replace the text in your script file (jb_testing_mel.mel) with the text below and save it.

global proc jb_testing_mel(string $text)
{
print ($text+”\n”);
}

Source the script again. Maya will give you a warning : // Warning: New procedure definition for “jb_testing_mel” has a different argument list and/or return type. You get this because you´ve added the arguments to the procedure. Now type the text:  jb_testing_mel  “Just an Example” ; ” in the command line and hit enter. This will output Just an Example. This is how you use an argument to pass in information to be used by your script.

The argument in this case is a variable of the data type string. That means it is a text variable. When you declare a variable you start by declaring the type of the variable. Before you give the variable a name you must use the $ sign. So to declare a string variable named text you would write: string $text. The variable types that you can use in MEL is as follows:

  • string $aString = “a string variable”;
  • string $aStringArray  = {“a”,”string”,”array”,”variable”};
  • float $aFloat = 3.14;
  • float $aFloatArray = {0.1,0.2,0.3}
  • int $anInteger = 1;
  • int $anIntegerArray = {1,2,3};
  • vector $aVector = <<0,1,0>>;
  • matrix $aMatrix[3][4] = <<1,2,3,4; 5,6,7,8; 9,10,11,12>>;

Return Value

A return value is a value that is returned by the procedure. For instance if you have a procedure that measure the distance between two points and you want to be able to use the distance calculated by one procedure in another procedure you would return the value and then use it. In our example we will return the name of the created sphere.

global proc string jb_testing_mel(string $text)
{
string $sphere[] = `sphere -n $text`;
return $sphere[0];
}

If we source the proc above and enter the text jb_testing_mel “jb_Sphere”;  in the script editor we will see // Result: jb_Sphere in the command line. We just created a sphere, given it a name by our argument and returned the name of the created object.