Add New Features to User Interface

First, we'll add controls to the dialog box that give the user a choice between having 10 and 20 vehicles in the simulation. A 20-vehicle simulation will have 4 leaders and 16 followers. A 10-vehicle simulation will have 2 leaders and 8 followers. Let's start by adding these options to the global proc crowdSystem window procedure. We will be able to test the window by cutting and pasting the changes into the Script Editor. Look through the script to find the code to define the window. -title...

Hints for Planning a Good User Interface

Building excellent user interfaces is an art which requires that many factors work well together. However, there are a few guidelines that can help you find a useful design. Do the repetition for the user When you know, or can reasonably anticipate, that your user is going to want to perform the same operation on multiple objects, build your script to handle operating on multiple objects at once. Alternatively, if you can traverse the directed graph or the transform hierarchy to figure out what...

A Few Hints for Efficient Particle Expressions

If your expression manipulates position or velocity directly, you can get strange behaviors that result from a confused dynamics engine, as in Example 2. If you can, use Runtime Expressions After Dynamics and manipulate acceleration then the dynamics engine will ensure that the positions and velocities stay continuous. Manipulating per-particle goal weights is also safe. When possible, only access per-particle attributes and variables in your particle expressions. If you use the values of...

What You Need to Know Before You Proceed

Making dialog boxes using web panels is a powerful, new feature in Maya 6 that will appeal most to MEL scripters who already know the tools and techniques to build web pages. If you are not familiar with web authoring, you may find it easier to use Maya's built-in user interface commands. For this chapter, you should know how to make a simple web page using Hypertext Markup Language HTML , what Uniform Resource Locators URLs are and how they are formatted, and how to create dialog boxes in MEL...

Pass a Float into panelBreakup

Maya Python Opengl Float

You can change the function to accept a float parameter and pass it to the -delta setting. In the panelBreakup function header, add the following global proc panelBreakup float myDelta Figure 20.67 Results of panelBreakup script. Figure 20.67 Results of panelBreakup script. nurbsToPoly -name polyPanel -polygonType 1 -delta myDelta panel Save the script, and re-source it. If the script is in one of the Maya environment script path directories, you can simply type and execute it in the Script...

Create mrBlahControlsmel

Open your favorite text editing program, and start building the script mrBlahControls.mel. At the top of the file, start writing the global proc mrBlahControls . global proc mrBlahControls window -title mrBlah Controls -widthHeight 450 450 MBcontrols This first command in the mrBlahControls procedure will give us the framework and title for the window. Now, let's create the static text headings to form sections for the buttons. string form formLayout string txt1 text -label Right Arm Left Arm...

Clearing the Selection

Many scripts start off by getting a list of what the user has selected. Since many commands require that objects be selected to tell the command on what to operate, often you'll want to consider clearing the selection immediately after using the ls -sl command to get the selection list. Clear the selection with this command Once you've run this, nothing is selected. This command is equivalent to clicking on empty space in the Maya interface to deselect everything.

Avoid Using MEL Commands in Expressions

As discussed in Chapters 3 and 4, using MEL commands is not recommended in expressions. Maya does support using MEL commands in expressions when necessary, as we've seen, but doing so often forces Maya to recalculate the entire scene graph to allow the MEL command to execute. If you avoid using MEL commands in an expression, Maya can take advantage of internal optimizations that keep your expressions running much faster. Often, when you are tempted to use a MEL command in a script to calculate...

deleteUI MBcontrols Display Only What You Want User to See

Let's hide all the locators, handles, and IK handles in the scene to reduce the temptation for the user to select them in the interface. We can do this by using the Show menu in the persp panel. Enlarge the persp panel to full screen by pressing the space bar with the mouse over the window. Now go to the Show menu, and turn off the options shown in Figure 22.28 by de-selecting them from the list. Figure 22.27 User interface for mrBlah. Figure 22.27 User interface for mrBlah.

Add Cancel Button

To close the window, thus far we have been using the Close box at the top of the window, which is provided automatically. To be consistent with Maya's dialog boxes, we should add a Cancel button next to the existing Create button that also closes the window without changing the scene. In addition, because our dialog box's operation should be run only once in the scene, we want the window to close after the script is run. To do this, we'll use the deleteUI command to tell Maya to close the...

