Align object's pivot to object's orientation

Align object's pivot to object's orientation

kenc
Advocate Advocate
9,295 Views
60 Replies
Message 1 of 61

Align object's pivot to object's orientation

kenc
Advocate
Advocate

I have a DXF from Softplan that I import into max. When the objects come in they all have the same pivot rotation orientation. I then center the pivots to each object's center as seen in image1.jpg

 

What I need to do is by maxscript is to align the object's pivot to the object's orientation. In image2.jpg the pivot for an object does not align with the object but rather at 45 degrees.

 

In image3.jpg this is how I need to rotate the pivot to align with the object.

 

Help would be much appreciated.

 

TIA

0 Likes
Accepted solutions (3)
9,296 Views
60 Replies
Replies (60)
Message 21 of 61

kenc
Advocate
Advocate

@leeminardi that script didn't work. X and Y are not in the x y plane.

Here's a max file.

I imported using the dialog shown in the JPG

I delete everything except the 2 PANEL and 2 SURFACE meshes. So now all I have is the PANEL and SURFACE meshes. I froze the SURFACE meshes and only there so you can see what the orientation of the PANELS should be.

I attach PANEL0 to PANEL.

I weld the vertices then detach by element so all the doors are separate. 

You can see that the pivots for all the doors are pointing in the same direction.

Some of the doors have their pivots perpendicular to the door and the rest are not. The ones not perpendicular are the ones that need the pivots rotated so they are perpendicular.

0 Likes
Message 22 of 61

leeminardi
Mentor
Mentor

@kenc the file appears to have all the panel doors as one object.  I thought they were going to be separate objects.

Checking, as you did, "Create one scene object..." sould make them separate objects.

 

leeminardi_0-1711244298081.png

@istan if the geometry and pivots are weirdly skewed in 3D usig matrices could work but I think quats are the safer, and maybe easier, route in that they avoid the potential for gimbal lock.

lee.minardi
0 Likes
Message 23 of 61

kenc
Advocate
Advocate

@leeminardi oh gees I attached the wrong file. Try this one

0 Likes
Message 24 of 61

denisT.MaxDoctor
Advisor
Advisor

@kenc wrote:

I have a DXF from Softplan that I import into max. When the objects come in they all have the same pivot rotation orientation. I then center the pivots to each object's center as seen in image1.jpg

 

 


In this particular case, the solution may be:

fn setPivotRotation obj tm =
(
	worldAlignPivot obj
	local rot = inverse tm.rotationpart
	
	animate off in coordsys local obj.rotation *= rot
	obj.objectoffsetrot *= rot
	obj.objectoffsetpos *= rot
	obj.objecttransform
)

mapped fn alignPivotToFace obj face:#max zUp:on = if iskindof obj Editable_Mesh do
(
	fn getMaxFace obj = 
	(
		fmax = meshop.getfacearea obj 1
		i = 1
		for k = 2 to obj.numfaces where (area = meshop.getfacearea obj k) > fmax do
		(
			fmax = area
			i = k
		)
		i
	) 
	fn matrixZUp tm =
	(
		side = normalize (cross z_axis tm[3])
		front = cross side z_axis
		
		matrix3 front side z_axis tm.pos
	)
	
	if face == #max do face = getMaxFace obj
	norm = getfacenormal obj face	
	tm = matrixfromnormal norm
	
	if zUp do
	(
		tm = matrixZUp tm
	)
	
	centerPivot obj
	setPivotRotation obj tm
)

alignPivotToFace geometry

 

0 Likes
Message 25 of 61

kenc
Advocate
Advocate

@denisT.MaxDoctor thank you. I'll give it a try.

0 Likes
Message 26 of 61

kenc
Advocate
Advocate

@denisT.MaxDoctor Almost. Is it possible to adjust the script so the y-axis is perpendicular to the doors?

0 Likes
Message 27 of 61

denisT.MaxDoctor
Advisor
Advisor
matrix3 side front z_axis tm.pos

and you probably have to change the sign of “side” or “front” to make the right order

0 Likes
Message 28 of 61

kenc
Advocate
Advocate

@denisT.MaxDoctor -front, -side and -front/-side don't work. I am going to go through your code and try to wrap my head around.

0 Likes
Message 29 of 61

denisT.MaxDoctor
Advisor
Advisor

@kenc wrote:

@denisT.MaxDoctor -front, -side and -front/-side don't work. I am going to go through your code and try to wrap my head around.


