Scripted node graph to extract camera position

Forum for OctaneRender Lua scripting examples, discussion and support.
manalokos
Licensed Customer
Posts: 441
Joined: Fri Nov 18, 2011 4:43 pm

Hello

I've been trying to make a scripted node graph to extract camera position and target position or other values as outputs.

This way I can connect an alembic camera to this scripted operator and connect the output nodes to a new camera where I can control the aperture.
But as I have no experience in programming in LUA for octane, I was only able to create an input and output for the nodes, but the connections I wasn't able to make.

Could someone show me how to do this?

Thanks
Kind regards
Filipe
User avatar
Goldorak
OctaneRender Team
Posts: 2321
Joined: Sun Apr 22, 2012 8:09 pm
Contact:

Thomas made one for me. I can look for it and post it here once I locate it...
manalokos
Licensed Customer
Posts: 441
Joined: Fri Nov 18, 2011 4:43 pm

Thanks Goldorak

Meanwhile I'll keep trying.
manalokos
Licensed Customer
Posts: 441
Joined: Fri Nov 18, 2011 4:43 pm

I spent the whole afternoon trying to make this work, finally I was able to get the results I was expecting...
The output shoud be a camera node but meanwhile I can connect the position, target, and focus distance to a secondary camera...
It has 2 inputs, one for the alembic camera, and another for another alembic camera to work as a focus point. It would be great if alembic import would get position of nulls into float output...


Code: Select all

    local Aextract = {}
    local inputs, tex

    -- onInit function, this is called once in the beginning.
    function Aextract.onInit(self, graph)
        -- input and output infos
        local inputInfos = {
            {type=octane.PT_CAMERA, label="camara", defaultNodeType=octane.NT_IN_CAMERA},
             {type=octane.PT_CAMERA, label="foco", defaultNodeType=octane.NT_IN_CAMERA}
                 
        }
        
        local outputInfos = {
            {type=octane.PT_FLOAT, label="pos"},
            {type=octane.PT_FLOAT, label="target"},
            {type=octane.PT_FLOAT, label="focusdistance"}
        }
        
        -- use these functions to set up input and output linkers. This will keep
        -- existing linkers so existing connections in the parent graph are kept.
        inputs = graph:setInputLinkers(inputInfos)
        local outputs = graph:setOutputLinkers(outputInfos)
        
        -- set up a node to give the graph some output value
        posnode = octane.node.create{ type=octane.NT_FLOAT, name="posicaocamara", graphOwner=graph }
        targetnode = octane.node.create{ type=octane.NT_FLOAT, name="posicaotarget", graphOwner=graph }
        targetvector =  octane.node.create{ type=octane.NT_FLOAT, name="pontodefoco", graphOwner=graph }
        focusdistance = octane.node.create{ type=octane.NT_FLOAT, name="distanciadefoco", graphOwner=graph }
        outputs[1]:connectTo("input", posnode)
        outputs[2]:connectTo("input", targetnode)
        outputs[3]:connectTo("input", focusdistance)
        self:setEvaluateTimeChanges(true)
    end




    function Aextract.onEvaluate(self, graph)


        local camara = inputs[1]:getInputNode("input")
        local posicao = camara:getPinValue("pos")
        local foco = inputs[2]:getInputNode("input")
        local target = foco:getPinValue("pos")
       

        print(posicao[1], posicao[2], posicao[3])
        posnode:setAttribute("value", posicao)
        targetnode:setAttribute("value", target)
        targetvector:setAttribute("value", {target[1]-posicao[1], target[2]-posicao[2], target[3]-posicao[3]})
        focusdistance:setAttribute("value",{ math.sqrt(math.pow((target[1]-posicao[1]),2)+math.pow((target[2]-posicao[2]),2)+math.pow((target[3]-posicao[3]),2)),0,0})
        print(math.sqrt(math.pow((target[1]-posicao[1]),2)+math.pow((target[2]-posicao[2]),2)+math.pow((target[3]-posicao[3]),2)))
      return true  
 end



    return Aextract
User avatar
bepeg4d
Octane Guru
Posts: 10328
Joined: Wed Jun 02, 2010 6:02 am
Location: Italy
Contact:

Hi,
here is my attempt but with just one input abc node:
FromABC2Thin.orbx
(4.21 KiB) Downloaded 1421 times
It takes the focal distance from the abc node and I had to add also the focal length for correctly matching between the two cameras.
It should work also with animated camera ;)
ciao beppe

Code: Select all

local ABC2ThinScript = {}


