DistributedCameraOV.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from panda3d.core import *
  2. from panda3d.direct import *
  3. from direct.distributed.DistributedObjectOV import DistributedObjectOV
  4. class DistributedCameraOV(DistributedObjectOV):
  5. def __init__(self, cr):
  6. DistributedObjectOV.__init__(self, cr)
  7. self.parent = 0
  8. self.fixtures = []
  9. self.accept('refresh-fixture', self.refreshFixture)
  10. def delete(self):
  11. self.ignore('escape')
  12. self.ignore('refresh-fixture')
  13. DistributedObjectOV.delete(self)
  14. def getObject(self):
  15. return self.cr.getDo(self.getDoId())
  16. def setCamParent(self, doId):
  17. self.parent = doId
  18. def setFixtures(self, fixtures):
  19. self.fixtures = fixtures
  20. def storeToFile(self, name):
  21. f = open('cameras-%s.txt' % name, 'w')
  22. f.writelines(self.getObject().pack())
  23. f.close()
  24. def unpackFixture(self, data):
  25. data = data.strip().replace('Camera','')
  26. pos,hpr,fov = eval(data)
  27. return pos,hpr,fov
  28. def loadFromFile(self, name):
  29. self.b_setFixtures([])
  30. f = open('cameras-%s.txt' % name, 'r')
  31. for line in f.readlines():
  32. pos,hpr,fov = self.unpackFixture(line)
  33. self.addFixture([pos[0],pos[1],pos[2],
  34. hpr[0],hpr[1],hpr[2],
  35. fov[0],fov[1],
  36. 'Standby'])
  37. f.close()
  38. def refreshFixture(self, id, data):
  39. pos,hpr,fov = self.unpackFixture(data)
  40. fixture = self.fixtures[id]
  41. fixture = [pos[0],pos[1],pos[2],
  42. hpr[0],hpr[1],hpr[2],
  43. fov[0],fov[1],
  44. fixture[8]]
  45. # distributed only
  46. self.d_setFixtures(self.fixtures)
  47. def b_setFixtures(self, fixtures):
  48. self.getObject().setFixtures(fixtures)
  49. self.setFixtures(fixtures)
  50. self.d_setFixtures(fixtures)
  51. def d_setFixtures(self, fixtures):
  52. self.sendUpdate('setFixtures', [fixtures])
  53. def addFixture(self, fixture, index = None):
  54. if index is not None:
  55. self.fixtures.insert(index, fixture)
  56. else:
  57. self.fixtures.append(fixture)
  58. self.b_setFixtures(self.fixtures)
  59. return self.fixtures.index(fixture)
  60. def blinkFixture(self, index):
  61. if index < len(self.fixtures):
  62. fixture = self.fixtures[index]
  63. fixture[6] = 'Blinking'
  64. self.b_setFixtures(self.fixtures)
  65. def standbyFixture(self, index):
  66. if index < len(self.fixtures):
  67. fixture = self.fixtures[index]
  68. fixture[6] = 'Standby'
  69. self.b_setFixtures(self.fixtures)
  70. def testFixture(self, index):
  71. if index < len(self.fixtures):
  72. self.getObject().testFixture(index)
  73. def removeFixture(self, index):
  74. self.fixtures.pop(index)
  75. self.b_setFixtures(self.fixtures)
  76. def saveFixture(self, index = None):
  77. """
  78. Position the camera with ~oobe, then call this to save its telemetry.
  79. """
  80. parent = self.getObject().getCamParent()
  81. pos = base.cam.getPos(parent)
  82. hpr = base.cam.getHpr(parent)
  83. return self.addFixture([pos[0], pos[1], pos[2],
  84. hpr[0], hpr[1], hpr[2],
  85. 'Standby'],
  86. index)
  87. def startRecording(self):
  88. self.accept('escape', self.stopRecording)
  89. for fixture in self.fixtures:
  90. fixture[6] = 'Recording'
  91. self.b_setFixtures(self.fixtures)
  92. def stopRecording(self):
  93. self.ignore('escape')
  94. for fixture in self.fixtures:
  95. fixture[6] = 'Standby'
  96. self.b_setFixtures(self.fixtures)