Tips on speeding up my procedures?

Ok, so I got my procedures running and executing well now, only they take so long that the progress window I made won't even update until it is done (even with a "refresh" command thrown into my loop), and everything freezes up while it is going through all these commands. The procedure is to import text value skin weights, and this is it:

proc applyWeights(int $vertNumb, string $vertices[], string $INF[], string $skinClus, string $NoReturns[])
{
int $k;
string $sValue[];
float $fValue[];

string $tempString = $NoReturns[$vertNumb];
tokenize $tempString $sValue;
$k = 0;
while ($k < size $INF)
{
$fValue[$k] = $sValue[$k];
float $comparison[] = skinPercent -q -value $skinClus $vertices[$vertNumb];
if ($fValue[$k] != $comparison[$k])
{
skinPercent -nrm 0 -tv $INF[$k] $fValue[$k] $skinClus $vertices[$vertNumb];
}
$k++;
}
}

proc applyCheckWeights(int $vertNumb, string $vertices[], string $INF[], string $skinClus, string $NoReturns[])
{
float $fCheck[] = skinPercent -q -value $skinClus $vertices[$vertNumb];
string $sCheck[];
int $counter;
string $oneString;
while ($counter < size $INF)
{
$sCheck[$counter] = $fCheck[$counter];
$oneString = $oneString + " " + $sCheck[$counter];
$counter++;
}
if ($NoReturns[$vertNumb] != $oneString)
{
int $k;
string $sValue[];
float $fValue[];
string $tempString = $NoReturns[$vertNumb];
tokenize $tempString $sValue;
$k = 0;
while ($k < size $INF)
{
$fValue[$k] = $sValue[$k];
float $comparison[] = skinPercent -q -value $skinClus $vertices[$vertNumb];
if ($fValue[$k] != $comparison[$k])
{
skinPercent -nrm 0 -tv $INF[$k] $fValue[$k] $skinClus $vertices[$vertNumb];
}
$k++;
}
}
}

