FunctionInterval.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """FunctionInterval module: contains the FunctionInterval class"""
  2. from PandaModules import *
  3. from Interval import *
  4. from MessengerGlobal import *
  5. class FunctionInterval(Interval):
  6. functionIntervalNum = 1
  7. # special methods
  8. def __init__(self, function, name = None):
  9. """__init__(function, name = None)
  10. """
  11. duration = 0.0
  12. self.prevt = 0.0
  13. self.function = function
  14. if (name == None):
  15. name = 'FunctionInterval-%d' % FunctionInterval.functionIntervalNum
  16. FunctionInterval.functionIntervalNum += 1
  17. Interval.__init__(self, name, duration)
  18. def setT(self, t, entry=0):
  19. """ setT(t, entry)
  20. Go to time t
  21. """
  22. if (t < 0):
  23. self.prevt = t
  24. return
  25. elif (t == 0) or (self.prevt < 0):
  26. self.function()
  27. self.prevt = 0.0
  28. ### FunctionInterval subclass for throwing events ###
  29. class EventInterval(FunctionInterval):
  30. # Initialization
  31. def __init__(self, event, sentArgs=[]):
  32. """__init__(name, sentArgs)
  33. """
  34. def sendFunc(event = event, sentArgs = sentArgs):
  35. messenger.send(event, sentArgs)
  36. # Create function interval
  37. FunctionInterval.__init__(self, sendFunc, name = event)
  38. ### Function Interval subclass for adjusting scene graph hierarchy ###
  39. class ParentInterval(FunctionInterval):
  40. # PosInterval counter
  41. parentIntervalNum = 1
  42. # Initialization
  43. def __init__(self, nodePath, parent, name = None):
  44. """__init__(nodePath, parent, name)
  45. """
  46. def reparentFunc(nodePath = nodePath, parent = parent):
  47. nodePath.reparentTo(parent)
  48. # Determine name
  49. if (name == None):
  50. name = 'ParentInterval-%d' % ParentInterval.parentIntervalNum
  51. ParentInterval.parentIntervalNum += 1
  52. # Create function interval
  53. FunctionInterval.__init__(self, reparentFunc, name = name)
  54. ### Function Interval subclasses for instantaneous pose changes ###
  55. class PosInterval(FunctionInterval):
  56. # PosInterval counter
  57. posIntervalNum = 1
  58. # Initialization
  59. def __init__(self, nodePath, pos, duration = 0.0,
  60. name = None, other = None):
  61. """__init__(nodePath, pos, duration, name)
  62. """
  63. # Create function
  64. def posFunc(np = nodePath, pos = pos, other = other):
  65. if other:
  66. np.setPos(other, pos)
  67. else:
  68. np.setPos(pos)
  69. # Determine name
  70. if (name == None):
  71. name = 'PosInterval-%d' % PosInterval.posIntervalNum
  72. PosInterval.posIntervalNum += 1
  73. # Create function interval
  74. FunctionInterval.__init__(self, posFunc, name = name)
  75. class HprInterval(FunctionInterval):
  76. # HprInterval counter
  77. hprIntervalNum = 1
  78. # Initialization
  79. def __init__(self, nodePath, hpr, duration = 0.0,
  80. name = None, other = None):
  81. """__init__(nodePath, hpr, duration, name)
  82. """
  83. # Create function
  84. def hprFunc(np = nodePath, hpr = hpr, other = other):
  85. if other:
  86. np.setHpr(other, hpr)
  87. else:
  88. np.setHpr(hpr)
  89. # Determine name
  90. if (name == None):
  91. name = 'HprInterval-%d' % HprInterval.hprIntervalNum
  92. HprInterval.hprIntervalNum += 1
  93. # Create function interval
  94. FunctionInterval.__init__(self, hprFunc, name = name)
  95. class ScaleInterval(FunctionInterval):
  96. # ScaleInterval counter
  97. scaleIntervalNum = 1
  98. # Initialization
  99. def __init__(self, nodePath, scale, duration = 0.0,
  100. name = None, other = None):
  101. """__init__(nodePath, scale, duration, name)
  102. """
  103. # Create function
  104. def scaleFunc(np = nodePath, scale = scale, other = other):
  105. if other:
  106. np.setScale(other, scale)
  107. else:
  108. np.setScale(scale)
  109. # Determine name
  110. if (name == None):
  111. name = 'ScaleInterval-%d' % ScaleInterval.scaleIntervalNum
  112. ScaleInterval.scaleIntervalNum += 1
  113. # Create function interval
  114. FunctionInterval.__init__(self, scaleFunc, name = name)
  115. class PosHprInterval(FunctionInterval):
  116. # PosHprInterval counter
  117. posHprIntervalNum = 1
  118. # Initialization
  119. def __init__(self, nodePath, pos, hpr, duration = 0.0,
  120. name = None, other = None):
  121. """__init__(nodePath, pos, hpr, duration, name)
  122. """
  123. # Create function
  124. def posHprFunc(np = nodePath, pos = pos, hpr = hpr, other = other):
  125. if other:
  126. np.setPosHpr(other, pos, hpr)
  127. else:
  128. np.setPosHpr(pos, hpr)
  129. # Determine name
  130. if (name == None):
  131. name = 'PosHprInterval-%d' % PosHprInterval.posHprIntervalNum
  132. PosHprInterval.posHprIntervalNum += 1
  133. # Create function interval
  134. FunctionInterval.__init__(self, posHprFunc, name = name)
  135. class PosHprScaleInterval(FunctionInterval):
  136. # PosHprInterval counter
  137. posHprScaleIntervalNum = 1
  138. # Initialization
  139. def __init__(self, nodePath, pos, hpr, scale, duration = 0.0,
  140. name = None, other = None):
  141. """__init__(nodePath, pos, hpr, scale, duration, other, name)
  142. """
  143. # Create function
  144. def posHprScaleFunc(np=nodePath, pos=pos, hpr=hpr, scale=scale,
  145. other = other):
  146. if other:
  147. np.setPosHprScale(other, pos, hpr, scale)
  148. else:
  149. np.setPosHprScale(pos, hpr, scale)
  150. # Determine name
  151. if (name == None):
  152. name = ('PosHprScale-%d' %
  153. PosHprScaleInterval.posHprScaleIntervalNum)
  154. PosHprScaleInterval.posHprScaleIntervalNum += 1
  155. # Create function interval
  156. FunctionInterval.__init__(self, posHprScaleFunc, name = name)