Page 1 of 1

Cant figure out how to copy material

PostPosted: Tue May 12, 2015 9:37 pm
by Wallan
Hi.

Need a little help to get me going.
I want to write some code that from an imported mesh creates material nodes for some known materials in the mesh.
In this case it's models exported from DAZ that I would like to handle.
If someone could supply some code snippets that help me get starteds I would be very greateful.

I want to create one material node for every type of material. I know as an example that legs, feets, hands, arms etc use the same material so for them i would want to create a material Limbs that has the same properties as Legs and then connect the material pin of legs, feets, hands, arms etc to that material instead.

Splitting it up it would be something like this.

1 Create a "mastermateriallist" list with the name of the master materials and the name I want for the created material node. [legs = Limbsmaterial,head = Headmaterial]

2 Create a "linklist" list with info on witch materials should use witch material node. [legs=Limbsmaterials,feets=Limbsmaterials,hands=Limbsmaterial] and so on.

3 Find the known mastermateriallist materials in the mesh by name and create a new material with the name given by the mastermateriallist that is a copy of of the found material in the mesh.

4 Use the linklist and connect all the the material pins in the mesh as given by the linklist to the material also given by the linklist.


Regards
Wallan

Re: In need for a little help to get me going

PostPosted: Wed May 13, 2015 6:33 pm
by Wallan
So far I figured out how to build lists and iterate the mesh pins.
Also figured out how to create default materials.

Still need to figure out how to copy materials in the most easy way....

LUA makes me somewhat crazy some things are straightforward but some stuff expected to be easy is not.
Mainly because i don't understand the syntax in the examples I find ;)

Re: In need for a little help to get me going

PostPosted: Wed May 13, 2015 6:49 pm
by grimm
If you post the code that you are having problems with, we can definitely help you understand it. :)

Re: In need for a little help to get me going

PostPosted: Wed May 13, 2015 7:33 pm
by Wallan
At the moment I don't have a one of those problems.
Have worked around them so far.

What I have problem figuring out is how to copy materials from the mesh node either as a new material node or to an existing node.
Whatever is the most simple.

Code below is where i want to either copy the material from the mesh pin with name "Legs" to the already created material "matLimbs" or if more easy to a new material.

if pinInfo.name == "Legs" then
print("Copy Legs to matLimbs");

Code: Select all
function tprint (tbl, indent)
  if not indent then indent = 0 end
  for k, v in pairs(tbl) do
    formatting = string.rep("  ", indent) .. k .. ": "
    if type(v) == "table" then
      print(formatting)
      tprint(v, indent+1)
    elseif type(v) == 'boolean' then
      print(formatting .. tostring(v))     
    else
      print(formatting .. tostring(v))
    end
  end
end



--
-- Select a material and a mesh
--


local mesh = nil


-- figure out the selection of the mesh
local selection = octane.project.getSelection()
if #selection ~= 1 then error("One mesh and one mesh only must be selected!") end
if selection[1]:getProperties().type == octane.NT_GEO_MESH then
    mesh = selection[1]
    print("Found mesh " .. mesh.name .. " with " .. mesh:getPinCount() .. " pins.");
else
    error("One mesh and one mesh only must be selected!")
end




--create materials

matToPin = {Legs="Limbs",EyeReflection="Eyes",Nostrils="Face",Lacrimals="Eyes",Pupils="Eyes",Lips="Face",Tear="Eyes",Gums="Mouth"}
masterMaterials = {Legs="Limbs",Irises="Eyes",Face="Face",Torso="Torso",Gums="Mouth"}

matEyes = octane.node.create{type = octane.NT_MAT_DIFFUSE,name = mesh.name.."_Eyes"};
matTorso = octane.node.create{type = octane.NT_MAT_DIFFUSE,name = mesh.name.."_Torso"};
matLimbs = octane.node.create{type = octane.NT_MAT_DIFFUSE,name = mesh.name.."_Limbs"};
matFace = octane.node.create{type = octane.NT_MAT_DIFFUSE,name = mesh.name.."_Face"};
matMouth = octane.node.create{type = octane.NT_MAT_DIFFUSE,name = mesh.name.."_Mouth"};



print("------  Looking for known materials to copy  -------")


for pinIx=1,mesh:getPinCount() do

    pinInfo = mesh:getPinInfoIx(pinIx);
    masterMaterialname = masterMaterials[pinInfo.name];

    if masterMaterialname ~= nil then
        print("Pin Name " .. pinInfo.name .. " should be copied to material " .. masterMaterialname)

        if pinInfo.name == "Legs" then
            print("Copy Legs to matLimbs");
           
        elseif pinInfo.name == "Irises" then
            print("Copy Irises to matEyes");

        elseif pinInfo.name == "Face" then
            print("Copy Face to matFace");

        elseif pinInfo.name == "Torso" then
            print("Copy Torso to matTorso");

        elseif pinInfo.name == "Gums" then
            print("Copy Gums to matMouth");

        end

    else
        --print("Pin Name " .. pinInfo.name .. " ignored")
    end
end


print("------  Looking for known materials to connect  ------")


for pinIx=1,mesh:getPinCount() do

    pinInfo = mesh:getPinInfoIx(pinIx);
    diffMatNodeName = matToPin[pinInfo.name];

    if diffMatNodeName ~= nil then
        print("Pin Name " .. pinInfo.name .. " should connect to " .. diffMatNodeName)
    else
        --print("Pin Name " .. pinInfo.name .. " has no material to connect to")
    end
end

Re: Cant figure out how to copy material

PostPosted: Thu May 14, 2015 9:37 am
by Wallan
Seems as I'm mostly talking to myself here ... ;)

Anyway, finally managed to transfer color from one node to another.

So, main issues are under control.

Re: Cant figure out how to copy material

PostPosted: Fri May 15, 2015 7:32 am
by grimm
Sorry about that, I got busy. I see that you have help though, from the master himself (Thomas). :)