Helping a hopeless maxscripting guy

Hello,
I’ll need some help with this from the experts out there. I was looking for a way to realign the bounding box of an object to his main “physical” axis (for example as it looks when rotating a primitive box, so the bbox stick together) and I found this thread on cgtalk:

http://forums.cgsociety.org/showthread.php?f=98&t=617614&highlight=align+axis

There is a couple of pieces of code but I don’t know how to apply in my objects. Could be that code easily converted in “button like” or “run script” action?
I must admit my ignorance here.

Thanks!
Manuel

Hi manuel,
you can use reset XForm utility for this,
or if you wanna to use script code, just open it in max script window, select all
and simple drag text to your main toolbar, it creates button.
hope it helps
filip

Thanks Filip for the info. Unfortunately I couldn’t work it out.
Draggin code tex to create a button is a killler tip, but when I try to execute the code I get a maxscrip error that I don’t know how to rewrite.
The Xform utility here doesn’t work as far as I know, because the objects aren’t primitives but detached elements from a big mesh.

I wish this was around for none-poly models. I’ve got nPower and would like to keep them as bReps.
Lots of times I have a door that comes in already open to an unspecified angle. It’s pretty much rectangular so I wouldn’t think it to be hard to have a script automatically align to the smallest fitting bounding box. That would make applying UV maps much easier to those objects. I’ve gotta always rotate those UV maps. I’d rather use the same UV maps for both doors and the body, assuming those are the same texture/material.
Rotating the pivot/bounding box would allow me to do that.

Anyone know if that script is reasonably possible from right within max? If so, maybe I’ll go and try to create it. I think it might be far beyond my scripting skills though. It takes me forever because I usually don’t even know what commands are possible and the help file isn’t always helpful.

Bobo’s got a solution :slight_smile: LOL, i guess you already saw that though…

http://forums.cgsociety.org/showthread.php?f=98&t=617614&highlight=align+axis

hmm, I’m getting an error with that code.

I just have to add, do at the end of all that to run it right?

Evaluate the code…IE:example (you can either C/P this to a new script or C/P in the macrorecorder pane of the listener, select all the text from the top down and hit numpad+enter to run)

fn AlignBBoxToFirstNormal theObject = (
 theCurrentTM = theObject.transform
 theMesh = snapshotasmesh theObject
 theZ = normalize (getFaceNormal theMesh 1)
 theFace = getFace theMesh 1
 theY = normalize ((getVert theMesh theFace.y)-(getVert theMesh theFace.x))
 theX = normalize (cross theY theZ)
 theTM = matrix3 theX theY theZ theCurrentTM.row4
 theObject.transform *= inverse theTM
 resetXForm theObject
 convertToMesh theObject
 theObject.transform = theTM
  )
