В конкретном примере - для скрипта, формирующего из замкнутой последовательности ребер правильный многоугольник (круг)
global proc perfectPoly()
{
polyExtrudeFacet -kft true -ls 0.5 0.5 0;
string $selected_face[] = `ls -sl -fl`;
int $number_of_sides = `intSliderGrp -q -v perPoly`;
////////////// POLYMESH ///////////////////////////////////////
string $polymesh[];
tokenize $selected_face[0] ".[]" $polymesh;
////////////// ORDER EDGES ///////////////////////////////////////
string $fte[] = `polyInfo -faceToEdge $selected_face[0]`;
string $order_edges[];
tokenize $fte[0] " :\n\r" $order_edges;
int $size_edgearray = `size $order_edges`;
string $edges[];
for($j=2;$j<$size_edgearray; ++$j)
{
$edges[$j-2] = $polymesh[0] + ".e[" + $order_edges[$j] + "]";
}
////////////// ORDER VERTS ///////////////////////////////////////
string $ftv[] = `polyInfo -faceToVertex $selected_face[0]`;
string $order_verts[];
tokenize $ftv[0] " :\n\r" $order_verts;
int $size = `size $order_verts`;
string $ordered_verts[];
for($i=2;$i<$size;++$i)
{
$ordered_verts[$i-2] = $polymesh[0] + ".vtx[" + $order_verts[$i] + "]";
}
////////////// ARRAY CLEAR ///////////////////////////////////////
clear $fte;
clear $order_edges;
clear $ftv;
clear $order_verts;
////////////// NUMBER OF VERTS ///////////////////////////////////////
int $n = `size $ordered_verts`;
////////////// SORT VERTS/EDGES ///////////////////////////////////////
////////////// SUBDIVIDE EDGES ///////////////////////////////////////
if($n<$number_of_sides)
{
int $remainder = $number_of_sides - $n;
float $g = $remainder/$n;
int $f = `trunc $g`;
int $residue = $remainder - ($f*$n);
select -r $edges[0];
polySubdivideEdge -dv ($f +$residue);
for($k=1;$k<$n;++$k)
{
select -r $edges[$k];
polySubdivideEdge -dv $f;
}
select -r $selected_face[0];
}
////////////// MERGE VERTS ///////////////////////////////////////
if($n>$number_of_sides)
{
int $remainder = $n - $number_of_sides;
if($remainder == 1)
{
select -r $ordered_verts[0];
select -add $ordered_verts[1];
polyMergeVertex -d 10;
}
select -cl;
if($remainder>1)
{
for($j=0;$j<$remainder;++$j)
{
select -add $ordered_verts[$j];
}
polyMergeVertex -d 10;
select -cl;
select -r $ordered_verts[0];
select -add $ordered_verts[1];
polyMergeVertex -d 10;
}
}
select -r $selected_face[0];
////////////// CLEAR ARRAYS ///////////////////////////////////////
clear $selected_face;
clear $edges;
clear $ordered_verts;
////////////////// CONSTRUCT REGULAR POLYGON //////////////////////////////////////////////////////////
string $selected_newverts[] = `ls -sl -fl`;
//Reselect all the verts
string $face_to_verts[] = `polyInfo -faceToVertex $selected_newverts[0]`;
string $order1[];
tokenize $face_to_verts[0] " :\n\r" $order1;
int $size = `size $order1`;
string $ordered_new_verts[];
for($i=2;$i<$size;++$i)
{
$ordered_new_verts[$i-2] = $polymesh[0] + ".vtx[" + $order1[$i] + "]";
}
clear $face_to_verts;
clear $order1;
int $n_gon = `size $ordered_new_verts`;
$theta = (2*3.14159265)/$n_gon;
vector $vert1 = `pointPosition -w $ordered_new_verts[0]`;
vector $vert2 = `pointPosition -w $ordered_new_verts[1]`;
vector $vert3 = `pointPosition -w $ordered_new_verts[$n_gon-1]`;
vector $a = $vert1 - $vert2;
vector $b = $vert3 - $vert2;
vector $cross_ab = cross($b,$a);
float $mag_cross_ab = mag ($cross_ab);
vector $normal = $cross_ab/$mag_cross_ab;
vector $sum = <<0,0,0>>;
for($i=0;$i<$n_gon;++$i)
{
vector $vnow = `pointPosition -w $ordered_new_verts[$i]`;
$sum = $sum + $vnow;
}
vector $centroid = $sum/$n_gon;
vector $v1 = $vert1 - $centroid;
for($i=1;$i<$n_gon;++$i)
{
float $theta_new = $theta*$i;
float $cos_tn = cos ($theta_new);
float $sin_tn = sin ($theta_new);
vector $cross_normV1 = cross($normal,$v1);
vector $v_new = ($v1*$cos_tn) + ($cross_normV1*$sin_tn) + $centroid;
select -r $ordered_new_verts[$i];
move -a ($v_new.x) ($v_new.y) ($v_new.z);
}
select -cl;
select -r $selected_newverts;
clear $ordered_new_verts;
clear $selected_newverts;
clear $polymesh;
}
{
window -title "Regular Polygon" -wh 416 78 Regular_Polygon;
columnLayout;
intSliderGrp -label "Number of Sides"
-fieldMinValue 3 -fieldMaxValue 200
-minValue 3 -maxValue 200 -value 4 -field true perPoly;
button -label "Execute" -c "perfectPoly()";
showWindow;
}