File graph1.py added (mode: 100644) (index 0000000..0e6be51) |
|
1 |
|
#!BPY |
|
2 |
|
|
|
3 |
|
""" |
|
4 |
|
Blendergraph project |
|
5 |
|
""" |
|
6 |
|
|
|
7 |
|
__author__ = "Catalin(ux) M. BOIE" |
|
8 |
|
__url__ = ("https://rocketgit.com/user/catalinux/blendergraph") |
|
9 |
|
__version__ = "v0.1" |
|
10 |
|
|
|
11 |
|
import bpy |
|
12 |
|
import math |
|
13 |
|
|
|
14 |
|
def mat_emission(ob, name, color, strength): |
|
15 |
|
bpy.ops.material.new() |
|
16 |
|
mat = bpy.data.materials[-1] |
|
17 |
|
mat.name = 'Emission-' + name |
|
18 |
|
mat.use_nodes = True |
|
19 |
|
mat.node_tree.nodes.clear() |
|
20 |
|
p = mat.node_tree.nodes.new(type='ShaderNodeEmission') |
|
21 |
|
p.inputs['Color'].default_value = color |
|
22 |
|
p.inputs['Strength'].default_value = strength |
|
23 |
|
p.location = (-200, 0) |
|
24 |
|
xout = p.outputs['Emission'] |
|
25 |
|
p = mat.node_tree.nodes.new(type='ShaderNodeOutputMaterial') |
|
26 |
|
xin = p.inputs['Surface'] |
|
27 |
|
mat.node_tree.links.new(xin, xout) |
|
28 |
|
ob.data.materials.append(mat) |
|
29 |
|
|
|
30 |
|
|
|
31 |
|
def mat_glossy(ob, name, color, roughness): |
|
32 |
|
bpy.ops.material.new() |
|
33 |
|
mat = bpy.data.materials[-1] |
|
34 |
|
mat.name = 'Glossy-' + name |
|
35 |
|
mat.use_nodes = True |
|
36 |
|
mat.node_tree.nodes.clear() |
|
37 |
|
p = mat.node_tree.nodes.new(type='ShaderNodeBsdfGlossy') |
|
38 |
|
p.inputs['Color'].default_value = color |
|
39 |
|
p.inputs['Roughness'].default_value = roughness |
|
40 |
|
p.location = (-200, 0) |
|
41 |
|
xout = p.outputs['BSDF'] |
|
42 |
|
p = mat.node_tree.nodes.new(type='ShaderNodeOutputMaterial') |
|
43 |
|
xin = p.inputs['Surface'] |
|
44 |
|
mat.node_tree.links.new(xin, xout) |
|
45 |
|
ob.data.materials.append(mat) |
|
46 |
|
|
|
47 |
|
|
|
48 |
|
def mat_glass(ob, name, color, roughness, ior): |
|
49 |
|
bpy.ops.material.new() |
|
50 |
|
mat = bpy.data.materials[-1] |
|
51 |
|
mat.name = 'Glass-' + name |
|
52 |
|
mat.use_nodes = True |
|
53 |
|
mat.node_tree.nodes.clear() |
|
54 |
|
p = mat.node_tree.nodes.new(type='ShaderNodeBsdfGlass') |
|
55 |
|
p.inputs['Color'].default_value = color |
|
56 |
|
p.inputs['Roughness'].default_value = roughness |
|
57 |
|
p.inputs['IOR'].default_value = ior |
|
58 |
|
p.location = (-200, 0) |
|
59 |
|
xout = p.outputs['BSDF'] |
|
60 |
|
p = mat.node_tree.nodes.new(type='ShaderNodeOutputMaterial') |
|
61 |
|
xin = p.inputs['Surface'] |
|
62 |
|
mat.node_tree.links.new(xin, xout) |
|
63 |
|
ob.data.materials.append(mat) |
|
64 |
|
|
|
65 |
|
|
|
66 |
|
def draw_bar(name, value, pos, percent): |
|
67 |
|
# text |
|
68 |
|
bpy.ops.object.text_add(enter_editmode=False, location=(pos, 0, 0)) |
|
69 |
|
txt_ob = bpy.context.object |
|
70 |
|
txt_ob.name = 'MyText-' + name |
|
71 |
|
txt_ob.data.align_x = 'LEFT' |
|
72 |
|
txt_ob.data.extrude = 0.02 |
|
73 |
|
txt_ob.data.space_character = 1.1 |
|
74 |
|
txt_ob.data.space_line = 1.2 |
|
75 |
|
txt_ob.data.bevel_depth = 0.01 |
|
76 |
|
txt_ob.data.body = name |
|
77 |
|
txt_ob.rotation_euler = 23 * 2 * math.pi / 360, 0, math.pi / 2 |
|
78 |
|
mat_glossy(txt_ob, 'TextMat-' + name, (0, 0, 1, 1), 1) |
|
79 |
|
# bar |
|
80 |
|
bpy.ops.mesh.primitive_cube_add(enter_editmode=False) |
|
81 |
|
bar_ob = bpy.context.object |
|
82 |
|
bar_ob.name = 'bar-' + name |
|
83 |
|
h = percent / 50 |
|
84 |
|
bar_ob.scale = .5, .5, h |
|
85 |
|
bar_ob.location = (pos - .4, -1, h) |
|
86 |
|
mat_glossy(bar_ob, 'BarMat-' + name, (0, 1, 0, 1), .9) |
|
87 |
|
|
|
88 |
|
|
|
89 |
|
def main(): |
|
90 |
|
scene = bpy.context.scene |
|
91 |
|
|
|
92 |
|
# Clean |
|
93 |
|
# Deselect all |
|
94 |
|
bpy.ops.object.select_all(action='DESELECT') |
|
95 |
|
for obj in scene.objects: |
|
96 |
|
obj.select_set(True) |
|
97 |
|
bpy.ops.object.delete() |
|
98 |
|
|
|
99 |
|
# Add camera |
|
100 |
|
bpy.ops.object.camera_add(enter_editmode = False, location=(20, 20, 10), rotation=(73 * 2 * math.pi / 360, 0, (90 + 45) * 2 * math.pi / 360)) |
|
101 |
|
cam_ob = bpy.context.object |
|
102 |
|
cam_ob.name = 'MyCam' |
|
103 |
|
cam_ob.data.type = 'ORTHO' |
|
104 |
|
cam_ob.data.ortho_scale = 20 |
|
105 |
|
scene.camera = cam_ob |
|
106 |
|
|
|
107 |
|
|
|
108 |
|
# Add lights |
|
109 |
|
bpy.ops.mesh.primitive_plane_add(location=(0, 0, 20)) |
|
110 |
|
lamp_ob = bpy.context.object |
|
111 |
|
lamp_ob.name = 'Lamp1' |
|
112 |
|
lamp_ob.scale = (15, 15, 15) |
|
113 |
|
mat_emission(lamp_ob, 'LampMat', (1, 1, 1, 1), 3) |
|
114 |
|
|
|
115 |
|
|
|
116 |
|
# World |
|
117 |
|
w = bpy.context.scene.world |
|
118 |
|
w.use_nodes = True |
|
119 |
|
w.color = (1, 1, 1) |
|
120 |
|
###if w: |
|
121 |
|
###w.horizon_color = (0.0, 0.1, 0.3) |
|
122 |
|
###w.zenith_color = (0, .2, .6) |
|
123 |
|
###w.use_sky_blend = True |
|
124 |
|
|
|
125 |
|
|
|
126 |
|
# Floor |
|
127 |
|
bpy.ops.mesh.primitive_plane_add(location=(0, 0, -0.2)) |
|
128 |
|
plane_ob = bpy.context.object |
|
129 |
|
plane_ob.name = 'Floor' |
|
130 |
|
plane_ob.scale = [10, 10, 10] |
|
131 |
|
mat_glossy(plane_ob, 'FloorMat', (1, 0, 0, 1), 0.8) |
|
132 |
|
|
|
133 |
|
|
|
134 |
|
# Compute heights |
|
135 |
|
values = { 'Fedora': 1, 'Ubuntu': 10, 'Arch': 20, 'DSL': 5 } |
|
136 |
|
no_of_values = len(values) |
|
137 |
|
max = 0 |
|
138 |
|
for k, v in values.items(): |
|
139 |
|
if v > max: |
|
140 |
|
max = v |
|
141 |
|
|
|
142 |
|
# compute percents |
|
143 |
|
percents = {} |
|
144 |
|
for k, v in values.items(): |
|
145 |
|
percents[k] = v * 100 / max |
|
146 |
|
print("percents: " + str(percents)) |
|
147 |
|
|
|
148 |
|
# Draw bars |
|
149 |
|
pos = - len(values) * 3 / 2 + 1 |
|
150 |
|
for k, v in values.items(): |
|
151 |
|
draw_bar(k + " (" + str(v) + ")", v, pos, percents[k]) |
|
152 |
|
pos += 3 |
|
153 |
|
|
|
154 |
|
print("Saving...") |
|
155 |
|
bpy.ops.wm.save_as_mainfile(filepath = 'graph1.blend') |
|
156 |
|
|
|
157 |
|
print("Rendering...") |
|
158 |
|
|
|
159 |
|
scene.frame_start = 1 |
|
160 |
|
scene.frame_end = 1 |
|
161 |
|
|
|
162 |
|
render = scene.render |
|
163 |
|
render.use_file_extension = True |
|
164 |
|
render.engine = 'CYCLES' |
|
165 |
|
render.filepath = 'graph1-#####' |
|
166 |
|
render.resolution_x = 1000 |
|
167 |
|
render.resolution_y = 500 |
|
168 |
|
render.resolution_percentage = 100 |
|
169 |
|
render.image_settings.file_format = 'PNG' |
|
170 |
|
render.image_settings.color_mode = 'RGBA' |
|
171 |
|
|
|
172 |
|
bpy.ops.render.render(write_still = True, animation = True) |
|
173 |
|
|
|
174 |
|
|
|
175 |
|
if __name__ == '__main__': |
|
176 |
|
main() |