-- Poor man's material assignment by wrapping geometry inside a node graph. -- -- @description Poor man's material assignment -- @author Roeland Schoukens -- @version 0.1 -- This function gets a name from an input pin and chops off any suffixes -- added by Octane and by the 3D application. The "-Material" is always -- added by Octane, and in this case Cinema 4D adds suffixes like -- "Shape", "_1Shape", "_2Shape" and so on. If your 3D application uses -- another convention you need to modify this function. function getMaterialInputName(name) -- These are Lua patterns. See -- chop off the "Shape" and "-Material" parts name = name:gsub("Shape%-Material$", "") -- chop off any sequence numbers name = name:gsub("_%d+$", "") return name end local scene = octane.project.getSelection()[1] if not scene or scene:getProperties().type ~= octane.GT_GEOMETRYARCHIVE or scene:getProperties().graphOwner == octane.nodegraph.getRootGraph() then octane.gui.showDialog { type = octane.gui.dialogType.BUTTON_DIALOG, title = "Metal.", text = "Please select a scene inside a node graph. If your scene is".. " in the root node graph wrap it using the \"group items\" feature.", } return end local parent = scene:getProperties().graphOwner local links = {} for _, l in pairs(parent:getInputNodes()) do local prop = l:getProperties() if prop.type == octane.NT_IN_MATERIAL then links[prop.name] = l end end local used = {} local pos = scene:getProperties().position pos[2] = pos[2] - 100 pos[1] = pos[1] - 100 for _, l in ipairs(scene:getInputNodes()) do local prop = l:getProperties() local name = getMaterialInputName(prop.name) if prop.type == octane.NT_IN_MATERIAL then if used[name] == nil then used[name] = links[name] links[name] = nil if used[name] == nil then used[name] = octane.node.create{type=octane.NT_IN_MATERIAL, name=name, graphOwner=parent} end used[name]:updateProperties{position=pos} pos[1] = pos[1] + 100 end l:disconnect(octane.P_INPUT) l:connectTo(octane.P_INPUT, used[name]) end end for _, l in pairs(links) do l:updateProperties{position=pos} pos[1] = pos[1] + 100 end