walk.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import hou
  2. import math
  3. # /////////////////////////////////////// utils
  4. def lerp(t, a, b):
  5. return a + t * (b - a)
  6. def roundVec(v, fac, seed):
  7. #seed = random.random()*0.1
  8. seed = 0.1 * seed - 0.05 * seed
  9. x = (round(v[0] * fac + seed)) / fac
  10. y = (round(v[1] * fac + seed)) / fac
  11. z = (round(v[2] * fac + seed)) / fac
  12. return hou.Vector3(x, y, z)
  13. def find_nearest(v, node, maxdist=3):
  14. # TODO probably take the nearest topmost point
  15. geo = node.displayNode().geometry()
  16. nearest = [maxdist, v]
  17. for p in geo.points():
  18. dist = p.position().distanceTo(v)
  19. if dist < nearest[0]:
  20. nearest[0] = dist
  21. nearest[1] = p.position()
  22. return nearest[1]
  23. # /////////////////////////////////////// setup
  24. def leg_capturepos(leg):
  25. p = leg.capture_pos
  26. t = hou.hmath.buildTranslate(p[0], p[1], p[2])
  27. return t * PARENT_WT
  28. def place_legs():
  29. for no, leg in enumerate(LEGS):
  30. # print leg.name(), no
  31. p = leg_capturepos(leg)
  32. leg.restpos = p.extractTranslates()
  33. def move_legs_gnd():
  34. # distribute_legs()
  35. place_legs()
  36. for no, leg in enumerate(LEGS):
  37. lt = hou.Vector3(leg.parmTuple("t").eval())
  38. next_p = find_nearest(lt, GROUND)
  39. dist = next_p.distanceTo(leg.restpos)
  40. if dist > COMFORT_LENGTH:
  41. # print 'setting', leg
  42. next_restp = find_nearest(leg.restpos, GROUND)
  43. t = hou.hmath.buildTranslate(
  44. next_restp[0], next_restp[1], next_restp[2])
  45. leg.setParmTransform(t)
  46. else:
  47. pass
  48. #t = hou.hmath.buildTranslate(next_p[0], next_p[1], next_p[2])
  49. # leg.setParmTransform(t)
  50. def remove_keyframes():
  51. if hou.frame() == 1:
  52. """
  53. delete all keyframes if rewound
  54. (@ frame 1)
  55. """
  56. for leg in LEGS:
  57. parm = leg.parmTuple("t")
  58. parm.deleteAllKeyframes()
  59. def set_keys():
  60. """
  61. Set keyframe for all legs
  62. """
  63. frame = hou.frame()
  64. if frame == CAPTURE_FRAME:
  65. return
  66. for leg in LEGS:
  67. pos = hou.Vector3(leg.parmTuple("t").eval())
  68. kfx = hou.Keyframe()
  69. kfx.setFrame(frame)
  70. kfx.setValue(pos[0])
  71. kfy = hou.Keyframe()
  72. kfy.setFrame(frame)
  73. kfy.setValue(pos[1])
  74. kfz = hou.Keyframe()
  75. kfz.setFrame(frame)
  76. kfz.setValue(pos[2])
  77. tchanx = hou.parm('../' + leg.name() + '/tx')
  78. tchany = hou.parm('../' + leg.name() + '/ty')
  79. tchanz = hou.parm('../' + leg.name() + '/tz')
  80. tchanx.setKeyframe(kfx)
  81. tchany.setKeyframe(kfy)
  82. tchanz.setKeyframe(kfz)
  83. # /////////////////////////////////////// globals
  84. THIS_NODE = hou.pwd()
  85. PARENT = THIS_NODE.inputs()[0]
  86. LEGS = hou.node('../l_parents').outputs()
  87. GROUND = hou.node('../ground')
  88. CAPTURE_FRAME = 1
  89. PARENT_WT = PARENT.worldTransform()
  90. COMFORT_LENGTH = 1
  91. def main():
  92. # This is our 'capture' frame
  93. if hou.frame() == CAPTURE_FRAME:
  94. """
  95. Basically, do nothing in the capture frame.
  96. Just store the actual leg position in the up vector
  97. to access it in any other frame as the capture position.
  98. """
  99. for leg in LEGS:
  100. # we use the up vector to store the capture pose per leg
  101. pos = hou.Vector3(leg.parmTuple("t").eval())
  102. leg.parmTuple('up').set(pos)
  103. print leg, pos
  104. leg.capture_pos = pos
  105. # move_legs_gnd()
  106. else:
  107. # do the dance
  108. for leg in LEGS:
  109. p = hou.Vector3(leg.parmTuple("up").eval())
  110. leg.capture_pos = p
  111. move_legs_gnd()
  112. set_keys()
  113. main()