python bounding box display

   6229   3   0
User Avatar
Member
79 posts
Joined: 3月 2012
Offline
what would be the best way to visualize the information in a bounding box using python

I found this class
hou.BoundingBox()

dont know how to use it.
how can I call my geo inside it? or how can I tell my geo to use this and create a bounding box, is this a right approach?
FX - Artist / TD
User Avatar
Member
23 posts
Joined: 3月 2013
Offline
I've found two ways you can call your geo into the hou.BoundingBox() class. If you plug your geo node into a python sop, you can call the BoundingBox class like this:

node = hou.pwd()
geo = node.geometry()

bbox = geo.boundingBox()
print bbox 

#print information about bounding box:
print "bbox minimum and maximum bounds: " + str(bbox) 
print "bbox minvec: " + str(bbox.minvec())
print "bbox maxvec: " + str(bbox.minvec())
print "bbox size in each x,y,z axes: " + str(bbox.sizevec())
print "bbox center: " + str(bbox.center())

Or specify a sop directly, which wouldn't require having it plugged into the python sop.

bboxnode = hou.node('../OUT') 
bboxgeo = bboxnode.geometry()

bbox = bboxgeo.boundingBox()


I don't see a way for the class to automatically create a bounding box for you in the viewport, but it gives you the information required to do so.

geo.boundingBox() returns a type class ‘hou.BoundingBox’ so you cannot iterate the array directly to create points but you can get that information from minvec() and maxvec(), both of which return vectors. By combing the x, y and z values from min and max vec, you can construct the bounding box corners with points:

# create points at bounding box corners:
a = bbox.minvec()
b = (bbox.minvec()[0], bbox.maxvec()[1], bbox.minvec()[2])
c = (bbox.maxvec()[0], bbox.maxvec()[1], bbox.minvec()[2])
d = (bbox.maxvec()[0], bbox.minvec()[1], bbox.minvec()[2])
e = bbox.maxvec()
f = (bbox.maxvec()[0], bbox.minvec()[1], bbox.maxvec()[2])
g = (bbox.minvec()[0], bbox.minvec()[1], bbox.maxvec()[2])
h = (bbox.minvec()[0], bbox.maxvec()[1], bbox.maxvec()[2])

corners = [a,b,c,d,e,f,g,h]

for position in corners:
    point = geo.createPoint()
    point.setPosition(position)

If you wanted to, you could then draw quads from the points as well, or create points the same way but from the other BoundingBox() methods, like center().

If your goal is to just create a bounding box for visualization in the viewport, you can plug your geometry into a box sop and it will create the box with the size of the bounding box of the input geometry.

I've attached a hip file which shows these different methods. I threw a transform below the test geo so you can see the bounding box react to changes in translation and rotation. Looks like both methods (python and box sop) don't create bounding boxes oriented to the geometry, which would have to be a different approach if that is your goal.
Edited by davidderwin - 2017年5月12日 14:31:05

Attachments:
python_bounding_box.hiplc (218.8 KB)

User Avatar
Member
79 posts
Joined: 3月 2012
Offline
this make total sense, and thats exactly what I wanted to understand, very helpful thanks.

I believe I can use these points and create primitives to represent a bounding box or may be I don't need a box just a center point would work, the goal is to control the visibility of my object geometry with a bounding box or like packed object(as a point), for which now I need to find a way to create a condition where I can still keep the information of my geometry but display it as a box or point.
FX - Artist / TD
User Avatar
Member
1 posts
Joined: 4月 2017
Offline
Do you guys perhaps know of a way to get the total bounds of all objects inside of a subnet? it seems like boundingBox() is sop level only.
  • Quick Links