Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / modeling-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
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-1: Faces : the pyramid
20# requires the basic-* lessons.
21
22# Models are usually designed in Blender and imported in Soya (as in lesson basic-1.py),
23# but you can also create them from scratch, using Soya primitives. Learning this is
24# the purpose of the modeling-* tutorial series.
25
26# In this lesson, we'll build a pyramid, made of a quad base and 4 triangles.
27
28
29# Imports and inits Soya (see lesson basic-1.py).
30
31import sys, os, os.path, soya
32
33soya.init()
34soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
35
36# Creates the scene.
37
38scene = soya.World()
39
40# Creates the World that will contain the pyramid. As previously stated, the
41# pyramid is composed of 5 faces; this world is used to group those 5 faces
42# (exactly like a Frame can be used in Tkinter to group different widgets).
43# The pyramid's parent is the scene.
44
45pyramid = soya.World(scene)
46
47# Creates the first face, the quad base of the pyramid.
48# The first argument of the Face constructor is the parent World (the pyramid),
49# the second is the list of vertices and the third is the material.
50# The number of vertices determines the Face's nature:
51#  - 1: a plot
52#  - 2: a line
53#  - 3: a triangle
55#  - +: a polygon
56
57# The vertices is a list of Vertex object. A Vertex is a subclass of Point that can have
58# some modeling attributes like colors or texture coordinates (we'll see them is further
59# lessons) The first argument of the constructor is (again) the parent, and the three
60# following one are the x, y and z coordinates.
61
62# As 3D object, Point, Vector or Vertex have a parent too, though they are not
63# considered as "children". The parent is used for automatic coordinates
64# conversion, which will be detailed in a further lesson. Here, the coordinates
65# are defined in the pyramid coordinate system.
66
67# The order of the vertices in the list determines which side of the face is
68# visible (for triangles, quads and polygons); you can get both side visible
69# by setting the "double_sided" attribute to 1.
70
71soya.Face(pyramid, [soya.Vertex(pyramid,  0.5, -0.5,  0.5),
72                    soya.Vertex(pyramid, -0.5, -0.5,  0.5),
73                    soya.Vertex(pyramid, -0.5, -0.5, -0.5),
74                    soya.Vertex(pyramid,  0.5, -0.5, -0.5),
75                    ])
76
77# Similarly, creates the 4 triangles.
78# Here, we create different vertices ; you can also use the same vertex when 2 vertices
79# have the same coordinates and attributes. Both yields the same final result (including
80# in performance).
81
82soya.Face(pyramid, [soya.Vertex(pyramid, -0.5, -0.5,  0.5),
83                    soya.Vertex(pyramid,  0.5, -0.5,  0.5),
84                    soya.Vertex(pyramid,  0.0,  0.5,  0.0),
85                    ])
86
87soya.Face(pyramid, [soya.Vertex(pyramid,  0.5, -0.5, -0.5),
88                    soya.Vertex(pyramid, -0.5, -0.5, -0.5),
89                    soya.Vertex(pyramid,  0.0,  0.5,  0.0),
90                    ])
91
92soya.Face(pyramid, [soya.Vertex(pyramid,  0.5, -0.5,  0.5),
93                    soya.Vertex(pyramid,  0.5, -0.5, -0.5),
94                    soya.Vertex(pyramid,  0.0,  0.5,  0.0),
95                    ])
96
97soya.Face(pyramid, [soya.Vertex(pyramid, -0.5, -0.5, -0.5),
98                    soya.Vertex(pyramid, -0.5, -0.5,  0.5),
99                    soya.Vertex(pyramid,  0.0,  0.5,  0.0),
100                    ])
101
102# Saves the pyramid.
103# First we set the filename attribute, and then call the save method.
104# Soya automatically saves it in directory soya.path[0] + "/worlds",
105# and adds a ".data" extention.
106
107pyramid.filename = "pyramid"
108pyramid.save()
109
110# Rotates the pyramid (for a better view)
111
112# Notice that we have saved the pyramid BEFORE the rotation, since we don't want
113# to save the rotation.
114
115pyramid.rotate_lateral(60.0)
116
117# Creates a light.
118
119light = soya.Light(scene)
120light.set_xyz(1.0, 0.7, 1.0)
121
122# Creates a camera.
123
124camera = soya.Camera(scene)
125camera.set_xyz(0.0, -0.6, 2.0)
126soya.set_root_widget(camera)
127
128soya.Idler(scene).idle()
129
130