Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / modeling-material-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-material-1: Material colors : a metallic blue cube
20
21# In this lesson, we introduce the Material object and build a material blue cube.
22# A material defines the properties of a surface, including shininess, color and
23# texture (skin). Each face can have a different material, as well as other objects.
24
25
26# Imports and inits Soya (see lesson basic-1.py).
27
28import sys, os, os.path, soya, soya.cube
29
30soya.init()
31soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
32
33# Creates the scene.
34
35scene = soya.World()
36
37# Creates the material
38
39material = soya.Material()
40
41# Sets the material's shininess to 0.5. The shininess ranges from 0.0 to 128.0;
42# 0.0 is the most metallic / shiny, and 128.0 is the most plastic.
43
44material.shininess = 0.5
45
46# Sets the material's diffuse color. The diffuse color is the basic color.
47# In Soya 3D, colors are tuples of 4 floats: (red, green, blue, alpha), where
48# each component normally ranges from 0.0 to 1.0 (though you can use values
49# out of this range, e.g. negative values to get a dark light), and alpha
50# is used for transparency (1.0 is opaque and 0.0 fully transparent).
51
52# We use here a blue diffuse color.
53
54material.diffuse   = (0.0, 0.2, 0.7, 1.0)
55
56# Sets the material's specular color. The specular color is the one used for
57# the specular / shiny effects.
58
59# We use here a light blue, to get metallic reflexions.
60
61material.specular  = (0.2, 0.7, 1.0, 1.0)
62
63# Activates the separate specular. This results in a brighter specular effect.
64
65material.separate_specular = 1
66
67# Other interesting attributes of materials are:
68# - wireframed        : draw in wireframe
69# - additive_blending : usefull for alpha-blending and special effect
70
71# The soya.cube module creates cubes by adding 6 faces inside a world ; it works
72# exactely as we do in the modeling-1.py lesson.
73# As usually, the first argument is the parent and the second is the material.
74# Here, we specify None as parent because we don't want to display this cube ;
75# we are going to compile it for speed purpose.
76
77# Similarly you can set a Material to a face, either in the constructor or with
78# face.material = material.
79
80# Notice that None is not a valid material ; use soya.DEFAULT_MATERIAL instead.
81
82cube_world = soya.cube.Cube(None, material)
83
84# Creates a subclass of Volume that permanently rotates.
86
87class RotatingVolume(soya.Volume):
89    self.rotate_lateral(2.0 * proportion)
90
91# Create a rotating volume in the scene, using the cube shape.
92
93cube = RotatingVolume(scene, cube_world.shapify())
94cube.rotate_vertical(30.0)
95
96# Creates a light.
97
98light = soya.Light(scene)
99light.set_xyz(0.5, 1.0, 2.0)
100
101# Creates a camera.
102
103camera = soya.Camera(scene)
104camera.set_xyz(0.0, 0.0, 2.0)
105soya.set_root_widget(camera)
106
107soya.Idler(scene).idle()
108
109