Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / modeling-3.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
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# modeling-3: Vertex colors : the multicolor pyramid
20
21# Models can be designed in Blender and then imported in Soya (as in lesson basic-1.py),
22# but you can also create them from scratch, using Soya primitive. Learning this is
23# the purpose of the modeling-* tutorial series.
24
25# In this lesson, we'll build a pyramid, made of a quad base and 4 triangles.
26
27
28# Imports and inits Soya.
29
30import sys, os, os.path, soya
31
32soya.init()
33soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
34
35# Creates the scene.
36
37scene = soya.World()
38
39# Creates the World that will contain the pyramid. We don't create the pyramid in the
40# scene since we are going to compile the pyramid into a shape.
41
42pyramid_world = soya.World()
43
44# This time, we create the 5 vertices first. We do so to be sure each vertex will have
45# the same color on the different faces it belongs to ; though you can still use the way
46# we use in lesson modeling-1.py.
47# The diffuse attribute of vertex is the color of the vertex ; in Soya, colors are
48# (red, green, blue, alpha) tuples where all components are usually in the range 0.0-1.0.
49# Set diffuse to None to disable vertex color.
50
51# Here, the apex of the pyramid is in white, the base1 vertex is red, base2 is yellow,
52# base3 is green and base4 is blue.
53
54apex  = soya.Vertex(pyramid_world,  0.0,  0.5,  0.0, diffuse = (1.0, 1.0, 1.0, 1.0))
55base1 = soya.Vertex(pyramid_world,  0.5, -0.5,  0.5, diffuse = (1.0, 0.0, 0.0, 1.0))
56base2 = soya.Vertex(pyramid_world, -0.5, -0.5,  0.5, diffuse = (1.0, 1.0, 0.0, 1.0))
57base3 = soya.Vertex(pyramid_world, -0.5, -0.5, -0.5, diffuse = (0.0, 1.0, 0.0, 1.0))
58base4 = soya.Vertex(pyramid_world,  0.5, -0.5, -0.5, diffuse = (0.0, 0.0, 1.0, 1.0))
59
60# Now, creates the face, using the vertices we creates above.
61
62soya.Face(pyramid_world, [base1, base2, base3, base4])
63soya.Face(pyramid_world, [base2, base1, apex])
64soya.Face(pyramid_world, [base4, base3, apex])
65soya.Face(pyramid_world, [base1, base4, apex])
66soya.Face(pyramid_world, [base3, base2, apex])
67
68# Compile the pyramid into a shape.
69
70pyramid_shape = pyramid_world.shapify()
71
72# Creates a subclass of Volume that permanently rotates.
74
75class RotatingVolume(soya.Volume):
77    self.rotate_lateral(2.0 * proportion)
78
79# Create a rotating volume in the scene, using the cube shape.
80
81pyramid = RotatingVolume(scene, pyramid_shape)
82pyramid.rotate_vertical(60.0)
83
84# Creates a light.
85
86light = soya.Light(scene)
87light.set_xyz(1.0, -1.0, 2.0)
88
89# Creates a camera.
90
91camera = soya.Camera(scene)
92camera.set_xyz(0.0, 0.0, 2.0)
93soya.set_root_widget(camera)
94
95soya.Idler(scene).idle()
96
97