DirectLights.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from PandaObject import *
  2. from DirectGeometry import *
  3. from string import lower
  4. class DirectLights(NodePath):
  5. def __init__(self, parent = None):
  6. # Initialize the superclass
  7. NodePath.__init__(self)
  8. # Use direct.group as default parent
  9. if parent == None:
  10. parent = direct.group
  11. # Create a node for the lights
  12. self.assign(parent.attachNewNode('DIRECT Lights'))
  13. # Create a light attribute
  14. self.la = LightAttribute()
  15. # Create a list of all active lights
  16. self.lightList = []
  17. self.nodePathList = []
  18. self.nameList = []
  19. # Counts of the various types of lights
  20. self.ambientCount = 0
  21. self.directionalCount = 0
  22. self.pointCount = 0
  23. self.spotCount = 0
  24. def __getitem__(self, index):
  25. return self.lightList[index]
  26. def __len__(self):
  27. return len(self.lightList)
  28. def getLightNodePath(self, index):
  29. return self.nodePathList[index]
  30. def getLightName(self, index):
  31. return self.nameList[index]
  32. def create(self, type):
  33. type = type.lower()
  34. if type == 'ambient':
  35. self.ambientCount += 1
  36. light = AmbientLight('ambient_' + `self.ambientCount`)
  37. light.setColor(VBase4(.3,.3,.3,1))
  38. elif type == 'directional':
  39. self.directionalCount += 1
  40. light = DirectionalLight('directional_' + `self.directionalCount`)
  41. light.setColor(VBase4(1))
  42. elif type == 'point':
  43. self.pointCount += 1
  44. light = PointLight('point_' + `self.pointCount`)
  45. light.setColor(VBase4(1))
  46. elif type == 'spot':
  47. self.spotCount += 1
  48. light = Spotlight('spot_' + `self.spotCount`)
  49. light.setColor(VBase4(1))
  50. else:
  51. print 'Invalid light type'
  52. return None
  53. # Add the new light
  54. self.addLight(light)
  55. # Turn it on as a default
  56. self.setOn(light)
  57. # Return the new light
  58. return light
  59. def createDefaultLights(self):
  60. self.create('ambient')
  61. self.create('directional')
  62. def addLight(self, light):
  63. # Attach node to self
  64. if isinstance(light, Spotlight):
  65. nodePath = self.attachNewNode(light.upcastToProjectionNode())
  66. else:
  67. nodePath = self.attachNewNode(light.upcastToNamedNode())
  68. name = light.getName()
  69. # Store it in the lists
  70. self.lightList.append(light)
  71. self.nodePathList.append(nodePath)
  72. self.nameList.append(name)
  73. # Send an event to all watching objects
  74. messenger.send('DirectLights_addLight', [light])
  75. def allOn(self):
  76. """ Turn on all DIRECT lights """
  77. base.initialState.setAttribute(LightTransition.getClassType(),
  78. self.la)
  79. def allOff(self):
  80. """ Turn off all DIRECT lights """
  81. base.initialState.clearAttribute(LightTransition.getClassType())
  82. def toggle(self):
  83. """ Toggles light attribute, but doesn't toggle individual lights """
  84. if base.initialState.hasAttribute(LightTransition.getClassType()):
  85. self.allOff()
  86. else:
  87. self.allOn()
  88. def setOnNum(self, index):
  89. self.setOn(self.lightList[index])
  90. def setOffNum(self, index):
  91. self.setOff(self.lightList[index])
  92. def setOn(self, light):
  93. self.la.setOn(light.upcastToLight())
  94. def setOff(self, light):
  95. self.la.setOff(light.upcastToLight())