2
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
# Lesson 007: Worlds : a solar system
20
# This lesson is about grouping object. It explains how to include an object
21
# (called the child) inside an other (called the parent), so as moving the parent
22
# will move its children.
24
import sys, soya, soya.soya3d as soya3d, soya.model as model, soya.sphere as sphere
28
scene = soya3d.World()
30
import os, os.path, sys
31
model.Image.PATH = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "data", "images")
33
# Creates the sun's material
35
fire = model.Material()
36
fire.tex_filename = "lava.png"
39
# The sun is inserted inside the scene
41
# The soya.sphere module works in a similar way than soya.cube.
43
sun = soya3d.World(scene)
44
sun.set_shape(sphere.Sphere(None, fire).shapify())
47
# Creates the earth's material
49
ground = model.Material()
50
ground.diffuse = (0.4, 0.7, 1.0, 1.0)
53
# The earth is inserted inside the sun !
54
# This implies than moving / rotating the sun moves the earth too!
56
earth = soya3d.World(sun)
57
earth.set_shape(sphere.Sphere(None, ground).shapify())
58
earth.scale(0.6, 0.6, 0.6)
62
# Creates the moon's material
64
crater = model.Material()
65
crater.diffuse = (0.4, 0.4, 0.4, 1.0)
68
# The moon is inserted inside the earth !
69
# This implies than moving / rotating the sun and/or the earth moves the moon too!
71
moon = soya3d.World(earth)
72
moon.set_shape(sphere.Sphere(None, crater).shapify())
73
moon.scale(0.4, 0.4, 0.4)
76
light = soya3d.Light(scene)
77
light.set_xyz(0.0, 0.2, 2.0)
79
camera = soya3d.Camera(scene)
81
soya.set_root_widget(camera)
84
# Makes it less simetric !
86
sun .turn_lateral (25.0)
87
earth.turn_vertical(35.0)
95
sun .turn_incline(3.0)
96
earth.turn_incline(5.0)
97
moon .turn_incline(3.0)
102
# The Soya object hierarchy is different to the branching philosophy used by
103
# several other 3D engine (such as java 3D).
105
# It can be compared to an UNIX file system:
106
# - Worlds are directories
107
# - Faces, Lights, Cameras,... are files
108
# - Volumes behave like (soft) links (to a directory): they can be used to have
109
# the same directory at different location (exactely like, e.g., /usr/src/linux
110
# is a link to /usr/src/linux-2.4.19-16mdk/ on my Linux file system).
112
# A World behaves exactely as a list (and technically it IS A list): you can use
113
# the append, remove,... methods, and use a World in a "for" loop
114
# (e.g.: for child in world: print child)
116
# Additionnal methods include:
117
# - world.recursive() : returns a list of ALL the children in this world,
118
# including children of its children, and so forth (e.g. sun.recursive()
119
# contains the earth but also the moon).
121
# Each 3D object can have a "name" property, which may be used to find them.
122
# The following methods deals with names:
123
# - world[name], world.__getitem__(name) : gets the child of the given name
124
# - world.search(name_regexp) : gets all the children whose name match the
126
# - world.search_all(name_regexp) : like search, but recursively.
127
# - world.subitem(name_path) : like __getitem__, but name_path may be composed
128
# of several names, separated by dots: "child.sub_child.leaf".
130
# You can get the World which contains a 3D object by using its "parent" property.
131
# The get_root() method returns the parent's parent's parent... and so forth
132
# (= often called "scene" in tutorials)