Figured it out… curious to know if I can make it any better than this…
Make a Camera001 camera and point it at a Teapot001 (link some other objects to it too) then run this.
targetCam = $Camera001
targetObj = $Teapot001
hierarchyObjs = (join #() targetObj)
function getLinePlaneIntersect p3LinePoint_1 p3LinePoint_2 rPlane =
(
--function to get point3 where a line defined by start and end point intersects with an infinite plane
--thanks Enrico Gullotti
local p3LineVector = p3LinePoint_2 - p3LinePoint_1
local p3Vector_1 = rPlane.Pos - p3LinePoint_1
local fNumer = dot rPlane.dir p3Vector_1
local fDenom = dot rPlane.dir p3LineVector
(p3LinePoint_1 + ((fNumer / fDenom) * p3LineVector))
)
fn getBoundingBoxVerts objs =
(
--Get bounding box corners as point3 array for all objects
vertArray = #()
for obj in objs do
(
local bb = nodeGetBoundingBox obj obj.transform
local bLength = (bb[2]-bb[1]).x
local bWidth =(bb[2]-bb[1]).y
local bHeight = (bb[2]-bb[1]).z
local vArray = #([bLength / 2, bWidth / 2, bHeight / 2],
[-bLength / 2, bWidth / 2, bHeight / 2],
[bLength / 2, -bWidth / 2, bHeight / 2],
[bLength / 2, bWidth / 2, -bHeight / 2],
[-bLength / 2, -bWidth / 2, bHeight / 2],
[bLength / 2, -bWidth / 2, -bHeight / 2],
[-bLength / 2, bWidth / 2, -bHeight / 2],
[-bLength / 2, -bWidth / 2, -bHeight / 2]
)
join vertArray (for v in vArray collect (v * obj.transform + (obj.center - obj.pivot)))
)
vertArray
)
theZ = normalize (targetCam.pos-targetObj.pos)
theX = normalize (cross theZ [0,0,1] )
theY = normalize (cross theX theZ)
card = Plane name:"Card"
card.transform = matrix3 theX theY theZ targetObj.pos
bmin = [0,0]
bmax = [0,0]
vertArray = getBoundingBoxVerts hierarchyObjs
for i = 1 to vertArray.count do
(
local pos = ((getLinePlaneIntersect targetCam.pos (vertArray[i]) (ray card.pos card.dir)) * inverse card.transform)
--get min and max point2 vals
if pos.x < bmin.x do
bmin.x = pos.x
if pos.y < bmin.y do
bmin.y = pos.y
if pos.x > bmax.x do
bmax.x = pos.x
if pos.y > bmax.y do
bmax.y = pos.y
)
cardSize = -bmin + bmax
card.width = cardSize.x
card.length = cardSize.y
--offset from base objects position to center of card
offset = (bmin + bmax) / 2
in coordsys local
card.pos += [offset.x,offset.y,0]