Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / basic-2-sage.pyOpen in CoCalc
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
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
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
34
import sys, os, os.path, soya
35
36
from sage.all import *
37
38
soya.init()
39
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
40
41
# Creates the scene.
42
43
scene = soya.World()
44
45
# Loads the sword model.
46
47
sword_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
54
class 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
64
65
66
# Calls the super implementation of advance_time. This IS NEEDED, as some Soya object
67
68
69
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
self.move(proporition)
80
81
82
# Creates a rotating volume in the scene, using the sword model.
83
84
sword = RotatingVolume(scene, sword_model)
85
86
# Creates a light.
87
88
light = soya.Light(scene)
89
light.set_xyz(0.5, 0.0, 2.0)
90
91
# Creates a camera.
92
93
camera = soya.Camera(scene)
94
camera.z = 3.0
95
soya.set_root_widget(camera)
96
97
def go():
98
soya.Idler(scene).idle()
99
100
101