Check for name, then +1?


#1

Hello!

I recently began writing in mel, and I’ve been trying to creat a script to create a sort of basic cityscape, however, I realized that when I create the first building, the script stops working cause its made to work with a specific name “Building”. How would I go about making so that when the script runs, when I press the button to launch the procedure, it checks for a count, names the object “Building+count” and adds a point to that count, something like “Building 1” “Building 2” etc.

Here is the code I’m using:

proc makeBuilding(){     
     polyCube -w 5 -h 20 -d 5 -sx 5 -sy 20 -sz 5 -n Building; 
     select -cl;
     select -r  Building.f[91] Building.f[93] Building.f[83] Building.f[81] Building.f[71] Building.f[73] Building.f[63] Building.f[61] Building.f[51] Building.f[53] Building.f[43] Building.f[41] Building.f[31] Building.f[33] Building.f[23] Building.f[21] Building.f[11] Building.f[13] Building.f[441] Building.f[443] Building.f[433] Building.f[431] Building.f[421] Building.f[423] Building.f[413] Building.f[411] Building.f[401] Building.f[403] Building.f[393] Building.f[391] Building.f[381] Building.f[383] Building.f[373] Building.f[371] Building.f[361] Building.f[363] Building.f[133] Building.f[131] Building.f[141] Building.f[143] Building.f[153] Building.f[151] Building.f[161] Building.f[163] Building.f[173] Building.f[171] Building.f[181] Building.f[183] Building.f[193] Building.f[191] Building.f[201] Building.f[203] Building.f[213] Building.f[211] Building.f[252] Building.f[261] Building.f[263] Building.f[271] Building.f[273] Building.f[281] Building.f[283] Building.f[291] Building.f[293] Building.f[301] Building.f[303] Building.f[311] Building.f[313] Building.f[321] Building.f[323] Building.f[331] Building.f[333] Building.f[341] Building.f[343] ;
     polyExtrudeFacet -constructionHistory 1 -keepFacesTogether 1 -pvx 2.5 -pvy -0.5 -pvz 0 -divisions 1 -twist 0 -taper 1 -off 0 -thickness 0 -smoothingAngle 30  Building.f[91] Building.f[93] Building.f[83] Building.f[81] Building.f[71] Building.f[73] Building.f[63] Building.f[61] Building.f[51] Building.f[53] Building.f[43] Building.f[41] Building.f[31] Building.f[33] Building.f[23] Building.f[21] Building.f[11] Building.f[13] Building.f[441] Building.f[443] Building.f[433] Building.f[431] Building.f[421] Building.f[423] Building.f[413] Building.f[411] Building.f[401] Building.f[403] Building.f[393] Building.f[391] Building.f[381] Building.f[383] Building.f[373] Building.f[371] Building.f[361] Building.f[363] Building.f[133] Building.f[131] Building.f[141] Building.f[143] Building.f[153] Building.f[151] Building.f[161] Building.f[163] Building.f[173] Building.f[171] Building.f[181] Building.f[183] Building.f[193] Building.f[191] Building.f[201] Building.f[203] Building.f[213] Building.f[211] Building.f[252] Building.f[261] Building.f[263] Building.f[271] Building.f[273] Building.f[281] Building.f[283] Building.f[291] Building.f[293] Building.f[301] Building.f[303] Building.f[311] Building.f[313] Building.f[321] Building.f[323] Building.f[331] Building.f[333] Building.f[341] Building.f[343];
     setAttr "polyExtrudeFace1.localTranslate" -type double3 0 0 -0.769078 ;
     select -r Building.f[239] ;
     doDelete;
     select -r Building.f[593] ;
     doDelete;
     polyCloseBorder -ch 1 Building.e[536] Building.e[1189];     
 }  
makeBuilding();

Also, If someone could explain how i could use sliders to move a selected object around, I would be very grateful :smiley:

Thanks!


#2

Writing this in mel would not be my first choice, but I thought it would be interesting if I could try to remember how mel works. I can see you are first doing the steps manually then taking code from the script editor. This is a great way to start, but you should try to avoid the “select” command when you write your proc, since most of the time it is not needed. Instead you can retrieve the results from other commands and build new commands from those results. So I have rewritten your code with this in mind.

Commands like polyCube and polyExtrudeFacet return lists, so in each case I am accessing the first element of the list using [0].

I’m also taking that long list of faces and coding it as a simple list of ints. Then in the code I construct the list of face names inside a for loop, before passing it to the polyExtrudeFacet command.

I’m using the select command just once at the end, so that when the proc finishes, the newly created building will be selected.

I’ve defined the proc with a string argument called “name” so you can pass in the name for the building when you run the proc. In the code I’m appending the # symbol when I name the cube. This is the maya way to make a name with the next available index.

proc makeBuilding(string $name){
    string $polyCubeCreationList[] = `polyCube -w 5 -h 20 -d 5 -sx 5 -sy 20 -sz 5 -n ($name +"#")`; 
    string $building = $polyCubeCreationList[0];
    
    int $faceIndexList[] = {91 ,93 ,83 ,81 ,71 ,73 ,63 ,61 ,51 ,53 ,43 ,41 ,31 ,33 ,23 ,21 ,11 ,13 ,441 ,443 ,433 ,431 ,421 ,423 ,413 ,411 ,401 ,403 ,393 ,391 ,381 ,383 ,373 ,371 ,361 ,363 ,133 ,131 ,141 ,143 ,153 ,151 ,161 ,163 ,173 ,171 ,181 ,183 ,193 ,191 ,201 ,203 ,213 ,211 ,252 ,261 ,263 ,271 ,273 ,281 ,283 ,291 ,293 ,301 ,303 ,311 ,313 ,321 ,323 ,331 ,333 ,341 ,343};
    string $faceList[] = {};
    for($i=0; $i<size($faceIndexList); $i++) {
        $faceList[$i] = ($building + ".f[" + $faceIndexList[$i] + "]");
    }
    
    string $polyExtrudeFacetList[] = `polyExtrudeFacet -constructionHistory 1 -keepFacesTogether 1 -pvx 2.5 -pvy -0.5 -pvz 0 -divisions 1 -twist 0 -taper 1 -off 0 -thickness 0 -smoothingAngle 30 $faceList`;
    string $polyExtrudeFacet = $polyExtrudeFacetList[0];
    
    setAttr ($polyExtrudeFacet + ".localTranslate") -type double3 0 0 -0.769078 ;
    
    delete ($building + ".f[239]");
    delete ($building + ".f[593]");
    
    polyCloseBorder -ch 1 ($building + ".e[536]") ($building + ".e[1189]"); 
    
    select -r $building;
    print ("Created " + $building +"
");
}

makeBuilding("tower");

David