Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / blender-auto-exporter-1.pyOpen in CoCalc
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
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.
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
# blender-auto-exporter-1: Blender auto-exporter : how to export models automatically
20
21
# This lesson is similar to basic-2.py, but it loads a Blender model !
22
# You NEED Blender to run this lesson.
23
24
# The Soya auto-exporter feature currently support the following file formats :
25
# - Blender model (Both static models, and Cal3D ones)
26
# - OBJ/MTL model (thanks David PHAM-VAN)
27
# - .png images
28
# - .jpeg images
29
30
31
# Imports and inits Soya (see lesson basic-1.py).
32
33
import sys, os, os.path, soya
34
35
soya.init()
36
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
37
38
# Creates the scene.
39
40
scene = soya.World()
41
42
# Loads the knife model.
43
44
# To use auto-exporter, simply put your Blender models in {soya.path}/blender/ , your
45
# OBJ/MTL models in {soya.path}/obj/ and your textures in {soya.path}/images/ .
46
# Soya will automatically exports models to Soya worlds ans shapes, and textures to
47
# materials, if the corresponding Soya doesn't exist OR is not up-to-date.
48
49
# Here, the knife model doesn't exist as a Soya model yet, but the original Blender model
50
# is in {soya.path}/blender/knife.blender ; so Soya will find it, export it to
51
# a world and then compile the world into a shape.
52
53
# Soya will save the exported world and shape, so if you run the lesson again, the model
54
# won't be exported again. If you want so, just modify the model and/or the texture.
55
56
knife_model = soya.Shape.get("knife")
57
58
# The Blender model has a Text buffer called "soya_params".
59
# This buffer is analyzed and gives additionnal information ; here it contains
60
# "cellshading=1" and thus enable cell_shading for the model.
61
62
# The list of ATTR=VALUE code usable in the text buffer corresponds to the options
63
# available at the beginning of the blender2soya.py script (see this script), e.g. :
64
# scale=1.0
65
# shadow=1
66
# cellshading=1
67
# cellshading_shader=shader_name
68
# cellshading_outline_width=5.0
69
# =5.0
70
# =5.0
71
#
72
# The MATERIAL_MAP option has a different syntax. E.g. the following will replace the
73
# material called "old_material_name" by the one called "new_material_name".
74
# material_old_material_name=new_material_name
75
#
76
# You can also make curent a specific position of an animation (called Action by Blender)
77
# as following :
78
# animation=animation_name
79
# animation_time=1
80
81
# If you want to generate SEVERAL different models from a SINGLE Blender file,
82
# you can use alternative text buffers. For example the following :
83
84
knife_model2 = soya.Shape.get("[email protected]_sword_material")
85
86
# will read the Blender text buffer "with_sword_material" in the model in addition to
87
# "soya_params". Here is contains "material_knife=epee_turyle", which replace the knife
88
# material by the sword one (In French, epee=sword).
89
90
91
# The rest of the script is the same than lesson-2.
92
93
# Creates a rotating volume class.
94
95
class RotatingVolume(soya.Volume):
96
def advance_time(self, proportion):
97
soya.Volume.advance_time(self, proportion)
98
self.rotate_lateral(proportion * 5.0)
99
100
101
knife = RotatingVolume(scene, knife_model )
102
knife.x = -1
103
104
knife2 = RotatingVolume(scene, knife_model2)
105
knife2.x = 1
106
107
# Creates a light.
108
109
light = soya.Light(scene)
110
light.set_xyz(0.5, 0.0, 2.0)
111
112
# Creates a camera.
113
114
camera = soya.Camera(scene)
115
camera.z = 3.0
116
soya.set_root_widget(camera)
117
118
soya.Idler(scene).idle()
119
120