```then select object 

run the function in the listener "AlignBBoxToFirstNormal $" (without quotes)

I tested with a box rotated 20 degrees on Z-axis then utilities reset X-form to align the bounding box back to world space. Then I ran the function and the bounding box realigns to the box.

Hi, I’ve also try that code without success. Could somebody more experience check it out and give us a tip?

Edit: Sorry, I send my message at the same time that your example. I’ll try it

ok, well, that Was simple in getting the script to run, only…it didn’t move the pivot at all. All it seemed to do was convert my part to editable mesh, which is Totally not what I wanted.

I’m sure if that was a limitation it wouldn’t be too hard just to have it duplicate to a new part, convert the new part to editable mesh, align that one, then use that information on the first part, and delete the new part. Terrible sentence. haha
But I can’t get it to even move the pivot at all. And by move I really mean rotate. i don’t care if it moves or not other than rotation, but itsn’t that really suppose to rotate the pivot so the bounding box is rotated to what the part should really be? I think that’s what we are all looking for.

I’m testing it on a cylindrical shape though, so maybe that’s the problem, however it should probably work on most anything, right?

The reset xform should realign the pivot. that is strange. Did it realign the bounbing box?

As for it not working maybe try the first piece of code: (this one works with a cylinder as the first did not)

fn AlignBBoxToFirstNormal theObject = 
  (
  theCurrentTM = theObject.transform --this is the current transformation
  theMesh = snapshotasmesh theObject --get the world space TriMesh
  theTM = matrixFromNormal (getFaceNormal theMesh 1) --build a matrix from normal
  delete theMesh --delete the TriMesh from memory
   theTM.row4 = theCurrentTM.row4 --set the position of the new matrix to the old pos
  theObject.transform *= inverse theTM --transform out of world space 
  resetXForm theObject --reset the XForm
  convertToMesh theObject --collapse the stack
  theObject.transform = theTM --transform back into world space
  )

I guess the bounding box alignment really depends on the topography of the node and how you want it to translate. As it is the pivot should be aligned with the bbox.

OK, yeah, that worked. Only, for odd shapes it doesn’t really do what you would want it to do.

I’d still like something that chose the absolute smallest bounding box, not one based off of a certain face. I’m not exactly sure how this one works. The box Did get smaller, a little, but it could have been much smaller.

Is there a rogue vertex anywhere that may be keeping it from conforming tighter to the object? I speak for myself, I have done that more often than I care to admit:)

Otherwise it must just be the shape. The script, as far as I can tell, only reads the one face to get the transform matrix so it knows how to realign the bbox to world space.

yeah, I wanted to test it out so I used a bit of a squigly shape. I grabbed a hose from an engineering part that has 2 bends in it.
Even in rhino i have to do it manually by chosing 3 points to align through.
That works Really close though. Although, I can’t translate that info to Max, so it doesn’t help.

Hi Jrandom, I try the first piece of code that you sent, following your instructions and it is working for me perfectly. I was applying it to a cilinders with no problem. Only, I would need to apply to several objects at the same time, could you help me on that? I suposse it is a matter of add a line in the code but I’m very poor with this issue. Many thanks, you make my day.

Andrew, I don’t exactly follow what you want but maybe the Soulburn scrips from Neil Blevins could help you. There is a tool included that is able to align the axis of the object in many pre-stablished (I think 27) positions with just one click. Maybe you know these already, anyway I put here the link
http://www.neilblevins.com/soulburnscripts/soulburnscripts.htm

from his web: “putPivot. Places the pivot of the selected objects in one of 27 spots such as center, bottom, top, high left side, etc”

Regards


try(destroyDialog AlignBboxToObject)Catch()
--arrays
Obj_arr = #()
list_arr = #()
--functions
fn AlignBBoxToFirstNormalfn1 theObject = --Function written by Borislav Petrov
      (
          theCurrentTM = theObject.transform --this is the current transformation
          theMesh = snapshotasmesh theObject --get the world space TriMesh
          theTM = matrixFromNormal (getFaceNormal theMesh 1) --build a matrix from normal
          delete theMesh --delete the TriMesh from memory
           theTM.row4 = theCurrentTM.row4 --set the position of the new matrix to the old pos
          theObject.transform *= inverse theTM --transform out of world space 
          resetXForm theObject --reset the XForm
          convertToMesh theObject --collapse the stack
          theObject.transform = theTM --transform back into world space
      )
 fn AlignBBoxToFirstNormalfn2 theObject = --Function written by Borislav Petrov
     (
         theCurrentTM = theObject.transform
         theMesh = snapshotasmesh theObject
         theZ = normalize (getFaceNormal theMesh 1)
         theFace = getFace theMesh 1
         theY = normalize ((getVert theMesh theFace.y)-(getVert theMesh theFace.x))
         theX = normalize (cross theY theZ)
         theTM = matrix3 theX theY theZ theCurrentTM.row4
         theObject.transform *= inverse theTM
         resetXForm theObject
         convertToMesh theObject
         theObject.transform = theTM
      ) 

fn Obj_filt obj = isKindOf obj GeometryClass 
fn objList =
    (
        list_arr = for o in Obj_arr where isValidNode (obj = getNodeByName o) collect obj
    )
--rollouts
rollout AlignBboxToObject "Align Bounding Box" width:200 height:200
(
    button btn_Load "load"
    ListBox lst_objs
    button btn_doFn1 "Function 1" across:2
    button btn_doFn2 "Function 2"

    on btn_Load pressed do
        (
            Obj_arr = #()
            SelObj = selectByName title:"List Objects" filter:Obj_filt()
            if SelObj != undefined do
                (
                    for i in SelObj do
                        (    
                            append Obj_arr i.name

                            objList()
                        )
                )
            if Obj_arr.count != 0 then
                (
                lst_objs.items = Obj_arr
                )
        )
    on btn_doFn1 pressed do
        ( 
            deselect
            for o in list_arr do
            (
                AlignBBoxToFirstNormalfn1 o
            )
        )
    on btn_doFn2 pressed do
        ( 
            deselect
            for o in list_arr do
            (
                AlignBBoxToFirstNormalfn2 o
            )
        )
    on AlignBboxToObject closed do
        (
            Obj_arr = #()
            list_arr = #()
        )
)
CreateDialog  AlignBboxToObject  width:200 height:200

Both functions are included. So if one doesn’t work use the other.

Thank you man!! I test it in my scene with cylinders and the Function 2 works perfect.
Once again the people of this forum is amazing! I hope to be able to help back someday.

Manuel