Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / basic-2.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
soya.init()
37
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
38
39
# Creates the scene.
40
41
scene = soya.World()
42
43
# Loads the sword model.
44
45
sword_model = soya.Shape.get("sword")
46
47
48
# Create a class of rotating volume. Soya if fully Object Oriented, and almost all
49
# Soya classes can be extended.
50
# Here, our class inherits from soya.Volume, and so can display a model.
51
52
class RotatingVolume(soya.Volume):
53
54
# The advance_time method is called repeatedly by the Idler, for all object in the scene.
55
# In Soya, the time unit is the "round" ; one round is 30 milliseconds (default value).
56
# The proportion argument of advance_time is the proportion of a round that has occured:
57
# e.g. 0.3 means that 30% of a round has occured since last call, i.e. 9 milliseconds.
58
59
# advance_time should be limited to animation code, and not decision code. We'll see
60
# another method for decision stuff in basic-3.py.
61
62
def advance_time(self, proportion):
63
64
# Calls the super implementation of advance_time. This IS NEEDED, as some Soya object
65
# already have an advance_time method.
66
67
soya.Volume.advance_time(self, proportion)
68
69
# Rotates the object around Y axis. The angle is proportional to proportion because
70
# the more time has been spent, the more we want to rotate, in order to achieve a
71
# smooth animation.
72
73
# Almost every rotations or moves that occurs in advance_time should be proportional
74
# to proportion.
75
76
self.rotate_lateral(proportion * 5.0)
77
78
79
# Creates a rotating volume in the scene, using the sword model.
80
81
sword = RotatingVolume(scene, sword_model)
82
83
# Creates a light.
84
85
light = soya.Light(scene)
86
light.set_xyz(0.5, 0.0, 2.0)
87
88
# Creates a camera.
89
90
camera = soya.Camera(scene)
91
camera.z = 3.0
92
soya.set_root_widget(camera)
93
94
soya.Idler(scene).idle()
95
96
97