Notifications
Clear all

[Closed] How can I calculate the camera-oriented bounding box of an object?

I am attempting export a sequence of a rotating teapot. Each frame is saved as a JPEG image, and I am trying to write the pixel coordinate 2D bbox (xmin, xmax, ymin, ymax) of the teapot from the camera’s perspective to a corresponding text file.

In the example picture here, I would be looking to output bbox values of [220,1200,200,830] for this frame.

I am comfortable with Python, but (as you can tell), MaxScript is new to me.

Thanks!!

bbox_teapot

2 Replies
(
	
	
	
	fn GetScreenBounds obj =
	(
		maxy = maxx = -99999999.0;
		miny = minx = 99999999.0;
		
		gw.setTransform(Matrix3 1);
		
		msh = snapshotasmesh obj;
		for v = 1 to msh.numverts  do
		(
			spos = gw.hTransPoint (getvert msh v);
			if spos.x > maxx then maxx = spos.x;
			if spos.x < minx then minx = spos.x;	
			if spos.y > maxy then maxy = spos.y;
			if spos.y < miny then miny = spos.y;	
		)
		delete msh;
		[minx,maxx,maxy,miny]	
	)
	
	
	b = GetScreenBounds $;
	
	
	gw.setTransform(Matrix3 1)
	rect = (box2 b.x b.w (b.y - b.x) (b.z - b.w));
	gw.hrect rect red
	eRect = rect 
	eRect.right += 1
	eRect.bottom += 1
	gw.enlargeUpdateRect eRect 
	gw.updateScreen()

)

Amazing @Klvnk, that works perfectly.