Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk 3ds® Max® / MaxScript / How do you get a bounding box's dimensions in relation to the object's local axis?
  RSS 2.0 ATOM  

How do you get a bounding box's dimensions in relation to the object's local axis?
Rate this thread
 
64663
 
Permlink of this thread  
avatar
  • Location: Seattle
  • Total Posts: 57
  • Joined: 27 January 2009 12:23 AM

I’m trying to figure out how to determine the length and width dimensions for all three sides of an object’s bounding box in relation to the object’s local axis. I’d like to know this so I can write a script that will automatically map objects based on these dimensions.

So, for example, if we have a box that is 30 w x 20 l x 10 h, I want to be able to examine the top of the bounding box and determine that that side of the bounding box is 30 x 20. And, if the box is rotated about the z axis 15 degrees, I’d like to still see that the top side of the bounding box is 30 x 20.

Or maybe I’m going about this the wrong way? Appreciate the help!

Tony



Tony
3ds Max Design 2012 and Inventor 2012
Win 7 Ultimate 64-bit, Intel Core i7, NVIDIA Quadro 4000, 24Gb RAM

Attachment Attachment
Attachment Attachment
Replies: 0
avatar
  • miauu
  • Posted: 22 February 2012 05:05 AM

Read this: http://forums.cgsociety.org/show...ight=nodeLocalBoundingBox



http://miauumaxscript.blogspot.com/

Replies: 1
/userdata/avatar/644hd3b9c_Tony-in-China-5.jpg

miauu—thank you! That helped a lot.

Based on your link and a lot of other searching, the code below is what I came up with today. It works just fine except in one circumstance: as long as I don’t have any primitive shapes anywhere in my scene. As soon as I add a primitive (ie, box or sphere), the script clogs at the first shape it encounters for which rotz=1 or rotx=1. The script doesn’t even GET to the primitive shape. Furthermore, even though the script runs through several shapes before it clogs, it doesn’t add the UVW Map modifiers to ANY of the shapes.

I’m sure I’m missing something simple. Here’s my code:

for obj in geometry do
 (

 
local xaxis=-- a 1 value will tell the script to align the UVW gizmo in the X direction
 local yaxis
=
 local zaxis
=
 local rotz
=-- a 1 value will tell the script to rotate the gizmo in the z direction 90 degrees
 local rotx
=

 current 
in coordsys local nodelocalboundingbox obj -- Get the bounding box extents in local space
 dims 
current[2] current[1] -- calculate the bounding box dimensions
 
 x
=dims.-- holds the size of the objects bounding box in the X direction
 y
=dims.y
 z
=dims.z
 
 
case of
 
(
 (
x==and y==z) : zaxis=1
 
(x==and y<z) :  zaxis=1
 
(y==and z<x) : xaxis=1
 
(z==and x<y) : yaxis=1
 
(x==and y>z) : xaxis=1
 
(y==and z>x) : yaxis=1
 
(z==and x>y) : zaxis=rotz=1
 
(x<and y<z) : zaxis=1
 
(x<and z<y) : yaxis=1
 
(z<and x<y) : xaxis=rotz=1
 
(y<and x<z) : zaxis=rotz=1
 
(y<and z<x) : xaxis=rotx=1
 
(z<and y<x) : xaxis=1
 
)

 print 
obj
 
if xaxis==do print "x axis=1"
 
if yaxis==do print "y axis=1"
 
if zaxis==do print "z axis=1"
 
if rotz==do print "rotz=1"
 
if rotx==do print "rotx=1"
 
 
select obj
 addmodifier 
$ (uvwmap maptype:4) -- adds a UVW Map modifier and sets the mapping to Box
 
$.modifiers[1].realworldmapsize=true -- sets the Real-World Map Size option
 
$.modifiers[1].length=$.modifiers[1].width=$.modifiers[1].height=1

 
print $.modifiers[1]
 
print"*****"
 
 
case of -- sets the axis alignment of the UVW Map modifier
 
(
 (
xaxis==1) : $.modifiers[1].axis=0
 
(yaxis==1) : $.modifiers[1].axis=1
 
(zaxis==1) : $.modifiers[1].axis=2
 
)
 
 case 
of -- rotates the gizmo axis if necessary
 
(
    --
THE SCRIPT CLOGS AT ONE OF THE NEXT TWO LINES IF THERE IS A PRIMITIVE IN THE SCENE

 
(rotx==1) : $.modifiers[1].gizmo.rotation=(angleaxis 90 [1,0,0]) as quat
 
(rotz==1) : $.modifiers[1].gizmo.rotation=(angleaxis 90 [0,0,1]) as quat
 
)

)
Author: TonyInSeattle

