Share

Script37_ScreenshotCameraControl.lua

-- Lua example for for Autodesk Netfabb 2021.0

-- Demonstrates camera and perspective for taking screenshots

Dialog = application:createdialog()
Dialog.caption = "Screenshots of all parts"
Dialog.translatecaption = false

Groupbox = Dialog:addgroupbox()
Groupbox.caption = "Screenshots of all parts"
Groupbox.translate = false

CheckboxMoveX = Groupbox:addcheckbox()
CheckboxMoveX.translate = false
CheckboxMoveX.caption = "Move camera in X direction"
CheckboxMoveX.checked = true

CheckboxMoveY = Groupbox:addcheckbox()
CheckboxMoveY.translate = false
CheckboxMoveY.caption = "Move camera in Y direction"
CheckboxMoveY.checked = true

CheckboxRotate = Groupbox:addcheckbox()
CheckboxRotate.translate = false
CheckboxRotate.caption = "Rotate camera around center"
CheckboxRotate.checked = true

CheckboxCenter = Groupbox:addcheckbox()
CheckboxCenter.translate = false
CheckboxCenter.caption = "Move center point with fixed camera"
CheckboxCenter.checked = true

CheckboxUp = Groupbox:addcheckbox()
CheckboxUp.translate = false
CheckboxUp.caption = "Rotate Up vector"
CheckboxUp.checked = true

CheckboxZoom = Groupbox:addcheckbox()
CheckboxZoom.translate = false
CheckboxZoom.caption = "Zoom camera"
CheckboxZoom.checked = true

CheckboxBoundingBox = Groupbox:addcheckbox()
CheckboxBoundingBox.translate = false
CheckboxBoundingBox.caption = "Rotate with bounding box center"
CheckboxBoundingBox.checked = true

Edit = Groupbox:addedit()
Edit.captionwidth = 120
Edit.translate = false
Edit.caption = "Output folder:"
Edit.text = "D:/37"

Splitter = Groupbox:addsplitter()
Splitter:settoleft()

Button = Splitter:addbutton()
Button.caption = "Save screenshots"
Button.translate = false
Button.onclick = "savescreenshots"

Splitter:settoright()

Button = Splitter:addbutton()
Button.caption = "Close"
Button.translate = false
Button.onclick = "closedialog"

function ScreenshotMoveX(BoundingBox)
    local PosX = BoundingBox.minx
    local PosY = BoundingBox.miny - 25
    local PosZ = (BoundingBox.maxz - BoundingBox.minz) / 2
    local StepX = (BoundingBox.maxx - BoundingBox.minx) / 10

    -- In a first loop we place the camera in Y direction off the buildroom and move the camera and the center point along the X axis
    for i = 1, 10 do
        local FileName = string.format("%s/screenshot_movex_%02i.png", Edit.text, i)
        local Options = system:createjson()
        Options:loadfromstring(
            string.format(
                '{ "camera": { "eye": [ %f, %f, %f ], "center": [ %f, %f, %f ], "up": [ 0.0, 0.0, 1.0 ] } }',
                PosX,
                PosY,
                PosZ,
                PosX,
                PosY + 25,
                PosZ
            )
        )
        local Image = system:createscreenshot(1920, 1080, Options)
        Image:saveto(FileName)

        PosX = PosX + StepX
    end
end

function ScreenshotMoveY(BoundingBox)
    local PosX = BoundingBox.minx - 25
    local PosY = BoundingBox.miny
    local PosZ = (BoundingBox.maxz - BoundingBox.minz) / 2
    local StepY = (BoundingBox.maxy - BoundingBox.miny) / 10

    -- In the second loop we place the camera in X direction off the buildroom and move the camera and the center point along the Y axis
    for i = 1, 10 do
        local FileName = string.format("%s/screenshot_movey_%02i.png", Edit.text, i)
        local Options = system:createjson()
        Options:loadfromstring(
            string.format(
                '{ "camera": { "eye": [ %f, %f, %f ], "center": [ %f, %f, %f ], "up": [ 0.0, 0.0, 1.0 ] } }',
                PosX,
                PosY,
                PosZ,
                PosX + 25,
                PosY,
                PosZ
            )
        )
        local Image = system:createscreenshot(1920, 1080, Options)
        Image:saveto(FileName)

        PosY = PosY + StepY
    end
end

function ScreenshotRotate(BoundingBox)
    local PosX = BoundingBox.minx - 25
    local PosY = BoundingBox.miny
    local PosZ = (BoundingBox.maxz - BoundingBox.minz) / 2
    local CenterX = (BoundingBox.maxx - BoundingBox.minx) / 2
    local CenterY = (BoundingBox.maxy - BoundingBox.miny) / 2

    PosZ = (BoundingBox.maxz - BoundingBox.minz) / 10

    -- The third loop shows a rotation around the center of the build platform in 10 steps
    for i = 1, 10 do
        PosX = 25 * math.cos((36 * i) * math.pi / 180.0) + CenterX
        PosY = 25 * math.sin((36 * i) * math.pi / 180.0) + CenterY

        local FileName = string.format("%s/screenshot_rotate_%02i.png", Edit.text, i)
        local Options = system:createjson()
        Options:loadfromstring(
            string.format(
                '{ "camera": { "eye": [ %f, %f, %f ], "center": [ %f, %f, 0.0 ], "up": [ 0.0, 0.0, 1.0 ] } }',
                PosX,
                PosY,
                PosZ,
                CenterX,
                CenterY
            )
        )
        local Image = system:createscreenshot(1920, 1080, Options)
        Image:saveto(FileName)
    end
end