fn setPivotRotation obj tm =
(
	worldAlignPivot obj
	local rot = inverse tm.rotationpart
	
	animate off in coordsys local obj.rotation *= rot
	obj.objectoffsetrot *= rot
	obj.objectoffsetpos *= rot
	obj.objecttransform
)

mapped fn alignPivotToFace obj face:#max zUp:on = if iskindof obj Editable_Mesh do
(
	fn getMaxFace obj = 
	(
		fmax = meshop.getfacearea obj 1
		i = 1
		for k = 2 to obj.numfaces where (area = meshop.getfacearea obj k) > fmax do
		(
			fmax = area
			i = k
		)
		i
	) 
	fn matrixZUp tm =
	(
		side = normalize (cross z_axis tm[3])
		front = cross z_axis side
		
		matrix3 side front z_axis tm.pos
	)
	
	if face == #max do face = getMaxFace obj
	norm = getfacenormal obj face	
	tm = matrixfromnormal norm
	
	if zUp do
	(
		tm = matrixZUp tm
	)
	
	centerPivot obj
	setPivotRotation obj tm
)

alignPivotToFace geometry
0 Likes
Message 30 of 61

istan
Advisor
Advisor

@leeminardi wrote:

@istan if the geometry and pivots are weirdly skewed in 3D usig matrices could work but I think quats are the safer, and maybe easier, route in that they avoid the potential for gimbal lock.


quod erat demonstrandum: Look at code from Denis..

0 Likes
Message 31 of 61

denisT.MaxDoctor
Advisor
Advisor

Since we are looking for a Z-Up matrix, a good improvement to the above solution might be to search only all faces orthogonal to the Z-axis.

0 Likes
Message 32 of 61

domo.spaji
Advisor
Advisor

Than author would came with something like:

c=cylinder sides:4

😁

 

I meant to ask him is that aligned to object orientation.

 

Also, I meant to ask him why does he need this.

I kind of doubt he have "real" reason, even wrong one...

 

But I rarely get answers here...

Wonder why 😄

0 Likes
Message 33 of 61

denisT.MaxDoctor
Advisor
Advisor

@domo.spaji wrote:

 

I meant to ask him why does he need this.

I kind of doubt he have "real" reason, even wrong one...

 

But I rarely get answers here...

Wonder why 😄


Perhaps your questions are philosophical in nature.

 

 

 

Who needs all the windows facing north?

Message 34 of 61

domo.spaji
Advisor
Advisor

Also, I meant to ask author of near by post how did he  managed to get "a big" working script from "AI".

I'm not surprised  at A"I" knowledge in script language, but the other one...

Not working exactly as commented but. i believe that part is human work 😁

0 Likes
Message 35 of 61

domo.spaji
Advisor
Advisor

😀

 


@denisT.MaxDoctor wrote:

Who needs all the windows facing north?

 


Didnt get it was really question...

And no way I would try to think about it or answer it!

I really doubt I would get it if somebody asked me out of all this orientation context...

 

Even checked in translator if north is still "north"😂

I'm laughing from the moment it hit me

0 Likes
Message 36 of 61

kenc
Advocate
Advocate

@domo.spaji 

 

@domo.spaji wrote:

 

I meant to ask him why does he need this.

I kind of doubt he have "real" reason, even wrong one...

 

The cabinet doors will be swappable in unreal. Y-axis is front and pivot is at center back. There is a lot more to the MAX to UNREAL workflow we have and too much to explain.

0 Likes
Message 37 of 61

kenc
Advocate
Advocate

@denisT.MaxDoctor thank you. The door pivots are perpendicular to the door orientation now.

I went through your code and I can not understand what is happening exactly. I'll have to come back to it when I have time. Right now I'm being pressured to get this working.

How do I rotate the pivot about the z-axis say by 180 degrees?

0 Likes
Message 38 of 61

denisT.MaxDoctor
Advisor
Advisor
prerotatez tm 180
0 Likes
Message 39 of 61

denisT.MaxDoctor
Advisor
Advisor
tm = prerotatez tm 180
0 Likes
Message 40 of 61

kenc
Advocate
Advocate

@denisT.MaxDoctor more specifically. In the attached image, the z-axis of the pivot needs to be rotated by 180 so the y-axis is pointing out away from the cabinet SURFACE. What I do is shoot a ray 2 inches in both directions in the y-axis. If the ray strikes SURFACE then I know the pivot needs to be rotated 180 in the z-axis. I already have the code setup to detect if it needs to be rotated. I just need to rotate it now.

0 Likes