-- Links material input pins of selected mesh nodes to the correspondingly named -- output linkers of a Node Graph, where your materials are located. -- If you prefer to copy the materials, just find the variable 'copy_materials' below and change to true -- -- @description Links selected mesh materials to matching outputs in a node graph -- @author Soft Office -- @version 0.1 -- @shortcut alt + l -- options local copy_materials = false -- functions function values (t) local i = 0 return function () i = i + 1; return t[i] end end function select_node_type(items, node_type) --returns all nodes in selection set matching given type selection = {} for i, n in pairs(items) do if n.nodeType == node_type then table.insert(selection, n) end end return selection end function find_first_graph(items, graph_type) --returns first graph in the selection set for n in values(items) do if n.graphType == graph_type then return n end end return nil end function select_pin_type(node, pin_type) --returns indexes of all pins matching given type pins = {} for i=1, node:getPinCount() do if node:getPinInfoIx(i).type == pin_type then table.insert(pins, node:getPinInfoIx(i).name) end end return pins end function print_table(table, str) --print a table plus an optional extra string for i,n in pairs(table) do print(i,n) end print(str) end --get selection local sel = octane.project.getSelection() --print("selection set:") --print_table(sel, "\n") if #sel == 0 then octane.gui.showError("Selection is empty!", "Material Linker") return nil end--if --get Node Graph mat_graph = find_first_graph(sel, octane.GT_STANDARD) --print("material graph:\n", mat_graph, "\n") if mat_graph == nil then octane.gui.showError("Please include a (single) node graph in your selection", "Material Linker") return nil end --get Material Outputs mat_outputs = mat_graph:findNodes(octane.NT_OUT_MATERIAL, false) --print("material outputs:") --print_table(mat_outputs, "\n") if #mat_outputs == 0 then octane.gui.showError("Please include at least one material output in the node graph", "Material Linker") return nil end --get Mesh nodes local geom_nodes = select_node_type(sel, octane.NT_GEO_MESH) --print("geometry nodes:") --print_table(geom_nodes, "\n") if #geom_nodes == 0 then octane.gui.showError("Please include at least one mesh node in your selection", "Material Linker") return nil end for n in values(geom_nodes) do print("material pins for ", n, ":") for pinIx=1, n:getPinCount() do if n:getPinInfoIx(pinIx).type == octane.PT_MATERIAL then --match pin name to a material output name local src = nil for m in values(mat_outputs) do if n:getPinInfoIx(pinIx).name == m:getProperties().name then src = copy_materials and m:getConnectedNodeIx(1) or m break end--if end print("> ", n:getPinInfoIx(pinIx).name, " : ", src) if src then if copy_materials then n:copyFromIx(pinIx, src) else n:connectToIx(pinIx, src) end--if end--if end--if end--for print(" ") end--for