Page 1 of 2

Copy material from mesh pin to new material node?

PostPosted: Thu May 14, 2015 12:23 pm
by Wallan
I'm almost tearing my hair of by now.
I'm getting totally lost in attributes, properties, items and LUA language on top of that. :shock:
I have had some succes but then I fail on something else.

Could someone please help me with example script that recreates a given pin in a mesh as a new material node instead.

I have had succes in some small parts but copying pin value by pin value with hard coded indexes.

I need something smart that either copies it all in one go or by recursive iteration or something so that materials will be identical.

Re: Copy material from mesh to new material node?

PostPosted: Thu May 14, 2015 12:40 pm
by glimpse
sorry, but I don't get exactly what You need (could You rephrase that?).
You want to copy material from one object to another? that's it..?mm..

Re: Copy material from mesh to new material node?

PostPosted: Thu May 14, 2015 1:37 pm
by Wallan
Yeah, something like that.

I want to do in code what i would do in the GUI by say...

Selecting my mesh, lets assume it's named Frog.obj.
Then go to the one of the materials in Frog it, lets assume that material is named Head.
Right click on it and do copy.
Then click my scene and past the material.
Select the newly pasted material and rename it to Frog_Head.
Then connect the Frog_Head material to the Head pin of the Frog.obj mesh.

The parts that I probably can figure out by myself is how to rename the material and how to do the connection but I'm not even close to figure out how to do a complete material copy with textures, medium and all. I have made several tries but gets lost on the way.

Re: Copy material from mesh to new material node?

PostPosted: Thu May 14, 2015 1:55 pm
by glimpse
You don't need any script for that =) look, I'll do some printscrreens =)..

Re: Copy material from mesh to new material node?

PostPosted: Thu May 14, 2015 2:09 pm
by Wallan
Sorry, I dont have english as main language so I might have expressed myself bad.

The manual process you did in those images, is the same process as I described in words when trying to explain what I want to accomplish in code.

I know that I can do it manually. ;)

So...

What I'm trying to do in code is an automation of that process as it has to be made on a lot of materials.

Re: Copy material from mesh pin to new material node?

PostPosted: Thu May 14, 2015 9:48 pm
by stratified
Hi Wallan,

I don't think I understand 100% what you're trying to achieve but I guess your feedback will flesh that out. Here's an example snippet that copies out the material nodes internal to the pins of a mesh node. Just select a mesh and run the script.

Code: Select all
-- Copies the nodes internal to the pin into the project as mesh nodes
-- usage: First select a mesh in the nodegraph editor and then run this script


-- fetch the selected mesh node (and check that the node really is a mesh node)
local meshNode = octane.project.getSelection()[1]
if not meshNode or meshNode.type ~= octane.NT_GEO_MESH then
    error("no mesh node was selected!")
end

-- the graph that contains the full scene
local sceneGraph = octane.project.getSceneGraph()

-- iterator over all the pins copying out all the materials and reconnecting them
for pinIx=1,meshNode:getPinCount() do
    -- make sure that the pin is a material pin
    if meshNode:getPinInfoIx(pinIx).type == octane.PT_MATERIAL then
        -- fetch the node from within the pin (can be nil when not connected)
        local materialNode = meshNode:getInputNodeIx(pinIx)
        -- copy the full tree rooted at the material node into our scene grap
        if materialNode then
            local copiedMaterialNode = sceneGraph:copyItemTree(materialNode)
            -- rename the copied node to something nicer
            copiedMaterialNode.name = string.format("%s_%s", meshNode.name, materialNode.name)
            -- connect the copied node to the original mesh
            meshNode:connectToIx(pinIx, copiedMaterialNode)
        end
    end
end

-- tidy it all up
sceneGraph:unfold()


Here's the before and after:

before_script.png
before running the script
before_script.png (8.23 KiB) Viewed 7320 times


after_script.png
after running the script


cheers,
Thomas

Re: Copy material from mesh pin to new material node?

