SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.

Home / forum / Animation / Mel Scripts & Programming / Help with renaming
Register FAQ Members List Calendar Mark Forums Read

New Thread
 
Thread Tools
Old 17-02-2012, 09:41 AM   #1
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
Default 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..
yoruneko is offline   Reply With Quote
Old 17-02-2012, 12:25 PM   #2
stwert
EduSciVis-er
 
Join Date: Dec 2005
Location: Toronto
Posts: 3,374
Default

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.
stwert is offline   Reply With Quote
Old 17-02-2012, 03:04 PM   #3
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
Default

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 :]
yoruneko is offline   Reply With Quote
Old 19-02-2012, 09:56 PM   #4
NextDesign
Technical Director
 
NextDesign's Avatar
 
Join Date: Feb 2004
Posts: 2,988
Default

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
__________________
Imagination is more important than knowledge.
NextDesign is offline   Reply With Quote
Old 20-02-2012, 04:18 PM   #5
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
Default

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.
yoruneko is offline   Reply With Quote
Old 21-02-2012, 05:24 AM   #6
NextDesign
Technical Director
 
NextDesign's Avatar
 
Join Date: Feb 2004
Posts: 2,988
Default

Quote:
Originally Posted by yoruneko View Post
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
__________________
Imagination is more important than knowledge.

Last edited by NextDesign : 21-02-2012 at 05:31 AM.
NextDesign is offline   Reply With Quote
Old 21-02-2012, 02:27 PM   #7
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
Default

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!
yoruneko is offline   Reply With Quote
Old 21-02-2012, 03:49 PM   #8
NextDesign
Technical Director
 
NextDesign's Avatar
 
Join Date: Feb 2004
Posts: 2,988
Default

Haha. Thanks a lot mate. Glad it's working for you.
__________________
Imagination is more important than knowledge.
NextDesign is offline   Reply With Quote
New Thread

« Previous Thread | Next Thread »

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off