PDA

View Full Version : quick question(mel for loop)


matrox89
01-28-2013, 03:02 AM
Hello everyone, I m trying to figure out how to use the node preset commend(Im a beginner). Just stuck and I need some help, so:

string $selection[] = `ls -sl`;
for ($each in $sel)
{
nodePreset -load $sel "selection";
}


My goal is to load the preset I saved to multiple objects. Since, I believe I can only load the preset 1 at a time I intended to use a for loop. yeah so its not working... what can I do?

thx.

Panupat
01-28-2013, 03:28 AM
Well, $selection in your first line disappears and becomes a $sel on your second line. That won't work.

And I suggest you try to understand the for loop from this example first.

$lotsOfItems = {"a", "b", "c", "d"};

for ( $individualItem in $lotsOfItems) {
print $individualItem;
print "\n";
}

matrox89
01-28-2013, 03:37 AM
thx, but I forget to change that while I was copying to here, my bad though that's not the problem...

matrox89
01-28-2013, 03:39 AM
this is the whole script, in chase you wanna look up, just saying I just started scripting... :)

global proc bh_CopyPreset()
{
//------Delete the existing UI if any.
if (`window -exists bh_CopyPresetUI`)
deleteUI bh_CopyPresetUI;
//------Window UI
window -wh 300 400 -t "Copy Preset Tool" -mxb 0 bh_CopyPresetUI;
//------Layout
columnLayout -adj 1 mainCol;
//------checkbox
checkBox -l "Check it to transfer Transform Node Attributes" myCheckBox;
//------Separator
separator -h 10
-horizontal 1
-style "none"
topSeparator;
//------Button
text -l "Select the source Object";
button -l "Source Object" -command "bh_sourceObject" allButton;

//------Separator
separator -h 20
-horizontal 1
-style "none"
mainSeparator;
//------TextScrollList
text -l "Current Selected Items";
textScrollList -numberOfRows 1 -h 100 -allowMultiSelection true selectedObjects;
//------Button
button -l "Reload Selected Objects" -command "bh_selectedObjects" reloadButton;
//------Separator
separator -h 30
-horizontal 1
-style "none"
copySeparator;
//------Button
text -l "Select the target Object";
button -l "Copy Preset" -command "bh_targetObject" backButton;
//------Separator
separator -h 30
-horizontal 1
-style "none"
bottomSeparator;
//------delete preset button
button -l "Delete Stored Preset" -command deleteObjectPreset ;
//------Separator
separator -h 10
-horizontal 1
-style "none"
quitSeparator;
//------create a couple of button
button -l "Quit" -command bh_commandquit ;
//------show window
showWindow bh_CopyPresetUI;
}
//------------------------save the particleshape as a preset
global proc bh_sourceObject ()
{
int $value = `checkBox -q -v myCheckBox`;
if ( $value==0)
{
string $sourceObject[]= `ls -sl`;
string $sourceObjectShape[] = `pickWalk -d down`;
nodePreset -save $sourceObjectShape "bh_sourceObject_shape";
print "Preset Created bro!\n";
}
else if ( $value==1)
{
string $sourceObject[]= `ls -sl`;
nodePreset -save $sourceObject "bh_sourceObject_transform";
print "Preset Created bro!\n";
}
}
//------------------------loads up the preset to the selected patricle
global proc bh_targetObject ()
{
int $value = `checkBox -q -v myCheckBox`;
if ( $value==0)
{
string $targetObject[]= `textScrollList -q -si selectedObjects`;
string $targetObjectShape[] = `pickWalk -d down`;
for ($each in $targetObject)
{
nodePreset -load $targetObjectShape "bh_sourceObject_shape";
}
print "Done Bro!\n";
}
else if ( $value==1)
{
string $targetObject[]= `textScrollList -q -si selectedObjects`;
for ($each in $targetObject)
{
nodePreset -load $targetObject "bh_sourceObject_transform";
}
print "Done Bro!\n";
}
}
//--------------------------create a command to delete the window
global proc bh_commandquit()
{
deleteUI bh_CopyPresetUI;
print "Thank you come again!!\n";
}
//--------------------------editscrolllist
global proc bh_selectedObjects()
{
textScrollList -e -removeAll selectedObjects;
int $value = `checkBox -q -v myCheckBox`;
if ( $value==0 )
{
string $scrollList[] = `ls -sl`;
string $scrollListShape[] = `pickWalk -d down`;
for ( $each in $scrollListShape)
{
textScrollList -e -append $each selectedObjects;
}
print "Shape Nodes are added to the List!\n";
}

else if ( $value==1)
{
string $scrolllist[] = `ls -sl`;
for ( $each in $scrolllist)
{
textScrollList -e -append $each selectedObjects;
}
print "Transform Nodes are added to the List!\n";
}

}
//--------------------------editscrolllist
global proc deleteObjectPreset()
{
int $value = `checkBox -q -v myCheckBox`;
if ( $value==1 )
{

nodePreset -load "meshPreset_bh_CopynParticles_transform";
print "Preset has been terminated Bro!\n";
}
else if ( $value==0)
{

nodePreset -delete "transformPreset_bh_CopynParticles_shape";
print "Preset has been terminated Bro!\n";
}
}

