Work with Observers

Using Lua to observe changes to nodes

These functions allow callbacks to be performed in response to nodes being updated.

The defined callbacks will take place following the execution of the script, or during a vrYield.

vrAddPropertyObserver

Adds an observer to monitor and respond to value changes for a single property.

local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())
vrYield(0)

local myCallback = function(node, value)
  print(node:getName() .. " enabled: " .. tostring(value))
end

vrAddPropertyObserver(
  "enabledObserver",
  myCallback,
  "Assembly",
  "Enabled"
)

myNode.Enabled = false
vrYield(0)

myNode.Enabled = true
vrYield(0)

vrRemoveObserver("enabledObserver")
vrDeleteNode(myNode)
This will output
ObservedNode enabled: false
ObservedNode enabled: true

vrAddNodeObserver

Adds an observer to monitor and respond to changes on a Node.

The function takes a map of callback functions. These will be called in response to the appropriate changes.

Key

Callback

Callback Signature

valuesChanged

Called in response to a property's value being changed.

function(node)

addingToRecycleBin

Called in repsonse to the node being sent to the recycle bin.

function(node)

restoredFromRecycleBin

Called in repsonse to the node being returned from the recycle bin.

function(node)

nodeDestroyed

Called in response to the node being destroyed.

function(node)

local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())
vrYield(0)

local callbackMap = {
  valuesChanged = function(node)
    print(node:getName() .. " enabled: " .. tostring(node.Enabled))
  end,

  addingToRecycleBin = function(node)
    print(node:getName() .. " removed")
  end
}

vrAddNodeObserver(
  "nodeObserver",
  myNode,
  callbackMap
)

myNode.Enabled = false
vrYield(0)

myNode.Enabled = true
vrYield(0)

vrDeleteNode(myNode)
vrYield(0)

vrRemoveObserver("nodeObserver")
This will output
ObservedNode enabled: false
ObservedNode enabled: true
ObservedNode removed

vrAddMetaNodeObserver

Adds an observer to monitor and respond to changes on instances of a MetaNode.

The function takes a map of callback functions. These will be called in response to the appropriate changes.

Key

Callback

Callback Signature

nodeCreated

Called in response to a node being created.

function(meta, node)

nodeRenamed

Called in repsonse to a node being renamed.

function(meta, node)

nodeParentChanged

Called in response to a node being moved.

function(meta, node, oldParent, newParent)

nodeAddingToRecycleBin

Called in repsonse to a node being sent to the recycle bin.

function(meta, node)

nodeRestoredFromRecycleBin

Called in repsonse to a node being returned from the recycle bin.

function(meta, node)

nodeDestroying

Called in response to a node being destroyed.

function(meta, node)

local callbackMap = {
  nodeCreated = function(meta, node)
    print(node:getName() .. " created")
  end,

  nodeRenamed = function(meta, node)
    print(node:getName() .. " renamed")
  end,

  nodeParentChanged = function(meta, node, old, new)
    if(old and new) then
      print(node:getName() .. " moved from " .. old:getName() .. " to " .. new:getName()) 
    end
  end,

  nodeAddingToRecycleBin = function(meta, node)
    print(node:getName() .. " removed")
  end
}

vrAddMetaNodeObserver(
  "nodeObserver",
  "Assembly",
  callbackMap
)

local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())

myNode:setParent(vrLibrariesNode())
vrYield(0)

myNode:setName("NamedNode")
vrYield(0)

vrDeleteNode(myNode)
vrYield(0)

vrRemoveObserver("nodeObserver")
This will output
ObservedNode created
ObservedNode moved from Scenes to Libraries
NamedNode renamed
NamedNode removed
NamedNode moved from Libraries to RecycleBin

vrAddTransformNodeObserver

Adds an observer to monitor and respond to the transform related value changes of a Node.

local myNode = vrCreateNode("Assembly", "ObservedNode", vrScenesNode())
vrYield(0)

local myTransformCallback = function(node, value)
  print(node:getName() .. " transformed:")
  print(tostring(value))
end

local myEnabledCallback = function(node, value)
  print(node:getName() .. " enabled: " .. tostring(value))
end

vrAddTransformNodeObserver(
  "transformObserver",
  myTransformCallback,
  myEnabledCallback,
  myNode
)

myNode.Enabled = false
vrYield(0)

myNode.Enabled = true
vrYield(0)

local transform = myNode.Transform
transform.position.x = transform.position.x + 1.0
vrYield(0)

vrRemoveObserver("transformObserver")

vrDeleteNode(myNode)
vrYield(0)
This will output
ObservedNode enabled: false
ObservedNode enabled: true
ObservedNode transformed:
1 0 0 0
0 1 0 0
0 0 1 0
1 0 0 1


No Results.

Getting StartedArchitectureBest PracticesHow ToAdvanced TopicsChangelogvrtreevrtree_cppvtCoreCoreForeign Function InterfaceMetanodesMigrationsObserversPropertiesSettingsTreeUtilitiesAPI DefinitionsVR ExchangePluginsLua API