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

« back to all changes in this revision

Viewing changes to tutorial/lesson-008.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 008: File management : saving materials and worlds
19
 
 
20
 
# This lesson explains how to save materials and worlds into files.
21
 
 
22
 
# The Soya file management system assumes that:
23
 
# - ALL your materials and your worlds are saved in a directory. The directory
24
 
#   (sometimes called "PATH") can be different for worlds and material, or it can
25
 
#   be the same.
26
 
 
27
 
 
28
 
# In this lesson, we are going to save the textured cube of the lesson 4.
29
 
# This implies saving the material, and the cube.
30
 
 
31
 
import soya, soya.soya3d as soya3d, soya.model as model, soya.cube as cube
32
 
import os, os.path, sys
33
 
 
34
 
soya.init()
35
 
 
36
 
scene = soya3d.World()
37
 
 
38
 
 
39
 
# Defines the texture, material and world directories. We use the directory
40
 
# "./data" in the tutorial directory. You may use 3 different directories.
41
 
 
42
 
data_dir = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "data")
43
 
 
44
 
model.Image   .PATH = os.path.join(data_dir, "images")
45
 
model.Material.PATH = os.path.join(data_dir, "materials")
46
 
soya3d.World  .PATH = os.path.join(data_dir, "worlds")
47
 
model.Shape   .PATH = os.path.join(data_dir, "shapes")
48
 
 
49
 
 
50
 
# Creates the textured cube -- see lesson 4
51
 
 
52
 
material = model.Material()
53
 
 
54
 
material.tex_filename = "block2.tga"
55
 
 
56
 
cube = cube.Cube(scene, material)
57
 
cube.rotate_vertical(30.0)
58
 
 
59
 
light = soya3d.Light(scene)
60
 
light.set_xyz(0.0, 0.2, 1.0)
61
 
 
62
 
camera = soya3d.Camera(scene)
63
 
camera.z = 2.0
64
 
soya.set_root_widget(camera)
65
 
 
66
 
 
67
 
# Defines the material's and the cube's filename. The filename is understood to
68
 
# be located in the material / world PATH.
69
 
 
70
 
material.filename = "wall_material"
71
 
cube    .filename = "textured_cube"
72
 
 
73
 
 
74
 
# Saves the material and the cube -- they'll be saved in the corresponding PATH
75
 
# The save() method can receive an optional FILENAME parameter, which can be used
76
 
# to save the object into an arbitrary location.
77
 
# The ".data" extension is automatically added to the file, but it must not be
78
 
# present in filenames !!!
79
 
 
80
 
material.save()
81
 
cube    .save()
82
 
 
83
 
 
84
 
# Renders and waits
85
 
 
86
 
soya.render()
87
 
 
88
 
import time
89
 
while 1: time.sleep(0.1)
90
 
 
91
 
 
92
 
# Notice that, if we had saved the cube without saving the material,
93
 
# Soya3D would have saved the material along with the cube !!!
94
 
 
95
 
# Saving the material in a separed file is interesting, though: it allows several
96
 
# worlds to share the same materials.
97
 
 
98
 
# The availables() methods of the Material / World class can be used to get a list
99
 
# of the names of all the available Materials / Worlds.