function ScreenshotCenter(BoundingBox)
    local PosX = BoundingBox.maxx + 10
    local PosY = (BoundingBox.maxy - BoundingBox.miny) / 2
    local PosZ = (BoundingBox.maxz - BoundingBox.minz) / 2
    local CenterX = (BoundingBox.maxx - BoundingBox.minx) / 2
    local CenterY = BoundingBox.miny
    local StepY = (BoundingBox.maxy - BoundingBox.miny) / 10

    -- This time we keep the camera fixed and move the center point along a line
    for i = 1, 10 do
        local FileName = string.format("%s/screenshot_movecenter_%02i.png", Edit.text, i)
        local Options = system:createjson()
        Options:loadfromstring(
            string.format(
                '{ "camera": { "eye": [ %f, %f, %f ], "center": [ %f, %f, 0.0 ], "up": [ 0.0, 0.0, 1.0 ] } }',
                PosX,
                PosY,
                PosZ,
                CenterX,
                CenterY
            )
        )
        local Image = system:createscreenshot(1920, 1080, Options)
        CenterY = CenterY + StepY
        Image:saveto(FileName)
    end
end

function ScreenshotUp(BoundingBox)
    local PosX = BoundingBox.minx - 25
    local PosY = (BoundingBox.maxy - BoundingBox.miny) / 2
    local PosZ = (BoundingBox.maxz - BoundingBox.minz) / 2
    local CenterX = (BoundingBox.maxx - BoundingBox.minx) / 2
    local CenterY = (BoundingBox.maxy - BoundingBox.miny) / 2

    -- This time it doesn't make any sense at all, but for demo purposes we just rotate the up vector by 180 degrees in 10 steps
    for i = 1, 10 do
        local UpY = math.cos(18 * i * math.pi / 180)
        local UpZ = math.sin(18 * i * math.pi / 180)

        local FileName = string.format("%s/screenshot_up_%02i.png", Edit.text, i)
        local Options = system:createjson()
        Options:loadfromstring(
            string.format(
                '{ "camera": { "eye": [ %f , %f, %f ], "center": [ %f, %f, 0.0 ], "up": [ 0.0, %f, %f ] } }',
                PosX,
                PosY,
                PosZ,
                CenterX,
                CenterY,
                UpY,
                UpZ
            )
        )
        local Image = system:createscreenshot(1920, 1080, Options)
        Image:saveto(FileName)
    end
end

function ScreenshotZoom(BoundingBox)
    local CenterX = (BoundingBox.maxx - BoundingBox.minx) / 2
    local CenterY = (BoundingBox.maxy - BoundingBox.miny) / 2
    local PosX = CenterX - 25
    local PosY = CenterY
    local PosZ = (BoundingBox.maxz - BoundingBox.minz) / 2

    local Zoom = 0.5
    -- Last but not least we iterate the zoom factor from 0.5 to 5 in 10 steps
    for i = 1, 10 do
        local FileName = string.format("%s/screenshot_zoom_%02i.png", Edit.text, i)
        local Options = system:createjson()
        Options:loadfromstring(
            string.format(
                '{ "camera": { "eye": [ %f, %f, %f ], "center": [ %f, %f, 0.0 ], "up": [ 0.0, 0.0, 1.0 ], "zoom": %f } }',
                PosX,
                PosY,
                PosZ,
                CenterX,
                CenterY,
                Zoom
            )
        )
        local Image = system:createscreenshot(1920, 1080, Options)
        Image:saveto(FileName)

        Zoom = Zoom + 0.5
    end
end

function ScreenshotBoundingBox(BoundingBox)
    local CenterX = (BoundingBox.maxx - BoundingBox.minx) / 2
    local CenterY = (BoundingBox.maxy - BoundingBox.miny) / 2
    local PosZ = (BoundingBox.maxz - BoundingBox.minz) / 10

    local BoundingBoxString =
        string.format(
        "%f, %f, %f, %f, %f, %f",
        BoundingBox.minx,
        BoundingBox.miny,
        BoundingBox.minz,
        BoundingBox.maxx,
        BoundingBox.maxy,
        BoundingBox.maxz
    )
    -- The third loop shows a rotation around the center of the build platform in 10 steps
    for i = 1, 10 do
        -- if you're using the bounding box the position of the camera is relative to the bounding box center
        local PosX = 25 * math.cos((36 * i) * math.pi / 180.0)
        local PosY = 25 * math.sin((36 * i) * math.pi / 180.0)

        local FileName = string.format("%s/screenshot_boundingbox_%02i.png", Edit.text, i)
        local Options = system:createjson()
        Options:loadfromstring(
            string.format(
                '{ "camera": { "eye": [ %f, %f, %f ], "up": [ 0.0, 0.0, 1.0 ], "outbox": [ %s ] } }',
                PosX,
                PosY,
                PosZ,
                BoundingBoxString
            )
        )
        local Image = system:createscreenshot(1920, 1080, Options)
        Image:saveto(FileName)
    end
end

function savescreenshots()
    local BoundingBox = tray.outbox

    if CheckboxMoveX.checked then
        ScreenshotMoveX(BoundingBox)
    end
    if CheckboxMoveY.checked then
        ScreenshotMoveY(BoundingBox)
    end
    if CheckboxRotate.checked then
        ScreenshotRotate(BoundingBox)
    end
    if CheckboxCenter.checked then
        ScreenshotCenter(BoundingBox)
    end
    if CheckboxUp.checked then
        ScreenshotUp(BoundingBox)
    end
    if CheckboxZoom.checked then
        ScreenshotZoom(BoundingBox)
    end
    if CheckboxBoundingBox.checked then
        ScreenshotBoundingBox(BoundingBox)
    end
end

function closedialog()
    Dialog:close(false)
end

Dialog:show()

Was this information helpful?