1
$\begingroup$

Hello I just recently learn about scripting in blender and I have problem attaching materials using script.

This is my code so far :

face = bpy.data.objects[nama]
face.scale =(0.00001,0.00001,0.00001)
face.rotation_euler.x = pi/2
face.location.z = 3.23

face.material.new(name = 'Skin')
bpy.data.materials["Skin"].node_tree.nodes["Vertex Color"].layer_name = "skin"

I try to copy the script from info when I do it via GUI and fail to do it on script.

The results that I want is like this, where the Base color change from default value into vertex color and select Col and then if possible named it into skin

endresult

Lemon's answer is almost correct but it has red label on vertex color and it makes my end result become dark

problem

$\endgroup$
2
  • $\begingroup$ What does 'nama' refer to ? $\endgroup$
    – Gorgious
    Mar 4, 2020 at 9:07
  • $\begingroup$ the name of 1 of my object. A .ply object that I have imported $\endgroup$ Mar 4, 2020 at 9:21

1 Answer 1

2
$\begingroup$
face.material.new(name = 'Skin')

'face' has no material property. And as the material is new, there is no vertex color in its node tree (and no node tree as it is).

You can:

mat = bpy.data.materials.new(name = "Skin")
mat.use_nodes = True #Make so it has a node tree

#Add the vertex color node
vc = mat.node_tree.nodes.new('ShaderNodeVertexColor')
#Assign its layer
vc.layer_name = "skin"

Now to link the vertex color output to the default principled shader:

#Get the shader
bsdf = mat.node_tree.nodes["Principled BSDF"]
#Link the vertex color to the shader
mat.node_tree.links.new( vc.outputs[0], bsdf.inputs[0] )

Additionnaly, if you want to add it to the 'face' object:

face.data.materials.append(mat)
$\endgroup$
8
  • $\begingroup$ Hi, I have tried your code. It works until creating a new material but it doesn't change base color into using vertex color and to be honest I still don't quite understand what adding the vertex color node is. thanks $\endgroup$ Mar 4, 2020 at 10:03
  • $\begingroup$ @Wanttobepro, what do you want to do or know exactly? I mean you said "what adding the vertex color node is." so? Could you give a hand made example of the node tree you want to have? $\endgroup$
    – lemon
    Mar 4, 2020 at 10:08
  • $\begingroup$ Sorry for being not clear. I hope the updated post will make it clear what I want $\endgroup$ Mar 4, 2020 at 10:15
  • $\begingroup$ @Wanttobepro, ok, thank for your edit. Answer is updated. $\endgroup$
    – lemon
    Mar 4, 2020 at 10:20
  • $\begingroup$ I encounter a bit of problem, when I tried your code, my model turn into black and below the vertex color it has red label, you can check my updated post. When I command the vc.layer name it become perfect as what I wanted. So can you help me explain why? $\endgroup$ Mar 4, 2020 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .