Rotate pivot to object normal?

Rotate pivot to object normal?

chroma123
Advocate Advocate
3,235 Views
6 Replies
Message 1 of 7

Rotate pivot to object normal?

chroma123
Advocate
Advocate

Hi!

I have a case where I want objects imported from ArchiCAD to have pivots aligned to a certain way.

Every object should have the pivot facing this direction on itself: (these are detached objects from building walls)

 

d1.JPG

 

I got a script to work, at least to an extent, which was modified from somewhere I now don't remember (sorry):

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_in: prerotateX ftm -90
)

itm = ftm*(inverse node.transform)
node.transform = ftm
node.objectOffsetPos *= inverse itm
node.objectOffsetRot *= inverse itm.rotation
node.transform

)


p = $
alignPivotToFace p type:#x_up

This script "consistently" makes my pivot like this:

d2.JPG

 

 

Which is a good starting point, I only need to get it aligned to leftmost down boundary box, and turned -90 degrees on the z axis. I didn't get this to work in the script, but I'm sure it works with an add of a few lines (Help appreciated here too). So far so good, as it doesn't seem like an unsolveable problem.

 

But when I'm executing the script, it seems like some polys from ArchiCAD aren't following the same terms.

Like this ****:

d3.JPG

 

How can I get this to work...? Any way to force / embed a z up position in the "bad" polys, before executing the script?

 

I'm lost now, and banging my head against the wall. Big thanks!

0 Likes
Accepted solutions (1)
3,236 Views
6 Replies
Replies (6)
Message 2 of 7

har1sf0x
Advocate
Advocate

Hello,

 

  I am not sure i have understood your concept but the following modifications might work:

fn alignPivotToFace node face:1 type:#z_up =
if iskindof node Editable_Poly do
(
/*
	If your pivot is in the right position when you import your model do not change anything.
	If you want to use for instance the position of the 1st vertex change "c = node.pos" with
	"c = polyop.getVert node 1"
*/
c = node.pos
n = polyop.getfacenormal node face
ftm = translate (matrixfromnormal n) c

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


itm = ftm*(inverse node.transform)
node.transform = ftm
node.objectOffsetPos *= inverse itm
node.objectOffsetRot *= inverse itm.rotation
node.transform

)


p = $
alignPivotToFace p type:#x_up

 

Enjoy,

har1sf0x

0 Likes
Message 3 of 7

chroma123
Advocate
Advocate

Hi, thanks for response!

 

It do align better, not it's not good enough.

Below, I've selected four detached elements, and you can see how inconsistently it works.

I can run the script once for each object, so I don't need any batch or while loop written in.

However, I really wanted this pivot alignment process to get automated through a script, but I'm no longer sure if it's possible at all.

 

 

d4.JPG

 

 

 

This is how every object should have it's pivot aligned. Left (I know that's a tricky one, since rotation of the objects can vary)

and with the pivot just like this.

d5.JPG

 

 

 

And seen in the bigger picture:

 

d6.JPG

0 Likes
Message 4 of 7

denisT.MaxDoctor
Advisor
Advisor

@chroma123 wrote:

Hi!

I have a case where I want objects imported from ArchiCAD to have pivots aligned to a certain way.

Every object should have the pivot facing this direction on itself: (these are detached objects from building walls)

 

I got a script to work, at least to an extent, which was modified from somewhere I now don't remember (sorry)...

 

 

 

this is my function shown once on CGTalk

 

in your case it can be modified as:

mapped fn alignPivotToFaceZUp node face:1 = if iskindof node Editable_Poly do
(
	c = polyop.getfacecenter node face
	n = polyop.getfacenormal node face

	side = cross z_axis n
	up = cross n side

	ftm = matrix3 n side up c
	
	itm = ftm*(inverse node.transform)
	node.transform = ftm
	node.objectOffsetPos *= inverse itm
	node.objectOffsetRot *= inverse itm.rotation
	
	bb = nodeGetBoundingBox node ftm
	node.pivot = bb[1] * ftm
	
	node.transform
)

/* using 
alignPivotToFaceZUp <selection or list of your wall objects>
*/

 

 (of course if we expect our "walls" vertical 

Message 5 of 7

chroma123
Advocate
Advocate

Wow, thanks for clearing that up! Respect!

 

And for what you just wrote: What an amazing script. How did you learn to be such a guru?

It almost does what I want it to do, from what I've tested now.

Only thing is that it should be turned 90 degrees counterclockwise, so that the red arrow will be where the green is now (along the edge of the object).

Do you also happen to have a good solution to that?

 

d7.JPG

0 Likes
Message 6 of 7

denisT.MaxDoctor
Advisor
Advisor
Accepted solution
mapped fn alignPivotToFaceZUp node face:1 = if iskindof node Editable_Poly do
(
	c = polyop.getfacecenter node face
	n = polyop.getfacenormal node face

	side = cross z_axis n
	up = cross n side

	ftm = matrix3 n side up c

	p = (nodeGetBoundingBox node ftm)[1] * ftm
	prerotateZ ftm -90 
	
	itm = ftm*(inverse node.transform)
	node.transform = ftm
	node.objectOffsetPos *= inverse itm
	node.objectOffsetRot *= inverse itm.rotation
	
	node.pivot = p
	node.transform
)
0 Likes
Message 7 of 7

chroma123
Advocate
Advocate

Thanks! I had to change -90 to 90, and then it aligned as it was supposed to. Big thanks again!

0 Likes