Lec 10 MEL for Dynamics Create user windows Create customize shelf command icon Create and use of expression within MEL script Create and use of particle and rigid body dynamics panelbreakup exercise (The skeleton code file can be found in G:\public\panelBreakup_ske.mel) Goal: We want to know more about the dynamics system within Maya and learn about the interaction of rigid body object (including particles). We are going to build a scene in Maya using particle collisions to act on multiple solid bodies with expressions. N.B.: In order to create interesting effects with particle, one extremely important topic is lighting and rendering issue. I skip this topic for today class and probably we will talk about it more on the next lecture as it involves lot of aspects. Rigid Body Dynamics RBD RBD object in Maya represents something that non-deformable (very hard), and their motion is based on physics law. RBD object can and will collide with each others. Maya has two types of RBD objects: Active rigid body: React to dynamics effects: force fields, collisions, and springs. Its position and rotation are totally calculated from physics law i.e. you don t have any control over except the initial position and rotation. Create an active RBD: 1. Select the object(s). 2. Soft/Rigid Bodies > Create Active Rigid Body Passive rigid body: It will collide with active RBD but it is NOT affect by other dynamics object in the scene. E.g. passive RBD is not affected with force fields. You can control its position and rotation within any frame. Create an passive RBD: 1. Select the object(s). 2. Soft/Rigid Bodies > Create Passive Rigid Body Class Exercise 1: Create a bouncing ball 1
active RBD passive RBD gravity field Exercise: Figure 1 Bouncing ball example 1. Create a NURBS sphere and a NURBS plane. 2. Make the sphere an active RBD object. 3. Make the plane a passive RBD object. 4. Add a gravity field into the scene. 5. Set a longer range for the animation frame. E.g. 100 150 frames. 6. Run the animation and see the effect. Create a rigid sphere rolling over a rough surface animation sequence. Your screen should look like the following but I expect you will build a more complex and interesting terrain. 2
Tips: 1. Creates a gravity field and make the field point to x-axis (negative or not) and y-axis (negative or not?). 2. The field strength should not very large. I.e. much less the default 9.8 value. 3. Have a long animation sequence in order to see the effect. 4. You can make an inclined plane instead. Particle and Surface Interaction Some of the most interesting animation effect within Maya is created using a combination of particle and dynamics effect. Let s start work on simple example of particle and polyplane interaction. Hand-on practice: Simple particle and surface interaction 1. Create a 4 x 4 plane using Create > Polygon Primitives > Plane. You can set the 4 x 4 option through the attribute editor or just use the option bottom when you are creating the plane. The equivalent MEL command is: polyplane weight 1 height 1 subdivisionsx 4 subdivisionsy 4 axis 0 1 0 2. Rename the plane from pplane1 to Floor. (Tips: rename pplane1 Floor or using the channel manual). 3. Change X, Y, Z scale of the plane to 6. 4. Create an Emitter with following properties: 3
5. Raise the Emitter above the plane a bit. Finish every above steps, you will have something look like: 4
6. We want to make the particle collide with the floor. Select the particle and then shift-select the Floor. Once you have them both selected, select Particles > Make Collide in the upper menu. 7. Set the time frame to 500 and run the animation. You will probably see the particles are bouncing upwards. It is not too realistic. 8. Here we have 2 more factors that we can tune. Open the Attribute Windows of the Floor and you will notice there are 2 more parameters here: - Resilience, it controls bouncing of the particles - Friction, it controls sliding of the particles. Tune the above 2 factors and make your particles bouncing left right upon the Floor instead of upwards. 9. Finally, we rotate the Floor a bit. You should have something like this: 5
However, we want the particle is falling off the edge of the plane. I.e. we need gravity. Select the particle and then add a Field (Fields > Gravity). Run the animation and see the result. 6
You can change the render attribute of the particleshape1 to spheres in the attribute editor of the particle1 object. Once you had changed it, you will have something like this: You can use a geometry object to replace the particle in your scene. It is called instancer. Create cylinder and then: 1. Select the instancer object i.e. the newly created cylinder. 2. Shift-select the particle object (with your instancer object already selected). 3. Particles > Instancer. 4. You may want to hide the original instancer object. Display > Hide > Hide Selection. Class Exercise 2: Flow of the particles Can you make another inclined plane just below the above one and run the animation and see the result? 7
Figure 2 Direction of the flow of particles Hand-on practice: Creating a NURBS plane In this exercise we want to create something as shown in Figure 1. Figure 3 Particle gun knocks away solid bodies Figure 1 shows the results of such scene. Also, we will create a small MEL window to control the direction for particle emission. 8
We begin by constructing the scene. In the Script Editor (or your favorite editor), create a NURBS plane: nurbsplane name panel p 0 0 0 ax 0 1 0 w 1 lr 1 d 3 u 1 v 1 ch1; xform worldspace scale 20 20 20 panel; xform worldspace rotation 0 0-90 panel; NURBS is actually a very useful object in Maya. We use it extensively for creating various kind of model. It is due to fact that it can be transformed and extended easily using MEL script. xform is the transformation command which let you transform Maya object using coding. Figure 4 The NURBS plane 9
These commands will create a NURBS plane in Figure 2. It will facilitate use to create the smaller dynamics planes later. The NURBS is placed in the center of the world and points normally in the positive X direction, which will be important for the collisions. Hand-on practice: Creating the Emitter Now we need to create a directional emitter called gun. In the Script Editor, we create the gun using the code: emitter -pos 25 0 0 -type direction -name gun -r 30 -spd 50 -mxd 0 -dx -1 -dy 0 -dz 0 -sp 0; particle; connectdynamic -em gun particle1; Figure 5 Emitter shooting particles at plane 10
Start the animation icon and the emitter, gun, will shoot particle to the NURBS plane. Hand-on practice: Creating Interface Window for Aim Control In here, we talk about a topic which you haven t learnt yet. We want to 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. Even though you haven t learnt this topic, the slider control is very natural for GUI application and the coding is very straight forward. We are going to learn MEL GUI programming starting next week. Figure 6 Gun Y rotation control window window -title "Gun Control" -width 400 -height 50; columnlayout; floatslidergrp -label "aim" -field true -minvalue -30.0 maxvalue 30.0 roty; connectcontrol roty gun.rotatey; showwindow; window title Gun control creates the name of the windows as it appears on the interface. -width and -height specify a 400 x 500 pixel window. columnlayout creates the layout of the rest of the settings called after it in a single column. floatslidergrp will make a slider and an associated floating-point field. -lable defines the name of the slider -minvalue and -maxvalue set the range of the slider roty is the name of the slider that floatslidergrp creates connectcontrol roty gun.rotatey connects the control roty to the attribute gun.rotatey, so that dragging the slider changes the attribute. showwindow displays the window in the Maya interface. Execute the above script in the Script Editor. Next, play back the scene while you move the slider back and froth to see the effect it has on the gun emitter as shown in Figure 5. 11
Figure 7 Spray of particle from rotating gun Hand-on practice: Add New Window Control to Shelf We can add the script to make the slider window we created and put it in Maya s shelf so that it can be executed with click of a button. Select the text in the Script Editor, and hold down the middle mouse button. With the button held down, drag it into the shelf in the Maya interface. Close the Gun control silder we just created, and click the new MEL icon on the shelf. The slider window will return. If you want to name the icon, as it is always a good idea to do so, use the shelf Editor, available from the pop-up menu indicated by the triangle icon at the left side of the shelf. 12
Figure 8 Installing script as MEL shelf button Hand-on practice: Convert Panel to Polygons and Break up into Pieces You can either use the MEL script or use the Maya GUI to convert the panel to polygons and break it into smaller pieces. First, let s use the Maya GUI. Select the panel in the Maya scene and then choose Modify > Convert > NURBS to Polygons > option. The scene, Figure 7, will appear. Figure 9 NURBS to Polygons option 13
Change 3D Delta to 0.13 and Type to Quads, and then click Apply. We will have nice polygonal grid with even distribution of square faces. The corresponding MEL script is: Figure 10 Polygonal grid nurbstopoly -name polypanel -polygontype 1 -delta 0.13 panel; hide panel; select -r polypanel.f["*"]; $facelist = `filterexpand -sm 34`; // get the number of faces size($facelist); polychipoff -keepfacestogether 0 polypanel.f[0:(size($facelist)+1)]; polyseparate -name smpanels polypanel; // delete the original panel delete smpanels; We use the filterexpand command with the -selectionmask 34. If you look up the Maya help for filterexpand you will find mask 34 will get you the Polygon Face which is what we want and we store all the Polygon Face s in the variable $facelist. 14
The statement polychipoff makes the Polygon Face s into individual objects. The statement polyseparate separates those faces into its own node as we are going to apply rigid dynamics on to those Polygon Face s. We don t need the original Polygon Face smpanels anymore as we just need each individual ones, staring from 1 to 16. Hand-on practice: Add Dynamics and Expressions to Pieces We will use a for loop to give each smpanels, smpanel1 to smpanel16 dynamic properties, to connect them to particle collisions, and then to expression. What we want for each of them is: 1. Make each a solid body with an initial setting to react to a particle collision. 2. Connect each with partcle1 to enable collisions with its particles. 3. Assign an expression to each piece that will turn off particle collisions once the pieces reacts to a particle striking it, and that will send the piece in positive X and negative Z to simulate a wind force. (Exercise) int $i = 0; for ($i = 1; $i <= size($facelist); $i++) { rigidbody -active -damping 1 -collisions 0 -particlecollision 1 ("smpanels" + $i); collision -r 0.2 -f 0.2 ("smpanels" + $i); connectdynamic -c ("smpanels" + $i) particle1; } The most common use of ls is to filter or match objects based on their name (using wildcards, i.e. *) or based on their type. By default ls will match any object in the scene but it can also be used to filter or list the selected objects when used in the conjunction with -selection flag. The first command in the for loop turns smpanel + $i into a rigidbody with particle collisions on, and the next command makes it a collision object. 15
The third command connects the collision object with particle1. We don t want collisions between the smpanels for the sake of speed, so we need to set collisions to 0 or off, in the rigidbody command. Finally we need to adjust the life span of particle1 so that the particles will not live forever in the scene: setattr particleshape1.lifespanmode 1; setattr particleshape1.lifespan 1.0; setattr gun.spread 0.1; cyclecheck -e off; playbackoptions -min 1 -max 300 -loop continuous; Figure 11 The particles shoot off the polygon faces Exercise 1: Adding a new slider bar Add a new silder bar to Gun control window and it will control the up and down of the emitter. Exercise 2: Convert the scripts into a function 16
Convert the above scripts into a function called panelbreakup() so that you can call the function directly after you Source Script the script again. Exercise 3*: Simulate the wind blow away effect Remember the Expression that we had talked about in the class. You can enter Expression for each frame in Maya using Expression Editor. However, we can insert expression in each frame as well using MEL script. We can insert the expression using the following command. The variable $exprstr contains the desired expression. expression -s $exprstr -o ("expression" + $i) -ae true -uc all; Exercise: The wind blows away effect is simulated using the expression: // At frame 1, enable the collision // Set the impulse in X direction to be 0 // Set the impulse in Z direction to be 0???? // If the velocity in X direction of rigid body is // less than 0, // Disable the collision // Set the impulse in X direction to be 2 // Set the impulse in Z direction to be -2???? 1. Why do we write the above in this way? Explain it according to your understanding of Expression in Maya. 2. Implement the above effect into your final panelbreakup script. Exercise 4*: Exploding Sphere Based on the particle-rbd interaction that we had learnt in this class, I want you to create an effect as: 17
After the explosion, the fragments should be blow away by a very strong wind. ** End of Lecture 10 ** 18