Skip to content

Instantly share code, notes, and snippets.

@hmasato
Created September 6, 2013 15:01
Show Gist options
  • Select an option

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

Select an option

Save hmasato/6465052 to your computer and use it in GitHub Desktop.
[Maya] _calcVolumeAndCOM
proc _calcVolumeAndCOM()
{
string $shps[]=`listRelatives -pa -ni -s -type "mesh"`;
for($s in $shps){
float $vol=0;
vector $com=<<0, 0, 0>>;
vector $vt[]=`xform -q -ws -t ($s+".vtx[*]")`;
int $nf=`getAttr -s ($s+".fc")`;
for($i=0; $i<$nf; $i++){
string $b[]=`polyInfo -faceToVertex ($s+".f["+$i+"]")`;
int $n=tokenize($b[0], ": ", $b)-3;
vector $p0=$vt[ int($b[2]) ];
for($j=1; $j<$n-1; $j++){
vector $p1=$vt[ int($b[2+$j]) ];
vector $p2=$vt[ int($b[2+$j+1]) ];
float $v=dot($p2, cross($p0, $p1))/6.0;
vector $c=($p0+$p1+$p2)/4.0;
$vol+=$v;
$com+=$v*$c;
}
}
$com/=$vol;
print ("mesh ["+$s+"] volume ["+$vol+"]\n");
print ("center of mass ["+($com.x)+", "+($com.y)+", "+($com.z)+"]\n");
}
}
_calcVolumeAndCOM;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment