-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniverse.py
executable file
·416 lines (378 loc) · 11.2 KB
/
universe.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#! /usr/bin/env python
from OpenGLContext.scenegraph import *
import math, random
from time import time
from OpenGLContext import displaylist
from OpenGLContext.scenegraph.text import toolsfont
from OpenGLContext.scenegraph import imagetexture
import PIL
from PIL import Image
class Universe( object ):
def __init__( self ):
#create scene graph (universe)
self.uni = basenodes.sceneGraph()
#create one child for the universe... a galaxy
tmpGalaxy = []
t = basenodes.Transform(
translation = (0,0,0),
children = [],
)
t.name = "galaxy01"
tmpGalaxy.append(t)
self.uni.children = tmpGalaxy #now one galaxy added to the universe... access it via self.uni.children[0]
def addSys( self, name ):
"""adds solar system (session) to scenegraph"""
coords = []
#transform to hold connect line
l = basenodes.Transform(
children = [
basenodes.Shape(
geometry = basenodes.IndexedLineSet(
coord = basenodes.Coordinate(
point = coords,
),
coordIndex = range( len(coords)),
),
appearance = basenodes.Appearance(
material = basenodes.Material( emissiveColor = (0.5,0.5,0.5) ),
),
),
],
)
l.name = "line"
#transform to hold solar system
p = random.random()*120-60, random.random()*24-12, random.random()*88-44 #position
r = random.random()*math.radians(180) #rotation
t = basenodes.Transform(
translation = p,
children = [ l ],
)
t.name = name
t.sensititive = 0
#add solar system to galaxy
tmpHolder = self.uni.children[0].children
tmpHolder.append(t)
self.uni.children[0].children = tmpHolder
def addPlanet( self, name, moonsNum, title, geom, urls ):
"""adds planet (webpage) to last solar system"""
#make moon children transform
moonChildren = []
for m in range( moonsNum ):
#make the translation Transform for the moon and add it to the rotation Transform
orbitD = random.random()*.5+.2
mTrans = basenodes.Transform(
translation = ( orbitD,0,0 ),
rotation = ( 0,0,0,0 ),
children = [
basenodes.Shape(
geometry = basenodes.Box( size = ( 0.01,0.2,0.2) ),
appearance = basenodes.Appearance(
material = basenodes.Material( emissiveColor = (.8,.8,.8) ),
),
),
],
)
#make the rotation Transforms for the moon
mRatio = random.random()*0.5
mRotY = basenodes.Transform(
translation = (0,0,0 ),
rotation = (0,1,0,0),
children = [ mTrans ],
)
mr = random.random()*math.radians(360) #rotation
mRotX = basenodes.Transform(
translation = (0,0,0 ),
rotation = (mRatio,0,0,mr),
children = [ mRotY ],
)
speedMultiplyer = random.random()*2.5+0.5
mRotX.name = speedMultiplyer
moonChildren.append( mRotX )
#make moons Transform
moons = basenodes.Transform(
translation = ( 0,0,0 ),
rotation = ( 0,0,0,0 ),
children = moonChildren
)
#make planet Transform
rad = random.random()*0.4+0.2 #size
#c = random.random(), random.random(), random.random() #color
plan = basenodes.Transform(
translation = ( 0,0,0 ),
children = [
basenodes.TouchSensor(),
basenodes.Shape(
geometry = basenodes.Box( size = (rad,rad,rad) ),
appearance = basenodes.Appearance(
material = basenodes.Material( emissiveColor = (.8,.8,.8) ),
),
),
]
)
#transform to hold new planet and moons
p = random.random()*16-8, random.random()*16-8, random.random()*16-8 #position
r = random.random()*math.radians(180) #rotation
planetAndMoons = basenodes.Transform(
translation = p,
rotation = (0,1,0,r),
children = [
plan,
moons
]
)
planetAndMoons.name = name
planetAndMoons.sensititive = 0
#made detail nodes
detailPlanet = self.makeSpiral( geom )
detailMoons = self.textureMoons( moons,urls )
xTrans = len(title.string[0])*-0.015
dTitle = basenodes.Transform(
translation = (xTrans,0.25,0),
rotation = (0,0,0,0),
children = [
basenodes.Shape(
geometry = title,
appearance = basenodes.Appearance(
material = basenodes.Material( emissiveColor = (.8,.8,.8) ),
),
)
]
)
dTitleBB = basenodes.Billboard(
axisOfRotation = (0,1,0),
children = [ dTitle ]
)
#make the detail leg of the switch
planetAndMoonsDetail = basenodes.Transform(
translation = p,
rotation = (0,1,0,0),
children = [
detailPlanet,
detailMoons,
dTitleBB
]
)
planetAndMoonsDetail.name = name
planetAndMoonsDetail.sensititive = 0
#make switch
detailSwitch = basenodes.Switch(
whichChoice = 0,
choice = [
planetAndMoons,
planetAndMoonsDetail,
],
)
#add (planet) to solar sys
tmpHolder = self.uni.children[0].children[-1].children
tmpHolder.append(detailSwitch)
self.uni.children[0].children[-1].children = tmpHolder
#calc coors of all planets in solarsys
coordsHolder = []
for planet in self.uni.children[0].children[-1].children:
if len(coordsHolder) == 0:
coordsHolder.append( planet.translation )
else:
coordsHolder.append( planet.choice[0].translation )
#put those calced points in the line object in the solarsys, and update length of line
self.uni.children[0].children[-1].children[0].children[0].geometry.coord.point = coordsHolder
self.uni.children[0].children[-1].children[0].children[0].geometry.coordIndex = range( 1,len(coordsHolder))
def findPlanet( self, objPath ):
"""helper function to find the planet in the scenegraph from a path"""
"""returns object you are looking for, or null if not found"""
print objPath
tmpSys = objPath[2].name
tmpPlanet = objPath[4].name
#find the solar system position
sysPos = -1
sysNum = 0
for sys in self.uni.children[0].children:
if sys.name == tmpSys:
sysPos = sysNum
sysNum += 1
if sysPos >= 0:
#find planet in solar system
planetPos = -1
planetNum = 0
for planet in self.uni.children[0].children[sysPos].children:
if planetNum == 0:
if planet.name == tmpPlanet:
planetPos = planetNum
else:
if planet.choice[0].name == tmpPlanet:
planetPos = planetNum
planetNum += 1
if planetPos >= 0:
print "found planet at:", planetPos
return self.uni.children[0].children[sysPos].children[planetPos]
else:
print "didn't find planet in system"
return ()
else:
print "did't find system"
return ()
def findSys( self, objPath ):
"""helper function to find solarsys in the scenegraph from a path"""
"""returns object you are looking for, or null if not found"""
print objPath
tmpSys = objPath[2].name
#find the solar system position
sysPos = -1
sysNum = 0
for sys in self.uni.children[0].children:
if sys.name == tmpSys:
sysPos = sysNum
sysNum += 1
if sysPos >= 0:
print "Found sys at: ", sysPos
return self.uni.children[0].children[sysPos]
else:
print "did't find system"
return ()
def renderDetail( self, obj ):
"""takes in an object, switches out its geometry for detailed geometry"""
#make the detail geometry
"""
t = basenodes.Transform(
translation = ( 0,0,0 ),
rotation = ( 0,0,0,0 ),
children = [
basenodes.Shape(
geometry = geom,
appearance = basenodes.Appearance(
material = basenodes.Material( emissiveColor = (.8,.8,.8) ),
#texture = imagetexture.ImageTexture( url = "200px-Bradypus.jpg"),
),
),
],
)
"""
obj.whichChoice = 1
"""
t = self.makeSpiral( geom )
obj.choice[1].children[0] = t
for m in range( len(obj.children[1].children) ):
try:
tmpTex = imagetexture.ImageTexture( url = [urls[m]] )
except IOError:
print 'oops'
obj.choice[1].children[1].children[m].children[0].children[0].children[0].appearance.texture = tmpTex
"""
def unRenderDetail( self, obj ):
"""puts the simple geometry back into the passes object"""
obj.whichChoice = 0
"""
obj.children[0].children = [
basenodes.TouchSensor(),
basenodes.Shape(
geometry = basenodes.Box( size = (0.3,0.3,0.3) ),
appearance = basenodes.Appearance(
material = basenodes.Material( emissiveColor = (.8,.8,.8) ),
),
),
]"""
def rotatePlanets( self, ang ):
"""rotate all the planets"""
for s in range( len( self.uni.children[0].children )):
for p in range( 1, len( self.uni.children[0].children[s].children ) ):
#get the set choice from the detail whitch in the planet
choice = self.uni.children[0].children[s].children[p].whichChoice
#rotate planet
self.uni.children[0].children[s].children[p].choice[choice].children[0].rotation = ( 0,1,0,ang )
#rotate all moons
for m in self.uni.children[0].children[s].children[p].choice[choice].children[1].children:
rotHolder = m.children[0].rotation
rotHolder[3] = ang * m.name
m.children[0].rotation = rotHolder
def rotateMoons( self, ang ):
"""rotates the moons on all the planets"""
def textureMoons( self,plainMoons,urls ):
texMoons = []
for m in plainMoons.children:
tmpTex = ()
aspect = 1.0
try:
imgURL = urls[ len(texMoons) ]
#tmpImg = Image.open( imgURL )
#aspect = tmpImg.size[0]/tmpImg.size[1]
tmpTex = imagetexture.ImageTexture( url = [imgURL] )
except IOError:
print 'OOOOOOOPs IO ERROR ON FILE'
#make the translation Transform for the moon and add it to the rotation Transform
mTrans = basenodes.Transform(
translation = m.children[0].children[0].translation,
rotation = ( 0,0,0,0 ),
children = [
basenodes.Shape(
geometry = basenodes.Box( size = ( 0.01,0.2,0.2*aspect) ),
appearance = basenodes.Appearance(
texture = tmpTex,
),
),
],
)
#make the rotation Transforms for the moon
mRotY = basenodes.Transform(
translation = (0,0,0 ),
rotation = (0,1,0,0),
children = [ mTrans ],
)
mr = random.random()*math.radians(360) #rotation
mRotX = basenodes.Transform(
translation = (0,0,0 ),
rotation = m.rotation,
children = [ mRotY ],
)
speedMultiplyer = random.random()*2.5+0.5
mRotX.name = m.name
texMoons.append( mRotX )
#make moons Transform
moonsWithTexture = basenodes.Transform(
translation = ( 0,0,0 ),
rotation = ( 0,0,0,0 ),
children = texMoons
)
return moonsWithTexture
def makeSpiral( self,geom ):
radi = 0.2
sPoints = []
zRot = 720
while zRot<2880:
xyRot = zRot/20
zxRad = math.sin( math.radians( xyRot ) )*radi
yD = math.cos( math.radians( xyRot ) )*radi
zD = math.cos( math.radians( zRot ) )*zxRad
xD = math.sin( math.radians( zRot ) )*zxRad
t = basenodes.Transform(
translation = ( xD,yD,zD ),
rotation = ( 0,1,.05,math.radians( zRot ) ),
children = [
basenodes.Shape(
geometry = geom[ len(sPoints) ],
appearance = basenodes.Appearance(
material = basenodes.Material( emissiveColor = (.8,.8,.8) ),
),
),
],
)
sPoints.append( t )
zRot += 11
spher = basenodes.Transform(
translation = ( 0,0,0 ),
rotation = ( 0,0,0,0 ),
children = [
basenodes.Shape(
geometry = basenodes.Sphere( radius = 0.17 ),
appearance = basenodes.Appearance(
material = basenodes.Material( diffuseColor = (0,0,0) ),
),
),
],
)
sPoints.append( spher )
spiral = basenodes.Transform(
translation = ( 0,0,0 ),
rotation = ( 0,0,0,0 ),
children = sPoints
)
print "NUM NODES IN SPIRSL: ", len(sPoints)
return spiral