DistributedCameraOV.py 3.6 KB

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