Anim8or Community

Please login or register.

Login with username, password and session length
Advanced search  

News:

An update to Anim8or, v1.00b, is available with a few bug fixes. Get your copy HERE. See the "ReadMe" file for details.

Pages: 1 2 [3] 4 5

Author Topic: Old Magnet Tool  (Read 34059 times)

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Multi-Magnet Tool (wannabe-script ;) )
« Reply #30 on: November 05, 2008, 06:09:03 pm »

Hi all,
here I am with a pre-release of my multi-magnet tool. In some way it does what Neirao suggested - that is, you can use any mesh as magnet and try to emboss or carve its shape over a finely faced surface. Sort of clothing, but really bumpy at this very stage.

In shorts: each vertex of the magnet becomes a magnet itself.

I was about to start from scratch with stuff like computing if a point falls here or there, when related to a magnet's face, but I think I need a firmer grasp on such things before writing the script.

Changing the spherical magnet into a bunch of smaller spherical magnets was easier and quicker to do, by modifying the original script, so here you have the first results in four files:
- an example picture;
- the "multimagnet" script;
- the "show ranges" script;
- a "multimagnet" project;

The "multimagnet" script is the one doing the work. If you already have the needed materials set in a project you can try it out at once (the needed materials are the same used by the "magnet script", they are in the "magnet project" I attached before in this thread).

The "show ranges" script can be used to display the range of action of your magnet. You only need a mesh named "MAGNET" to run this script. The script adds a sphere for each vertex of your magnet displaying the range of action of each vertex. When the script stops, all the added spheres remain selected, so, if you had nothing selected before running it, you can get rid of all the spheres hitting "DEL" on your keyboard.

The "multimagnet" project isn't really needed, but there you have the magnet I used in the example picture and the all-triangles sheet I use to test the magnet. I saved this project in the state it was when I took the screenshots below, if you care to know. I didn't attach the bitmaps this time, so I suggest you to paste the project in the same folder of those AXYZ pictures, if you want Anim8or to load them.

The picture:
1 - the magnet alone;
2, 3 - a sphere for each vertex' range;
4 - mesh01 and the magnet;
5 - range of action from another angle;
6 - result on mesh01 after fiddling around with M_PUSH_Z

An important detail: the range of action of each vertex is nothing more than its distance from the nearest neighbour. Hence, more points will give you a more detailed and less bumpy magnet. The "show ranges" script will show you exactly what you are dealing with.

Sorry if, again, it looks all messy, incomplete and not so clear with the explanations, but I'm in a hurry.

Let me know your opinion, if it sounds promising I can keep this script and continue working on it, otherwise I would switch to the "other way" (checking and changing mesh01 against each magnet's face).

Anyway, by the way, have fun,
Francesco
Logged

odp04y

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: Magnet Tool
« Reply #31 on: November 05, 2008, 08:21:09 pm »

This is a really useful script ;D

Posting an example of what it can do.
Logged

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Re: Magnet Tool
« Reply #32 on: November 05, 2008, 09:06:35 pm »

Thank you odp04y, also for posting that example.

A side note: now that the surface of the magnet is made up of several small magnets, you should take care of approaching the magnet to the mesh step by step. If you intersect the two too quick some points of the mesh will fall in the "backside" of each magnet, and those points will be pulled instead of being pushed (or vice versa, it depends from the material you are using).

I look forward for your experiments with this script, people, and even more for your advice (otherwise I'll go on making this stuff the way I see fitting ;) ).

Thanks a lot to all of you for your attention, I'm really glad.
Logged

bamman62

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: Magnet Tool
« Reply #33 on: November 08, 2008, 03:34:31 am »

Francesco VERY COOL SCRIP!!! CONGRATULATIONS!! ;)

I have two sugestions for Future using your script OK... ::)


Somehow you always manage to make these awesome looking pictures that perfectly display your idea... :P

Anyway, reminds me of nCloth in Maya.

Great idea. (A carve script would be really awesome though)
Logged

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Re: Magnet Tool
« Reply #34 on: November 08, 2008, 04:42:39 pm »

First things first: the Multi-Magnet Script is pure crap - although the Showranges script is funny in my opinion.

I wonder why nobody suggested me to throw away that crap, but I think that Bamman62's parenthetical "(A carve script would be really awesome though)" would kindly mean the same, and I agree ;-)

So then, I struggled a lot and I came up with the Polimagnet script, that I share in a .a8s and a .jpg, both attached to this post.

So please go ahead and try clothing or carving something, but bear in mind what follows - I will remove all those limitations, but I need some of your help before doing that:

