Advertisement
QuantumWarpCode

Blender Audio Visualizer V1

Jul 9th, 2016
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. import bpy
  2. from math import radians
  3.  
  4. rows = 5
  5. columns = 1
  6.  
  7. file_path = "Location_Here"
  8.  
  9. r = 0
  10. c = 0
  11.  
  12. highFreq = 1000
  13. lowFreq = 0
  14.  
  15. lowF = 0.1
  16. mediumF = 0.4
  17. highF = 0.5
  18.  
  19. endFrame = 600
  20.  
  21. bpy.context.area.type = 'TIMELINE'
  22. bpy.ops.anim.change_frame(frame=0)
  23. bpy.data.scenes[-1].frame_start = 1
  24. bpy.data.scenes[-1].frame_end = endFrame
  25.  
  26. for i in range (0, rows*columns):
  27.     if c == columns:
  28.         r += 1
  29.         c = 0
  30.    
  31.     bpy.ops.mesh.primitive_cube_add(location = (r * 2 - rows + 1, c * 2 - columns + 1, 1))
  32.     bpy.context.scene.cursor_location = bpy.context.active_object.location
  33.     bpy.context.scene.cursor_location.z -= 1
  34.     bpy.ops.object.origin_set(type = 'ORIGIN_CURSOR')
  35.    
  36.     bpy.context.active_object.scale.x = 0.5
  37.     bpy.context.active_object.scale.y = 0.5
  38.     bpy.context.active_object.scale.z = 5
  39.     bpy.ops.object.transform_apply( scale = True)
  40.    
  41.     bpy.ops.anim.keyframe_insert_menu(type = 'Scaling')
  42.     bpy.context.active_object.animation_data.action.fcurves [0]. lock = True
  43.     bpy.context.active_object.animation_data.action.fcurves [1]. lock = True
  44.    
  45.     bpy.context.area.type = 'GRAPH_EDITOR'
  46.    
  47.     step = ((highFreq - lowFreq) / (rows * columns)) + lowFreq
  48.     bpy.ops.graph.sound_bake(filepath = file_path, low = i * step, high = i * step + step)
  49.    
  50.     bpy.context.active_object.animation_data.action.fcurves [2]. lock = True
  51.    
  52.     materialName = "Visualizer " + str(i)
  53.     bpy.ops.material.new(name = materialName)
  54.     material = bpy.data.materials[-1]
  55.     nodes = material.node_tree.nodes
  56.     diffuse = nodes.get("Diffuse BSDF")
  57.    
  58.     for f in range(1, endFrame):
  59.         bpy.context.scene.frame_set(f)
  60.         height = bpy.context.active_object.scale.z
  61.         diffuse.inputs[0].keyframe_insert("default_value")
  62.         if height > highF:
  63.             diffuse.inputs[0].default_value = 1.000, 0.000, 0.000 , 1.000
  64.         elif height > mediumF:
  65.             colorG = 1 - (height - mediumF) / (highF - mediumF)
  66.             diffuse.inputs[0].default_value = 1.000, colorG, 0.000, 1.000
  67.         elif height > lowF:
  68.             colorR = (height - lowF) / (mediumF - lowF)
  69.             diffuse.inputs[0].default_value = colorR, 1.000, 0.000, 1.000
  70.         else:
  71.             diffuse.inputs[0].default_value = 0.000, 1.000, 0.000, 1.000
  72.         diffuse.inputs[0].keyframe_insert("default_value")
  73.    
  74.     bpy.context.active_object.active_material = material
  75.    
  76.     bpy.context.area.type = 'TIMELINE'
  77.     bpy.ops.anim.change_frame(frame=0)
  78.    
  79.     c +=1
  80.  
  81. bpy.ops.object.camera_add(location = (0, -20, 5), rotation = (radians(87.5), 0, 0))
  82. bpy.ops.object.lamp_add(location = (0, 0, 9), rotation = (radians(50), radians(-50), 0), type = 'SUN')
  83.  
  84. bpy.context.area.type = 'VIEW_3D'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement