File: __init__.py

package info (click to toggle)
soya 0.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,580 kB
  • ctags: 20,171
  • sloc: cpp: 45,252; python: 7,241; ansic: 5,226; perl: 273; makefile: 227; sh: 65
file content (315 lines) | stat: -rw-r--r-- 13,308 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# -*- indent-tabs-mode: t -*-

# Soya 3D
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import Tkinter, dircache, os, os.path, sets
import editobj, editobj.main, editobj.editor as editor, editobj.custom as custom
import soya, soya.editor.material, soya.editor.world
import soya.laser        as laser
import soya.ray          as ray
import soya.facecutter   as facecutter
import soya.widget

#
# Config and hacks for EditObj.
#

custom.register_method("rotate_y", soya.CoordSyst, editor.FloatEditor)
custom.register_method("rotate_x", soya.CoordSyst, editor.FloatEditor)
custom.register_method("rotate_z", soya.CoordSyst, editor.FloatEditor)

custom.register_children_attr("children"  , "add", "__delitem__", clazz = soya.World)
custom.register_children_attr("vertices"  , clazz = soya.Face)
custom.register_children_attr("generators", clazz = soya.Particles)

soya.Particles.generators = property(lambda self: [self.generator], None)

# Register properties' editors

class XTextureCoordEditor(editor.Editor, Tkinter.Frame):
	require_right_menu = 0
	
	def __init__(self, master, obj, attr):
		editor.Editor.__init__(self, master, obj, attr)
		
		Tkinter.Frame.__init__(self, master)
		self.columnconfigure(0, weight = 1)
		
		if obj.face.material and obj.face.material.texture:
			try:
				import Image, ImageTk

				self.image = Image.open(os.path.join(soya.path[0], obj.face.material.texture.filename))
				
				self.image_width = self.image_height = 0
				
				self.label = Tkinter.Label(self, bd = 0, width = 500)
				self.label.grid(row = 0, column = 0, sticky = "EWN")
				self.label.bind("<Configure>"     , self.label_conf)
				self.label.bind("<Button-1>"      , self.motion)
				self.label.bind("<Button1-Motion>", self.motion)
			except ImportError: # PIL is not installed !
				self.label = None
				
		self.internal_editor = editor.FloatEditor(self, obj, attr)
		self.internal_editor.grid(row = 1, column = 0, sticky = "EWS")
		self.cancel = self.master.cancel
		
	def label_conf(self, event = None):
		import Image, ImageTk
		
		if self.image_width != self.label.winfo_width():
			self.image_width  = self.label.winfo_width()
			self.image_height = (self.image.size[1] / float(self.image.size[0])) * self.image_width
			
			self.imagetk = ImageTk.PhotoImage(self.image.resize((int(self.image_width), int(self.image_height))))
			
			self.label.configure(image = self.imagetk)
			
	def get_value(self): return self.internal_editor.get_value()
	def set_value(self, value): self.internal_editor.set_value(value)
	def update(self): self.internal_editor.update()
	
	def motion(self, event):
		self.obj.tex_x = max(0.0, min(1.0, float(event.x) / self.image_width ))
		self.obj.tex_y = max(0.0, min(1.0, float(event.y) / self.image_height))
		
