~jtaylor/ubuntu/oneiric/soya/fix-780305

« back to all changes in this revision

Viewing changes to tutorial/lesson-007.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Dequènes (Duck)
  • Date: 2005-01-30 09:55:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050130095506-f21p6v6cgaobhn5j
Tags: 0.9.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Soya 3D tutorial
2
 
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
3
 
#
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.
8
 
#
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.
13
 
#
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
17
 
 
18
 
# Lesson 007: Worlds : a solar system
19
 
 
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.
23
 
 
24
 
import sys, soya, soya.soya3d as soya3d, soya.model as model, soya.sphere as sphere
25
 
 
26
 
soya.init()
27
 
 
28
 
scene = soya3d.World()
29
 
 
30
 
import os, os.path, sys
31
 
model.Image.PATH = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "data", "images")
32
 
 
33
 
# Creates the sun's material
34
 
 
35
 
fire = model.Material()
36
 
fire.tex_filename  = "lava.png"
37
 
 
38
 
# Creates the sun
39
 
# The sun is inserted inside the scene
40
 
 
41
 
# The soya.sphere module works in a similar way than soya.cube.
42
 
 
43
 
sun = soya3d.World(scene)
44
 
sun.set_shape(sphere.Sphere(None, fire).shapify())
45
 
 
46
 
 
47
 
# Creates the earth's material
48
 
 
49
 
ground = model.Material()
50
 
ground.diffuse = (0.4, 0.7, 1.0, 1.0)
51
 
 
52
 
# Creates the earth
53
 
# The earth is inserted inside the sun !
54
 
# This implies than moving / rotating the sun moves the earth too!
55
 
 
56
 
earth = soya3d.World(sun)
57
 
earth.set_shape(sphere.Sphere(None, ground).shapify())
58
 
earth.scale(0.6, 0.6, 0.6)
59
 
earth.x = 4.0
60
 
 
61
 
 
62
 
# Creates the moon's material
63
 
 
64
 
crater = model.Material()
65
 
crater.diffuse = (0.4, 0.4, 0.4, 1.0)
66
 
 
67
 
# Creates the moon
68
 
# The moon is inserted inside the earth !
69
 
# This implies than moving / rotating the sun and/or the earth moves the moon too!
70
 
 
71
 
moon = soya3d.World(earth)
72
 
moon.set_shape(sphere.Sphere(None, crater).shapify())
73
 
moon.scale(0.4, 0.4, 0.4)
74
 
moon.x = -3.0
75
 
 
76
 
light = soya3d.Light(scene)
77
 
light.set_xyz(0.0, 0.2, 2.0)
78
 
 
79
 
camera = soya3d.Camera(scene)
80
 
camera.z = 10.0
81
 
soya.set_root_widget(camera)
82
 
 
83
 
 
84
 
# Makes it less simetric !
85
 
 
86
 
sun  .turn_lateral (25.0)
87
 
earth.turn_vertical(35.0)
88
 
 
89
 
 
90
 
import time
91
 
 
92
 
while 1:
93
 
  soya.render()
94
 
  
95
 
  sun  .turn_incline(3.0)
96
 
  earth.turn_incline(5.0)
97
 
  moon .turn_incline(3.0)
98
 
  
99
 
  time.sleep(0.02)
100
 
 
101
 
 
102
 
# The Soya object hierarchy is different to the branching philosophy used by
103
 
# several other 3D engine (such as java 3D).
104
 
 
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).
111
 
 
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)
115
 
 
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).
120
 
 
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
125
 
#   given regexp
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".
129
 
 
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)
133