PDA

View Full Version : ls -sl storage


RaGzMaN
11-07-2005, 04:36 PM
If i want to use the commang `ls -sl` and i have more than one thing selected, how can i access those individual items.

For example.

I have a cube and a sphere selected it will return:
// Result: pSphere1 pCube1 //

Is it possible to allocate these names into a string so that e.g $mystring is pSphere1 and $mystring2 is pCube1.

cheers

Segmoria
11-07-2005, 06:18 PM
When you use the commang `ls -sl` you usually store its results into an array or strings so that you can store in it the name of more than one object that is selected.
So you can say: string $selection[] = `ls -sl`;

By this you are declaring an (undefined sized) array of strings and if you have objects pCube1 and pSphere1 selected at runtime the array will be filled with the current selection stack. So you have:
$selection[0] = "pCube1"
$selection[1] = "pSphere1"

You can access certain members in that array using the respective subscripts.

Robert Bateman
11-07-2005, 06:28 PM
or loop through each one inturn...

$items = `ls -sl`;
for($item in $items) {
print($item+"\n");
}

RaGzMaN
11-07-2005, 07:11 PM
hi there thanks for your replies.
I have been experimenting with this but have encountered an issue somewhere.

What im trying to do is store the translate values of the object selected and place them inside a vector.

iM GETTING AN ERROR ON LINE 8 -
// Error: line 8: Cannot convert data of type float[] to type vector due to array size mismatch (6 elements). //


proc locbetobjtwo (int $per)

{

string $selection[] = `ls -sl`;

string $obj1 = $selection[0];

string $obj2 = $selection[1];

vector $myvecobj1 = `getAttr $obj1.translate`;

vector $myvecobj2 = `getAttr $obj2.translate`;



float $tx = $myvecobj1.x;

float $ty = $myvecobj1.y;

float $tz = $myvecobj1.z;

float $tx2 = $myvecobj2.x;

float $ty2 = $myvecobj2.y;

float $tz2 = $myvecobj2.z;

float $xpos = (($myvecobj1.x + $myvecobj2.x) /100) *$per;

float $ypos = (($myvecobj1.y + $myvecobj2.y) /100) *$per;

float $zpos = (($myvecobj1.z + $myvecobj2.z) /100) *$per;

spaceLocator;

setAttr("locator1.translate", $xpos, $ypos, $zpos);

}



locbetobjtwo (25);









/////////Take the name of 2 objects and place a locator between the 2 objects depending on the percentage.

RonGriswold
11-07-2005, 07:35 PM
instead of: `getAttr $obj1.translate`;
try: `getAttr ($obj1 + ".translate")`;

Ron Griswold
R!OT
rgriswold@rioting.com

RaGzMaN
11-07-2005, 08:15 PM
beautiful, thanks man.

frogspasm
11-07-2005, 08:32 PM
I've run into that before...
Any idea why doesn't that first one work?

~Mike D.

instead of: `getAttr $obj1.translate`;
try: `getAttr ($obj1 + ".translate")`;

Ron Griswold
R!OT
rgriswold@rioting.com

Bonedaddy
11-07-2005, 09:50 PM
I've run into that before...
Any idea why doesn't that first one work?

~Mike D.

It's looking for an object named $obj1, instead of calculating what $obj1 is. By enclosing it in parentheses, you make it evaluate what the string contained in $obj1 is, by tacking .translate onto the end of it.

mhovland
11-07-2005, 10:04 PM
I've run into that before...
Any idea why doesn't that first one work?

~Mike D.

What you are doing is called string catenation, and I post a simple explaination of what that is here:

http://forums.cgsociety.org/showthread.php?t=287553

RonGriswold
11-07-2005, 11:30 PM
This has probably been answered, but here goes. When you use $obj1.translate like that it is looking for an object named $Obj1.translate instead of what your variable would expand to. To get around this you need to use a string expression ($obj1 + ".translate") that evaluates to the object/attr name you are looking for.

Hope that helps,

Ron
R!OT

RaGzMaN
11-08-2005, 10:10 AM
Thanks for all your replies.

Your answers totally make sense to me. Ive just started learning MEL so its really a case of just understanding the grammar and syntax for me.

I completely understand what you guys have said though.

What i was trying to tell it is look for a variable with $obj1.translate of which of course does not exist. What i needed to do was evaluate what was inside of $obj1 and concatenate .translate onto the end of it.

Thanks guys,

CGTalk Moderation
11-08-2005, 10:10 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.