custom.register_attr("lefthanded"          , None)
custom.register_attr("matrix"              , None)
custom.register_attr("root_matrix"         , None)
custom.register_attr("inverted_root_matrix", None)
custom.register_attr("face"                , None)
custom.register_attr("normal"              , None)
custom.register_attr("vertices"            , None)
custom.register_attr("name"                , editor.StringEditor)
custom.register_attr("filename"            , editor.StringEditor)
custom.register_attr("x"                   , editor.FloatEditor)
custom.register_attr("y"                   , editor.FloatEditor)
custom.register_attr("z"                   , editor.FloatEditor)
custom.register_attr("scale_x"             , editor.FloatEditor)
custom.register_attr("scale_y"             , editor.FloatEditor)
custom.register_attr("scale_z"             , editor.FloatEditor)
custom.register_attr("front"               , editor.FloatEditor)
custom.register_attr("back"                , editor.FloatEditor)
custom.register_attr("fov"                 , editor.FloatEditor)
custom.register_attr("rot_y"               , editor.FloatEditor)
custom.register_attr("rot_x"               , editor.FloatEditor)
custom.register_attr("rot_z"               , editor.FloatEditor)
custom.register_attr("red"                 , editor.FloatEditor)
custom.register_attr("green"               , editor.FloatEditor)
custom.register_attr("blue"                , editor.FloatEditor)
custom.register_attr("alpha"               , editor.FloatEditor)
custom.register_attr("shininess"           , editor.FloatEditor)
custom.register_attr("point_size"          , editor.FloatEditor)
custom.register_attr("initial_speed"       , editor.FloatEditor)
custom.register_attr("exponent"            , editor.FloatEditor)
custom.register_attr("angle"               , editor.FloatEditor)
custom.register_attr("directional"         , editor.BoolEditor)
custom.register_attr("constant"            , editor.FloatEditor)
custom.register_attr("linear"              , editor.FloatEditor)
custom.register_attr("quadratic"           , editor.FloatEditor)
custom.register_attr("clamp"               , editor.BoolEditor)
custom.register_attr("additive_blending"   , editor.BoolEditor)
custom.register_attr("environment_mapping" , editor.BoolEditor)
custom.register_attr("separate_specular"   , editor.BoolEditor)
custom.register_attr("wireframed"          , editor.BoolEditor)
custom.register_attr("visible"             , editor.BoolEditor)
custom.register_attr("lit"                 , editor.BoolEditor)
custom.register_attr("static_lit"          , editor.BoolEditor)
custom.register_attr("smooth_lit"          , editor.BoolEditor)
custom.register_attr("double_sided"        , editor.BoolEditor)
custom.register_attr("solid"               , editor.BoolEditor)
custom.register_attr("bound_atm"           , editor.BoolEditor)
custom.register_attr("camera_go_through"   , editor.BoolEditor)
custom.register_attr("infinite"            , editor.BoolEditor)
custom.register_attr("use_clip_plane"      , editor.BoolEditor)
custom.register_attr("static"              , editor.BoolEditor)
custom.register_attr("static_only"         , editor.BoolEditor)
custom.register_attr("top_level"           , editor.BoolEditor)
custom.register_attr("reflect"             , editor.BoolEditor)
custom.register_attr("acceleration"        , editor.SubEditor)
custom.register_attr("endpoint"            , editor.SubEditor)
custom.register_attr("portal_linked"       , editor.BoolEditor)

def _all_parent(item):
	if isinstance(item, soya.World):
		inner_worlds = item.recursive()
		inner_worlds.append(item)
		return filter(lambda item: isinstance(item, soya.World) and (not getattr(item, "name", "").startswith("_")) and (not item in inner_worlds), item.get_root().recursive())
	else:
		return filter(lambda item: isinstance(item, soya.World) and (not getattr(item, "name", "").startswith("_")), item.get_root().recursive())
	
custom.register_attr("parent" , editor.LambdaListEditor(_all_parent))

def _get_all_texture_names(obj):
	names = sets.Set([None])
	for path in soya.path:
		print path, os.path.join(path, soya.Material.DIRNAME)
		for name in dircache.listdir(os.path.join(path, soya.Image.DIRNAME)):
			print name
			names.add(name)
	return list(names)

custom.register_attr("texture", editor.LambdaListEditor(lambda obj: [None] + soya.Image.availables(), lambda name: name and soya.Image.get(name)))
custom.register_attr("image"  , None, soya.Material)

class MaterialEditor(editor.WithButtonEditor):
	INTERNAL_EDITOR_CLASS = editor.LambdaListEditor(lambda obj: filter(lambda filename: not (filename and filename.startswith("_")), soya.Material.availables()), lambda filename: filename and soya.Material.get(filename))
	
	def __init__(self, master, obj, attr):
		editor.WithButtonEditor.__init__(self, master, obj, attr, self.INTERNAL_EDITOR_CLASS, "...")
		
	def button_click(self, event = None):
		edit(getattr(self.obj, self.attr))
		
class ModelEditor(editor.WithButtonEditor):
	INTERNAL_EDITOR_CLASS = editor.LambdaListEditor(lambda obj: filter(lambda filename: not (filename and filename.startswith("_")), soya.Model.availables()), lambda filename: filename and soya.Model.get(filename))
	
	def __init__(self, master, obj, attr):
		editor.WithButtonEditor.__init__(self, master, obj, attr, self.INTERNAL_EDITOR_CLASS, "...")
		
	def button_click(self, event = None):
		edit(getattr(self.obj, self.attr).to_world())
		
class WorldEditor(editor.WithButtonEditor):
	INTERNAL_EDITOR_CLASS = editor.LambdaListEditor(lambda obj: filter(lambda filename: not (filename and filename.startswith("_")), soya.World.availables()), lambda filename: filename and soya.World.get(filename))
	
	def __init__(self, master, obj, attr):
		editor.WithButtonEditor.__init__(self, master, obj, attr, self.INTERNAL_EDITOR_CLASS, "...")
		
	def button_click(self, event = None):
		edit(getattr(self.obj, self.attr))
		
custom.register_attr("model"            , ModelEditor)
custom.register_attr("material"         , MaterialEditor)
custom.register_attr("beyond"           , WorldEditor)

custom.register_attr("path"             , editor.DirnameEditor)

custom.register_attr("tex_x"            , XTextureCoordEditor)
custom.register_attr("tex_y"            , editor.FloatEditor)

def _reverse_vertices(self): self.vertices.reverse()
soya.Face.reverse_vertices = _reverse_vertices

custom.register_method("reverse_vertices", soya.Face)


def ImmatureVertex():
	vertex = soya.Vertex()
	vertex.immature = 1
	return vertex

_last_face = None

def _Gone(vertices):
	global _last_face
	
	f = soya.Face(None, vertices)
	if _last_face:
		f.double_sided = _last_face.double_sided
		f.material     = _last_face.material
		f.smooth_lit = _last_face.smooth_lit
		f.lit = _last_face.lit
		f.solid = _last_face.solid
		f.static_lit = _last_face.static_lit
	_last_face = f
	return f

soya.Triangle = lambda : _Gone([ImmatureVertex(), ImmatureVertex(), ImmatureVertex()])
soya.Quad     = lambda : _Gone([ImmatureVertex(), ImmatureVertex(), ImmatureVertex(), ImmatureVertex()])

custom.register_available_children(["soya.Triangle()", "soya.Quad()", "soya.Body()", "soya.World()", "soya.Camera()", "soya.Light()", "soya.Portal()", "soya.Fountain()", "laser.Laser()", "ray.Ray()"], soya.World)
custom.register_available_children(["soya.Vertex()"], soya.Face)

custom.register_values("color"       , ["None", "(1.0, 1.0, 1.0, 1.0)", "(0.0, 0.0, 0.0, 1.0)"])
custom.register_values("diffuse"     , ["None", "(1.0, 1.0, 1.0, 1.0)", "(0.0, 0.0, 0.0, 1.0)"])
custom.register_values("specular"    , ["None", "(1.0, 1.0, 1.0, 1.0)", "(0.0, 0.0, 0.0, 1.0)"])
custom.register_values("to_model_args", ["None", """("tree", {})""", """("cell-shading", { 'shader':'shader2', 'line_color':(0.0, 0.0, 0.0, 1.0), 'line_width':8.0 })"""])
#custom.register_values("to_model_args", ["None", """("tree", {"min_node_content":0, "min_node_radius":5.0, "min_node_distance":0.0})""", """("morph", {})"""])

editobj.EVAL_ENV.update({
	"soya"       : soya,
	"laser"      : laser,
	"ray"        : ray,
	"cut"        : facecutter.cut,
	"check_quads": facecutter.check_quads,
	})

# Better Tk look
_tk = Tkinter.Tk(className = 'EditObj')
_tk.withdraw()
_tk.option_add("*bd", 1)
_tk.option_add("*List.background", "#FFFFFF")
_tk.option_add("*Text.background", "#FFFFFF")
_tk.option_add("*Text.relief", "flat")
_tk.option_add("*Entry.background", "#FFFFFF")
_tk.option_add("*Entry.relief", "flat")


CURRENT = None

def edit_material(self, window):
	if not soya.inited: soya.init("Soya Editor")
	
	ed = soya.editor.material.MaterialEditor(self, window)
	
	def on_activate(event = None):
		global CURRENT
		
		if CURRENT: CURRENT.deactivate()
		ed.activate()
		CURRENT = ed
		
	window.bind("<FocusIn>" , on_activate)

def edit_world(self, window):
	if not self.parent:
		if not soya.inited: soya.init("Soya Editor")
		
		ed = soya.editor.world.WorldEditor(self, window)
		
		def on_activate(event = None):
			global CURRENT
			
			if CURRENT: CURRENT.deactivate()
			ed.activate()
			CURRENT = ed
			
		window.bind("<FocusIn>" , on_activate)

custom.register_on_edit(edit_material, soya.Material)
custom.register_on_edit(edit_world,    soya.World)


def _subedit(self, window):
	if hasattr(CURRENT, "select_handles_for"): CURRENT.select_handles_for(self)
	
def _subedit_face(self, window):
	if hasattr(CURRENT, "select_handles_for"):
		for vertex in self: CURRENT.select_handles_for(vertex)
		
def _editchildren(self, visible):
	if hasattr(CURRENT, "children_edited"): CURRENT.children_edited(self, visible)
	
custom.register_on_edit            (_subedit,      soya.CoordSyst)
custom.register_on_edit            (_subedit,      soya.Vertex)
custom.register_on_edit            (_subedit_face, soya.Face)
custom.register_on_children_visible(_editchildren, soya.World)

edit = editobj.edit