global proc importSkinWeights ()
{
// Declare Vars:

//Selection Vars:
string $selection[] = ls -sl;
if ($selection [0] == "")
error ("No object has been selected.");
if (size $selection > 1)
error ("More than one object has been selected.");
string $selShape[] = ls -dagObjects -sl -hd 1 -type mesh;
if ($selShape[0] == "")
error ("A shape could not be found for the geometry.");
if (size $selShape > 1)
error ("More than one shape was found for the geometry.");
string $objHistory[] = listHistory $selShape[0];
string $skinCluster[] = ls -typ skinCluster $objHistory;
if (size $skinCluster == 0)
error ("A skinCluster could not be found for the selected geometry.");

// File Vars:
string $fileName = ( internalVar -userWorkspaceDir + workspace -q -act + "/text/" + "*.txt");
string $importFile = fileDialog -dm $fileName;
int $fileRead = fopen $importFile "r";

// Skin Influences:
string $INFs[] = listConnections ($skinCluster[0] + ".matrix");
string $compactVerts[] = ls ($selShape[0] + ".vtx
[*]")
;
string $verts[] = filterExpand -sm 31 $compactVerts;

// Commands:

// Read input file, and store in $read[]:
int $i = 0;
string $read[];
while ($i < `size $verts`)
{
$read[$i] = `fgetline $fileRead`;
$i++;
} // # of lines = 5393

// Remove "n" characters from $read[] strings and returns $noReturns Array of strings:
int $count = 0;
string $noReturns[];
string $tempArray[];

// Create a work in progress window:
if (window -exists inProgress)
deleteUI inProgress;
window
-title "Work in progress..."
-widthHeight 200 100
inProgress;
columnLayout -adjustableColumn true;
text -label "Sit tight while I compute n~~snortsnort~*~";
showWindow inProgress;

// Remove all "n" characters:
while ($count < `size $verts`)
{
string $input = $read[$count];
tokenize $input "n" $tempArray;
$noReturns[$count] = $tempArray[0];
$count++;
clear $tempArray;
}

fclose $fileRead;

// If normalization was on, turn it off:
int $normalWeights = getAttr ($skinCluster[0] + ".normalizeWeights");
if ($normalWeights == 1)
setAttr ($skinCluster[0] + ".normalizeWeights") false;

// Assign weights:
int $j = 0;
while ($j < `size $verts`)
{
applyWeights($j, $verts, $INFs, $skinCluster[0], $noReturns);
$j++;
refresh;
}
// Reset weight normalization:
if( $normalWeights == 1 )
setAttr( $skinCluster[0] + ".normalizeWeights" ) true;
deleteUI inProgress;

}

As you can see, I have two procedures to do the applying of skin weight values. The first one simply checks all the skin weight vlaues for a certain vertex, influence by influece, to make sure they aren't the same as the weight of the joint/influence that is being overwriten, one at a time.

The second procedure, applyCheckWeights, I tried to make it also check for all the skin weights of a vertex and if they are the same as those being overwritten, skip that vertex all together.

Anyways, is there anyway to make it so the screen doesn't freeze up? Also any tips on making it evaluate any faster would be appreciated, as it currently is taking a good 5-15 minutes to apply the weights for a character with 5393 vertexes, and 93 joints/influence objects.

Thanks for any help!

Without doing too much analysis of the code the first slow thing I notice is recalculations every time you loop that are a complete waste of time, EG in applyCheckWeights you have

while ($counter < `size $INF`)

and
while ($k < `size $INF`)

You're recalculating the size every loop, just do it once before the loop and save the pain.....

Also noticed that you never actually create a progress bar to update.. you've made a window called "inProgress" but there's no progress bar there. Have a look at the Mel docs for the commands "progressBar" and "ProgressWindow", that''ll stop it appearing to freeze up on you.

Oh hey, thanks! Yeah, good point, I hadn't thought about it having to compute the size for each pass of the loop, I figured I was saving myself from creating another variable, but durh. Good point, i'll put that in there.

And yeah, this version of the script that I posted doesn't have the progressWindow command... This was from after I had made it and got frustrated that it kept freezing up on me, and so I tried just putting in this generic window. But before, I had something like this:

{
int $amount;
progressWindow -title "Work in progress..." -progress $amount -status "Completed: 0%" -isInterruptable true;
int $j;
float $oneUnit = (1 / $vertSize);
...

while ($j < $vertSize)
{
do commands to assign skinWeights...
if (`progressWindow -query isCancelled`) break;
$j++;
$amount = $j * $oneUnit;
progressWindow -edit -progress $amount -status ("Completed: " + $amount + "%");
refresh;
}

progressWindow -endProgress;
}

And like I said, it would start out fine, the progress bar would advance to like anywhere from 2-6 percent at the most, then freeze up, seems like it was doing to much processing or something? I'm executing all of this from the Script Editor though, would it go faster if I just save it to the "Scripts" folder and execute it that way? I still haven't gotten around to trying that out yet.

I've just browsed the code too, but I noticed in your applyWeights procedure that you execute a skinPercent command for every single influence transform. Also, the $comparison array is (re)built for every single influence, even though it gives you the values of all the influences.
This just might be what's causing your error.

Let's say you have a character with 5000 verts, and 20 influences (you typically have more). Your code will then execute the skinPercent command 5000 * 20 + 5000 * 20 = 200,000 times (!). That's 190,000 times more than necessary.

The nice thing about the skinPercent command is that the -tv flag is a multiple use flag, so you can have as many -tv flags as you want in a single skinPercent command (or rather as many times as you have different influences you want to set for one vertex or cv).

So, to solve the problem, first of all get the $comparison array before the while loop (why aren't you using a for loop btw? - just curious), and then let the while loop build a string which appends ( "-tv " + $INF[$k] + " " + $fValue[$k]) at every loop round, and then when the loop is done (and the string is built), put an eval command which executes the skinPercent command for all the influences of that vertex. Like this (i'm using a for loop instead - easier for me to read - but you can use the while loop instead):

proc applyWeights (int $vertNumb, string $vertices[], string $INF[], string $skinClus, string $NoReturns[])
{
string $sValue[];
float $fValue[];
string $tempString = $NoReturns[$vertNumb];
tokenize $tempString $sValue;
float $comparison[] = skinPercent -q -value $skinClus $vertices[$vertNumb];
string $skinPercEval = "skinPercent -nrm 0";
int $numInf = size $INF;
for ($k = 0; $k < $numInf; $k++)
{
$fValue[$k] = $sValue[$k];
if ($fValue[$k] != $comparison[$k])
{
$skinPercEval += (" -tv " + $INF[$k] + " " + $fValue[$k]);
}
}
$skinPercEval += (" " + $skinClus + " " + $vertices[$vertNumb] + ";");
eval $skinPercEval;
}Note: There's no way for me to test this, so it might not work.

/edit: reading on, I see the same thing happening in the applyCheckWeights procedure, but the remedy is the same.

/edit2: wrong variable name for eval command corrected.

Oh hey, wow thatnks! Yeah, I too was noticing it was looping an insane number of times and I wasn't really sure how to go about cutting down on that number.

And I'm just starting out coding with this stuff, and had some problems getting the for loops to work, so I just started using while loops, I should probably get back to working with the for ones though I suppose.

And thanks for the tip on the -tv flag, that will help a ton.

The reason I was redoing the comparison every loops was I thought I should compare the new vertex value to the one it was overwriting, and if they were the same, have it skip writing those values, so it wouldn't have to do quite as many computations. I'm going to have to look more into how you used your's. But yeah, thanks a ton!

So I noticed, and it's a good idea. The procedure I suggested does this too. The only difference is that mine does it outside the loop, i.e. once for every vertex, while yours does it inside the loop, i.e. once for every transform for every vertex. Maybe I should clarify why you can do it outside of the loop:

The thing is, when you execute the "skinPercent -q -value" command, you get an array of floats back. Each of these floats is the weight of an influence transform affecting the skin cluster, as I'm sure you know. So, each time you execute the command, you get the weight values of all the influence transforms for this particular vertex. Since your loop iterates over the transforms it's unnecessary to get the array inside the loop - you get them all the first time you execute the "skinPercent -q -value" command. You just have to make sure you compare the right $comparison element with its corresponding element in the $fValue array.

Which leads me to something I missed before:

When you get the $INFs array, you run the risk of ordering your influence transforms differently than the $comparison array or the $noReturns input. Since this doesn't seem to be a problem, I guess you're alright, but a safer way to get your $INFs array is instead of using

string $INFs[] = listConnections ($skinCluster[0] + ".matrix"); ,

you can once again use the skinPercent command:

string $INFs[] = skinPercent -q -t $skinCluster[0] ($selShape[0] + ".vtx[0]");

This will make sure that your influence array has the same ordering as the weight value arrays. (But it may be that the listConnections method gives the same result, and is just as safe.)

Good luck optimizing.

By the way, I assume you know there's quite a few skin weight import/export scripts out there (I've made one of them - aeSkinWeightsTransfer), right?

