PDA

View Full Version : Setting variables based on user input


trancor
11-15-2007, 12:22 AM
Hi, not sure that this can be done in mel, but it needs to be in mel. I would like a string array to be assigned based on what a user puts into a text box. What I mean is, if someone put the text "selectedVar" into a text box, I want a new string array to be made called $selectedVar to be used by a global proc.

Basically what I'm doing is I'm setting up groups of selections to be reselected with a click of a button. So if someone selects the verts that make up an eye, I would like the user to be able to designate the selection as $eye and to maintain all of the verts in the eye selection under the array $eye[].

I've come up with a round about way of doing this, by making a int array to maintain how many verts are in a selection and to add all of the verts into one string array. So the script would read the next element in the int array, lets say "20", and it would read out the next recurring 20 elements in the string array and then select those elements.

Basically the main reason I want multiple arrays is just so things run faster and I can make a bin of variables in a window so the user can select one selection group and have it select. Rather than running through all of the elements to lead up to the selected group the user wants reselected.

ashishdantu
11-15-2007, 02:31 AM
hi trancor,

have you considered using selection sets ? because, untill your animators r not completely aware of how this tool works, they might end messing with names.. in the sense, they migth repeat the name of the variable, which might overwrite your earlier selection variables..

using sets, you atleast have a default check to prevent it. and then once u make selection sets, they can use maya's UI to navigate to them .

but anyways something like this would work ?


string $sel[]=`ls -sl`;

string $text;
string $result = `promptDialog
-title "Enter name"
-message "Enter Name:"
-button "OK" -button "Cancel"
-defaultButton "OK" -cancelButton "Cancel"
-dismissString "Cancel"`;

if ($result == "OK") {
$text = `promptDialog -query -text`;
}

string $newArrayDef1 = ("$"+$text);
string $newStrArray = ("string "+$newArrayDef1+"[]");
eval $newStrArray;
eval ("$"+$text +"= $sel");

print ("contents of variable $"+$text + " are :\n");
eval ("print "+$newArrayDef1);


a bit nasty way of doing this, but just wanted to give it a shot ! :scream:

trancor
11-15-2007, 04:55 PM
Yeah, I realize sets would be easier, but I want the user to be able to work with selections on the fly on any object, lets say you duplicate an object that you were making blendshapes with. I would like for the user to hit a button in a window I have made to select the same verts on that duplicated object as well. Also, I feel sets clutter up the scene, I always try to work as clean as possible with modeling and I feel maintaining arrays that clear themself when you close the program is beneficial compared to adding nodes to the scene.

But, again, I forget about that little command eval, probably one of the more important commands, and I never remember to use it. Thanks ashishdantu, as always, a fast response and helpful.

I'm going to make a list for the user to select from for the selections. So I'll have my script check if there is an existing variable and add a number to the name if so. That should be able to solve and duplicate naming conventions set by the user.

ashishdantu
11-16-2007, 01:51 AM
hi trancor,

i understand the issue with sets.. uumm.. so yes may be u can try this eval thingy..

and coming to the point of having selecting same set of components on the duplicate obj .. can be addressed too..

this code is some thing i came up with, when i had to store selections using buttons .. imagine, you have selected something like :

// Result:pCube1.vtx[2:4] pCube1.vtx[6] pSphere1.vtx[205:208] pSphere1.vtx[224:229] pSphere1.vtx[243:250] pSphere1.vtx[262:271] pSphere1.vtx[281:292] pSphere1.vtx[300:314] pSphere1.vtx[319:379] pSphere1.vtx[381]//

so running this code will give you an array like :

pCube1.vtx[2:4] pCube1.vtx[6]
pSphere1.vtx[205:208] pSphere1.vtx[224:229] pSphere1.vtx[243:250] pSphere1.vtx[262:271] pSphere1.vtx[281:292] pSphere1.vtx[300:314] pSphere1.vtx[319:379] pSphere1.vtx[381]

so basically the $collecteditemsArrayBig array will have per object list of selected items.

string $selobjs[]=`ls -sl`;
int $count=0;
string $components[];
string $multiObjs[];

int $colectcount=0;
string $collecteditemsArray="";
string $collecteditemsArrayBig[]={};
string $labelmulti="";

for ($e in $selobjs)
{
tokenize $e "." $components;
$multiObjs[$count]=$components[0];
$count++;
}
$multiObjs = stringArrayRemoveDuplicates ($multiObjs);

for ($f in $multiObjs)
{
for ($p in $selobjs)
{
if (`startsWith $p $f`)
{
$collecteditemsArray += (" "+$p);
}
}
$collecteditemsArrayBig[$colectcount] = $collecteditemsArray;
$collecteditemsArray ="";
$colectcount++;
}

print $collecteditemsArrayBig;

so in your case, you can say get $collecteditemsArrayBig[0] which shud give u :
pCube1.vtx[2:4] pCube1.vtx[6] so to select the same verts onthe duplicate , exchange the pCube1 with the other obj name :)

might not be an efficient method so far, but concept proof wise , will work !

trancor
11-16-2007, 04:38 AM
Oops, I guess I should have said that I got it. I have basically just completely forgot about eval, everything else fell into place once I got that in my code. I now have a selection box with a list, and different verts based on the name of the selection in the list. Like if the list item is headVerts; it is linked up to the string array $headVerts in my code. And I just used tokenize to select the same vert numbers but on a different object. I have it run through for edges, faces, verts, and translate nodes to decide what it does as a selection.

I'm now thinking about writing files with selections, but at that point I should just use sets, but I feel like having extra files doesn't clutter something or become a potential problem in maya. But I really don't see a great need for writing files yet. That I know how to do, just need to decide if its useful.

I thank you for the continued work you put in. I'll take a closer look at the code to see if there is something you did that would speed up my existing code. Thanks again.


edit - Heheh, I looked at the code and just saw that you used tokenize as well. Tokenize works so well, heh.

CGTalk Moderation
11-16-2007, 04:38 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.