Mercurial > touhou-blender
comparison io_scene_touhou/import_std.py @ 1:ba73c663a227 default tip
Make it work with the latest PyTouhou.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 20 Dec 2013 17:11:29 +0100 |
parents | 8265ef6db380 |
children |
comparison
equal
deleted
inserted
replaced
0:8265ef6db380 | 1:ba73c663a227 |
---|---|
26 stage file to open. | 26 stage file to open. |
27 """ | 27 """ |
28 | 28 |
29 | 29 |
30 from pytouhou.formats.std import Stage | 30 from pytouhou.formats.std import Stage |
31 from pytouhou.formats.anm0 import ANM0 | 31 from pytouhou.formats.anm0 import ANM |
32 from pytouhou.resource.anmwrapper import AnmWrapper | |
33 from pytouhou.vm.anmrunner import ANMRunner | 32 from pytouhou.vm.anmrunner import ANMRunner |
34 from pytouhou.game.sprite import Sprite | 33 from pytouhou.game.sprite import Sprite |
35 from pytouhou.ui.sprite import get_sprite_rendering_data | 34 from pytouhou.ui.opengl.sprite import get_sprite_rendering_data |
36 import bpy | 35 import bpy |
37 import os.path | 36 import os.path |
38 | 37 |
39 | 38 |
40 def build_models(stage, anm_wrapper): | 39 def build_models(stage, anm): |
41 """Taken from pytouhou.game.background.""" | 40 """Taken from pytouhou.game.background.""" |
42 models = [] | 41 models = [] |
43 for obj in stage.models: | 42 for obj in stage.models: |
44 quads = [] | 43 quads = [] |
45 for script_index, ox, oy, oz, width_override, height_override in obj.quads: | 44 for script_index, ox, oy, oz, width_override, height_override in obj.quads: |
46 sprite = Sprite(width_override, height_override) | 45 sprite = Sprite(width_override, height_override) |
47 anm_runner = ANMRunner(anm_wrapper, script_index, sprite) | 46 anm_runner = ANMRunner(anm, script_index, sprite) |
48 anm_runner.run_frame() | |
49 quads.append((ox, oy, oz, sprite)) | 47 quads.append((ox, oy, oz, sprite)) |
50 models.append(quads) | 48 models.append(quads) |
51 return models | 49 return models |
52 | 50 |
53 | 51 |
58 nb_vertices = 0 | 56 nb_vertices = 0 |
59 faces_indices = [] | 57 faces_indices = [] |
60 | 58 |
61 for ox, oy, oz, sprite in model: | 59 for ox, oy, oz, sprite in model: |
62 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) | 60 key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite) |
63 (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4) = vertices | 61 x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices |
64 left, right, bottom, top = uvs | 62 left, right, bottom, top = uvs |
65 r, g, b, a = colors | 63 r, g, b, a = colors |
66 | 64 |
67 #verts.extend(((x1 + ox, y1 + oy, z1 + oz), | 65 #verts.extend(((x1 + ox, y1 + oy, z1 + oz), |
68 # (x2 + ox, y2 + oy, z2 + oz), | 66 # (x2 + ox, y2 + oy, z2 + oz), |
73 verts.extend(((x1 + ox, y1 + oy, -(z1 + oz)), | 71 verts.extend(((x1 + ox, y1 + oy, -(z1 + oz)), |
74 (x2 + ox, y2 + oy, -(z2 + oz)), | 72 (x2 + ox, y2 + oy, -(z2 + oz)), |
75 (x3 + ox, y3 + oy, -(z3 + oz)), | 73 (x3 + ox, y3 + oy, -(z3 + oz)), |
76 (x4 + ox, y4 + oy, -(z4 + oz)))) | 74 (x4 + ox, y4 + oy, -(z4 + oz)))) |
77 | 75 |
78 texcoords.extend(((left, bottom), | 76 texcoords.extend(((left, 1.-bottom), |
79 (right, bottom), | 77 (right, 1.-bottom), |
80 (right, top), | 78 (right, 1.-top), |
81 (left, top))) | 79 (left, 1.-top))) |
82 | 80 |
83 #TODO: use them. | 81 #TODO: use them. |
84 cols.append((r, g, b, a)) | 82 cols.append((r, g, b, a)) |
85 | 83 |
86 faces_indices.append((nb_vertices, nb_vertices + 1, nb_vertices + 2, nb_vertices + 3)) | 84 faces_indices.append((nb_vertices, nb_vertices + 1, nb_vertices + 2, nb_vertices + 3)) |
129 with open(stage_path, "rb") as filehandle: | 127 with open(stage_path, "rb") as filehandle: |
130 stage = Stage.read(filehandle) | 128 stage = Stage.read(filehandle) |
131 | 129 |
132 anm_path = os.path.join(dirname, 'stg{}bg.anm'.format(stage_number)) | 130 anm_path = os.path.join(dirname, 'stg{}bg.anm'.format(stage_number)) |
133 with open(anm_path, "rb") as filehandle: | 131 with open(anm_path, "rb") as filehandle: |
134 anm = ANM0.read(filehandle) | 132 anm = ANM.read(filehandle) |
135 anm_wrapper = AnmWrapper((anm,), (0,)) | |
136 | 133 |
137 models = build_models(stage, anm_wrapper) | 134 entry = anm[0] #XXX |
135 models = build_models(stage, entry) | |
138 | 136 |
139 texture_path = os.path.join(dirname, 'stg{}bg.png'.format(stage_number)) | 137 texture_path = os.path.join(dirname, 'stg{}bg.png'.format(stage_number)) |
140 image = bpy.data.images.load(texture_path) | 138 image = bpy.data.images.load(texture_path) |
141 | 139 |
142 meshes = [] | 140 meshes = [] |
144 name = '{}-mesh{}'.format(obj_name, i) | 142 name = '{}-mesh{}'.format(obj_name, i) |
145 mesh = read_mesh(name, stage, model, image) | 143 mesh = read_mesh(name, stage, model, image) |
146 meshes.append(mesh) | 144 meshes.append(mesh) |
147 | 145 |
148 for i, (model_id, ox, oy, oz) in enumerate(stage.object_instances): | 146 for i, (model_id, ox, oy, oz) in enumerate(stage.object_instances): |
149 name = '{}-object{}'.format(obj_name, i) | 147 name = '{}-object{:02}'.format(obj_name, i) |
150 add_object(name, meshes[model_id], (ox, oy, -oz)) | 148 add_object(name, meshes[model_id], (ox, oy, -oz)) |