MorpheusArch

Audio Visualisation Script [Mirror]

Sep 12th, 2016
206
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Audio visualisation script
  2. #
  3. #Created by sirrandalot for Blender 2.71
  4. #Modified by TobiLaForge for Blender 2.76
  5. #Modified by sirrandalot for Blender 2.77
  6. #
  7. #Feel free to modify this script to suit your needs
  8.  
  9. import bpy
  10. import math
  11.  
  12. #Here are the variables you can change if you want to quickly
  13. #change your result
  14. #================================================================================
  15. #================================================================================
  16. filename = "file_name.mp3"   #of course you can use other formats than mp3
  17. filepath = "path goes here"  
  18. #always use two backslashes \\ instead of one \
  19. #Example:  "C:\\Users\\Marco\\Desktop"
  20.                        
  21.                        
  22. deleteSoundInSequencer = True  #Delete the sound strip currently in blender?                  
  23. addSoundToSequencer = True  #Import the sound strip into blender?
  24.  
  25. startFrame = 1 #The frame on which to start the song
  26.  
  27. bars = 64 #number of bars
  28. hScale = 8.0 #vertical scale of the bars
  29. width = 0.8 #width scale of the bars
  30. xDist = 2.25 #horizontal distance between bars (for non-radial vosualiser)
  31.  
  32. radial = False #True if you want the visualiser to be radial, false otherwise
  33. radius = 20.0 #Radius of the circle
  34. #================================================================================
  35. #================================================================================
  36.  
  37.  
  38.  
  39. #full path and name
  40. filepathAndName = filepath + "\\" + filename
  41.  
  42. #Set the window context to the sequencer
  43. bpy.context.area.type = 'SEQUENCE_EDITOR'
  44.  
  45. #Delete sound strip
  46. if deleteSoundInSequencer:
  47.     bpy.ops.sequencer.select_all(action='SELECT')
  48.     bpy.ops.sequencer.delete()
  49.  
  50. #Add sound strip
  51. if addSoundToSequencer:
  52.     bpy.ops.sequencer.sound_strip_add(filepath=filepathAndName,frame_start=startFrame, channel=1)
  53.  
  54.  
  55. #Set the window context to the Default 3D window
  56. bpy.context.area.type = 'VIEW_3D'
  57. #Set Keyframe and Cursor location to default
  58. bpy.data.scenes["Scene"].frame_current=startFrame
  59. bpy.context.scene.cursor_location=(0,0,0)
  60.  
  61.  
  62. #Number of half steps each bar will cover (approximately)
  63. noteStep = 120.0/bars
  64.  
  65. #Twelfth root of 2
  66. a = 2**(1.0/12.0)
  67.  
  68. #start frequencies
  69. l = 0.0
  70. h = 16.0
  71.  
  72. print('--------------------')
  73.  
  74. #Iterate through the number of bars
  75. for i in range(0, bars):
  76.     #Add a plane and set it's origin to one of its edges
  77.     bpy.ops.mesh.primitive_plane_add(location = (0, 1, 0))
  78.     bpy.context.scene.cursor_location = bpy.context.active_object.location
  79.     bpy.context.scene.cursor_location.y -= 1
  80.     bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
  81.    
  82.     loc = [0.0, 0.0, 0.0]
  83.    
  84.     #If this is a radial visualiser
  85.     if radial:
  86.         #Rotate bars in equal angles around circle with given radius
  87.         angle = -2*i*math.pi/(bars)
  88.         bpy.context.active_object.rotation_euler[2] = angle
  89.         loc = [-math.sin(angle)*radius, math.cos(angle)*radius, 0]
  90.     else:
  91.         loc[0] = i*xDist
  92.    
  93.     #Set the bar's corrent clocation
  94.     bpy.context.active_object.location = (loc[0], loc[1], loc[2])
  95.     #Set origin to one of its edges again
  96.     bpy.context.scene.cursor_location = (loc[0], loc[1], loc[2])
  97.     bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
  98.    
  99.     #Scale the plane on the x and y axis, then apply the transformation
  100.     bpy.context.active_object.scale.x = width
  101.     bpy.context.active_object.scale.y = hScale
  102.     bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
  103.    
  104.     #Insert a scaling keyframe and lock the x and z axis
  105.     bpy.ops.anim.keyframe_insert_menu(type='Scaling')
  106.     bpy.context.active_object.animation_data.action.fcurves[0].lock = True
  107.     bpy.context.active_object.animation_data.action.fcurves[2].lock = True
  108.    
  109.     #Set the window context to the graph editor
  110.     bpy.context.area.type = 'GRAPH_EDITOR'
  111.    
  112.     #Expression to determine the frequency ranges of the bars
  113.     l = h
  114.     h = l*(a**noteStep)
  115.    
  116.     #Print the current bar and frequency range to the console/terminal
  117.     print('Bar ' + str(i) + ': ' + str(l) + ' to ' + str(h))
  118.    
  119.     #Bake that range of frequencies to the current plane (along the y axis)
  120.     bpy.ops.graph.sound_bake(filepath=filepathAndName, low = (l), high = (h))
  121.    
  122.     #Lock the y axis
  123.     bpy.context.active_object.animation_data.action.fcurves[1].lock = True
  124.  
  125. #Change Back to Text Editor to change Equalizer Settings
  126. bpy.context.area.type = 'TEXT_EDITOR'
  127.  
  128. #Set Animation time to song length
  129. bpy.context.scene.frame_end = bpy.context.scene.sequence_editor.sequences_all[filename].frame_final_duration + startFrame
RAW Paste Data