-
Notifications
You must be signed in to change notification settings - Fork 13
/
light.py
200 lines (138 loc) · 6.01 KB
/
light.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import FreeCAD
import FreeCADGui
from pivy import coin
import arch_texture_utils.faceset_utils as faceset_utils
class Light():
def __init__(self, obj):
obj.Proxy = self
self.setProperties(obj)
def setProperties(self, obj):
pl = obj.PropertiesList
if not 'Color' in pl:
obj.addProperty("App::PropertyColor", "Color", "Light",
"The color of the light").Color = (1.0, 0.94, 0.91)
if not 'Intensity' in pl:
obj.addProperty("App::PropertyFloatConstraint", "Intensity", "Light",
"The intensity of the light").Intensity = (1.0, 0.0, 1.0, 0.1)
def onDocumentRestored(self, obj):
self.setProperties(obj)
def __getstate__(self):
return None
def __setstate__(self,state):
return None
def execute(self, ob):
pass
class ViewProviderLight:
def __init__(self, vobj):
vobj.Proxy = self
self.setProperties(vobj)
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
# Setting properties does not work here as the pl is not filled yet :/
sceneGraph = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
self.switch = coin.SoSwitch()
self.geometryNode = coin.SoSeparator()
self.transform = coin.SoTransform()
self.material = coin.SoMaterial()
self.coinLight = self.createLightInstance()
actualGeometry = self.createGeometry()
self.geometryNode.addChild(self.transform)
self.geometryNode.addChild(self.material)
if actualGeometry is not None:
self.geometryNode.addChild(actualGeometry)
sceneGraph.insertChild(self.coinLight, 1)
self.switch.addChild(self.geometryNode)
vobj.addDisplayMode(self.switch, "Light")
self.updateLightVisibility()
self.updateDirection()
self.updateColor()
self.updateIntensity()
# self.updateGeometryVisibility()
def setProperties(self, vobj):
pl = vobj.PropertiesList
if not 'ShowGeometry' in pl:
vobj.addProperty("App::PropertyBool", "ShowGeometry", "Light",
"Show the light as geometry in the 3D View").ShowGeometry = True
def createLightInstance(self):
raise NotImplementedError()
def createGeometry(self):
raise NotImplementedError()
def getDisplayModes(self,obj):
'''Return a list of display modes.'''
return ["Light"]
def getDefaultDisplayMode(self):
'''Return the name of the default display mode. It must be defined in getDisplayModes.'''
return "Light"
def updateData(self, fp, prop):
if prop in ['HorizontalRotation', 'VerticalRotation']:
self.updateDirection()
elif prop == 'Color':
self.updateColor()
elif prop == 'Intensity':
self.updateIntensity()
elif prop == 'Location':
self.updateLocation()
def onChanged(self, vp, prop):
if prop == 'Visibility':
self.updateLightVisibility()
elif prop == 'ShowGeometry':
self.updateGeometryVisibility()
def __getstate__(self):
return None
def __setstate__(self,state):
return None
def updateLocation(self):
if hasattr(self.Object, 'Location'):
location = self.Object.Location
coinVector = coin.SbVec3f(location.x, location.y, location.z)
self.coinLight.location.setValue(coinVector)
self.updateGeometryLocation(coinVector)
def updateDirection(self):
if hasattr(self.Object, 'HorizontalRotation') and hasattr(self.Object, 'VerticalRotation'):
horizontalRotation = self.Object.HorizontalRotation
verticalRotation = self.Object.VerticalRotation
# Defaults to south to north
direction = FreeCAD.Vector(0, 1, 0)
# Negative Z because we want the light to follow the real sun path from East to west.
rotateZ = FreeCAD.Rotation(FreeCAD.Vector(0, 0, -1), horizontalRotation)
# Negative X because a positive rotation should let the light point downwards
rotateX = FreeCAD.Rotation(FreeCAD.Vector(-1, 0, 0), verticalRotation)
rotation = rotateZ.multiply(rotateX)
direction = rotateZ.multVec(direction)
direction = rotateX.multVec(direction)
coinVector = coin.SbVec3f(direction.x, direction.y, direction.z)
self.coinLight.direction.setValue(coinVector)
self.updateGeometryDirection(rotation)
#print('h: %s, v: %s, d: %s' % (horizontalRotation, verticalRotation, direction))
def updateLightVisibility(self):
self.coinLight.on.setValue(self.ViewObject.Visibility)
def updateColor(self):
color = self.Object.Color
r = color[0]
g = color[1]
b = color[2]
coinColor = coin.SbColor(r, g, b)
self.coinLight.color.setValue(coinColor)
self.material.diffuseColor.setValue(coinColor)
def updateIntensity(self):
self.coinLight.intensity.setValue(self.Object.Intensity)
def updateGeometryLocation(self, coinVector):
self.transform.translation.setValue(coinVector)
def updateGeometryDirection(self, rotation):
# Nothing to do right now. Subclasses override this
pass
def updateGeometryVisibility(self):
if not hasattr(self, 'switch') or self.switch is None:
return
if self.ViewObject.ShowGeometry:
self.switch.whichChild.setValue(0)
else:
self.switch.whichChild.setValue(coin.SO_SWITCH_NONE)
def createDirectionalLight():
obj = FreeCAD.ActiveDocument.addObject("App::FeaturePython", "DirectionalLight")
light = DirectionalLight(obj)
ViewProviderDirectionalLight(obj.ViewObject)
return obj
if __name__ == "__main__":
createDirectionalLight()