attachOppositeControl

-attachOppositeControl fixes an object's side a specific number of pixels from the same side of another control. You could use -attachOppositeControl to set a distance between the right sides of two buttons, as in -attachOppositeControl button_1 right 100 button_2 This would place the right side of button_1 100 pixels to the left of the right side of button_2. This time, unlike in the -attachOppositeForm example above, we have chosen an offset in the same direction as -attachControl would have...

Example Making the Example Dialog Box 1

The best way to see how these parts work together is to create a dialog box and examine the script you have used to do so. Try entering and running this script, which will create the dialog box in Figure 14.1. Once you have done so, try manipulating some of the controls in the window that appears. Note Maya requires that the flag to create a collapsible frameLayout be spelled collapsable. string window_name window -title Window 1 -menuBar true -widthHeight 483 203 Everything below this point...

What Is Operator Precedence

Because mathematical expressions can be ambiguous, Maya follows a set of operator precedence rules to determine the order in which math operations happen. Here's an example of this ambiguity if you write out the expression 2 3 4, you might imagine that Maya could evaluate the expression from left to right, first computing 2 3 which is 5 and then multiplying that result by 4 to get 20. However, if you open the Script Editor in Maya and type and then press Enter on the numeric keypad, you'll see...

Using Other Controls in geoConnector Node

Other control options that the geoConnector node provides are Resilience and Friction. You can think of resilience as a bounce setting and friction as a slide control. If we change the current settings on both of these controls, we can see the difference. Select the Floor object, and get a listing for geoConnectorl in the Channel Box. Set Resilience to 0.0 and Friction to 0.5 Figure 20.14 . You can set the resilience and friction controls in MEL as follows setAttr geoConnectorl.resilience 0.0...

Create Interface Window for Aim Control Using MEL

Let's create a floating MEL slider in a window that we will hook up to the gun's Y rotation so that we can control the direction of the particles. First, we have to determine the limits that we want on the rotation. A range of -30 to 30 should be fine since we do not always want the particles to hit the panel throughout the simulation. Also, we want to use floating-point numbers and not integers since the floats will give a smoother transition between -30 and 30. Figure 20.56 Directional...

Controlling the Flow of Script Execution

Most of the scripts discussed this far have run from beginning to end. However, many useful scripts either need to do something repeatedly or do certain things only if certain preconditions are satisfied. When a part of your script should only be run if certain conditions have been met, you can use a feature of MEL called a conditional operation to allow your script to decide whether to run that code. When a part of your script should run repeatedly, you use a feature of MEL called a loop...

Unique Names and Writing Scripts

By keeping node names unique in your scenes, you can avoid potential confusion that can result from relying on transform paths to distinguish between nonunique nodes. To the extent that your scripts need to walk the node tree in your scene, keep track of nodes by their fully qualified transform paths. Even if you prefer to enforce rules of unique names in your scenes, when you have multiple instances of a node, the node has a single name but multiple transform paths. The alternative, which is...

Installing Custom Menus

Installing custom menus in the main menu bar works just like adding menus to a dialog box. However, instead of adding the menus to your dialog box window, you must add them to the main Maya window. The name of the main Maya window object is stored in a global variable called SgMainWindow. You can use the setParent command to indicate that you want to add your menu items to the main window's menu bar by doing something like the following global string SgMainWindow setParent SgMainWindow menu...

Active and Passive Rigid Bodies

Now it's time to create a sphere and make it a dynamic object. First, create Ball. In the Script Editor, type the following code polySphere -name Ball -r 1 -subdivisionsX 12 -subdivisionsY 12 -ax 0 1 0 -tx 1 -ch 1 xform -worldSpace -scale 1.0 1.0 1.0 xform -worldSpace -translation 0 5 0 In the Dynamics menu group, select Ball, and choose Soft Rigid Body gt Create Active Rigid Body gt . As you can see, the solid dynamics options for objects are quite extensive. The Ball will be an active rigid...

Add Main Launch Procedure

We want to add a Main procedure to our script that will serve two functions. First, it will initialize a global string array that will contain a list of names of the selected objects in the Maya interface at the time the script launches. Later, we'll add code that will make these selected objects obstacles. Second, the Main procedure will add a test to see whether the window already exists. If it does, an old window from a previous execution of the script is still open, and a previous crowd...

