View Full Version : First script - Prefix Renamer
woadiestyol 09-24-2008, 06:19 PM Hey all,
I just finished writing my first MEL script :). This script opens a window where the user specifies an "old" and "new" prefix. All selected objects with the old prefix are renamed with the new prefix instead.
I don't have any programming experience, but I've been combing throught the wonderful Art of Rigging books and learning by example. So, any and all tips, suggestions, etc are very much welcome. I'm sure there are better ways to accomplish this, and I've love to hear how I can improve this (and future) scripts!!
Thanks,
JP
|
|
Buexe
09-24-2008, 07:05 PM
congrats! While glancing quickly over it I wondered why you used the "while" loop thing instead of a "for ( $bla in $blaArray)" loop. And I thought if this script can handle renaming if there are several objects with the same name in the scene. But as long as it works, no worries...
cheers
(What I didn`t dare to ask: isn`t there a "Search and Replace Names" functionality in the "Modify" menu?)
woadiestyol
09-24-2008, 08:05 PM
Thanks for the feedback. I realized when I read your post that I made a major, major noob mistake - I forgot that objects parented other under objects are named "parentObject|childObject," not just "childObject."
Back to the drawing board!
NaughtyNathan
09-25-2008, 09:56 AM
good work John, that's a nice first script (disregarding it's actual relevance of course! ;) ). you seem to demonstrate some decent and "correct" programming traits in your scripting; good commenting, decent indenting/formatting, sensible named variables, local procs, etc.. quite rare for a "first" script!
I have a couple of tips for you, seeing as how you asked! ;)
You've called your procedure jpRenamePrefix, but you've called the script file jpPrefixRename. This means that it cannot be found and auto-sourced by Maya. This is not good as it means you have to manually source it when you want to use it (a waste of time and effort). Make sure you call your mel scripts the same name as their main procedure, that way, in Maya you can simply type the name of the proc you want to run and it will run (as long as the .mel script file is somewhere in your script path!).
Possibly related to this, you've made the main work proc local (which, btw, is excellent to see in theory), but you call it from a button. Unfortunately this will not work as all procs called by a UI control have to be global. (If this script has ever worked for you in this format it's probably because you executed the script inside Maya, rather than sourced it from a file and that initiailised the local proc as global. This seems likely given my previous comment about proc/script names).
As Buexe mentioned, a for ($object in $list) loop would maybe be more appropriate here than a while... While your method works fine (pardon the pun!), while loops often require extra data/variables to set up, are more prone to infinite loop lockups and are less obvious to read what is happening at a casual glance. (ultimately just a matter of taste though..)
Always try to avoid using the select command in your scripts (unless of course selection is actually a function of the script (e.g. a script that selects all objects using lambert1))
Changing the selection state places an unnecessary extra burden on Maya and 99% of MEL commands take object names as parameters so you rarely, if ever, need to rely on the selection list. A rename script certainly doesn't need to use the select command!
finally, a slightly more advanced suggestion, try to make your various work procs take input parameters, this makes them more versatile. For example, if you made renamePrefixProc() take both prefixes as input strings, you could call and utilise this function from within other scripts you write where you needed to do prefix renaming!
:nathaN
woadiestyol
09-25-2008, 02:46 PM
Awesome, thanks for the tips! Lots of good nuggets of wisdom in there, and I'll try to incorporate them into the next revision of the script. I may get a chance to look at this again tonight - if so, I'll post up the new version soon...
-JP
ewerybody
09-26-2008, 09:29 AM
hi guys!
And hey Naughty: I have to argue on your 2nd tip chapter: :D
The main proc that has the same name of the script file always has to be global! Of course especially when called from UI but not only then! Otherwise maya cannot find it (unless as you said its sourced directly).
maybe we could bundle our wisdom about mel-hashing somewhere ^^
Another thing I found out when using local procs: The local ones always have to be defined above the proc thats calling it!
This stuff is actually not findable in the Maya docs isn't it?
Buexe
09-26-2008, 10:18 AM
Another thing I found out when using local procs: The local ones always have to be defined above the proc thats calling it!
This stuff is actually not findable in the Maya docs isn't it?
AFAIK this is because maya sources only the local procs until it finds the global proc with the name of the file. So the main proc should be at the end of the file. I read it somewhere but can`t remember where, prolly in the docs or in "MEL scripting for Maya ...". I use only global procs anyway ...
ewerybody
09-26-2008, 11:10 AM
I usually too .. but If I tend to release a script I prefer making script dedicated procs local only.
Btw: There is a difference between hashing and sourcing: All scripts in the Maya available folders are hashed at maya start. Means collecting and validating all procedure definitions and preparing them for execution.
Dropping a script in the viewport, stuff in userSetup.mel and of course the source command itself causes the execution of the code.
I guess we are getting far beyond woadiestyols needs by now ^^
But good to know such stuff anyway it is.
NaughtyNathan
09-26-2008, 11:10 AM
The main proc that has the same name of the script file always has to be global!
yes, of course it does! :) I never thought stating something so obvious was worth it.. which is why I didn't.. :)
the local proc I was talking about in his script wasn't the script name main proc, but the WORK proc. This is the proc that does all the WORK of the function.. not the main calling proc. :) If you'd looked at his code you would have realised that Eric! ;) but I'll let you off.. this time! ;) :D
:nathaN
CGTalk Moderation
09-26-2008, 11: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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.