Limitations:
- the magnet must be a well formed, all-triangles mesh;
- you cannot rotate the magnet, you cannot rotate mesh01;
- you can only use "M_PUSH" materials (I seriously think that M_PULL modes are unneeded);

The juice in few words: the script checks if a point of mesh01 lies between magnet's origin and one of its faces. In such a case the point will be pushed away from magnet's origin till magnet's surface.

Notice that magnet's origin is not its pivot. You might want to manually set magnet's location to (0,0,0). Magnet's origin will be on world's origin then. I'm gonna fix that.

Temporary tweaks:
- you can modify the magnet or the mesh in point edit mode (you can also rotate them);

Final note: the script only cares about mesh01 points, hence mesh01 can have all sorts of facets (tri, quad, poly). But resulting faces could be messed up, up to you to use non-triangular facets (converting to subdivided could eventually fix that?).

Now the most important thing: I need some help with the transformation matrices for applying the rotations... somebody wants to give me advice? It's something I cannot grab my mind on.
Logged

odp04y

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: Magnet Tool
« Reply #35 on: November 08, 2008, 07:09:05 pm »

I can't get it to work  (i got a triangle faced mesh as MAGNET with M_PUSH material, neither are rotated, magnet script works but not this one) ???
Logged

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Re: Magnet Tool
« Reply #36 on: November 08, 2008, 07:28:00 pm »

Uhm, sorry but I have no real idea... I think I have to let the script output some debug information.

As for now I suggest you to check whether the magnet's origin is where you think it is.

Doubleclick the magnet and enter (0,0,0) as its location. If then it lies centered around world's (object's) origin, then the magnet's origin should be right.

If setting magnet's location to zero you happen to find it displaced away from object's origin, then enter point edit mode, select all points and bring them around the object's origin. Once you have done this, you can freely move the magnet.

I came to realise that there is no way to access pivot's position from within ASL, I cannot make this script easier to use until ASL allows me accessing pivot's position.

I am about to modify the script so that it creates a copy of the magnet and triangulates it, so that you will be able to use any kind of mesh as magnet.

I still need help with rotation matrix, I hope that someone could give me a quick insight on that and on its use.

[edit: here is a project whose magnet and mesh01 should work - I've just tested them. I attach a picture of another example, applied the magnet 4 times with M_PUSH_Z, wireframe, raw render, subdivided render. ]
« Last Edit: November 08, 2008, 08:06:56 pm by Francesco »
Logged

NickE

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Magnet Tool
« Reply #37 on: November 08, 2008, 08:31:54 pm »

You can access and change the object's pivot position in ASL.  It is referred to as "orientation" as a quaternion type.  You can construct a new quaternion of the rotation you desire and apply it to the whole object or you can can use the "toFloat4x4" and "Project" methods to change the orientation of the object on a point-by-point basis.   See the chain maker script or the copy source mesh to target mesh scripts for examples of how the orientation can be manipulated.
Logged

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Re: Magnet Tool
« Reply #38 on: November 08, 2008, 09:39:49 pm »

Silly me, I was looking for a point3 just like the location... Sorry, my fault. Thank you NickE, also for the pointers.
Logged

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Re: Magnet Tool
« Reply #39 on: November 09, 2008, 01:56:26 am »

Uhm... I fear that the pivot's position is not truly accessible from ASL...  pivot's position and orientation are stored separately in projects files, that should be the same in ASL.

Please tell me if I'm banging my head or if I can extract pivot's position from orientation, I really need this.
Logged

Claude

  • Full Member
  • ***
  • Posts: 249
    • View Profile
Re: Magnet Tool
« Reply #40 on: November 09, 2008, 03:49:39 am »

The pivot point location and orientation aren't  accessible.

Could you describe what you want to do exactly,maybe
you don't really need the pivot point.
As an example,if you want to calculate the true coordinates
of the points,you don't need it.
Logged

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Re: Magnet Tool
« Reply #41 on: November 09, 2008, 10:46:34 am »

Hi Claude,
thanks for dropping in.

I need to know the pivot's position to make the polimagnet script more flexible and easy to use.

Currently, the script checks if a point of mesh01 falls in the "cone" (let me pass this term for clarity) built from a face of the magnet and its origin.

The thing I want to do is to build that "cone" from the pivot's position, so that users can easily modify the center of the magnet.

I'm about to make a sketch for clarity, this way I can clearly illustrate how the polimagnet script works - for the people that's interested in using it.

I attach the sketch here:
« Last Edit: November 09, 2008, 11:24:37 am by Francesco »
Logged

NickE

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Magnet Tool
« Reply #42 on: November 09, 2008, 02:36:10 pm »

Francesco,
Thank you for the diagram.  That helps me understand what you are attempting.

As far as I can tell, an object's position and orientation are a combination of three factors:  the raw point coordinates retrieved by the GetPoints() function, the Location (loc), and the Orientation.  When you move or rotate a whole object in the object editor, you are only changing the Location and the Orientation.  When you move a point, you are changing the raw point coordinates relative to the current Location and Orientation.   Whenever you need to know how one object's points relate to another object's points, both objects must be in the same coordinate system. 

Below is some code that moves two meshes into the same coordinate system.
Code: [Select]
file $output;
string $fname;
$fname="$console";
$output.open($fname,"w");

object $curObject;
shape $shape, $shapes[0], $childShapes[1];
shape $source,$target;

int $i,$j,$havesource,$havetarget;
int $ii,$jj,$kk;

int $sourcenumpts,$sourcenumfaces,$sourcenumedges;
point3 $p,$sourcemin,$sourcemax,$sourcecenter,$sourceloc;
quaternion $sourceorientation,$zeroor;
float4x4 $sourcerotmat;


point3 $zeropoint;

float $sourcedepth,$sourcedepthoffset;

int $targetnumpts,$targetnumfaces,$targetnumedges;
point3 $targetmin,$targetmax,$targetcenter,$targetloc;
quaternion $targetorientation;
float4x4 $targetrotmat;

$zeropoint.x=0.0;
$zeropoint.y=0.0;
$zeropoint.z=0.0;
$zeroor.x=0.0;
$zeroor.y=0.0;
$zeroor.z=0.0;
$zeroor.w=1.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 && $shape.name == "source")
    {
       $source=$shape;
       $sourcenumpts=$source.GetNumPoints();
       $sourcenumfaces=$source.GetNumFaces();
       $sourcenumedges=$source.GetNumEdges();
       $sourceloc=$source.loc;
       $sourceorientation=$source.orientation;
       $sourcerotmat=toFloat4x4($sourceorientation);
       $havesource=1;
    }
   
    if ($shape.GetKind() == SHAPE_KIND_MESH && $shape.name == "target")
    {
       $target=$shape;
       $targetnumpts=$target.GetNumPoints();
       $targetnumfaces=$target.GetNumFaces();
       $targetnumedges=$target.GetNumEdges();
       $targetloc=$target.loc;
       $targetorientation=$target.orientation;
       $targetrotmat=toFloat4x4($targetorientation);
       $havetarget=1;
    }
}

