View Full Version : centre pivot to grid .??
robbeddall 05032007, 03:46 PM hey,
has anyone here seen a script that would be able to automatically set a mesh's pivot point to the closest grid point to the centre of the object? (not the ACTUAL centre)
if anyone know's of one then you'd really be helping me out.
cheers.


EigenPuff
05072007, 09:16 PM
sorry it took so long  I was going to answer last week but ran out of time. The basic idea of the proc below is that it finds the center of the bounding box around the object, and then uses integer math to "round" the center point to the nearest gridpoint. This proc is general case because I don't know what circumstances you're under, and I've only tested it briefly, but it should be a good starting point.
NOTE: this doesn't actually find the center of the object, just the center of the bounding box. Mileage might vary.
global proc CenterPivotGridlike(string $object)
{
float $bb[6] = `xform ws bb q $object`;
float $cp[3];
//find the "center" of the object  really just center of bbox
$cp[0] = ($bb[0] + $bb[3])/2.0;
$cp[0] = ($bb[1] + $bb[4])/2.0;
$cp[0] = ($bb[2] + $bb[5])/2.0;
//find the distance between gridlines to round to
float $dist = `grid q spacing`;
float $div = `grid q divisions`;
float $quantizer = $dist/$div;
//find some fudge factor so that we round to nearest gridline instead of "round left"
float $epsilon = $quantizer  $quantizer/10.0;
int $temp[3];
//do the rounding
$temp[0] = int( ($cp[0]+ $epsilon)/$quantizer);
$temp[1] = int( ($cp[0]+ $epsilon)/$quantizer);
$temp[2] = int( ($cp[0]+ $epsilon)/$quantizer);
$temp[0] *= $quantizer;
$temp[1] *= $quantizer;
$temp[2] *= $quantizer;
//set pivot
xform ws piv $temp[0] $temp[1] $temp[2] $object;
}
robbeddall
05082007, 06:20 AM
hey,
Thanks for the reply.
I haven't had chance to test out the script yet though I will try it as soon as i get to work.
I'll let you knwo how things go with it.
Thankyou very much for taking the time to write that. :)
cheers.
robbeddall
05082007, 08:56 AM
hey,
unforunately i couldn't get the script to work.
it just kept returning these errors.
// Error: left"
//
// Error: Unterminated string. //
// Error: $temp[0] = int( ($cp[0]+ $epsilon)/$quantizer);
//
// Error: "$epsilon" is an undeclared variable. //
// Error: $temp[1] = int( ($cp[0]+ $epsilon)/$quantizer);
//
// Error: "$epsilon" is an undeclared variable. //
// Error: $temp[2] = int( ($cp[0]+ $epsilon)/$quantizer);
//
// Error: "$epsilon" is an undeclared variable. //
i was just running th escript with an object selected. is there somethign i should be doing that i maty be unaware of?
cheers.
tbaypaul
05102007, 01:42 AM
you simply copied it wrong to the script editor...so a comment got mixed up in the code and screwed up the defination of variable $epsilon......try again....
CGTalk Moderation
05102007, 01:42 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©20002015, Jelsoft Enterprises Ltd.