Using Utility Commands that Support the ch on Flag

Some utility MEL commands that get information from the scene operate by creating a utility node, reading its result, and destroying it. One example of this that we saw in Chapter 4 on particle expressions is the pointOnCurve command. Some of these commands, including pointOnCurve, support creating a utility node that is automatically hooked up by the command. To do this, pass the command the -ch on flag, as in string nodeName pointOnCurve -ch on curvel Usually, as we saw in Chapter 4, keeping...

What to Remember About How to Use MEL Without Writing Scripts

If you want to see what commands you've executed or learn whether your commands are generating errors, look in the Status Message Area of the Script Editor window. The Script Editor and the command line provide convenient places to try commands before you put them in your MEL scripts. Using Show All Commands will tell you the MEL commands that correspond to what you are doing in the Maya user interface by hand, but you should read and understand the generated commands before using them in your...

Create Matrix Variable to Use for Placement

A matrix is a two-dimensional array of floats the floats can be accessed by referring to their row and column index. You specify the size of a matrix at creation, and unlike an array variable, it can't be resized once it's created. Matrices are often used to represent transformations because the matrix can represent a combination of translation, scale, and rotation all at the same time. You can also perform math functions on the entire matrix in a fast and efficient way. For this example,...

Saving a MEL Script

While placing a MEL script on the shelf is often convenient, most of the time you will want to save your MEL script as a file on disk. Three reasons for this follow You can share the MEL script with other people. Since shelf buttons are hidden away in your Maya configuration, it's difficult to pass them around to fellow animators. Saving your MEL scripts as files avoids cluttering up your shelf with lots of MEL buttons that you seldom use. Later on, you will find that it is often convenient for...

Example Creating a Vehicle

First, we need to make what we will call a vehicle. This vehicle will be constructed from a polygonal cube that we'll make a solid dynamic object so that we can rely on Maya's dynamics engine to compute its motion. Later on, we will add other parameters to give it a personality Figure 21.2 . Before starting, set some grid settings that will help you to build your vehicle. Choose Display gt Grid gt menu item, and set your options as shown in Figure 21.3. Also, create a four-view window...

Add Boundary Obstacles

Now let's create a few polygonal cubes that will become obstacles in the scene. To make a positive Z obstacle object, place a polyCube at 0 0 20 in XYZ, and scale it to 50 4 1 with the name ob1. In the Script Editor, type the following polyCube -name ob1 -sx 2 -sy 2 -sz 1 As in the previous examples, -sx, -sy, and - sz control the number of subdivisions along each axis. By increasing the number of subdivisions in X and Y to 2, we'll ensure that Maya more accurately calculates the effects of...

Forin Loops

The for-in loop is useful when you have to do something to each element in an array. For example, make three spheres and select all three of them. Then type in the Script Editor, and press Enter on the numeric keypad. The ls -sl command lists all of the selected objects, giving back the result in an array. So, if you want to perform an operation on each selected object, you can use the ls -sl command to get a list of all of the selected objects and a for-in loop to operate on them, as follows...

Example Using the plusMinusAverage Node to Find the Midpoint Between Two

Suppose that you wish to use plusMinusAverage to find the midpoint between two locators in world space and place a third locator there 1. Make a new scene, and create two locators with the default names locator and locator2, as well as a plusMinusAverage node by executing these commands in the script editor file -f -new spaceLocator spaceLocator 2. Connect the translate attributes of locator1 and locator2 to plusMinusAveragel's input3D input. Note that because translate is a float3 or float...

What Are Namespaces

Namespaces are a feature that was added in recent versions of Maya to better manage names when you import or reference other scenes into the scene on which you're working. Namespaces work something like transform paths in that they separate node names into groups. If you choose to use namespaces to refer to your nodes, you provide a namespace in front of the transform path. For example, all the nodes that are in the namespace called my_namespace would be called my_name-space lt node name gt ....

Add Integer Pass from One Procedure to Another

Before we test the script, we need to add a parameter to the createCrowd global procedure that will allow us to pass the number of vehicles to the procedure. New code is boldface. global proc createCrowd int vNumber We also need to change the call to createCrowd in the button command for Create. At the bottom of the window launch section in the global proc crowdSystem , make the following change. New code is boldface. button -edit -command createCrowd vNumber deleteUI crowdWin Sbuttonl This...

