PDA

View Full Version : give objects a prefix?


AtrusDni
12-26-2006, 07:11 PM
Hi so this is what im going to be doing . . . I have the code to find active rigid bodies in the scene:



// Find all Rigid Body Nodes in the scene

string $rigidBodyNodes[] = `ls -type rigidBody`;

// Check if the Rigid Body is Active

select -cl;

for ($i=0;$i<size($rigidBodyNodes);$i++)

{

string $activeRigidBody = `rigidBody -q -act $rigidBodyNodes[$i]`;

if($activeRigidBody == "true")

{

//add to array

select -tgl $rigidBodyNodes[$i];

}

}

//Populate activeRigidBodies list

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


so now i have $activeRigidBodies[] list and i want to go through and add a "arb_" prefix (Active Rigid Body) in front of the objects name so it will look like this:
arb_objectName
But I also need to check if it already has that prefix, so it doesnt name it arb_arb_objectName . . . .

Is there a way to check the first 4 characters of a string if they are equal to arb_ if so, dont do anything but if it doesnt equal arb_ to add it to the front of the objects name? Any info would be much appreciated. Thanks guys.

micle
12-26-2006, 08:26 PM
for ($obj in $activeRigidBodies)
{
int $size=size($obj);
string $prefix=`substring $obj 1 4`;
if ($prefix != "arb_")
{
rename $obj ("arb_" + $obj);
}
}

AtrusDni
12-26-2006, 08:27 PM
a ha! got it! yay for searching maya help docs ;-) lol.

Well if anyone is interested . . . the way i did it was like this:
// Find all Rigid Body Nodes in the scene

string $rigidBodyNodes[] = `ls -type rigidBody`;

// Check if the Rigid Body is Active

select -cl;

for ($i=0;$i<size($rigidBodyNodes);$i++)

{

string $activeRigidBody = `rigidBody -q -act $rigidBodyNodes[$i]`;

if($activeRigidBody == "true")

{

//add to array

select -tgl $rigidBodyNodes[$i];

}

}

//Populate activeRigidBodies list

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

// Get list of objects associated with active rigid bodies

select -cl;

for ($i=0;$i<size($activeRigidBodies);$i++)

{

string $activeObject[] = `listRelatives -p $activeRigidBodies[$i]`;

select -tgl $activeObject[0];

}

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

// Rename all active rigid bodies to arb_##### //

for($i=0;$i<size($activeObjects);$i++)

{

string $checkPrefix = `match "^arb_" $activeObjects[$i]`;

if ($checkPrefix != "arb_")

{

rename $activeObjects[$i] ("arb_" + $activeObjects[$i]);

}

}


The trick was the "match" command, and using the "^arb_" value, which checks at the beginning of a string for "arb_". Then using an if statement with != checks if it does not equal arb_ then add it. Cool beans. Yay for mel scripting.

AtrusDni
12-26-2006, 11:07 PM
hi micle! sorry i we must have posted a reply at the same time, because your reply wasnt there when i posted mine. Interesting, substring? I will have to check it out, Thanks for the help! Gonna go read up on it.

CGTalk Moderation
12-26-2006, 11:07 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.