View Full Version : Trouble with MEL script, trying to make UI for ball with gravity dynamic

12 December 2010, 04:08 AM
Hey, I've been struggling with this for the last two days. I am trying to create a game with mel script, not by choice but for a project. The last piece that I need to make this possible is to attach transformation controls to a sphere that has a gravity dynamic (g.d.) on it. However, I think because the sphere has the g.d. on it , that overrides any transformation scripting. I tried breaking the connections of the g.d. on the axis' that I need to move but that afffects the precision of the g.d. , so no good. I have also tried creating another sphere, and attaching the UI buttons to transform that, while parenting the original sphere to it, kinda of works but not great. Originally, I was hoping to use a script similar to the throttle in the "make motor boat" effect. By adjusting the throttle and rudder you can make a boat turn. It is easy to turn this into buttons. However, I can't figure out how to re-script those attributes.

My code is such so far: In order to recreate you would need to create plane with passive rigid body and have a sphere (sphere3) with gravity
//////Ensures script will run if previously ran
if ( `window -exists MyWindow` ) {
deleteUI MyWindow;
window -t "melBallMadness V 1.0"
-wh 500 400
-mxb off
-resizeToFitChildren on
-sizeable off
///////Declaring UI variables
formLayout myForm;
/////Image location
image -image (`internalVar -usd` + "MO/mxp_snowJob/titleImages/mxp_snowJob2.jpg") -w 60 -h 140 titleImage;
text -fn "boldLabelFont" -l "Welcome to melBallMadness press Start to play!" titleText;
/////Starts "Game"
button -l "Start/Pause"
-command "playButtonForward;"
/////Resets "Game"
button -l "Reset"
-command "playButtonStart;"
separator -w 400 sepLine;
text -l"Sound" soundText;
////Enables sound on or off
checkBox -l"On/Off"
-onCommand "setSoundDisplay Goldfrapp___Strict_Machine__We_Are_Glitter_Mix_ 1;"
-offCommand "setSoundDisplay `timeControl -q -s $gPlayBackSlider` 0;" checkBox;
button -l "Left" -c moveLeft leftButton;
button -l "Right" -c moveRight rightButton;
button -l "Foward" -c moveFoward fowardButton;
button -l "Back" -c moveBack backButton;
global proc moveLeft(){
select -r pSphere3;
move -r 0 0 .1;
global proc moveRight(){
select -r pSphere3;
move -r 0 0 -.1;
global proc moveFoward(){
select -r pSphere3;
move -r .1 0 0;
global proc moveBack(){
select -r pSphere3;
move -r -.1 0 0;
/////////////UI Setup -af = attach Form, -ac = attach Control , -ap = attach Position
formLayout -e

-af titleText top 15
-af titleText left 150
-af titleText right 0

-af startButton left 0
-ac startButton top 10 titleText
-ap startButton right 0 50
-af resetButton right 0
-ac resetButton top 10 titleText
-ac resetButton left 1 startButton

-af sepLine left 0
-ac sepLine top 10 startButton
-af sepLine right 0
-af titleImage left 250
-ac titleImage top 10 sepLine
-af titleImage right 275
-af soundText left 0
-ac soundText top 5 backButton
-af soundText right 400
-af checkBox left 0
-af checkBox bottom 0
-af checkBox right 600
-af fowardButton left 195
-ac fowardButton top 20 titleImage
-af fowardButton right 200

-af leftButton left 130
-ac leftButton top 1 fowardButton
-ap leftButton right 0 50
-af rightButton right 130
-ac rightButton top 1 fowardButton
-ac rightButton left 1 startButton
-af backButton left 195
-ac backButton top 1 leftButton
-af backButton right 200

showWindow MyWindow;

I appreciate any insight, thanks!

12 December 2010, 01:07 PM
I am a beginner in scripting.
I think you forgot to put "" in the button commands.
button -l "Left" -c "moveLeft" leftButton;
button -l "Right" -c "moveRight" rightButton;
button -l "Foward" -c "moveFoward" fowardButton;
button -l "Back" -c "moveBack" backButton;

Plz check it. I not sure if it is the issue

12 December 2010, 08:14 PM
Hey tdsmad,

I'm new to scripting too,lol, but from what I picked up I don't think I need quotes there because I am calling on a global procedure which I named below. I did try with and without and the buttons still worked. The orginal problem was when I tried to use the buttons on the sphere1, where the gravity has the sphere1 transformation attributes connected to it and therefor are locked so the buttons won't work. Now when I parent the sphere1 that has a gravity dynamic on it, to a "controller object" sphere3, all though they move with the butttons, the gravity doesn't work right because the gravity is based on the transformation coordinates of sphere1. P.S. At this point my script is showing the latter, a little confusing with the different sphere names. Thanks for your reply though : )

12 December 2010, 05:05 AM
Check out Impulse attribute it work in my case but make sure
if set one attribute set other to zero

I came up with this script

p, li { white-space: pre-wrap; }
global proc Reset ()
setAttr "rigidBody2.impulseZ" 0;
setAttr "rigidBody2.impulseX" 0;
setAttr "rigidBody2.impulseY" 0;
global proc moveLeft(){
setAttr "rigidBody2.impulseZ" 0;
setAttr "rigidBody2.impulseX" 0;
setAttr "rigidBody2.impulseY" 0;
setAttr "rigidBody2.impulseX" -0.09;
global proc moveRight(){
setAttr "rigidBody2.impulseZ" 0;
setAttr "rigidBody2.impulseX" 0;
setAttr "rigidBody2.impulseY" 0;
setAttr "rigidBody2.impulseX" 0.09;
global proc moveFoward(){
setAttr "rigidBody2.impulseZ" 0;
setAttr "rigidBody2.impulseX" 0;
setAttr "rigidBody2.impulseY" 0;
setAttr "rigidBody2.impulseZ" -0.09;
global proc moveBack(){
setAttr "rigidBody2.impulseZ" 0;
setAttr "rigidBody2.impulseX" 0;
setAttr "rigidBody2.impulseY" 0;
setAttr "rigidBody2.impulseZ" 0.09;

Value may be differs in your case

12 December 2010, 06:22 AM
Thanks rahulsg2901 I will try that out

CGTalk Moderation
12 December 2010, 06:22 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.