Mel
|
IntroductionThe purpose of this tutorial is to help you get started with Mel scripting. The tutorial concentrates on the use of,
|
Executing a Mel Script with Cutter
Cutter can be of assistance in writing and running Mel scripts. The tutorial, Step 1
After launching Maya enter the following command into the Mel text field shown in figure 1.
The command can be "executed" by pressing the enter key on the numeric keypad.
The command tells Maya to "listen" on port 2222 for data sent to it by an external application.
Step 2
After launching Cutter, described in the tutorial
Cutter: Installation,
copy and paste the text from listing 1 into a new window. Open the network panel
by clicking on the network icon located in the top right hand corner of the window - figure 2.
Ensure the IP fields display the following text,
Save the document as listing1.mel - note the .mel file extension. The first Mel
script that is either saved or opened causees Cutter to automatically read documentation
about Maya's library of Mel commands. Command documentation is read only once per session.
A progress window, figure 3, will be displayed for the few seconds it
takes Cutter to read the documentation.
Step 3
Upon clicking the checkbox labeled Connect, refer to figure 2, you should see
an error message displayed in the lower right hand corner of the Maya window - figure 4.
The message is caused by Cutter sending an initial message to Maya. The error
is benign and serves only as a visual confirmation that Cutter and Maya are "connected"
on port 2222.
Unless you are using a computer with dual monitors you will find it helpful to resize
the Cutter and Maya windows so that they "share" the desktop, figure 5.
Step 4
You are now ready to run your Mel script directly from Cutter. Hide the network panel
by clicking the network icon. The keyboard short-cuts Alt + e or Control + e can be used
to send the script to Maya via port 2222. In the Maya window you should see the graphics
shown in figure 6.
Listing 1 - grid1d.mel
Listing 2 demonstrates how nested loops can be used to produce a 2D array of
spheres..
Listing 2 - grid2d.mel
|
Nesting a third loop (see listing 4) produces a 3D array of spheres - figure 8. |
|
Holes / VoidsListing 3 demonstrates the use of a "if" test to determine when spheres should not be inserted. |
Listing 3 - holes2d.mel
|
Changing the "AND" (&&) and the "OR" (||) of the test produces very different gaps within the 2D array of spheres. Moving the code that inserts the spheres into the "empty" block produces the "reverse" of the pattern. For example, |
if($x > 2 && $x < 5 || $y > 2 && $y < 5) { sphere -r 0.25; move (0.5 * $x) (0.5 * $y) 0; } else { // leave a "hole" } |
|
In listing 4 the loops have been changed so that a sequence of poly cubes are inserted into the scene in 'Z' (depth) order. The inner 'Y' loop has also been modified so as to produce the wedge effect shown in figure 11. |
Listing 4 - wedge.mel
|
Object InstancingIn terms of memory useage, the Mel code shown above can be improved by the use of object instancing. For example, listings 5 shows how multiple instances of a single "master" sphere can be used to create the same vertical column of spheres as listing 1. Listing 5
Note that using the setAttr command to change the size of the master sphere will also effect the instances. To avoid rendering artifacts, caused by the position of the surface of the master sphere coinciding with that of the first instance, it is adviseable to switch off the visibility of the master object. |
Converting to a Custom ProcListing 6 shows the previous code "packaged" as a custom Mel procedure. Listing 6
A more useful version of the proc is shown in the next listing. Note the proc can accept any shape as an input. $master has been declared as a global variable so that it can be referenced by an external Mel script. |
Listing 7
|
Adding a User InterfaceThis section of the tutorial shows how to make a simple user interface (figure 13) that enables a user to create a random grid of cones - all of which can be resized as a result of a slider modifying the master object from which the cones were instanced. |
|
|
Listings 8 and 9 show two scripts that work in tandem. The first script, randomConesUI.mel, isolates the user-interface code that is responsible for creating a window containing a button and a slider. Listing 8 (randomConesUI.mel)
|
The second script, randomCones.mel, isolates the code that instances the master cone created by the other script. The procs randomConesUI and randomCones could have been included in a single script. However, it is good practice to keep UI code in a separate file. The scripts also demonstrate the use of a global variable named $master. Listing 9 (randomConesUI.mel)
|
© 2002- Malcolm Kesson. All rights reserved.