Skip to content

Instantly share code, notes, and snippets.

@p2or
Last active May 21, 2023 19:13
Show Gist options
  • Save p2or/7bf9dc37b3278e35486cbd0d6bcee160 to your computer and use it in GitHub Desktop.
Save p2or/7bf9dc37b3278e35486cbd0d6bcee160 to your computer and use it in GitHub Desktop.
VColor Shader #Blender
# http://blender.stackexchange.com/questions/69671/how-to-convert-this-vertex-paint-into-a-useable-material-scripted
import bpy
# context object
obj = bpy.context.object
# create the material
mat = bpy.data.materials.new('MaterialName')
mat.diffuse_color = (0.1,0.0,0.7) # viewport color
mat.use_nodes = True
# Assign it to object
if obj.data.materials:
# assign to 1st material slot
obj.data.materials[0] = mat
else:
# no slots
obj.data.materials.append(mat)
# get the material nodes
nodes = mat.node_tree.nodes
# clear all nodes to start clean
for node in nodes:
nodes.remove(node)
# create vertex color node
node_attribute = nodes.new(type='ShaderNodeAttribute')
node_attribute.attribute_name = "Col"
node_attribute.location = -200,0
# create emission node
node_diffuse = nodes.new(type='ShaderNodeBsdfDiffuse')
node_diffuse.inputs[0].default_value = (0,1,0,1) # green RGBA
node_diffuse.inputs[1].default_value = 5.0 # strength
node_diffuse.location = 0,0
# create output node
node_output = nodes.new(type='ShaderNodeOutputMaterial')
node_output.location = 200,0
# link nodes
links = mat.node_tree.links
link_att_diff = links.new(node_attribute.outputs[0], node_diffuse.inputs[0])
link_dif_out = links.new(node_diffuse.outputs[0], node_output.inputs[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment