CoCalc Public Fileswww / talks / 2006-05-09-sage-digipen / tutorial / basic_2_sage.py
Author: William A. Stein
1# Soya 3D tutorial
2# Copyright (C) 2001-2004 Jean-Baptiste LAMY
3#
4# This program is free software; you can redistribute it and/or modify
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
19# basic-2: Time management : a rotating 3D model
20
21# This lesson is the same as basic-1.py, except that the model rotates.
22
23# You'll learn about time management and rotation.
24
25# Basic Soya's rotation functions are :
26#
27# rotate_lateral  : rotates around the Y axis (=in the horizontal plane)
28# rotate_vertical : rotates around the X axis
29# rotate_incline  : rotates around the Z axis
30
31
32# Imports and inits Soya (see lesson basic-1.py).
33
34import sys, os, os.path, soya
35
36from sage.all import *
37
38soya.init()
39soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
40
41# Creates the scene.
42
43scene = soya.World()
44
46
47sword_model = soya.Shape.get("sword")
48
49
50# Create a class of rotating volume. Soya if fully Object Oriented, and almost all
51# Soya classes can be extended.
52# Here, our class inherits from soya.Volume, and so can display a model.
53
54class RotatingVolume(soya.Volume):
55
56  # The advance_time method is called repeatedly by the Idler, for all object in the scene.
57  # In Soya, the time unit is the "round" ; one round is 30 milliseconds (default value).
58  # The proportion argument of advance_time is the proportion of a round that has occured:
59  # e.g. 0.3 means that 30% of a round has occured since last call, i.e. 9 milliseconds.
60
61  # advance_time should be limited to animation code, and not decision code.  We'll see
62  # another method for decision stuff in basic-3.py.
63
65
66    # Calls the super implementation of advance_time. This IS NEEDED, as some Soya object
68
70
71    # Rotates the object around Y axis. The angle is proportional to proportion because
72    # the more time has been spent, the more we want to rotate, in order to achieve a
73    # smooth animation.
74
75    # Almost every rotations or moves that occurs in advance_time should be proportional
76    # to proportion.
77
78    self.rotate_lateral(proportion * 5.0)
79
80
81# Creates a rotating volume in the scene, using the sword model.
82
83sword = RotatingVolume(scene, sword_model)
84
85# Creates a light.
86
87light = soya.Light(scene)
88light.set_xyz(0.5, 0.0, 2.0)
89
90# Creates a camera.
91
92camera = soya.Camera(scene)
93camera.z = 3.0
94soya.set_root_widget(camera)
95
96def go():
97  soya.Idler(scene).idle()
98
99
100