Level One Magic
"Any sufficiently advanced technology is indistinguishable from magic." - Arthur C. Clarke
VRP_checkVertexSnapping.mel
// SUMMARY: Test the border vertices of any amount of selected objects within a set tolerance to make sure they are still snapped after deformation.

proc int IS_NEARLY_ZERO(float $value) {
return (abs($value) < 0.0000000001);
}

proc string[] getSharedVertsOnSelected()
{
  string $verts[] = {};
  string $selected[] = `ls -sl -l`;
  float $tolerance = 0;
  string $snapped[];
  int $numSnapped = 0;
  // print("getSharedVertsOnSelected()\n");
  print("Comparing vertices on " + size($selected) + " meshes.\n");
  string $noBorders[] = {};
  for($each in $selected)
  {
    select -r `polyListComponentConversion -tv $each`;
    ConvertSelectionToShellBorder;
      if(size(`ls -sl -fl`) == 0)
        $noBorders[size($noBorders)] = $each;
  }
  string $borders[] = stringArrayRemove($noBorders, $selected);
  print("Ignoring " + size($noBorders) + " meshes without border vertices.\n");
  string $others[] = $borders;
  for($i=0; $i  {
    print("\tChecking vertices against " + $borders[$i] + "\n");
    $others = stringArrayRemove({$borders[$i]}, $others);
    select -r `polyListComponentConversion -tv $borders[$i]`;
    ConvertSelectionToShellBorder;
    string $borderVerts1[] = `ls -sl -fl`;
    select -r `polyListComponentConversion -tv $others`;
    ConvertSelectionToShellBorder;
    string $borderVerts2[] = `ls -sl -fl`;
    // print("Verts1: " + size($borderVerts1) + "\tVerts2: " + size($borderVerts2) + "\n");
    for($vert1 in $borderVerts1)
    {
      float $v1[] = `xform -q -ws -t $vert1`;
      for($vert2 in $borderVerts2)
      {
        float $v2[] = `xform -q -ws -t $vert2`;
        vector $difference = <<$v1[0] - $v2[0], $v1[1] - $v2[1], $v1[2] - $v2[2]>>;
        float $distance = `mag $difference`;
        if(IS_NEARLY_ZERO($distance) == 1)
        {
          // print("\t" + $distance + "\t" + $vert1 + "\t" + $vert2 + "\n");
          int $found = 0;
          for($j=0;$j          {
            if(`gmatch $verts[$j] $vert1` == 1)
            {
              $verts[$j] = $verts[$j] + ";" + $vert2;
              $found = 1;
            }
            else if(`gmatch $verts[$j] $vert2` == 1)
            {
              $verts[$j] = $verts[$j] + ";" + $vert1;
              $found = 1;
            }
          }
          if($found == 0)
            $verts[size($verts)] = $vert1 + ";" + $vert2;
        }
      }
    }
  }
  print("Found " + size($verts) + " vertex sets that should be snapped.\n");
  // print($verts);
  select -r $verts;
  return $verts;
}

global proc vrcvs_findVertices()
{
  scrollField -e -tx "" vrcvs_vertList;
  for($vert in getSharedVertsOnSelected())
    scrollField -e -it ($vert + "\n") -ip 0 vrcvs_vertList;
}

global proc vrcvs_testVertices()
{
  select -cl;
  float $tolerance = `floatSliderGrp -q -v vrcvs_sliderTolerance`;
  string $vertList[] = stringToStringArray(`scrollField -q -tx vrcvs_vertList`, "\n");
  for($each in $vertList)
  {
    string $verts[] = stringToStringArray(substituteAllString($each, "\n", ""), ";");
    for($i=0;$i    {
      float $v1[] = `xform -q -ws -t $verts[$i]`;
      for($j=$i+1;$j      {
        float $v2[] = `xform -q -ws -t $verts[$j]`;
        vector $difference = <<$v1[0] - $v2[0], $v1[1] - $v2[1], $v1[2] - $v2[2]>>;
        float $distance = `mag $difference`;
        if(($distance > $tolerance) && (IS_NEARLY_ZERO($distance) != 1))
        {
          print("GAP!!\t" + $verts[$i] + "\t" + $verts[$j] + "\n");
          select -add $verts[$i] $verts[$j];
        }
      }
    }
  }
  print("Test vertices complete.\n");
}

global proc VRP_checkVertexSnapping()
{
if(`window -q -ex vrcvs_guiWindow`)
deleteUI -window vrcvs_guiWindow;
window -t "Find / Test Vertex Snapping" -w 600 -toolbox 1 -s 0 vrcvs_guiWindow;
columnLayout -adj true;
      button -label "Find Vertices" -command vrcvs_findVertices;
      button -label "Test Snapping" -command vrcvs_testVertices;
      floatSliderGrp -v 0.001 -f 1 -fmn 0 -fmx 1 -min 0 -max 1 -l "Tolerance" -pre 3 vrcvs_sliderTolerance;
      scrollField -vis 0 -tx "" vrcvs_vertList;
showWindow vrcvs_guiWindow;
  window -e -w 400 -h 90 vrcvs_guiWindow;
}

Back to Main

© 2003-2013 Andrew Kelts