MAXSCRIPT: Proportional Reference Plane

by

Script I often use while setting up reference planes from simple pictures:
1. Make plane object (any size/proportions).
2. Drag and drop image from folder to plane.
3. Select plane with now applied texture.
3. Click on button/toolbar/quad you assigned script to.
4. Plane width/height in max units is set according image width / height in pixels. Just scale the plane afterwards to desired size.

macroScript proportionalRefPlane 
    category:"_Piro_Tools" 
    tooltip:"UI: proportionalRefPlane" 
    buttontext:"UI: proportionalRefPlane"
(
    clearlistener()
    planeL = 0
    planeW = 0
    planeName = "testref"
    
    function isSelectedObjectPlane = 
(
    -- Get the currently selected object
    local selectedObj = selection[1]
    
    -- Check if the selected object is a plane object
    if (classof selectedObj == Plane) then
    (
        return true -- The selected object is a plane object
    )
    else
    (
        return false -- The selected object is not a plane object
    )
)
    
function selectObjectWithLargestBoundingBox =
(
    local maxBBSize = 0 -- Initialize the maximum bounding box size to 0
    local objWithMaxBB -- Initialize the object with the largest bounding box to undefined
    
    for obj in objects do -- Iterate through all objects in the scene
    (
        local objBB = obj.boundingBox
        local objBBSize = objBB.max - objBB.min
        local objBBDiagonal = length objBBSize
        
        if objBBDiagonal > maxBBSize do -- Check if this object has a larger bounding box than the previous largest
        (
            maxBBSize = objBBDiagonal
            objWithMaxBB = obj
        )
    )
    
    return objWithMaxBB -- Select the object with the largest bounding box
)

--selectObjectWithLargestBoundingBox() -- Call the function to select the object with the largest bounding box


fn scaleToFit objToScale refObj = 
(
    -- Get the bounding boxes of both objects
    local objBB = (nodeTransformBoundingBox objToScale).minmax
    local refBB = (nodeTransformBoundingBox refObj).minmax
    
    -- Get the sizes of the objects' bounding boxes along each axis
    local objSize = objBB[2] - objBB[1]
    local refSize = refBB[2] - refBB[1]
    
    -- Calculate the ratio of sizes along each axis
    local scaleFactor = [1,1,1]
    for i = 1 to 3 do (
        if refSize[i] != 0 do scaleFactor[i] = objSize[i] / refSize[i]
    )
    
    -- Scale the object using the calculated scale factor
    objToScale.scale = objToScale.scale * scaleFactor
)



function getBitmapNameOnly bitmapPath = 
(
    nameOnly = ""
    -- Get the filename from the bitmap path
    local fileName = getFilenameFile bitmapPath
    
    -- Remove the extension from the filename
    --local nameOnly = filterString fileName ".*" ""
    
    -- Return just the name of the file without the path or extension
    return fileName
)

    fn GetBitmapTextures theObjects =
    (
        texMaps = #()
        for obj in theObjects do
        (
            join texMaps (getClassInstances bitmapTexture target:obj asTrackViewPick:off)
        )
        makeUniqueArray texMaps
    )
    texMaps = (GetBitmapTextures selection)
    for texMap in texMaps do 
    (
        testTexture = bitmaptexture filename:texMap.filename
        print (texMap.filename)
        print (testTexture.Bitmap.height as string +"px H x "+testTexture.Bitmap.width as string +"px W")
        
        if isSelectedObjectPlane() then
        (
            selection[1].length = testTexture.Bitmap.height
            selection[1].width = testTexture.Bitmap.width
            selection[1].name = getBitmapNameOnly(texMap.filename)
                    print ("Reference plane changed.")
            --testObj = selectObjectWithLargestBoundingBox()
                --scaleToFit selection[1] testObj
            )
        
    )
    

        OK
    
)