-
Notifications
You must be signed in to change notification settings - Fork 67
/
seg.py
66 lines (48 loc) · 1.95 KB
/
seg.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import csv
import os
import bpy
import math
def newMaterial(id):
mat = bpy.data.materials.get(id)
if mat is None:
mat = bpy.data.materials.new(name=id)
mat.use_nodes = True
if mat.node_tree:
mat.node_tree.links.clear()
mat.node_tree.nodes.clear()
return mat
def newShader(id, type, r, g, b):
mat = newMaterial(id)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output = nodes.new(type='ShaderNodeOutputMaterial')
if type == "diffuse":
shader = nodes.new(type='ShaderNodeBsdfDiffuse')
nodes["Diffuse BSDF"].inputs[0].default_value = (r, g, b, 1)
elif type == "emission":
shader = nodes.new(type='ShaderNodeEmission')
nodes["Emission"].inputs[0].default_value = (r, g, b, 1)
nodes["Emission"].inputs[1].default_value = 1
elif type == "glossy":
shader = nodes.new(type='ShaderNodeBsdfGlossy')
nodes["Glossy BSDF"].inputs[0].default_value = (r, g, b, 1)
nodes["Glossy BSDF"].inputs[1].default_value = 0
links.new(shader.outputs[0], output.inputs[0])
return mat
def to_blender_color(c):
c = min(max(0, c), 255) / 255
return c / 12.92 if c < 0.04045 else math.pow((c + 0.055) / 1.055, 2.4)
filename = os.path.abspath(bpy.path.abspath("//color_coding_semantic_segmentation_classes.csv"))
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(f'\t Creating shader {row[8]}: {row[5]}')
r,g,b = row[5].strip("()").split(",")
newShader(row[8], "emission", to_blender_color(float(r)), to_blender_color(float(g)), to_blender_color(float(b)))
line_count += 1
print(f'Processed {line_count} lines.')