Connecting Attributes in MEL

In MEL, you can connect two nodes' attributes with the connectAttr command. As in the Connection Editor, the attributes must be of the same data type to connect them. In Maya, make a new scene, and create two NURBS spheres. Move them apart then type this command in the Script Editor window connectAttr nurbsSpherel.translateX nurbsSphere2.translateY Next, move nurbsSpherel in the X direction to see what happens. When you move nurbsSpherel in X, nurbsSphere2 moves along the Y axis. Maya also...

Load Scene

First, load the scene mrBlah.mb. From the File gt Open Scene menu, select the scene mrBlah.mb in the archive on the Web site. Figure 22.2 shows what you should see once the scene loads. Depending on your preferences in Maya, the joints might look different. You can change the size of the joints by choosing Display gt Joint Size gt 25 . You can zoom out a little in the Hypergraph panel on the upper right side of the screen to get a better view of the nodes that make up the character.

Collections

A collection is an object type that MEL provides to make several controls work together. A common use for collections is to make all the rest of a group of radio buttons turn off when one turns on. Normally, when you make a radio group, the radioButtonGrp command makes a collection automatically so that the radio buttons work together without your intervention. One example of a case in which you may want to interact directly with collections occurs when you want to have multiple radioButtonGrp...

Make Each Grid a Passive Collision Object

There are two ways in MEL to make the grids passive collision objects for the Ball. The easiest is to select all the grids and run the rigidBody command. select -r grid grid1 grid2 grid3 grid4 grid5 grid6 grid7 grid8 rigidBody -passive -m 1 -dp 0.1 -sf 0.2 -df 0.2 -b 0.7 -l 0 -tf 600 -iv 0 0 0 -iav 0 0 0 -c 0 -pc 0 -i 0 0 0 -imp 0 0 0 -si 0 0 0 -sio none The other is to use a for loop, as we did when we duplicated the grids. While using the loop is not as simple in this instance, if you needed...

Finish Scene Using MEL

Now let's finish the scene by adding a second Floor object and rotating it so that particlel particles slide off one floor to the other. We will also add a gravity field to the simulation so that the particles fall downward. First rotate the Floor object -26.00 in X. Copy Floor, and move it 3.0 units in Z, -3.0 units in Y. Then rotate it 26.0 units in X. duplicate -name Floorl Floor xform -a translation 0 -3 -3 Floorl xform -a rotation 26 0 0 Floorl Figure 20.14 Dynamic collision settings for...

Add Simple MEL Interface

In the text editor, let's add the code to create a dialog box. For this dialog box, we will use the formLayout command so that we can control the precise layout of the box's contents. At the top of the existing script, add a procedure definition for a global proc called createCrowd. At the end of the existing script, add a curly bracket to end the procedure. The createCrowd procedure is your script's main body. As we develop the crowd system further, this procedure will be controlled by a...

Transform and Shape Nodes

At first, it seems that there are many different kinds of nodes that have the basic set of attributes that are needed for a node to make sense in the hierarchy translation, rotation, scaling, scale pivot point location, rotation pivot point location, and so on. Actually, most nodes that appear in the Outliner by default are actually a combination of two nodes, a transform node whose attributes store all of the information about where an object is in space and how it's scaled, and a shape node...

Overview 1

We will create an emitter, positioned such that the particles fall into the first bucket, bucketi, and then a collision event will make bucketi spill the particles into bucket2. Then another collision event will trigger bucket2 to spill into the last bucket, bucket3. The collision events will call a MEL expression that will make the buckets rotate and spill the particles.

When and When Not to Use an Expression

Using expressions is one of a few ways Maya allows you to drive the value of one attribute with the values of one or more other attributes. Here are some advantages of expressions over other techniques Expressions are best when you need an arbitrarily complex mathematical relationship between the attribute that the expression drives and the driving attribute. For example, you could use an expression to calculate an object's X position based on the cube root of the object's Y position....

How the Script Works