PostPosted: Thu May 14, 2015 10:40 pm
by Wallan
Ahhh, the key to everything, "copiedMaterialNode = sceneGraph:copyItemTree(materialNode)" :D

Not having to create materials in advance also means less code.
Will post an image of what my code does when it's rewritten.

Could you maybe help me with another thing that will pop up a before i can get it all together.

How do I make GLOSS and SPECULAR versions of a DIFFUSE material.
I did read in another thread that one can't just change the type?

Grateful for your help :)

Re: Copy material from mesh pin to new material node?

PostPosted: Thu May 14, 2015 10:49 pm
by stratified
Wallan wrote:Ahhh, the key to everything, "copiedMaterialNode = sceneGraph:copyItemTree(materialNode)" :D

Not having to create materials in advance also means less code.
Will post an image of what my code does when it's rewritten.

Could you maybe help me with another thing that will pop up a before i can get it all together.

How do I make GLOSS and SPECULAR versions of a DIFFUSE material.
I did read in another thread that one can't just change the type?

Grateful for your help :)


Hi,

We don't have functions in the Lua API to replace items (but I will add them). Right now your best option is to do it all manually - create a diffuse or glossy material and copy over the relevant connected nodes. Unfortunately that requires some work on your end.

cheers,
Thomas

Re: Copy material from mesh pin to new material node?

PostPosted: Fri May 15, 2015 12:12 am
by Wallan
Well. I was afraid of that ;)

If I can get a little help on most easy way to do such a copy.
Maybe en example on how to copy over a diffuse materials diffuse channel (one that's based on an image) to the transmission channel in a specular material.

If you can show an example of best way to do that then I can hopefully manage to figure out the rest.

Thank's to your example code I now has managed to get the material copy to work as I wanted it to.

Grateful for the help.

Re: Copy material from mesh pin to new material node?

PostPosted: Fri May 15, 2015 1:37 am
by stratified
Wallan wrote:Well. I was afraid of that ;)

If I can get a little help on most easy way to do such a copy.
Maybe en example on how to copy over a diffuse materials diffuse channel (one that's based on an image) to the transmission channel in a specular material.

If you can show an example of best way to do that then I can hopefully manage to figure out the rest.

Thank's to your example code I now has managed to get the material copy to work as I wanted it to.

Grateful for the help.


It's a bit convoluted. What you need to do is:

  • copy the full tree connected to the diffuse pin into a temp graph (via copyItemTree)
  • make sure that this graph has a texture output pin (we do this by adding 1 texture linker node to the graph)
  • connect the item tree copied into the graph to the output linker node
  • copy this whole graph into the transmission pin of the specular material

Here's an example snippet:

Code: Select all
-- Copies the content of the diffuse pin of the diffuse material into the transmission pin
-- of the specular material.

-- create materials
local diffuseMatNode = octane.node.create{ type = octane.NT_MAT_DIFFUSE, name = "source node" }
local texNode        = octane.node.create{ type = octane.NT_TEX_CHECKS, "checkers" }
diffuseMatNode:connectTo(octane.P_DIFFUSE, texNode)
local specularMatNode = octane.node.create{ type = octane.NT_MAT_SPECULAR, name = "dest. node" }

-- wrap up the full tree connect to the diffuse pin in a seperate nodegraph and make sure that
-- this graph has a texture output node
local tmpGraph = octane.nodegraph.create{ type = octane.GT_STANDARD, name = "Mess wrapper" }
local matOutputNode = octane.node.create{ type = octane.NT_OUT_TEXTURE, graphOwner = tmpGraph, name = "texture output" }
local copy = tmpGraph:copyItemTree(texNode)
matOutputNode:connectTo(octane.P_INPUT, copy)

-- copy the full graph into the transmission pin of the specular material
specularMatNode:copyFrom(octane.P_TRANSMISSION, tmpGraph)

-- tidy up
octane.project.getSceneGraph():unfold()


It doesn't matter what is connected to the diffuse pin - it works for every kind of item tree, even those with image textures ;)

cheers,
Thomas