--------------------------- -- Entangled material. -- The reference material should be connected to this script. -- Any material node in the scene with the same name and node type as the reference will be kept in sync with it. -- -- @version 0.1 ---------------------------- local inputLinkers = {} local function setInputLinkers(graph) local linkers = { { label = "Input material", type = octane.pinType.PT_MATERIAL, defaultNodeType = octane.nodeType.NT_MAT_DIFFUSE, } } inputLinkers = graph:setInputLinkers(linkers) end -------------------------------------------- local node = {} node._name = "Entangled material" node._evaluateAllChanges = true -- Callback called after initializing the scripted graph with the code. function node.onInit(self, graph) setInputLinkers(graph) end -- Callback called when the connected value of one of the input linker nodes changes. function node.onEvaluate(self, graph) -- Find any Material node with the same name and node type as our reference -- and replace them with a copy of the reference. local refMaterial = inputLinkers[1]:getConnectedNode(octane.pinId.P_INPUT, true) if refMaterial == nil then return end local props = refMaterial:getProperties() local name = props.name local nodeType = props.type local root = octane.project.getSceneGraph() local nodes = root:findNodes(nodeType, true) for _, nodeMaterial in ipairs(nodes) do if nodeMaterial ~= refMaterial then local propsClone = nodeMaterial:getProperties() if propsClone.name == name then local position = propsClone.position -- To update the clones we need to completely replace them with new copies of the reference. -- This is necessary to copy textures and other upstream nodes connected to the reference. local copy = root:copyItemTree(refMaterial) copy:updateProperties({position = position}) -- Replace connections towards destination nodes. local destinationNodes = nodeMaterial:getDestinationNodes() for _, v in ipairs(destinationNodes) do v.node:connectTo(v.pin, copy, true) end -- Destroy the old clone. nodeMaterial:destroy() end end end end return node