Rotating an object's pivot using maxscript

Rotating an object's pivot using maxscript

explosivepants
Explorer Explorer
703 Views
9 Replies
Message 1 of 10

Rotating an object's pivot using maxscript

explosivepants
Explorer
Explorer

I'm trying to write a simple maxscript to rotate just the pivot of an object, i.e.: rotating with 'Affect Pivot Only' mode enabled. If I run the macro recorder it gives me this:

 

select $Root
rotate $ (angleaxis -90 [1,0,0])

 

The problem however is that this code rotates the object, not the pivot only. I've tried adding:

 

maxOps.pivotMode = #pivotOnly

 

But it still rotates the object.

 

What am I missing here?

 

Thanks.

0 Likes
Accepted solutions (1)
704 Views
9 Replies
Replies (9)
Message 2 of 10

A娘
Advocate
Advocate

delete

0 Likes
Message 3 of 10

MartinBeh
Advisor
Advisor

From the online help:

-- define function for rotating only the pivot point
fn RotatePivotOnly obj rotation = (
   local rotValInv=inverse (rotation as quat)
   animate off in coordsys local obj.rotation*=RotValInv
   obj.objectoffsetpos*=RotValInv
   obj.objectoffsetrot*=RotValInv
)
Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
0 Likes
Message 4 of 10

explosivepants
Explorer
Explorer

This is rotating the entire object and its child objects for me. 

0 Likes
Message 5 of 10

explosivepants
Explorer
Explorer

Sorry but this rotated the entire object and subobjects on my end for some reason.

0 Likes
Message 6 of 10

MartinBeh
Advisor
Advisor

@explosivepants wrote:

This is rotating the entire object and its child objects for me. 


Seems to work for me. Can you post a .max file? 

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
0 Likes
Message 7 of 10

MartinBeh
Advisor
Advisor

Here is a short demo:

fn RotatePivotOnly obj rotation = (
   local rotValInv=inverse (rotation as quat)
   animate off in coordsys local obj.rotation*=RotValInv
   obj.objectoffsetpos*=RotValInv
   obj.objectoffsetrot*=RotValInv
)
delete objects
t = teapot pos:[10,20,30] rotation:(eulerangles 45 45 12)
b = box pos:[-10,-20,-30] parent:t
toolMode.coordsys #local
select t
for i = 1 to 5 do (
	format "rotation step %\n" i
	RotatePivotOnly t (EulerAngles 0 33 0)	-- rotate 33 deg around Y
	redrawViews()
	sleep 0.3
)

and yes, the children will be rotated, too, as they are relative to the the parent's pivot, so if you rotate the pivot this will be inherited.

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
0 Likes
Message 8 of 10

MartinBeh
Advisor
Advisor
Accepted solution

Here is a slightly more complex demo with a hierarchy of objects:

fn RotatePivotOnly obj rotation = (
   local rotValInv=inverse (rotation as quat)
   animate off in coordsys local obj.rotation*=RotValInv
   obj.objectoffsetpos*=RotValInv
   obj.objectoffsetrot*=RotValInv
)
delete objects
t = teapot pos:[10,20,30] rotation:(eulerangles 45 45 12)
-- quickly create a hierarchy below the teapot
for i = 1 to 5 do (
	p = random (-150*[1,1,1]) (150*[1,1,1])
	b = box pos:p 
	if i<5 then (
		b.parent = t
	) else (
		b.parent = $Box001
	)
)

-- rotate teapot's pivot only
toolMode.coordsys #local
select t
for i = 1 to 5 do (
	format "rotation step %\n" i
	-- collect all children
	local cArray = for c in t.children collect c
	-- unlink all children
	for c in cArray do c.parent = undefined
	RotatePivotOnly t (EulerAngles 0 33 0)	-- rotate 33 deg around Y
	-- re-parent the children again
	for c in cArray do c.parent = t	
	redrawViews()
	sleep 0.3
)
Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
Message 9 of 10

A娘
Advocate
Advocate

delete

0 Likes
Message 10 of 10

explosivepants
Explorer
Explorer

Thank you for this example. Looks like I need to unlink the child objects first before rotating then relink again.

0 Likes