if ($havesource==1 && $havetarget==1)
{

   $output.print("Source Before:\n");
   $output.print("Loc: (%0.2f,%0.2f,%0.2f)\n",$source.loc.x,$source.loc.y,$source.loc.z);
   $output.print("Center: (%0.2f,%0.2f,%0.2f)\n",$sourcecenter.x,$sourcecenter.y,$sourcecenter.z);
   $output.print("Min: (%0.2f,%0.2f,%0.2f) Max: (%0.2f,%0.2f,%0.2f)\n",$sourcemin.x,$sourcemin.y,$sourcemin.z,$sourcemax.x,$sourcemax.y,$sourcemax.z);
   $output.print("Orientation (%0.2f,%0.2f,%0.2f,%0.2f)\n",$sourceorientation.x,$sourceorientation.y,$sourceorientation.z,$sourceorientation.w);



   /* apply orientation and location to shapes */
   for $ii = 0 to $sourcenumpts-1 do
   {
     $p=$source.GetPoint($ii);
     $p=$sourcerotmat.Project($p)+$source.loc;
     $source.SetPoint($ii,$p);
   }
   $source.loc=$zeropoint;
   $source.orientation=$zeroor;
   
   for $ii = 0 to $targetnumpts-1 do
   {
     $p=$target.GetPoint($ii);
     $p=$targetrotmat.Project($p)+$target.loc;
     $target.SetPoint($ii,$p);
   }
   $target.loc=$zeropoint;
   $target.orientation=$zeroor;

   /* get source stats */
   $sourcemin.x=10000;
   $sourcemin.y=10000;
   $sourcemin.z=10000;
   $sourcemax.x=-10000;
   $sourcemax.y=-10000;
   $sourcemax.z=-10000;

   for $i=0 to $sourcenumpts-1 do
   {
    $p=$source.GetPoint($i);
    if ($p.x < $sourcemin.x) $sourcemin.x=$p.x;
    if ($p.y < $sourcemin.y) $sourcemin.y=$p.y;
    if ($p.z < $sourcemin.z) $sourcemin.z=$p.z;
    if ($p.x > $sourcemax.x) $sourcemax.x=$p.x;
    if ($p.y > $sourcemax.y) $sourcemax.y=$p.y;
    if ($p.z > $sourcemax.z) $sourcemax.z=$p.z;
   }

   $sourcecenter.x = 0.5 * ($sourcemax.x + $sourcemin.x);
   $sourcecenter.y = 0.5 * ($sourcemax.y + $sourcemin.y);
   $sourcecenter.z = 0.5 * ($sourcemax.z + $sourcemin.z);

   $output.print("Source:\n");
   $output.print("Points: %d, Faces: %d, Edges: %d\n",$sourcenumpts,$sourcenumfaces,$sourcenumedges);
   $output.print("Loc: (%0.2f,%0.2f,%0.2f)\n",$source.loc.x,$source.loc.y,$source.loc.z);
   $output.print("Center: (%0.2f,%0.2f,%0.2f)\n",$sourcecenter.x,$sourcecenter.y,$sourcecenter.z);
   $output.print("Min: (%0.2f,%0.2f,%0.2f) Max: (%0.2f,%0.2f,%0.2f)\n",$sourcemin.x,$sourcemin.y,$sourcemin.z,$sourcemax.x,$sourcemax.y,$sourcemax.z);
   $output.print("Orientation (%0.2f,%0.2f,%0.2f,%0.2f)\n",$sourceorientation.x,$sourceorientation.y,$sourceorientation.z,$sourceorientation.w);


   /* get target stats */
   $targetmin.x=10000;
   $targetmin.y=10000;
   $targetmin.z=10000;
   $targetmax.x=-10000;
   $targetmax.y=-10000;
   $targetmax.z=-10000;

   for $i=0 to $targetnumpts-1 do
   {
    $p=$target.GetPoint($i);
    if ($p.x < $targetmin.x) $targetmin.x=$p.x;
    if ($p.y < $targetmin.y) $targetmin.y=$p.y;
    if ($p.z < $targetmin.z) $targetmin.z=$p.z;
    if ($p.x > $targetmax.x) $targetmax.x=$p.x;
    if ($p.y > $targetmax.y) $targetmax.y=$p.y;
    if ($p.z > $targetmax.z) $targetmax.z=$p.z;
   }

   $targetcenter.x = 0.5 * ($targetmax.x + $targetmin.x);
   $targetcenter.y = 0.5 * ($targetmax.y + $targetmin.y);
   $targetcenter.z = 0.5 * ($targetmax.z + $targetmin.z);

   $output.print("Target:\n");
   $output.print("Points: %d, Faces: %d, Edges: %d\n",$targetnumpts,$targetnumfaces,$targetnumedges);
   $output.print("Loc: (%0.2f,%0.2f,%0.2f)\n",$target.loc.x,$target.loc.y,$target.loc.z);
   $output.print("Center: (%0.2f,%0.2f,%0.2f)\n",$targetcenter.x,$targetcenter.y,$targetcenter.z);
   $output.print("Min: (%0.2f,%0.2f,%0.2f) Max: (%0.2f,%0.2f,%0.2f)\n",$targetmin.x,$targetmin.y,$targetmin.z,$targetmax.x,$targetmax.y,$targetmax.z);
   $output.print("Orientation (%0.2f,%0.2f,%0.2f,%0.2f)\n",$targetorientation.x,$targetorientation.y,$targetorientation.z,$targetorientation.w);


} else {
   $output.print("No source and/or target\n");
} /* end of have source or target if */
$output.close;
Logged

Francesco

  • Full Member
  • ***
  • Posts: 182
    • View Profile
Re: Magnet Tool
« Reply #43 on: November 09, 2008, 04:37:59 pm »

Actually the diagram shows what I already have done in the Polimagnet script attached some posts above (that is not the MultiMagnet scripts, sorry for this mess of names).

A little resume:
Magnet script --> a single spherical magnet;
MultiMagnet script --> a spherical magnet for each magnet's vertex (the "crap" script);
Polimagnet script --> the last one, the one that checks each vertex of mesh01 against each magnet's face;

Now I know that I cannot access pivot's position, thus I have to wait for the next ASL release before fixing that.

The other thing I needed to know was how to apply the rotation, I think that I can spill it out from your code NickE.

Thank you all for your support.
Logged

neirao

  • Sr. Member
  • ****
  • Posts: 570
  • Neirao
    • View Profile
Re: Magnet Tool
« Reply #44 on: November 09, 2008, 05:03:56 pm »

Hi Francesco,

your script is time in time better.. ;)

soon he will be perfect for to recognize collision with all object formats!

congratulations! :D

neirão
Logged
Pages: 1 2 [3] 4 5