Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. # Author: Eric Jang
  2. # 2013-Jul-17
  3.  
  4. # The problem with using audioWave + time nodes + audioWave bonus tool in Maya
  5. # to drive animation is that we can't really do spectral analysis (high pass/low pass filters)
  6. # short of implementing it using hypershade nodes, so we can't get really fine-tuned animation
  7. # ... but Python is good for this!
  8.  
  9. # caveat: this was coded up in one night so it may be unstable. Use with caution.
  10. # future work: instead of just the simple amplitude, we can perform FFT/spectral analysis to extract
  11. # more interesting information, tweak the audio driving keys
  12.  
  13. # audio track: arbitrary wav file. currently uses the "World Engine" noise from Man of Steel.
  14.  
  15. import maya.cmds as cmds
  16. import numpy as np
  17. import random
  18. from scipy.io import wavfile
  19.  
  20. # settings
  21. l_sph = 12 # num copies of the sphere along each dimension
  22. xy_s = 3 # x or y distance of each sphere from each other
  23. afile = "/home/cello/pythonfun/wubwub/zod.wav"
  24. width = 3
  25. maxR = 10
  26.  
  27. ###
  28. rate, a = wavfile.read(afile)
  29. a = np.average(a, 1) # merge audio channels into mono
  30. nf = int((float(len(a)) / rate * 24) / width) #number of frames we will animate
  31. spf = int(rate/24 * width) # audio samples per animation frame (1837). Increase width to smooth animation out
  32. # normalized sine "bump" + some array comprehension
  33. # see the heatmap_sample to get an idea of what it looks like
  34. dz_s = [ [np.sin(i*np.pi/l_sph)*np.sin(j*np.pi/l_sph)*5.0 for j in range(l_sph)] for i in range(l_sph)]
  35.  
  36. orig = cmds.ls("JuliaSphere")[0] # we don't animate this one
  37. for i in range(l_sph):
  38. for j in range(l_sph):
  39. print(str(i*l_sph+j))
  40. cp = cmds.duplicate(orig)
  41. pos = [i*xy_s, j*xy_s, 0.0]
  42. cmds.makeIdentity(cp, apply=True, t=1, r=1, s=1, n=0, pn=1) #freeze transforms
  43. for frame in range(1,nf+1):
  44. s = (frame -1 ) * spf #start of audio sampling frame
  45. amp = np.average(np.abs(a[s:s+spf]))/1000 - 4
  46. z = amp * dz_s[i][j] + random.uniform(0, 1) # a bit of jitter
  47. dr = [random.uniform(0.01, maxR) * amp for k in range(3)]
  48. if z < 0.0:
  49. z *= 0.3
  50. cmds.move(0, 0, z, cp, rpr=True)
  51. cmds.xform(cp, ws=True, ro=dr) #translate it, rotate it
  52. for key in ["translateZ", "rotateX", "rotateY", "rotateZ"]:
  53. cmds.setKeyframe(cp, at=key, t=frame * width)# set keyframe
  54. cmds.move(pos[0], pos[1], 0, cp, rpr=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement