Home Technical Talk

MEL script For Loop problem

I'm fairly new to MEL so please bare with me.

I'm trying to create a script that will create a polyCube with a spotLight after pressing a button, but each time you press the button in the menu it would create another copy of the two, but right beside it and not on top of the original. Basically creating a loop of the cube and light, but placed right beside it. All of my code works except for anything trying to add in a for loop. I'm not sure where in my code it should be placed. Anyone have a suggestion? Here is the working code without the for loop.





//Create a window with a gray working menu bar. Window will Exit and go to Maya Help.
//Window has an image based button.
//User can create a PolyCube with 5 subdivision in the X,Y & Z axis. PolyCube also has unwrapped UVs and a Spot Light.


//Window will close previous window if there is already a window present.
{
if (`window -exists myMenuBar`)
{
deleteUI myMenuBar;
windowPref -remove myMenuBar;
}

window -title "Kevin's Window" -menuBar true -width 50 -height 100 myMenuBar;
gridLayout -bgc .2 .2 .2 -numberOfColumns 3 -cellWidthHeight 150 50;
$command = ("polyCube -sx 5 -sy 5 -sz 5 -createUVs 1 ; spotLight -position 4.403 1.919 6.585 -rotation -11.235 34.113 0 ;");
symbolButton -image "Network/AIRDSERV01/Student Drive/Cherme/GAD3333 - 3DScripting/Final/kevinautry/cube.xpm" -width 50 -height 100 -command $command;



//Create a tearOff menu with menu item.
menu -label "File" -tearOff true;
//Added menu items
menuItem -label "Exit" -command "deleteUI myMenuBar";

//Add a help menu.
menu -label "Help" -helpMenu true;
global string $docs = ("Commands/index.html"); //this string tells where the help file is located
menuItem -label "MEL Command Reference" -command " showHelp -docs $docs " ;



showWindow myMenuBar;
}

Replies

  • DireWolf
    Options
    Offline / Send Message
    So you want to create cube, and has it move to new spot each time you press a button, am I correct?

    If that's the case, this is not a loop thing. It's a 1 time function that executes as you press the button. You'll need to find a way to store the location so that you can add to it each time the function is called.

    Here's an example. I hope it gives you some idea.
    int $x;
    $x = 0;
    
    proc int createIt(int $x){
         string $cubeName;
        $cubeName = `CreatePolygonCube`;
        move -absolute $x 0 0 $cubeName;
        $x = $x +5;
        return $x;
    }
    
    Once you run that, use this command to execute.
    $x = createIt($x);
    
  • baldboykev
    Options
    Offline / Send Message
    How would I incorporate that into the button I've created?

    Would that be placed in the () before polyCube?
  • DireWolf
    Options
    Offline / Send Message
    Your button would need to call a function instead of having a command embed.

    I'm sorry I haven't script in MEL for a very long time so I won't be able to cook you any examples. I can only give you the idea.
  • passerby
    Options
    Offline / Send Message
    passerby polycounter lvl 12
    here is a python way to do the same
    import pymel.core as pm
    
    
    class UI(object):
        def __init__(self):
            if pm.window('cubeTest', exists=True):
                pm.deleteUI('cubeTest')
    
            with pm.window('cubeTest', title='Create Cube', width=50, height=100) as window:
                with pm.columnLayout():
                    pm.button(l='Create Cube', w=150, c=self.CreateCube)
    
        def CreateCube(*args):
            sel = pm.selected()
            cube = pm.polyCube(sx=1, sy=1, sz=1)[0]
            if sel:
                cube.translateX.set(sel[0].translateX.get() + 5)
    
    UI()
    
  • claydough
    Options
    Offline / Send Message
    claydough polycounter lvl 10
    why did u choose the position values for the light in the script?
    More importantly what is the importance of the position of the light relative to the cube?
    ( fer example will the cube be a look at "aim" object or a position constraint for the light? )
    Without knowing it's next to impossible to suggest a strategy for initial position at creation.

    Also...
    Just wondering why 5 subdivisions?
Sign In or Register to comment.