#!BPY
"""
Blendergraph project
"""
__author__ = "Catalin(ux) M. BOIE"
__url__ = ("https://rocketgit.com/user/catalinux/blendergraph")
__version__ = "v0.1"
import bpy
import math
def mat_emission(ob, name, color, strength):
bpy.ops.material.new()
mat = bpy.data.materials[-1]
mat.name = 'Emission-' + name
mat.use_nodes = True
mat.node_tree.nodes.clear()
p = mat.node_tree.nodes.new(type='ShaderNodeEmission')
p.inputs['Color'].default_value = color
p.inputs['Strength'].default_value = strength
p.location = (-200, 0)
xout = p.outputs['Emission']
p = mat.node_tree.nodes.new(type='ShaderNodeOutputMaterial')
xin = p.inputs['Surface']
mat.node_tree.links.new(xin, xout)
ob.data.materials.append(mat)
def mat_glossy(ob, name, color, roughness):
bpy.ops.material.new()
mat = bpy.data.materials[-1]
mat.name = 'Glossy-' + name
mat.use_nodes = True
mat.node_tree.nodes.clear()
p = mat.node_tree.nodes.new(type='ShaderNodeBsdfGlossy')
p.inputs['Color'].default_value = color
p.inputs['Roughness'].default_value = roughness
p.location = (-200, 0)
xout = p.outputs['BSDF']
p = mat.node_tree.nodes.new(type='ShaderNodeOutputMaterial')
xin = p.inputs['Surface']
mat.node_tree.links.new(xin, xout)
ob.data.materials.append(mat)
def mat_glass(ob, name, color, roughness, ior):
bpy.ops.material.new()
mat = bpy.data.materials[-1]
mat.name = 'Glass-' + name
mat.use_nodes = True
mat.node_tree.nodes.clear()
p = mat.node_tree.nodes.new(type='ShaderNodeBsdfGlass')
p.inputs['Color'].default_value = color
p.inputs['Roughness'].default_value = roughness
p.inputs['IOR'].default_value = ior
p.location = (-200, 0)
xout = p.outputs['BSDF']
p = mat.node_tree.nodes.new(type='ShaderNodeOutputMaterial')
xin = p.inputs['Surface']
mat.node_tree.links.new(xin, xout)
ob.data.materials.append(mat)
def draw_bar(name, value, pos, percent):
# text
bpy.ops.object.text_add(enter_editmode=False, location=(pos, 0, 0))
txt_ob = bpy.context.object
txt_ob.name = 'MyText-' + name
txt_ob.data.align_x = 'LEFT'
txt_ob.data.extrude = 0.02
txt_ob.data.space_character = 1.1
txt_ob.data.space_line = 1.2
txt_ob.data.bevel_depth = 0.01
txt_ob.data.body = name
txt_ob.rotation_euler = 23 * 2 * math.pi / 360, 0, math.pi / 2
mat_glossy(txt_ob, 'TextMat-' + name, (0, 0, 1, 1), 1)
# bar
bpy.ops.mesh.primitive_cube_add(enter_editmode=False)
bar_ob = bpy.context.object
bar_ob.name = 'bar-' + name
h = percent / 50
bar_ob.scale = .5, .5, h
bar_ob.location = (pos - .4, -1, h)
mat_glossy(bar_ob, 'BarMat-' + name, (0, 1, 0, 1), .9)
def main():
scene = bpy.context.scene
# Clean
# Deselect all
bpy.ops.object.select_all(action='DESELECT')
for obj in scene.objects:
obj.select_set(True)
bpy.ops.object.delete()
# Add camera
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))
cam_ob = bpy.context.object
cam_ob.name = 'MyCam'
cam_ob.data.type = 'ORTHO'
cam_ob.data.ortho_scale = 20
scene.camera = cam_ob
# Add lights
bpy.ops.mesh.primitive_plane_add(location=(0, 0, 20))
lamp_ob = bpy.context.object
lamp_ob.name = 'Lamp1'
lamp_ob.scale = (15, 15, 15)
mat_emission(lamp_ob, 'LampMat', (1, 1, 1, 1), 3)
# World
w = bpy.context.scene.world
w.use_nodes = True
w.color = (1, 1, 1)
###if w:
###w.horizon_color = (0.0, 0.1, 0.3)
###w.zenith_color = (0, .2, .6)
###w.use_sky_blend = True
# Floor
bpy.ops.mesh.primitive_plane_add(location=(0, 0, -0.2))
plane_ob = bpy.context.object
plane_ob.name = 'Floor'
plane_ob.scale = [10, 10, 10]
mat_glossy(plane_ob, 'FloorMat', (1, 0, 0, 1), 0.8)
# Compute heights
values = { 'Fedora': 1, 'Ubuntu': 10, 'Arch': 20, 'DSL': 5 }
no_of_values = len(values)
max = 0
for k, v in values.items():
if v > max:
max = v
# compute percents
percents = {}
for k, v in values.items():
percents[k] = v * 100 / max
print("percents: " + str(percents))
# Draw bars
pos = - len(values) * 3 / 2 + 1
for k, v in values.items():
draw_bar(k + " (" + str(v) + ")", v, pos, percents[k])
pos += 3
print("Saving...")
bpy.ops.wm.save_as_mainfile(filepath = 'graph1.blend')
print("Rendering...")
scene.frame_start = 1
scene.frame_end = 1
render = scene.render
render.use_file_extension = True
render.engine = 'CYCLES'
render.filepath = 'graph1-#####'
render.resolution_x = 1000
render.resolution_y = 500
render.resolution_percentage = 100
render.image_settings.file_format = 'PNG'
render.image_settings.color_mode = 'RGBA'
bpy.ops.render.render(write_still = True, animation = True)
if __name__ == '__main__':
main()