Hey, thanks!

Yeah, I realized there are several scripts for importing/exporting skin weights. I'm trying to get a junior level job at Imageworks, and they want to see some MEL programming, so I'm using this as my first real experience with it... Making a GUI for holding/unholding all influence objects, importing/exporting skin weights via text files, and mirroring skin weights via text values as well.

I just finished all of these procedures (and used some of your suggestions, thanks!), and am just tying the procedures into the GUI, which is giving me a bit of difficulty, but I'm almost doneee and it feels good!

Anyways, I'm going to make a new post about the problem I was having tying somethings together with the GUI, any help in that one is appreciated as well...

I'm not very good at file management in mel, so I won't reply to that other thread, cause then maybe someone who actually can help you sees that the thread has been replied to and concludes that the problem was solved and doesn't bother to look

I'd once again like to suggest that you take a look at other scripts that involve interactive file management. Different scripters use different methods, and the more scripts you check out, the bigger the chance is that you find a method that suits you.

But, actually, the main reason for my reply was just to say:

GOOD LUCK WITH THE JOB!!!

Ah, thanks! Yeah, I've been looking at other scripts and seeing how those work and all of that. Here's a procedure I found for applying weights as well:

proc assignNewWeights(string $influences[ ], float $weights[ ], string $skinCluster, string $component)
{
int $k;
for($k = 0; $k < size($influences); $k++)
if($weights[$k] > 0)
{
string $newInfluence = mirrorInfluences($influences[$k]);
skinPercent -nrm 0 -tv $newInfluence $weights[$k] $skinCluster $component;
}
}

But I kind of like your's, how it cycles through and applies all of them. I don't understand how one would be any faster than the other, do you think each would be the same? Your's has to cycle through to write out all of the weights and influences, and these have to cycle through applying the weights... Is applying them one by one slower or something?

But yeah, thanks again!

Ok, here is my practically completed script, although I am still having problems with it freezing up when it goes to apply the weights, and the work in progress bar doesn't update nor close either. Here it is:

// PURPOSE: Procedure applyWeights take a given vertex as input, and applies skin weights to it based
// on an array of string values for the sking weights, which is then broken up into individual value
// weight strings, and then converted to floating point numbers before being applied to the current
// skin cluster.
// Call Procedure like "applyWeights(4817, $verts, $INFs, $skinCluster[0], $lineOfValues);"

proc applyWeights(int $vertNumb, string $verts[], string $INFs[], string $skinCluster, string $NoReturns[])
{
string $sValue[];
float $fValue[];
string $tempString = $NoReturns[$vertNumb];
tokenize $tempString $sValue;
float $comparisson[] = skinPercent -q -value $skinCluster $verts[$vertNumb];
int $j;
string $skinPercEval = "skinPercent -nrm 0";
int $INFsize = size $INFs;

for ($j = 0; $j < $INFsize; $j++)
{
$fValue[$j] = $sValue[$j];
if ($fValue[$j] != $comparisson[$j])
{
$skinPercEval += (" -tv " + $INFs[$j] + " " + $fValue[$j]);
}
}
$skinPercEval += (" " +$skinCluster + " " + $verts[$vertNumb] + ";");
eval $skinPercEval;

}

