The full code is below. Unfortunately, I have opened and closed the mesh properly.
/* fix normals
change all faces to the same orientation by starting with the first face.
The direction of vertex winding is noted and compared to each edge adjacent face.
If the direction of vertex winding is the same (should be opposite), fix it.
*/
/* section to initialize console output */
file $output;
string $fname;
$fname="$console";
$output.open($fname,"w");
object $curObject;
shape $shape, $shapes[0], $childShapes[1],$source;
int $havesource,$sourcenumpts,$sourcenumfaces,$sourcenumedges;
int $i,$j,$k,$m,$n;
int $tempedgeindex,$epi1,$epi2,$epi1_p,$epi2_p,$firstface;
int $tepi1,$tepi2,$tepi1_p,$tepi2_p;
int $f1,$f2;
int $cw1,$cw2;
int $flipped;
int $facestatus[0],$edgeface1[0],$edgeface2[0];
int $mainedge,$checkedge;
int $otherface;
int $ofptindex[0];
$curObject = project.curObject;
$curObject.GetShapes($childShapes);
$shapes.size = 0;
while ($childShapes.size > 0)
$shapes.push($childShapes.pop());
while ($shapes.size > 0) {
$shape = $shapes.pop();
if ($shape.GetKind() == SHAPE_KIND_MESH && $havesource == 0)
{
$source=$shape;
$sourcenumpts=$source.GetNumPoints();
$sourcenumedges=$source.GetNumEdges();
$sourcenumfaces=$source.GetNumFaces();
$facestatus.size=$sourcenumfaces;
$edgeface1.size=$sourcenumedges+1;
$edgeface2.size=$sourcenumedges+1;
$havesource=1;
}
}
$shape.Open();
if ($havesource == 1) {
/* build index of Edges to Faces to save cycles*/
for $i = 1 to $sourcenumedges do
{
$firstface=0;
for $j = 0 to $sourcenumfaces-1 do
{
for $k = 0 to $source.GetNumSides($j)-1 do
{
$tempedgeindex = $source.GetEdgeIndex($j,$k);
if (abs($tempedgeindex) == $i) {
if ($firstface==0) {
$edgeface1[$i]=$j;
} else {
$edgeface2[$i]=$j;
}
$firstface=1;
}
}
}
}
/* output list of the edges with the faces connected to it */
for $i = 1 to $sourcenumedges do {
$output.print("E:%d F1:%d F2:%d\n",$i,$edgeface1[$i],$edgeface2[$i]);
}
/* Cycle through faces edges to determine if adjacent faces have the same normals
If the clockwise-counterclockwise order of face points is the same for adjacent faces,
the normals are flipped
*/
for $i = 0 to $sourcenumfaces-1 do {
for $j = 0 to $source.GetNumSides($i)-1 do {
$mainedge = $source.GetEdgeIndex($i,$j); /* current edge of current face */
$epi1=$source.GetEdgeIndex0($mainedge); /* end point of current edge */
$epi2=$source.GetEdgeIndex1($mainedge);
for $k = 0 to $source.GetNumSides($i)-1 do {
if ($epi1==$source.GetFacePointIndex($i,$k)) $epi1_p=$k; /* position in face vertex winding */
if ($epi2==$source.GetFacePointIndex($i,$k)) $epi2_p=$k;
}
$output.print("F:%d E:%d ep1:(%d,%d) ",$i,$mainedge,$epi1_p,$epi2_p);
if ($edgeface1[$mainedge] == $i) {
$otherface=$edgeface2[$mainedge];
} else {
$otherface=$edgeface1[$mainedge];
}
$ofptindex.size=$source.GetNumSides($otherface); /* retrieve other face with same edge */
for $k = 0 to $source.GetNumSides($otherface)-1 do {
$ofptindex[$k] = $source.GetFacePointIndex($otherface,$k);
if ($epi1==$source.GetFacePointIndex($otherface,$k)) $tepi1_p=$k; /* position in other face vertex winding */
if ($epi2==$source.GetFacePointIndex($otherface,$k)) $tepi2_p=$k;
}
$output.print("F:%d E:%d ep1:(%d,%d) ",$otherface,$mainedge,$tepi1_p,$tepi2_p);
if (abs($epi1_p - $epi2_p)==1) { /* logic to determine direction of winding */
if ($epi1_p > $epi2_p) { /* can be simplified a bit :) */
$cw1=0;
} else {
$cw1=1;
}
} else {
if ($epi1_p > $epi2_p) {
$cw1=1;
} else {
$cw1=0;
}
}
if (abs($tepi1_p - $tepi2_p)==1) {
if ($tepi1_p > $tepi2_p) {
$cw2=0;
} else {
$cw2=1;
}
} else {
if ($tepi1_p > $tepi2_p) {
$cw2=1;
} else {
$cw2=0;
}
}
$flipped=0;
if ($cw1 == $cw2) $flipped=1;
$output.print("->%d\n",$flipped);
if ($cw1 == $cw2) {
for $k = 0 to $source.GetNumSides($otherface)-1 do {
$source.SetFacePointIndex($otherface,$k,$ofptindex[$source.GetNumSides($otherface)-1-$k]); /* reverse winding order of other face */
$output.print("Alter F:%d v:%d was:%d now:%d\n",$k,$otherface,$ofptindex[$k],$ofptindex[$source.GetNumSides($otherface)-1-$k]);
}
}
}
}
}
$shape.Close();
$output.close;
Thank you!