Replied: 22 February 2012 12:12 PM  
avatar
  • miauu
  • Posted: 22 February 2012 05:46 PM

I tested your code with 10 editable_poly object and one BOx primitive, but there is no error. Can you attach a sample scene, so I can test the code again?



http://miauumaxscript.blogspot.com/

Replies: 1
/userdata/avatar/644hd3b9c_Tony-in-China-5.jpg

First of all, thank you so much, miauu, for taking the time to help me with this issue. I really appreciate it. It’s invaluable to have a community of users willing to help like this…

OK, I’m attaching my latest script (which isn’t really changed from above much), but I’ve added print statements throughout to give me feedback while it runs. I’m also attaching a test max file for you. (See the attachments to my original post at the top of the page.)

Here are the steps to reproduce the error:

1. Open the max file. You’ll see a bunch of editable polys.
2. Run the script. It should run fine. You’ll get output in the maxscript listener.
3. Select all of the boxes and convert them to Editable Polys.
4. Create a box primitive anywhere.
5. Run the script again. This time you get an error at this line:

(rotx==1) : $.modifiers[1].gizmo.rotation=(angleaxis 90 [1,0,0]) as quat

(I’ve posted a JPG of the error that I get to my first post above.)

Notice a couple of things:

1. The script stopped at Box003 because that is the first shape for which rotx or rotz =1.
2. The loop went through Box001 and Box002 before stopping and without error, however, there are NO UVW Maps applied to those shapes. It ran, but didn’t work.
3. If you run the script again right now, it’ll work.

You can repeat this whole process by selecting all, converting everything to an editable poly again, and then adding another primitive.

I hope you can reproduce this on your end ... otherwise, I’ll feel like I’m going insane!

Author: TonyInSeattle

Replied: 23 February 2012 04:34 AM  
avatar
  • miauu
  • Posted: 24 February 2012 05:45 AM

Hi!
I managed to reproduce the error. It is strange, but it seems that max add a UVWMap mod, but… the mod is not there. I don’t know why? Try this code:

(
 for 
obj in geometry do
 (

 
local xaxis=-- a 1 value will tell the script to align the UVW gizmo in the X direction
 local yaxis
=
 local zaxis
=
 local rotz
=-- a 1 value will tell the script to rotate the gizmo in the z direction 90 degrees
 local rotx
=

 current 
in coordsys local nodelocalboundingbox obj -- Get the bounding box extents in local space
 
 
print "current = " current as string
 
 dims 
current[2] current[1] -- calculate the bounding box dimensions
 
 x
=dims.-- holds the size of the objects bounding box in the X direction
 y
=dims.y
 z
=dims.z
 
 
case of
 
(
 (
x==and y==z) : zaxis=1
 
(x==and y<z) :  zaxis=1
 
(y==and z<x) : xaxis=1
 
(z==and x<y) : yaxis=1
 
(x==and y>z) : xaxis=1
 
(y==and z>x) : yaxis=1
 
(z==and x>y) : zaxis=rotz=1
 
(x<and y<z) : zaxis=1
 
(x<and z<y) : yaxis=1
 
(z<and x<y) : xaxis=rotz=1
 
(y<and x<z) : zaxis=rotz=1
 
(y<and z<x) : xaxis=rotx=1
 
(z<and y<x) : xaxis=1
 
)

 print 
obj
 
if xaxis==do print "x axis=1"
 
if yaxis==do print "y axis=1"
 
if zaxis==do print "z axis=1"
 
if rotz==do print "rotz=1"
 
if rotx==do print "rotx=1"
 
 
select obj
 addmodifier obj 
(uvwmap maptype:4) -- adds a UVW Map modifier and sets the mapping to Box
 
 format 
"Name: % Mod: %\n " obj.name  obj.modifiers[1]
                
-- check if the mod is in the stack
 
if obj.modifiers[1] == uvwmap() then
 
(
 
obj.modifiers[1].realworldmapsize true -- sets the Real-World Map Size option
 obj
.modifiers[1].length obj.modifiers[1].width obj.modifiers[1].height=1
 
 
print"*****"
 
 
case of -- sets the axis alignment of the UVW Map modifier
 
(
 (
xaxis==1) : $.modifiers[1].axis=0
 
(yaxis==1) : $.modifiers[1].axis=1
 
(zaxis==1) : $.modifiers[1].axis=2
 
)
 
 case 
of -- rotates the gizmo axis if necessary
 
(
 (
rotx==1) : $.modifiers[1].gizmo.rotation=(angleaxis 90 [1,0,0]) as quat
 
(rotz==1) : $.modifiers[1].gizmo.rotation=(angleaxis 90 [0,0,1]) as quat
 
)
 )
 else
 (print 
"ERROR!!! No UVWMap mod !")
 )
)

It works evry time in my max.



http://miauumaxscript.blogspot.com/

Replies: 1
/userdata/avatar/644hd3b9c_Tony-in-China-5.jpg

Sorry for the slow reply… I had a very busy weekend and had to set this down for a couple of days.

Your script works for me, too. Silly question, but do you think that it has something to do with the starting and ending parentheses? My original script was missing them and your script has them…

Thank you!

Author: TonyInSeattle

Replied: 27 February 2012 04:41 AM  
avatar
  • miauu
  • Posted: 27 February 2012 07:31 AM

Your script works for me, too.

Glad to hear it. :)

Silly question, but do you think that it has something to do with the starting and ending parentheses? My original script was missing them and your script has them…

No. As I said I don’t know why max add the modifier, but the mod is not in the object modifier stack. I notice that sometimes max can’t remember the value of some varibles, but when I put

print var.value

before the row where I use this variable, the script works.

About starting and ending parenthesis. If you don’t use it all variables in the code are global. If you use it all variblaes are local, except the vars that are defined as global.

Try this:
Create two objects in the empty scene and execute this in the maxscript editor(no starting and ending parenthesis)

obj1 selection[1]
obj2 
selection[2]

Then write in the maxscript listener:

obj1.name

The result will be the name of the obj1.

But, if you use starting and ending parenthesis:

(
 
obj01 selection[1]
 obj02 
selection[2]
)

then the

obj1.name

will be—Unknown property: “name” in undefined

As you know all global variables will remain until you restart 3ds max.



http://miauumaxscript.blogspot.com/

Replies: 1
/userdata/avatar/644hd3b9c_Tony-in-China-5.jpg

Ahh… makes sense. I watched a series of videos about Maxscript so I got the whole “local” and “global” variable thing. Didn’t realize the parentheses had anything to do with it, but that makes sense, now. Just like variables defined in a for loop within parens, they are not recognized outside the parens… Thanks for the explanation!

Regarding the script. I got my final version working… so it’s there. Not sure why Maxscript seemed to choke on my first version…

Again, I really appreciate your help and time… I hope I can return the favor to you or someone else on the Area soon!

Tony

Author: TonyInSeattle

Replied: 27 February 2012 08:58 AM  
avatar
  • miauu
  • Posted: 28 February 2012 01:53 AM

Glad I help. :)



http://miauumaxscript.blogspot.com/

Replies: 0