-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vertex.py
36 lines (29 loc) · 1.2 KB
/
Vertex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import bpy
import random
# start in object mode
def load_vertex(obj: object, material: bpy.types.Material):
mesh = obj.data
if(material.node_tree.nodes.get("Color Attribute") is None):
Color_Attribute = material.node_tree.nodes.new('ShaderNodeVertexColor')
else:
Color_Attribute = material.node_tree.nodes.get("Color Attribute")
if not mesh.vertex_colors:
mesh.vertex_colors.new()
color_layer = mesh.vertex_colors["Col"]
# or you could avoid using the color_layer name
# color_layer = mesh.vertex_colors.active
i = 0
for poly in mesh.polygons:
for idx in poly.loop_indices:
r, g, b = [random.random() for i in range(3)]
color_layer.data[i].color = (r, g, b, 1.0)
i += 1
disp=material.node_tree.nodes["Principled BSDF"].inputs['Base Color']
material.node_tree.links.new(disp,Color_Attribute.outputs[0])
# set to vertex paint mode to see the result
# bpy.ops.object.mode_set(mode='VERTEX_PAINT')
def delete_vertex(material: bpy.types.Material):
if(material.node_tree.nodes.get("Color Attribute") is not None):
disp=material.node_tree.nodes["Color Attribute"].outputs['Color']
if(disp.links != ()):
material.node_tree.links.remove(disp.links[0])