1# Soya 3D tutorial
2# Copyright (C) 2001-2004 Jean-Baptiste LAMY
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.
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.
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
19# basic-1: Setting up : displaying a 3D model
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.
30# Imports sys, os modules and the Soya module.
32import sys, os, os.path, soya
34# Initializes Soya (creates and displays the 3D window).
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
47# Notice the use of sys.argv to get the directory where this script lives.
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.
55scene = soya.World()
57# Loads the sword model (from file "tutorial/data/shapes/sword.data").
59# A shape is an optimized model ; the sword shape we use here was designed in Blender.
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.
66sword_model = soya.Shape.get("sword")
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.
76sword = soya.Volume(scene, sword_model)
77#sword = soya.World.get("sword")
79#sword.advance_time = lambda p: sword.rotate_lateral(2.0)
81# The default position is 0.0, 0.0, 0.0
82# To view it better, we moves the sword to the right.
84sword.x = 1.0
86# Rotates the sword on the Y axis, of 60.0 degrees.
87# (in Soya, all angles are in degrees).
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).
94light = soya.Light(scene)
95light.set_xyz(0.5, 0.0, 2.0)
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.
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 !)
105camera = soya.Camera(scene)
106camera.z = 2.0
108# Say to Soya that the camera is what we want to be rendered.
112# Uncomment this line to save a 320x240 screenshot in the results directory.
114#soya.render(); soya.screenshot().resize((320, 240)).save(os.path.join(os.path.dirname(sys.argv), "results", os.path.basename(sys.argv)[:-3] + ".jpeg"))
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