The starting point for writing the script is to make a list of what it needs to do first, make a list of all the nodes to rename and second, rename each one in order. The usual starting point for making lists of nodes is the ls command, so re-reading its documentation in the MEL Command Reference is a good place to start. Also, hunting through the MEL Command Reference gives us the rename command, which changes node names. Around these two commands we'll construct a way to rename the nodes in...

How It Works Qlq

Before we examine in detail how the script builds the window, let's look at the script's overall structure. Generally, scripts that build dialog boxes in MEL consist of commands that build windows and layouts, and between these commands are other commands that make menus, menu items, and groups. Once you have created a window or layout, all further layouts and controls you create will go into that window or layout until you end its creation with either a call to showWindow, another window...

Why Naming Conventions Are Important

As you develop scripts or create Maya scenes, you have tremendous freedom in naming your nodes, scripts, and variables. When your scenes are simple, often Maya's default node names, for example, are enough to organize your thoughts about what you are manipulating. However, once your scenes are sufficiently complex, you have begun to build up a library of MEL scripts, and you have defined enough variables in a particular script to be at risk of forgetting their function, naming becomes an...

Installing a Script to Run When Maya Starts

To make a procedure run whenever you start Maya, first install it in the script path as described in the previous section. Then, edit your userPrefs .mel file to include a call to the procedure. The commands in userPrefs .mel are run whenever you start Maya. This file is in the maya lt version gt prefs directory under your home directory or in Windows, in your My Documents directory . The lt version gt part is the version of Maya you're running, such as 4.0. Once you have edited userPrefs.mel,...

floatFieldGrp

Like a textFieldGrp, a floatFieldGrp provides a text box into which a user can type. However, a floatFieldGrp greatly simplifies validation because only a valid floating-point number can be entered into the text box. To create a window containing a floatFieldGrp, try the following script string myWindow 'window' rowLayout string SmyFloatFieldGrp 'floatFieldGrp -label The resulting window looks something like the one shown in Figure 14.10. Try typing both valid floating-point numbers and invalid...

Structure of a Dialog Box

Dialog boxes consist of a number of controls that are bound together in an overall structure called a layout. The controls provide the means that the user uses to fill in the dialog box with information the script needs, while the layout determines how those controls will be displayed. In Chapter 14, you will see how controls and layouts are constructed, but for now we will just examine some of the common component parts Figure 12.2 . Menus in a dialog box work just like menus in the main Maya...

Example GeometryConstrained Locators 1

In this example, we create a script that creates three spheres, each with a locator constrained to its surface with a geometry constraint to keep them on the spheres' surfaces and a normal constraint to keep them oriented perpendicular to the spheres' surfaces. At the highest level, this script needs to do the following Make a sphere, moving it away from the origin. Create a constrained locator on the sphere's surface. Repeat the last two steps two more times. At this point, it's worth thinking...

What Are Procedures and Functions

Procedures and functions are simple ways to group together a number of MEL statements into a single operation that your script can use again and again. When a script or another procedure uses one of these predefined groupings of MEL statements, that script is said to call the procedure or function, in the sense that the procedure or function is called upon to do its job. A function is a special type of procedure that is able to hand a data value back to the script that called it. This value is...

Is Mayas Expression Language the Same as MEL

Although they are substantially the same, Maya's expression language and MEL are not identical. The most significant difference is that Maya's expression language permits you to get the value of an object's attribute just by putting the attribute's name on the right-hand side of an assignment, and it permits you to set the value of an attribute just by putting the attribute's name on the left-hand side of an assignment. In MEL, as opposed to the expression language, you must use the command...

Example Introduction to Particle Goals

What are goals Simply, a goal is like a particle constraint between different particle systems or between particle systems and objects. A goal relationship is like a controlled elastic cord that has an adjustable tension. To a greater or lesser degree, depending on the goal settings, a particle attempts to place itself where the goal is Figure 19.1 . Goals can be more useful in controlling simulations than fields. Let's create a couple of particles and see how goals can be used to control them....

button

Buttons in MEL dialog boxes are the most basic control. Normally, you will associate a command with a button with the -command flag, which tells Maya what command to execute when the button is clicked. This script creates a window that contains one button that creates a sphere in your scene when it's clicked string myWindow window rowLayout button -label Make me a sphere -command sphere showWindow myWindow Figure 14.8 shows the resulting window. Note that your window may be a different size...