Align object's pivot to object's orientation

Align object's pivot to object's orientation

kenc
Advocate Advocate
9,262 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,263 Views
60 Replies
Replies (60)
Message 2 of 61

denisT.MaxDoctor
Advisor
Advisor

Is only DXF format available for export from Softplan? 
If we are talking about AutoCAD DXF, there should be no problem with pivot (tm) orientation.

 

0 Likes
Message 3 of 61

kenc
Advocate
Advocate

it is exported directly from softplan. I just asked the cad guys to export to 3ds and fbx

0 Likes
Message 4 of 61

denisT.MaxDoctor
Advisor
Advisor

Usually, the "oriented bounding box" method is used to solve this problem. As far as I know, there is no built-in solution for this, but I remember some scripts to do the job.

 

For 2D case, the script is quite simple.

0 Likes
Message 5 of 61

denisT.MaxDoctor
Advisor
Advisor

What other formats the Softplan can export? Can it do FBX?

0 Likes
Message 6 of 61

kenc
Advocate
Advocate

@denisT.MaxDoctor  no FBX. Also has 3ds which is no good all the meshes are called "Mesh". Checking DWG now

 

0 Likes
Message 7 of 61

kenc
Advocate
Advocate

@denisT.MaxDoctor DWG and DXF output exactly the same thing

 

0 Likes
Message 8 of 61

leeminardi
Mentor
Mentor

What happens when you use Akign to object?

leeminardi_0-1711145622922.png

 

Are the object composed  of lines or 2d, or 3D objects.

 

A simple script could be used that prompts the user to specify a pivot origin, a point on the x axis, and a point on the xy plane and then reorient the pivot and center it.   Would this be acceptable?

 

 

lee.minardi
0 Likes
Message 9 of 61

kenc
Advocate
Advocate

@leeminardi this needs to be run by maxscript. No user input.

The Align To Object does not work

0 Likes
Message 10 of 61

leeminardi
Mentor
Mentor

Can you post a sample dxf file?

 

lee.minardi
0 Likes
Message 11 of 61

klvnk
Collaborator
Collaborator

getting a tight object aligned bounding box and therefore it's "orientation"/pivot for all general cases is a non trivial operation. Though in special cases it maybe easier.

0 Likes
Message 12 of 61

istan
Advisor
Advisor

@klvnk wrote:

Though in special cases it maybe easier.


In this particular case, when objects always look like in image1 (a "thin" object), at least one vector could be easily aligned per MXS.

0 Likes
Message 13 of 61

leeminardi
Mentor
Mentor

It looks like the application is related to cabinetry and therefore the pivot Z axis is correct.  So yes, only one vector is needed to define orientation.  A sample file would be helpful. Searching for consecutive vertices that have the same z coordinate may be sufficient to determine the vector.

lee.minardi
0 Likes
Message 14 of 61

kenc
Advocate
Advocate

@leeminardi actually it's an entire house. The items in my screenshots are the kitchen cabinets.

I'm not at the office but I will remote in and get the dxf.

0 Likes
Message 15 of 61

leeminardi
Mentor
Mentor

I created a couple of "panels" (boxes) in AutoCAD and determined that the first two vertices could be used to define the orientation of the pivot.

Try the following program and let me know how it works on your file.

-- Modifies selected object pivots such that their
-- first two vertices define the local x axis and
-- the local z axis is in the world z axis direction.
obj = selection
for o in obj do 
(
v1 = getVert o 1
v2 = getVert o 2
xaxis = normalize(v2 - v1)
yaxis = normalize(cross [0,0,1] xaxis)
a = acos(dot [1,0,0] yaxis)
o.transform = o.transform * (RotateZMatrix a)
resetxform o
maxOps.CollapseNodeTo o 1 true
o.transform = o.transform * (RotateZMatrix -a)
)

 

lee.minardi
0 Likes
Message 16 of 61

kenc
Advocate
Advocate

@leeminardi apparently all our servers are under maintenance this weekend so I can't upload the dxf till Monday.

 

I will try out your script.

 

How do I rotate just the pivot of an object without rotating the object?

0 Likes
Message 17 of 61

leeminardi
Mentor
Mentor

@kenc wrote:

 

How do I rotate just the pivot of an object without rotating the object?


leeminardi_0-1711226262004.png

 

lee.minardi
0 Likes
Message 18 of 61

kenc
Advocate
Advocate

Sorry I should have been more specific. Rotate the pivot with maxscript.

Everything I do is by maxscript only.

0 Likes
Message 19 of 61

leeminardi
Mentor
Mentor

I do it by rotating the object orthogonal to the world reset the transform and rotate the object back.

Take a look at my code.

[Edit]

Since in your case we just need to rotate about the z axis we can use a z-axis matrix rotation. If the rotation were more complex you would have to use quats.

lee.minardi
0 Likes
Message 20 of 61

istan
Advisor
Advisor

@leeminardi wrote:

 If the rotation were more complex you would have to use quats.


Remark: there's no need to use quats, if you would omit using angle values in your calculation.. there exists another solution using simple matrix calculations 😉

0 Likes