View Full Version : Function Question
ajk48n 07-12-2003, 07:59 PM Say I have a function that makes a button which calls another function:
proc callEdit()
{
$name = `button`;
$callFunc = `button -c ("editButton($name)")`;
}
proc editButton(string $name)
{
print $name;
)
My question is, when I call editButton with $name as a parameter, how do I have the command use the actual string that was originally in $name.
Basically, if during callEdit(), $name = UserWindow|ColumnLayout|Button5, how do I make the commad be editButton("UserWindow|ColumnLayout|Button5"), not editButton($name).
|
|
mark_wilkins
07-13-2003, 02:00 PM
Button action procedures (which are called as commands in the -c flag for a button) need to be global procedures and they need to only use global variables.
Here's corrected code... you MAY need the global string $buttonName within the -c flag's argument, though it may not be necessary. I'd have to try it, as I don't remember, but in any case it won't hurt anything.
proc callEdit()
{
global string $buttonName = `button`;
$callFunc = `button -c ("editButton($buttonName)")`;
}
global proc editButton(string $name)
{
print $name;
)
ajk48n
07-13-2003, 07:59 PM
Thanks for the response. I think my problem still exists, though. Any time that I click on the $callFunc button, it will run editButton with whatever value is currently in the $buttonName variable. I need whatever is in that variable to be written as a string at the time of creation.
I need the button to always call editButton with whatever was in $buttonName at the time of creation, and always call it with that parameter.
mark_wilkins
07-13-2003, 09:49 PM
Oh, ok, that's easy.
global proc editButton(string $name)
{
print $name;
}
proc callEdit()
{
string $buttonName = `button`;
string $editButtonCreateCmd = ( "button -c \"editButton\(\\\"" + $buttonName + "\\\"\)\"" );
print $editButtonCreateCmd;
$editButtonName = `eval ($editButtonCreateCmd)`;
}
window;
columnLayout;
callEdit;
showWindow;
All those backslashes (\) are used for the following reason:
Suppose you want to build a string that contains
"
you'd normally think "Oh, I'll just type this"
"""
Unfortunately that doesnt work because Maya thinks the second quote ends the string and the third one is an error. A backslash says to treat the quote as a literal character:
"\""
Now, suppose you want a string that contains
\"
You'll have to add two extra backslashes, one to make Maya treat \ as a literal string and one to treat " that way too:
"\\\""
So if you try
print "\\\""
you'll get
\"
as the output.
Knowing that, now you can read the above script, which builds (at runtime) the command to create the edit button and sticks it in $editButtonCreateCmd. Then, the eval command is used to actually execute that command you've built in the string.
Incidentally, it usually takes a lot of tries to know how many backslashes to put in, and where. The trick is that you generally need to backslash (or, in C-language terminology, you need to "escape") parentheses, backslashes you actually want in the string, and double quotes.
-- Mark
ajk48n
07-13-2003, 10:12 PM
Thanks again. I knew about the escape characters, but I didn't know the eval command would get what I needed.
mark_wilkins
07-13-2003, 10:20 PM
There was another guy who posted here saying "use eval" but then deleted his post. We should have listened to him!! :D
-- Mark
CGTalk Moderation
01-15-2006, 03:00 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.