I've rewritten all the scripts and I've released them in another topic:
Magnet Tools Package
Please refer to that topic for further discussion, thank you.
Consider this thread as locked.[EDIT]
SCRIPT UPDATED: take a look to this post for the latest:
http://www.anim8or.com/smf/index.php?topic=1492.msg11109#msg11109[/EDIT]
Hi all, that's my very first attempt at making a script. I've read somewhere that some 3D editors have a magnet tool. Honestly I never used one and I have no idea about the way it is intended to work, anyway, I tried to give a shot at it.
The idea by itself is simple, and the script I'm going to paste here below does compile without errors, but well, it simply doesn't work.
I took inspiration from the mirror script but I ended up with another way for exiting the script.
As with the mirror script, the CPU goes up 100% in my script, try it at your own risk.
I really don't know where I went wrong scripting, seems that moving the magnet shape around the screen doesn't make any difference, I think I'll have to debug it by outputting some values to a file.
As you will read in the code, only the first point of the "magnet" shape is used as reference. When this script gets fixed (if ever) it would be cool to use more than a point to model the "clay" shape, so that we could have some sort of "variable" magnet tool.
[EDIT]
I modified one line and actually now it works (well, no, it won't work as expected but it does work somehow).
Try this: add a large multifaceted sphere in an empty object, convert the sphere to a mesh and ensure it is named "mesh01", add another mesh and name it "magnet", then run the script. Moving around the magnet while the script is running will somehow modify the sphere.
The problem is that I am computing the distances checking each coordinate separately, which is too wrong... I'm trying to fix it.
[/EDIT]
[EDIT 2]
Cool! it works! and it's also quite easy to use... although it must be refined... try this, and notice that the script does not move the points as long as you keep the mouse button pressed to move the magnet. It does move the points only after that you release the mouse button. Enough for today, tomorrow I'll try to post a better version with some deeper instructions. Anyway, here below is the updated script, give it a try. The mesh "mesh01" must be centered at origin - for the moment...
[/EDIT 2]
/* magnet pseudo-tool
to stop the script change one of the shapes' names,
the while-loop goes on as long as both shapes are found */
#command("object");
shape $magnet; /* shape named "magnet" */
shape $clay; /* shape named "mesh01" */
point3 $clay_p; /* clay point */
point3 $mag_p; /* magnet location */
point3 $cur_p; /* currently modified clay point */
float $distance; /* actual distance between clay point and magnet location */
point3 $dist; /* coord distances between clay point and magnet location */
float $min_dist; /* min distance for modifying clay point */
float $max_dist; /* max distance for modifying clay point */
float $step; /* step of change */
int $continue; /* flag for exiting the loop */
int $i;
$min_dist = 2;
$max_dist = 10;
$step = 1;
$continue = 1;
while ( $continue ) {
$magnet = project.curObject.LookupShape("magnet");
$clay = project.curObject.LookupShape("mesh01");
if ( ($magnet != NULL) && ($clay != NULL) ) {
$clay.Open();
$mag_p = $magnet.loc;
for $i = 0 to $clay.GetNumPoints()-1 do {
$clay_p = $clay.GetPoint($i);
$cur_p = $clay_p;
$dist.x = $mag_p.x - $clay_p.x;
$dist.y = $mag_p.y - $clay_p.y;
$dist.z = $mag_p.z - $clay_p.z;
$distance = sqrt($dist.x * $dist.x + $dist.y * $dist.y + $dist.z * $dist.z);
if ( ($distance > $min_dist) && ($distance < $max_dist) ) {
if ( $mag_p.x > $clay_p.x ) {
$cur_p.x = $cur_p.x + $step;
} else {
$cur_p.x = $cur_p.x - $step;
}
if ( $mag_p.y > $clay_p.y ) {
$cur_p.y = $cur_p.y + $step;
} else {
$cur_p.y = $cur_p.y - $step;
}
if ( $mag_p.z > $clay_p.z ) {
$cur_p.z = $cur_p.z + $step;
} else {
$cur_p.z = $cur_p.z - $step;
}
}
$clay.SetPoint($i, $cur_p);
}
$clay.Close();
refresh();
} else {
$continue = 0; /* one or both shapes not found, exit loop */
}
}
Use this script at your will of course, feel free to. By my side I think I'll have to get a firmer grasp on ASL before getting to work things correctly.