// PURPOSE: Procedure importSkinWeights imports skin weights writen to a text file by the
// exportSkinWeights procedure defined above. This allows for more accurate skin weighting, than
// Maya's built in approach, which is based on bitmap images.

global proc importSkinWeights()
{
// Declare Vars:

//Selection Vars:
string $selection[] = ls -sl;
if ($selection [0] == "")
error ("No object has been selected.");
if (size $selection > 1)
error ("More than one object has been selected.");
string $selShape[] = ls -dagObjects -sl -hd 1 -type mesh;
if ($selShape[0] == "")
error ("A shape could not be found for the geometry.");
if (size $selShape > 1)
error ("More than one shape was found for the geometry.");
string $objHistory[] = listHistory $selShape[0];
string $skinCluster[] = ls -typ skinCluster $objHistory;
if (size $skinCluster == 0)
error ("A skinCluster could not be found for the selected geometry.");

// File Vars:
string $fileName = ( internalVar -userWorkspaceDir + workspace -q -act + "/text/" + "*.txt");
string $importFile = fileDialog -dm $fileName;
int $fileRead = fopen $importFile "r";

// Skin Influences:
string $INFs[] = listConnections ($skinCluster[0] + ".matrix");
string $compactVerts[] = ls ($selShape[0] + ".vtx
[*]")
;
string $verts[] = filterExpand -sm 31 $compactVerts;
int $vertSize = size $verts;
int $INFsize = size $INFs;

// Commands:

// Read input file, and store in $read[]:
int $i = 0;
string $read[];
while ($i < $vertSize)
{
$read[$i] = `fgetline $fileRead`;
$i++;
}

// Remove "n" characters from $read[] strings and returns $noReturns Array of strings:
int $count = 0;
string $noReturns[];
string $tempArray[];

// Remove all "n" characters:
while ($count < $vertSize)
{
string $input = $read[$count];
tokenize $input "n" $tempArray;
$noReturns[$count] = $tempArray[0];
$count++;
clear $tempArray;
}

fclose $fileRead;

// Create a work in progress window:
int $amount = 0;
float $oneUnit = 100 / $vertSize;
progressWindow
-title "Work in progress..."
-progress $amount
-status "Completed: 0%"
-isInterruptable true;

// If normalization was on, turn it off:
int $normalWeights = getAttr ($skinCluster[0] + ".normalizeWeights");
if ($normalWeights == 1)
setAttr ($skinCluster[0] + ".normalizeWeights") false;

// Assign weights:
int $j = 0;
while ($j < $vertSize)
{
if (`progressWindow -query -isCancelled`) break;

applyWeights($j, $verts, $INFs, $skinCluster[0], $noReturns);
$j++;

//  Update Progress Window:
$amount = $j * $oneUnit;
progressWindow -edit -progress $amount -status ("Completed: " +$amount + "%");
}

// Reset weight normalization:
if( $normalWeights == 1 )
setAttr( $skinCluster[0] + ".normalizeWeights" ) true;
progressWindow -endProgress;

}

Any ideas on why it still isn't updating as it goes along and why it's freezing?

But I kind of like your's, how it cycles through and applies all of them. I don't understand how one would be any faster than the other.

Because the order at wich you execute things might affect how the guess next move thing of cache works, as a example my friends did a test where yhey had a a big matrix (okay so it was c) mutltiplication, and they did two versions of the prog one wich was logarithmicaly better. And one bruteforce that in theory was 10 times slower, but was optimized cacheable.

Now the burteforce one did win the speed test! Why, simply becasue sthe processors what comes next guess had easy a time prdicting that the next block to querry is gone be thet part there (a bruteforcew looping arrays the other way around eg the second dimension first was horribly slow).

Theres more to computing than merea lagos and loops, execution order matters too.

Ok, I didn't understand anything of what Joojaa said , but I did some speed tests when I wrote aeSkinWeightsTransfer, and the string building method is a lot faster.
I think the reason is that the skinPercent command is quite slow when you apply weights, at lot slower than adding a string to another string at least. So yes, executing 100 skinPercent -tv commands is slower than first executing 100 string + string operations and then executing 1 skinPercent -tv command, if I understand things correctly.

Note that the string method never actually writes out anything, if it did, it would certainly be slower than the "apply one by one" method. It just stores a string value in the memory and builds on it until it's complete, then it converts it into a mel command (using eval) which is then executed.

Sorry if I can't help with the lockup problem.

Ahh, thanks man. That does make sense. I'm just trying to iron out the kinks in the order of the influences now. Lol, does it ever seem like these things never end???