local mInput
local mOutputF
local mOutputD
local mOutputP
local mOutputT


function ABC2ThinScript.onInit(self, graph)

    -- create input linkers
    local inputs = graph:setInputLinkers
    {
        {
            type            = octane.PT_CAMERA,
            label           = "ABCcam",
            fromNodeType    = octane.NT_OUT_CAMERA,
            fromPinId       = octane.P_INPUT
        }
    }
    mInput = inputs[1]
        
    -- create output linkers
    local outputs = graph:setOutputLinkers
        {
            {
                type = octane.PT_FLOAT,
                label = "ABCfocal_length"
            },
            {
                type = octane.PT_FLOAT,
                label = "ABCfocal_depth"
            },
            {
                type = octane.PT_FLOAT,
                label = "ABCposition"
            },
            {
                type = octane.PT_FLOAT,
                label = "ABCtarget"
            },
        }
        
    -- create float value node that will provide the ABC Camera value
    mOutputF = octane.node.create
    {
        type         = octane.NT_FLOAT,
        name         = outputs[1].name,
        pinOwnerNode = outputs[1],
        pinOwnerIx   = 1
    }
    mOutputD = octane.node.create
    {
        type         = octane.NT_FLOAT,
        name         = outputs[2].name,
        pinOwnerNode = outputs[2],
        pinOwnerIx   = 1
    }
    mOutputP = octane.node.create
    {
        type         = octane.NT_FLOAT,
        name         = outputs[3].name,
        pinOwnerNode = outputs[3],
        pinOwnerIx   = 1
    }
    mOutputT = octane.node.create
    {
        type         = octane.NT_FLOAT,
        name         = outputs[4].name,
        pinOwnerNode = outputs[4],
        pinOwnerIx   = 1
    }
end


function ABC2ThinScript.onEvaluate(self, graph)

    local inCam		= mInput:getConnectedNode(octane.P_INPUT)
    if inCam == nil then
    local inABC		= nil
    local inFov	 	= nil
    local inDpt	 	= nil
    local inPos	 	= nil
    local inTarg 	= nil
    elseif inCam ~= nil then 
    local inABC		= inCam:getConnectedNode(octane.P_INPUT)
    local inFov	 	= inABC:getPinValue(octane.P_FOCAL_LENGTH)
    local inDpt	 	= inABC:getPinValue(octane.P_FOCAL_DEPTH)
    local inPos	 	= inABC:getPinValue(octane.P_POSITION)
    local inTarg = inABC:getPinValue(octane.P_TARGET)
    mOutputF:setAttribute(octane.A_VALUE, inFov)
    mOutputD:setAttribute(octane.A_VALUE, inDpt)
    mOutputP:setAttribute(octane.A_VALUE, inPos)
    mOutputT:setAttribute(octane.A_VALUE, inTarg)
    ABC2ThinScript:setEvaluateTimeChanges(true)
	end
end

return ABC2ThinScript
manalokos
Licensed Customer
Posts: 441
Joined: Fri Nov 18, 2011 4:43 pm

Wow, so much different way to write the code...
There are so few examples of Lua scripted graphs that is difficult to learn.

This is good, but the software I use won't export the focus distance so I had to make the double camera hack.
User avatar
bepeg4d
Octane Guru
Posts: 10328
Joined: Wed Jun 02, 2010 6:02 am
Location: Italy
Contact:

hi,
this is my second scripted graph and probably my approach is not the best, better to wait for the Thomas' example for a correct reference ;)
ciao beppe
prehabitat
Licensed Customer
Posts: 495
Joined: Fri Aug 16, 2013 10:30 am
Location: Victoria, Australia

Did Thomas' script ever turn up?
Win10/3770/16gb/K600(display)/GTX780(Octane)/GTX590/372.70
Octane 3.x: GH Lands VARQ Rhino5 -Rhino.io- C4D R16 / Revit17
User avatar
stratified
OctaneRender Team
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

I think it's this one. If you want to inspect/modify the script you need to enable "Scripted graph development" in the application preferences.
camera-explode.orbx
(100.3 KiB) Downloaded 1410 times
cheers,
Thomas
prehabitat
Licensed Customer
Posts: 495
Joined: Fri Aug 16, 2013 10:30 am
Location: Victoria, Australia

Cheers :D
Win10/3770/16gb/K600(display)/GTX780(Octane)/GTX590/372.70
Octane 3.x: GH Lands VARQ Rhino5 -Rhino.io- C4D R16 / Revit17
Post Reply

Return to “Lua Scripting”