CoCalc Public Fileswww / talks / 2006-05-09-sage-digipen / tutorial / modeling-transparency-1.py
Author: William A. Stein
1# Soya 3D tutorial
2# Copyright (C) 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-transparency-1: Transparency : semi-transparent cubes
20
21# In this lesson, we'll build a semi-transparent cube.
22
23
24# Imports and inits Soya.
25
26import sys, os, os.path, soya, soya.cube
27
28soya.init()
29soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
30
31# Creates the scene.
32
33scene = soya.World()
34
35# Creates a material, and set its diffuse color to a semi-transparent white (alpha = 0.5).
36# See lesson modeling-material-1.py about materials and material colors.
37# Similarly, you can use per-vertex semi-transparent colors.
38
39material = soya.Material()
40material.diffuse = (1.0, 1.0, 1.0, 0.5)
41
42# Creates a cube using our material.
43
44cube_world = soya.cube.Cube(material = material)
45
46# For optimization pupose, the cube module creates one-sided faces, i.e. only the exterior
47# side of the faces are drawn, and not the interior side. Here, as the cube is
48# semi-transparent, the interior side becomes visible and so we need to draw both sides.
49# This is done by setting the face's double_sided attribute to 1.
50
51for face in cube_world.children:
52  face.double_sided = 1
53
54# Compile the cube model.
55
56cube_shape = cube_world.shapify()
57
58# Creates several semi-transparent cubes.
59
60cube1 = soya.Volume(scene, cube_shape)
61cube1.set_xyz(-0.3, 0.3, 0.0)
62cube1.rotate_lateral(30.0)
63
64cube2 = soya.Volume(scene, cube_shape)
65cube2.set_xyz(0.0, 0.0, -1.5)
66cube2.rotate_lateral(30.0)
67
68cube3 = soya.Volume(scene, cube_shape)
69cube3.set_xyz(0.6, -0.4, 0.5)
70cube3.rotate_lateral(30.0)
71
72# Creates a light.
73
74light = soya.Light(scene)
75light.set_xyz(1.0, 0.7, 1.0)
76
77# Creates a camera.
78
79camera = soya.Camera(scene)
80camera.set_xyz(0.0, 0.0, 3.0)
81soya.set_root_widget(camera)
82
83soya.Idler(scene).idle()
84
85