Align pivot to face normal


#1

I am looking for help if someone is willing to code a solution that sets the pivot point to the normal direction of selected objects (as long as each object doesnt have more than 1 normal direction) or selected subobject.

I used this code:


fn RotatePivotOnly obj index=
(
rot = (matrixFromNormal (polyop.getFaceNormal obj index)*obj.transform ) as quat
rotValInv=inverse rot
animate off in coordsys local obj.rotation*=RotValInv
obj.objectoffsetrot*=RotValInv
obj.objectoffsetpos*=RotValInv
)
for a in selection do
(
setrefCoordSys #local
CenterPivot a
theNormal=polyop.getFaceNormal a 1
RotatePivotOnly a 1
)

… but it had limitations

  • worked on sub-object only. If I have tenth of objects, I have to select one at a time, enter sub-object and select that only polygon and apply the code.
  • if I run the code a second time, the pivot point that was facing properly, now points somewhere else.

#2

I found this thread with a solution from Denis

But I dont know what to type when I want to use alignPivotToFace on a current selection.

fn alignPivotToFace node face:1 type:#z_up = if iskindof node Editable_Poly do
 (
 	c = polyop.getfacecenter node face
 	n = polyop.getfacenormal node face
 	ftm = translate (matrixfromnormal n) c
 	
 	case type of 
 	(
 		#x_up: prerotateY ftm -90
 		#y_up: prerotateX ftm 90
 	)
 	
 	itm = ftm*(inverse node.transform)
 	node.transform = ftm
 	node.objectOffsetPos *= inverse itm
 	node.objectOffsetRot *= inverse itm.rotation
 	node.transform
 )
 
 /*** Sample ***/
 (
	delete objects
	p = teapot segments:3
	converttopoly p
	while p.numfaces > 0 do
	(
		polyop.detachfaces p 1 asNode:on
		alignPivotToFace objects[objects.count] type:#y_up
	)
	delete p
	gc light:on
)
 

#3

(
	fn AlignPivotToFace node face:1 type:#z_up = if iskindof node Editable_Poly do
	(
		c = polyop.getfacecenter node face
		n = polyop.getfacenormal node face
		ftm = translate (matrixfromnormal n) c

		case type of 
		(
			#x_up: prerotateY ftm -90
			#y_up: prerotateX ftm 90
		)

		itm = ftm*(inverse node.transform)
		node.transform = ftm
		node.objectOffsetPos *= inverse itm
		node.objectOffsetRot *= inverse itm.rotation
		node.transform
	)
	
	for a in selection do
	(
		AlignPivotToFace a face:1 type:#z_up
	)
)


#4

Thanks a lot, Miauu!

Works wonderful!