Skip to content

Instantly share code, notes, and snippets.

@hmasato
Created September 10, 2013 05:47
Show Gist options
  • Select an option

  • Save hmasato/6505445 to your computer and use it in GitHub Desktop.

Select an option

Save hmasato/6505445 to your computer and use it in GitHub Desktop.
[MAYA] _optimizeMatTex (optimize tons of materials / textures)
//
// _optimizeMatTex
//
//-----------------------------------
proc string _getProfileAttr(string $plug)
{
string $typ=`getAttr -type $plug`;
string $cnt[]=`listConnections -s 1 -d 0 -p 1 -c 0 $plug`;
if(size($cnt)){
string $buf[]={};
tokenize $cnt[0] "." $buf;
return($typ+"| connect "+objectType($buf[0])+"."+$buf[1]);
}
string $sv=$typ+"|";
if(`gmatch $typ "*[2-9]"` || $typ=="matrix"){
for($f in `getAttr $plug`) $sv+=" "+$f;
}else if($typ=="TdataCompound"){
}else{
$sv+=" "+`getAttr $plug`;
}
return($sv);
}
//-----------------------------------
proc string _getProfileNode(int $level, string $node)
{
string $res="";
string $sp="";
for($i=0; $i<$level; $i++) $sp+="\t";
$res+=($sp+"nodetype: "+objectType($node)+"\n");
string $attrs[]=`listAttr -hasData -w -c $node`;
for($attr in $attrs){
if(!`attributeQuery -n $node -exists $attr`) continue;
string $v=_getProfileAttr($node+"."+$attr);
if($level<0) $res+=($sp+$attr+": "+$v+"\n");
else $res+=($sp+"\t+---"+$attr+": "+$v+"\n");
}
clear $attrs;
return($res);
}
//-----------------------------------
proc string _getProfileHierNode(int $level, string $node)
{
string $res=_getProfileNode($level, $node);
string $srcs[]=`listConnections -s 1 -d 0 -p 0 -c 0 $node`;
$srcs=stringArrayRemoveDuplicates($srcs);
for($src in $srcs){
if($level<0) $res+=_getProfileHierNode($level, $src);
else $res+=_getProfileHierNode($level+1, $src);
}
clear $srcs;
return($res);
}
//-----------------------------------
proc _deleteHierNode(string $node, string $parent)
{
string $srcs[]=`listConnections -s 1 -d 0 -p 0 -c 0 $node`;
$srcs=stringArrayRemoveDuplicates($srcs);
for($src in $srcs){
_deleteHierNode($src, $node);
}
clear $srcs;
if($parent!=""){
string $dsts[]=`listConnections -s 0 -d 1 -p 0 -c 0 $parent`;
$dsts=stringArrayRemoveDuplicates($dsts);
for($dst in $dsts){
if($node!=$dst) return;
}
clear $dsts;
}
delete $node;
}
//-----------------------------------
// progress bar
proc _beginPBar(string $title) { progressWindow -t $title -pr 0 -ii true; }
proc _endPBar() { progressWindow -endProgress; }
proc int _PBar(int $par)
{
progressWindow -e -pr $par -status ("now ... "+$par+"% done.");
int $cancel=`progressWindow -q -isCancelled`;
if($cancel) _endPBar();
return($cancel);
}
//-----------------------------------
proc _optimizeMatTex(int $mode)
{
// init
string $nodes[]={};
switch($mode){
case 1: $nodes=`ls -tex`; break;
case 2: $nodes=`ls -mat`; break;
default: $nodes=`ls -sl -mat -tex`; break;
}
// get stats by node
string $nodeInfos[]={};
_beginPBar("check process (1/2)");
for($i=0; $i<size($nodes); $i++){
int $pr=(100.0*$i/size($nodes));
if(_PBar($pr)) return;
$nodeInfos[size($nodeInfos)]=_getProfileHierNode(-1, $nodes[$i]);
}
_endPBar();
// find duplicate nodes
int $dups[]={};
_beginPBar("optimize process (2/2)");
for($i=0; $i<size($nodes); $i++){
int $pr=(100.0*$i/size($nodes));
for($j=$i+1; $j<size($nodes); $j++){
if(_PBar($pr)) return;
int $already=0;
for($dup in $dups){
if($j==$dup){ $already=1; break; }
}
if($already) continue;
if($nodeInfos[$i]==$nodeInfos[$j]){
$dups[size($dups)]=$j;
print ("find: "+$nodes[$i]+" == "+$nodes[$j]+"\n");
// re-connect
string $dsts[]=`listConnections -s 0 -d 1 -p 1 -c 0 $nodes[$j]`;
for($dst in $dsts){
string $dplgs[]=`listAttr -hasData -w -c -v $dst`;
if(size($dplgs)<1) continue;
string $srcs[]=`listConnections -s 1 -d 0 -p 1 -c 0 $dst`;
if(size($srcs)<1) continue;
string $splgs[]={};
tokenize $srcs[0] "." $splgs;
disconnectAttr $srcs[0] $dst;
connectAttr -f ($nodes[$i]+"."+$splgs[1]) ($dst);
_deleteHierNode($nodes[$j], "");
clear $splgs;
clear $srcs;
clear $dplgs;
}
clear $dsts;
}
}
}
_endPBar();
clear $dups;
clear $nodeInfos;
clear $nodes;
}
//-----------------------------------
// 1. optimize texture
// _optimizeMatTex( 1 );
//
// 2. optimize material
// _optimizeMatTex( 2 );
//
// 3. optimize mat & tex
// _optimizeMatTex( 2 );
// _optimizeMatTex( 1 );
//
// 4. optimize selected mat (or tex)
// _optimizeMatTex( 0 );
//-----------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment