Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Programming (https://simplymaya.com/forum/forumdisplay.php?f=32)
-   -   Help with renaming (https://simplymaya.com/forum/showthread.php?t=37261)

yoruneko 17-02-2012 10:41 AM

Help with renaming
 
Hi there.
I don't know crap about mel.
BUT I'm asking anyway, I have a script that assigns new materials from a selection and renames them based on the object's names.
It's almost perfect from my use, but I need just one more thing to save hours of mind numbing work, a string that changes the first letter of the materials name to an uppercase, this way I have no duplicate names and no numbers as a postfix to material's names.
I have ZERO idea on how to do this, so here's the script (it's not mine I salvaged it from some other guy and modified it a bit)

string $nodeName[] = `ls -sl`;
string $matName, $sgName, $fileName;
for($i=0; $i<size($nodeName); $i++)
{
$matName = `shadingNode -as phong -n ($nodeName[$i])`;
$sgName = `sets -renderable true -noSurfaceShader true -empty -name ($nodeName[$i] + "SG")`;
connectAttr -f ($matName + ".outColor") ($sgName + ".surfaceShader");
select $nodeName[$i];
sets -e -forceElement $sgName;
}
clear $nodeName;

//script by Paul Lohman

Any help much appreciated really, this would make my life a ton easier..

stwert 17-02-2012 01:25 PM

My convention that I've seen elsewhere is to have an M at the end of the name, e.g. floorM (for material). That would be easier to append than changing the case of the existing string.

yoruneko 17-02-2012 04:04 PM

I have no choice, I can't change the naming convention. It's not my decision to make.
We use camelcase, but no uppercase for the name of the objects.
But the names of the materials needs to have full camelcase, like this:
someObject ->obj
SomeObject ->mat
I have to assign a material per object with the freakin uppercase, can't avoid that.
Seems like nothing but I can't just copy the name with the script thus keeping me from "one button press bliss" and into "spend precious minutes doing repetitive crap hell". Story of my life :]

NextDesign 19-02-2012 10:56 PM

So you want it to automatically find words in a string, and camel case them? This would take forever in a scripting language, as it would need to run through a large dictionary of words, and compare them. Changing the first letter to upper case in a material though, is simple.

Code:

string $name = "thisisatest";

int $nameSize = `size $name`;
string $firstCharacter = `substring $name 1 1`;
string $rest = `substring $name 2 $nameSize`;

string $newName = `toupper $firstCharacter` + $rest;

print ($newName);

// Result: Thisisatest


yoruneko 20-02-2012 05:18 PM

This script is exactly what I need!
I'm ashamed to ask but I have no idea how to use it.
Would it be possible to merge it with the first script just as to uppercase the first letter of the material?
That would save me a lot of time and possibly the rest of the team too.

NextDesign 21-02-2012 06:24 AM

Quote:

Originally Posted by yoruneko (Post 333457)
Would it be possible to merge it with the first script just as to uppercase the first letter of the material?

Of course. Give this a try:

Code:

string $nodeName[] = `ls -sl -tr -dag`;
string $matName, $sgName, $fileName;

for($i=0; $i<size($nodeName); $i++)
{
    $matName = `shadingNode -as phong -n ($nodeName[$i])`;
    $sgName = `sets -renderable true -noSurfaceShader true -empty -name ($nodeName[$i] + "SG")`;
    connectAttr -f ($matName + ".outColor") ($sgName + ".surfaceShader");
    select $nodeName[$i];
    sets -e -forceElement $sgName;

    int $nameSize = `size $matName`;
    string $firstCharacter = `substring $matName 1 1`;
    string $rest = `substring $matName 2 $nameSize`;

    string $newName = `toupper $firstCharacter` + $rest;

    rename $matName $newName;
}

clear $nodeName;

I've also changed the way $nodeName is populated. The way you had it, `ls -sl` would grab all selected nodes, not just geometry. I switched it to `ls -sl -tr -dag` to fix this. It means that it will grab all transform dag nodes for you.

Hope this works for you.

-John

yoruneko 21-02-2012 03:27 PM

Awesome, it's working perfectly!
Thanks a hundred times, approximately the number of hours it will save me!

Vive le Canada! Merci vraiment c'est la super classe!

NextDesign 21-02-2012 04:49 PM

Haha. Thanks a lot mate. Glad it's working for you.


All times are GMT. The time now is 02:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018