djx
01-28-2013, 04:43 AM
so its not working... what can I do?
Don't take this the wrong way, but your question is a bit vague.

In your first post you have incorrectly copy-and-pasted some code, which makes it difficult to guess what you were trying to do or where the real problem was. And in your second post the formatting makes it really difficult for anyone to read (admittedly this is not entirely your fault since these forums are pretty bad for code posting, but you should at least try to use the code tags, by selecting your code and clicking the # symbol in the message window menu bar).
But most importantly, it is always advisable to include an error message, or a description of what happens that is "not working".

People here are pretty helpful, so someone might spend the time figuring it out anyway, but the easier you make it for them, the quicker you'll get an answer.

David

djx
01-28-2013, 04:50 AM
Ok, so I dont think this would work...
string $targetObjectShape[] = `pickWalk -d down`;
for ($each in $targetObject)
{
nodePreset -load $targetObjectShape "bh_sourceObject_shape";
}
Its realy still what Panupat suggested. You are looping through the $targetObject array, but within the loop you are trying to do something with $targetObjectShape array. My guess as to what you wanted is this:
string $targetObjectShape[] = `pickWalk -d down`;
for ($each in $targetObjectShape)
{
nodePreset -load $each "bh_sourceObject_shape";
}

Since your other loops seem to indicate you do understand how they work, maybe this was just a typo?

David

matrox89
01-28-2013, 05:04 AM
Yeah you are right I m sorry about that mess, I wasnt aware of the `#` option.

So the problem is with the loop, because when you select olny one object and apply the nodePreset command it works. But when you put it inside of a loop it doesnt, and this is the error line:

// Warning: line 4: Found two selection items when one was expected. Choosing one. //

Thus you probably cant use each in there:

string $targetObjectShape[] = `pickWalk -d down`;
for ($each in $targetObjectShape)
{
nodePreset -load $each "bh_sourceObject_shape";
}


Only because each is the variable of the 'for loop' command, right? I tried it and it didnt work... I ve spend a lot of time looking at the `for loop`examples but notting came up...

Btw thank you for your concern.

matrox89
01-30-2013, 04:25 AM
I figured it out, thanks for your helps everyone.

Panupat
01-30-2013, 07:28 AM
Glad you have to sorted out.

Hope I'm not stating the obvious, but allow me to use your code as an example. Modified a bit for demonstration purposes.

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

for ($each in $targetObjectShape)
{
nodePreset -load $each "bh_sourceObject_shape";
}


Let's say you have 4 objects when you run this script. $targetObjectShape will become
[ "sphere", "box", "cone", "tube" ]

Next, in your for-loop, since the array has 4 items the command in the middle will happen 4 times. Each time, $each will become each items in the array.

1 - nodePreset -load sphere "bh_sourceObject_shape";
2 - nodePreset -load box "bh_sourceObject_shape";
3 - nodePreset -load cone "bh_sourceObject_shape";
4 - nodePreset -load tube "bh_sourceObject_shape";

However if you use $targetObjectShape instead of $each, it will run the exact same command 4 times. Destroying the whole point of a for-loop

nodePreset -load ["sphere", "box", "cone", "tube"] "bh_sourceObject_shape"; (x4)

Which obviously will not work because "nodePreset -load" only accepts string and not array according to the documentation.

CGTalk Moderation
01-30-2013, 07:28 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.