Anim8or Community

General Category => ASL Scripts => Topic started by: CobraSpectre on September 24, 2008, 10:37:09 pm

Title: EXPERIMENTAL Mirroring Script
Post by: CobraSpectre on September 24, 2008, 10:37:09 pm
First, I have to thank Kubajzz for finding this 'feature' in anim8or. The code below is obviously built on his code.

Second, this is still highly experimental, it causes my CPU to go to 100% utilization which may cause problems and random restarts due to heat. Use at your own risk.

Notes:

There's still more that can be done to improve the script. I'm hoping Anim8or can improve to make this easier to work with.

Update:
Correct Normals
Code: [Select]
#command("object");

/* X axis mesh mirror, USE AT YOUR OWN RISK */

shape $S, $NewS;
attribute $Attr;
int $i, $j;
point3 $pointedit;
quaternion $rotedit;

$S = project.curObject.LookupShape("mesh01"); /*get the original shape */

if ($S != NULL) {

$Attr = project.curObject.LookupAttribute("STOP");

/* create a copy */
$NewS = mesh();


while ($Attr == NULL) {

  $NewS.Open();

  /* copy the points and faces */
  for $i = 0 to $NewS.GetNumPoints()-1 do {
    $NewS.SetPointSelected($i, true);
  }
  $NewS.DeleteSelectedPoints();

  /* correct orientaton */
  $rotedit = $S.orientation;
  $rotedit.y = -$rotedit.y;
  $rotedit.z = -$rotedit.z;
  $NewS.orientation = $rotedit;

  for $i = 0 to $S.GetNumPoints()-1 do {
    $pointedit = $S.GetPoint($i);
    $pointedit.x = -$pointedit.x; /* mirror around X axis, invert x values*/
    $NewS.AddPoint($pointedit);
  }

  for $i = 0 to $S.GetNumFaces()-1 do {
    $NewS.OpenFace(0, 0);
    for $j = 0 to $S.GetNumSides($i)-1 do {
      $NewS.VertexN($S.GetFacePointIndex($i, $S.GetNumSides($i)-1-$j));
    }
    $NewS.CloseFace();
  }

    $NewS.Close();

  /* check for attribute to stop the cycle */
  $Attr = project.curObject.LookupAttribute("STOP");
}

}
Title: Re: EXPERIMENTAL Mirroring Script
Post by: CowTail on September 24, 2008, 11:04:43 pm
Err, have you really even tried this script out? It doesn't mirror exactly across the Y-axis from the original mesh, the normals are flipped, and on line 48:
Code: [Select]
NewS.CloseFace();

Needs to be:
Code: [Select]
$NewS.CloseFace();

Or else there'll be errors.

Well, that's for me anyway, with 0.97d. I do hope you get this working well enough! Perhaps turn it into a button in the toolbar where you select the mesh instead, then press the button to activate the mirroring on that mesh? Also, support for subdivision meshes would be outstanding (and as I mentioned before, group the mirrored portion so that it's locked and can't be manipulated by the user)
Title: Re: EXPERIMENTAL Mirroring Script
Post by: CobraSpectre on September 24, 2008, 11:12:37 pm
Oops, fixed now.
Title: Re: EXPERIMENTAL Mirroring Script
Post by: neirao on September 25, 2008, 09:37:13 pm
if has possible set "Division Work" by asl script,

be good for make "MIRROR SUBDIVIDED OBJECTS"... ,

- the script converter object to -- >
1 - division work = 0;
2 -converter to mesh --> and --> mirror object;
3 - converter to Subidivided object and back to "division work"  previos...

i make this question in: http://www.anim8or.com/smf/index.php?topic=1158.0

but...its no is possible.... :'(
Title: Updated Mirroring Script
Post by: CobraSpectre on September 29, 2008, 05:12:56 am
New version info:

Instead of using the mesh's name, select the mesh to mirror before running the script.

You can choose to mirror the X, Y, or Z axis. It defaults to X automatically. For Y and Z, create a string attribute named "Mirror" with a value of "Y" or "Z".

Code: [Select]
#command("object");

/* X, Y, Z axis mesh mirror, USE AT YOUR OWN RISK */

shape $S, $NewS, $AS[0], $AS2[0];
attribute $Attr, $AxisAttr;
int $i, $j;
point3 $pointedit;
quaternion $rotation;
string $AxisStr;

project.curObject.GetShapes($AS);

for $i = $AS.size-1 to 0 step -1 do {
  if ($AS[$i].Selected == 1)
    $AS2.push($AS.pop());
  else
    $AS.pop();
}

if ($AS2.size == 1)
  $S = $AS2[0];

if ($S.Selected == 1) {

$Attr = project.curObject.LookupAttribute("STOP");

/* create a copy */
$NewS = mesh();


while ($Attr == NULL) {

  $NewS.Open();

  /* copy the points and faces */
  for $i = 0 to $NewS.GetNumPoints()-1 do {
    $NewS.SetPointSelected($i, true);
  }
  $NewS.DeleteSelectedPoints();

  $AxisAttr = project.curObject.LookupAttribute("Mirror");
  $AxisStr = $AxisAttr.GetStringValue();

  if ($AxisStr != "Y" && $AxisStr != "Z") { /* X axis mirror is default behavior */
    /* correct orientaton for X*/
    $rotation = $S.orientation;
    $rotation.y = -$rotation.y;
    $rotation.z = -$rotation.z;
    $NewS.orientation = $rotation;

    for $i = 0 to $S.GetNumPoints()-1 do {
      $pointedit = $S.GetPoint($i);
      $pointedit.x = -$pointedit.x; /* mirror around X axis */
      $NewS.AddPoint($pointedit);
    }
  }

  if ($AxisStr == "Y") {
    /* correct orientaton for Y*/
    $rotation = $S.orientation;
    $rotation.x = -$rotation.x;
    $rotation.z = -$rotation.z;
    $NewS.orientation = $rotation;

    for $i = 0 to $S.GetNumPoints()-1 do {
      $pointedit = $S.GetPoint($i);
      $pointedit.y = -$pointedit.y; /* mirror around Y axis */
      $NewS.AddPoint($pointedit);
    }
  }

  if ($AxisStr == "Z") {
    /* correct orientaton for Z*/
    $rotation = $S.orientation;
    $rotation.x = -$rotation.x;
    $rotation.y = -$rotation.y;
    $NewS.orientation = $rotation;

    for $i = 0 to $S.GetNumPoints()-1 do {
      $pointedit = $S.GetPoint($i);
      $pointedit.z = -$pointedit.z; /* mirror around Z axis */
      $NewS.AddPoint($pointedit);
    }
  }

  for $i = 0 to $S.GetNumFaces()-1 do {
    $NewS.OpenFace(0, 0);
    for $j = 0 to $S.GetNumSides($i)-1 do {
      $NewS.VertexN($S.GetFacePointIndex($i, $S.GetNumSides($i)-1-$j));
    }
    $NewS.CloseFace();
  }

    $NewS.Close();

  /* check for attribute to stop the cycle */
  $Attr = project.curObject.LookupAttribute("STOP");
}
}
Title: Re: EXPERIMENTAL Mirroring Script
Post by: Kubajzz on October 01, 2008, 06:07:59 am
Great job CobraSpectre!

Nice to see crazy ideas coming true ;)
Title: Re: EXPERIMENTAL Mirroring Script
Post by: RavenWorks on October 11, 2008, 02:37:47 pm
Interesting! This is the one feature that Anim8or is truly missing, for me...

It looks like the script causes so much slowdown because it's running constantly, right?
Would it be possible (and more stable) to make a button that updates the duplicate whenever you click it?