PDA

View Full Version : Get the last deformer name?


ecas
11-15-2011, 10:59 PM
Hey guys

I have a project which I need to give to a lot of object 6 bends,
main bend, front bend and back bend X 2 axis. so I wrote this simple script for 1 axis


nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
select -addFirst bend1 ;
rename "bend1" "Main_bend";
nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
select -addFirst bend1 ;
rename "bend2" "Front_bend";
nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
select -addFirst bend1 ;
rename "bend3" "Back_bend";


My problem is that the names of the bends won't be just bend1, 2, 3...
since I talking about 10 objects it will get to bend22, bend 39 ect...

So how can I get the latest deformer name and change it?
I can tell in the result I have the bend name but I don't know how to grab it
and isolate it. I tried to look in the docs and google, but didn't find something helpful.

WesHowe
11-16-2011, 12:55 AM
You can use the ls command with a wildcard:
string $bends[]=`ls "bend*"`;
That will fill the array $bends with every node that is names "bend" + something, and will sort them in naming order.

<* Wes *>

Edit: I think also what I should have mentioned is that you can use the "select" command, as in:
select -r $bends[0]; // or [1] or [2], etc.
to make one of them the sole selected item.

ecas
11-16-2011, 10:07 AM
Hi wes, thank you for the help but I don't really understand how
to implent what you said.

I did something a bit diffrent, since the bent numbers will always follow, I wrote this
string $objName = "polySurface27";
$bendNum = 4;

nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
select -addFirst bend$bendNum ;
rename "bend"+$bendNum "Main_bend";
$bendNum++;
select -r $objName ;
nonLinear -type bend -lowBound -1 -highBound 0 -curvature 0.2 -parallel;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
select -addFirst bend$bendNum ;
rename "bend"+$bendNum "Front_bend";
$bendNum++;
select -r $objName ;
nonLinear -type bend -lowBound 0 -highBound 1 -curvature 0.2 -parallel;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
select -addFirst bend$bendNum ;
rename "bend"+$bendNum "Back_bend";
$bendNum++;
select -r $objName ;
nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel;
rotate -r -os 0 0 -90 ;
select -addFirst bend$bendNum ;
rename "bend"+$bendNum "Side_bend";
of course it's not working but I don't understand why, I basicly put 2 vvariables
in the begining for the object name and the bent number. and I get the following error:
" Error: rename "bend"+$bendNum "Main_bend"; " for each rename line. What is wrong?

btw this is my first MEL script, I learn PHP a few years ago so I understand
a little bit but I will need a bit more explanation.

Azrail
11-16-2011, 01:02 PM
You should capture the result of every nonLinear command, which actually returns the name of the deformer and the handle as string:

string $bend[] = `nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel`;
// Result: bend1 bend1Handle //

so $bend[0] is the deformer and $bend[1] is the handle;

Btw the best practice is to always capture the output of the command, which creates something, than relying on the presumption that object names would be the same as you've expected.

WesHowe
11-16-2011, 04:36 PM
You had, for one, some syntax errors in your script. Following the other good suggestion that was made by Azrail, here is a way you can structure your script:
string $objName = "polySurface27";

string $thisNewBend[];

select -r $objName ;
$thisNewBend=`nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel`;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
rename $thisNewBend[1] "Main_bend";

Test this and make it work, then copy and rework the last 5 lines for your other bends.

To make your script more useful, here are some things other people do:

* put the code in a global proc, and make the script file have the same name as the proc, like this:
global proc bendMaker() {
// put code here
}
and put that in a script file named bendMaker.mel

* instead of coding "polySurface27" into the script, why not make teh script do that you make a selection and it operates from that (this is how most Maya tools and scripts work). Here's how:
string $objName[]=`ls -sl`;
You will then need to change lines like this:
select -r $objName ;
to this:
select -r $objName[0] ;
Putting that all together would leave this:

global proc bendMaker() {
string $objName[]=`ls -sl`;
string $thisNewBend[];

select -r $objName[0];
$thisNewBend=`nonLinear -type bend -lowBound -1 -highBound 1 -curvature 0.2 -parallel`;
rotate -r -os 0 90 0 ;
rotate -r -os 0 0 -90 ;
rename $thisNewBend[1] "Main_bend";
}

The code to manage the other bends you want would be created from the last five lines of the script.

<* Wes *>

EDIT: I gave up on trying to manage indentation on this sorry excuse for forum software.

ecas
11-24-2011, 09:41 PM
Hey guys, thank you both for the help, sorry for not replying sooner anyway
this code took me too much time to mass with so I just used the basic script
without all the fancy stuff, however I must say I got more intersted in MEL, this thing
can really speed up work sometimes, Im pretty sure Ill come back to this soon.

CGTalk Moderation
11-24-2011, 09:41 PM
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.