SimplyMaya Forums

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


SM Forums / Maya Questions / Programming / Help with renaming

Programming MEL Scripts, Expressions & Programming Related Topics

Join the conversation. Reply to post
 
Thread Tools
17-02-2012, 10:41 AM   #1
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
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..
  Reply with quote
17-02-2012, 01:25 PM   #2
stwert
EduSciVis-er
 
Join Date: Dec 2005
Location: Toronto
Posts: 3,374
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.
  Reply with quote
17-02-2012, 04:04 PM   #3
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
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 :]
  Reply with quote
19-02-2012, 10:56 PM   #4
NextDesign
Technical Director
 
NextDesign's Avatar
 
Join Date: Feb 2004
Posts: 2,988
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.
  Reply with quote
20-02-2012, 05:18 PM   #5
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
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.
  Reply with quote
21-02-2012, 06:24 AM   #6
NextDesign
Technical Director
 
NextDesign's Avatar
 
Join Date: Feb 2004
Posts: 2,988
Quote:
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 06:31 AM.
  Reply with quote
21-02-2012, 03:27 PM   #7
yoruneko
Registered User
 
Join Date: Jan 2012
Posts: 43
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!
  Reply with quote
21-02-2012, 04:49 PM   #8
NextDesign
Technical Director
 
NextDesign's Avatar
 
Join Date: Feb 2004
Posts: 2,988
Haha. Thanks a lot mate. Glad it's working for you.
__________________
Imagination is more important than knowledge.
  Reply with quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem renaming material in attribute editor SealBaby Maya Basics & Newbie Lounge 3 19-06-2014 02:57 AM
importing obj files, renaming issues ktynn Maya Basics & Newbie Lounge 3 14-02-2013 10:50 AM
Renaming joints kerragan Maya Basics & Newbie Lounge 2 19-10-2006 07:58 PM
Renaming Objects using MEL Scripts oneshotflinch Maya Basics & Newbie Lounge 0 21-10-2005 01:25 PM
quick renaming joints luke Maya Basics & Newbie Lounge 5 25-04-2004 09:44 PM