Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / basic-1.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-1: Setting up : displaying a 3D model
20
21
# This is the first lesson of the Soya tutorial.
22
# This lesson just sets up everything and display a 3D model. In order to do that,
23
# we need :
24
# - a model,
25
# - a light,
26
# - a camera,
27
# - a scene, to group the three other 3D objects.
28
29
30
# Imports sys, os modules and the Soya module.
31
32
import sys, os, os.path, soya
33
34
# Initializes Soya (creates and displays the 3D window).
35
36
soya.init()
37
38
# Add the path "tutorial/data" to the list of soya data path. When soya loads some data,
39
# like a model or a texture, it always searches the data in soya.path.
40
# soya.path behaves like Python's sys.path.
41
# Soya's data directory should be organized as following :
42
# ./images : the image file
43
# ./materials : the materials (including textures, optimized forms of images)
44
# ./worlds : the model
45
# ./shapes : the optimized model
46
47
# Notice the use of sys.argv to get the directory where this script lives.
48
49
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
50
51
# Creates a scene. The scene is a World, which contains all the 3D elements we are
52
# about to render. A World is a 3D object that can contain other 3D objects (including
53
# other worlds) ; think to World as a group of 3D objects.
54
55
scene = soya.World()
56
57
# Loads the sword model (from file "tutorial/data/shapes/sword.data").
58
59
# A shape is an optimized model ; the sword shape we use here was designed in Blender.
60
61
# Shape.get is a static method that returns the object of the corresponding filename,
62
# and loads it if needed, i.e. if you call get a second time, it will return the same
63
# object instead of loading it again.
64
# Any dependancy of the model (e.g. materials) are loaded too.
65
66
sword_model = soya.Shape.get("sword")
67
68
# Create the model.
69
# A Volume displays a shape. The first argument of the Volume constructor is the
70
# parent of the new volume ; here we put the volume in the scene. The parent must be
71
# a World or a World derivative (or None).
72
# (this is a convention, similarly to Tkinter, where the first argument of a
73
# widget's constructor is the master).
74
# The second argument of the Volume constructor is the shape : our sword model.
75
76
sword = soya.Volume(scene, sword_model)
77
#sword = soya.World.get("sword")
78
#scene.add(sword)
79
#sword.advance_time = lambda p: sword.rotate_lateral(2.0)
80
81
# The default position is 0.0, 0.0, 0.0
82
# To view it better, we moves the sword to the right.
83
84
sword.x = 1.0
85
86
# Rotates the sword on the Y axis, of 60.0 degrees.
87
# (in Soya, all angles are in degrees).
88
89
sword.rotate_lateral(90.0)
90
91
# Creates a light in the scene (same convention: the first argument of the
92
# constructor is the parent) and moves it to (1.5, 2.0, 0.2).
93
94
light = soya.Light(scene)
95
light.set_xyz(0.5, 0.0, 2.0)
96
97
# Creates a camera in the scene and moves it to z = 5.0. The camera looks in the
98
# -Z direction, so, in this case, towards the cube.
99
#
100
# When relevant, Soya always considers the X direction to be on the right,
101
# the Y direction to be on the top and the -Z to be the front.
102
# (Using -Z for front seems odd, but using Z for front makes all coordinate systems
103
# indirect, which is a mathematical nightmare !)
104
105
camera = soya.Camera(scene)
106
camera.z = 2.0
107
108
# Say to Soya that the camera is what we want to be rendered.
109
110
soya.set_root_widget(camera)
111
112
# Uncomment this line to save a 320x240 screenshot in the results directory.
113
114
#soya.render(); soya.screenshot().resize((320, 240)).save(os.path.join(os.path.dirname(sys.argv[0]), "results", os.path.basename(sys.argv[0])[:-3] + ".jpeg"))
115
116
# Creates an 'Idler' for the scene, and launch it.
117
# The Idler is the object that manages the Soya's mainloop. It take care of :
118
# - looping
119
# - regulating the frame rate to 40 FPS
120
# - smoothing the animation
121
# - computing FPS
122
# - rendering the screen
123
124
soya.Idler(scene).idle()
125
126
127