Rotate pivot to Targeted Obj

How would I create this same script but without the hassle of using the point object and just modifying the actual objects pivot?

I'm trying to rotate a pivot to look in the direction of a target with an axis constraint.

To test just create a bunch of boxes on different position in 3d space and then create a sphere at 0,0,0 using that as the target.

targetObj = $Sphere001
sel = selection as array
for obj in sel do
(
	--pivotLookAt obj $Sphere001
	local dummyPt = point()
 
	dist = obj.pos - targetObj.pos
	dist = normalize dist
	upVec = [0,0,1]
	dp = dot dist upVec
	t = acos dp
	c = cross upVec dist
	c = normalize c	
	dummyPt.transform = obj.transform
	dummyPt.dir = c
	dummyPt.pos = obj.center
	in coordsys local rotate dummyPt (eulerangles -90 0 0)
	in coordsys local rotate dummyPt (eulerangles 0 0 -90)
 
	worldAlignPivot obj
	theRot = dummyPt.rotation
	in coordsys local obj.rotation *= theRot
	obj.objectoffsetrot*= theRot
	obj.objectoffsetpos*= theRot
	delete dummyPt
)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ashmantills's picture

well that is the code that i

well that is the code that i need thanks for sharing this with us.

dry cleaning epos

Graph's picture

no biggie Jack :P

no biggie Jack :P

Raphael Steves

JokerMartini's picture

I think this works. I'll let

I think this works. I'll let you know how it goes. Thanks for the help Ralph

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Graph's picture

hey grant, have you tried the

hey grant, have you tried the code i suplied?
with it you just gotta make sure to set yAxis to the local y of the object to rotate
local yAxis = [0,1,0]*$Teapot007.transform.rotationPart

to wrap it in a method:

(
	fn lookAtFN target others vec:[0,1,0] = 
	(
		for o in others do
		(
			local yAxis = vec*o.transform.rotationPart
 
			local pivPos = o.pos
			local objPos = target.pos
 
			local ang = acos(dot(normalize (objPos-pivPos)) (normalize (yAxis)))
 
			local rot_box = if pivPos.x > objPos.x then eulerangles 0 0 ang else eulerangles 0 0 -ang
 
			rotate  o rot_box
		)
	)
 
	lookAtFN $Box001 (for o in selection where o != $Box001 collect o)
)

Raphael Steves

blented's picture

I tried to implement this,

I tried to implement this, the following doesn't work even though it seems to match the answer given by Graph.

targetObj = $Sphere001
sel = selection as array
for obj in sel do
(	
	pivotZVector =  obj.dir
	degrees = acos (dot (normalize (targetObj.pos - obj.pos)) pivotZVector)	
	axisVector = normalize (targetObj.pos - obj.pos)
	offsetRot = angleaxis degrees axisVector
 
	pos = obj.pos
	obj.rotation = offsetRot
	obj.pos = pos
)

Thoughts on what I'm doing wrong?

Grant Miller
Creative Director
ievfx.com

Graph's picture

like this?( local pivPos =

like this?

(
	local yAxis = [0,1,0]
 
	local pivPos = $Teapot007.pos
	local objPos = $Box001.pos
 
	local ang = acos(dot(normalize (objPos-pivPos)) (normalize (yAxis)))
 
	local rot_box = if pivPos.x > objPos.x then eulerangles 0 0 ang else eulerangles 0 0 -ang
 
	rotate  $Teapot007 rot_box
)

yAxis should of course be the teaPots's local y vector

Raphael Steves

JokerMartini's picture

Attachment

The attached image just clarifies what I'm after.
I just have a selection of objects who's pivots I want to point in the direction of a "targeted object"

It's just a one time deal. The user runs the script and it just changes the pivots rotation accordingly.

It would rotate them in a way that points the Y towards the target object while maintaining the Z axis pointing directly up, perpendicular to the ground.

AttachmentSize
sample.jpg 293.3 KB

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Graph's picture

do you need it in

do you need it in realtime?

if not the simplest way is to rotate the object.tm by (anle*axis) and then set the verts to their old coords ;)

Raphael Steves

JokerMartini's picture

assigning?

I'm not sure how you are assigning this to the objects pivot?

objectPoint3 = $Sphere001.pos
pivotPoint3 = $.pos
PivotZVector = [0,0,1]
theAngle = acos(dot(normalize (objectPoint3-pivotPoint3)) (normalize (PivotZVector)))
theAxis = normalize(objectPoint3-pivotPoint3) 

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Graph's picture

correct, if the pivot is

correct, if the pivot is currently pointing up ;)

Raphael Steves

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.