-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport_model.py
175 lines (142 loc) · 5.9 KB
/
export_model.py
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
from pathlib import Path
import logging
import os
import re
import sys
from typing import Union
import bmesh
import bpy
import numpy
from mathutils import Vector
sys.path.append(os.path.dirname(__file__))
import world_json
from dirs import dest, src
from selection import all_mesh_objects, editmode, select_object, select_objects_in_collection
from blender_file import saveScene, writeGlb
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
log = logging.getLogger()
def separate_rect(obj_name: str, xlo: float, xhi: float, ylo: float, yhi: float) -> Union[str, None]:
names_before = set(o.name for o in bpy.data.objects)
select_object(obj_name)
with editmode():
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
bpy.ops.mesh.select_all(action='DESELECT')
mesh = bmesh.from_edit_mesh(bpy.data.objects[obj_name].data)
sel_verts = set()
for i, v in enumerate(mesh.verts):
if xlo <= v.co.x < xhi and ylo <= v.co.y < yhi:
sel_verts.add(i)
v.select = True
for e in mesh.edges:
if all(i in sel_verts for i in e.verts):
e.select = True
for f in mesh.faces:
if all(i in sel_verts for i in f.verts):
f.select = True
mesh.select_flush(True)
bpy.ops.mesh.separate()
names_after = set(o.name for o in bpy.data.objects)
new_names = names_after.difference(names_before)
if not new_names:
return None
new_name = new_names.pop()
select_object(new_name)
with editmode():
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.cube_project(
cube_size=1, scale_to_bounds=True) # todo needs to go to a separate uv, to not break the big ground dif texture
return new_name
def make_lightmap_uv_layer(obj, outData):
uvs = obj.data.uv_layers
lightmap_uv = uvs.new()
lightmap_uv.name = 'lightmap'
obj_uv = outData['objs'][obj.name]
obj_uv['lightmap_uv'] = lightmap_uv.name
return lightmap_uv
def objectBbox(obj):
ptsObject = obj.bound_box
ptsWorld = numpy.array([obj.matrix_world @ Vector(pt) for pt in ptsObject])
centerWorld = numpy.average(ptsWorld, axis=0)
centerToPts = ptsWorld - centerWorld
radius = numpy.linalg.norm(centerToPts, axis=1).max()
return {'center': [round(x, 3) for x in centerWorld], 'radius': round(radius, 3)}
def storeExistingUvLayer(outData, obj):
obj_uv = outData['objs'][obj.name]
try:
render_uv_layer = obj.data.uv_layers.active.name
obj_uv['render_uv'] = render_uv_layer
except AttributeError:
pass
def delete_extra_objs():
try:
key_collection, = [c for c in bpy.data.collections if c.name != 'Collection' and c.library is None]
except ValueError:
log.error(list(bpy.data.collections))
raise
select_objects_in_collection(key_collection)
keep = len(bpy.context.selected_objects)
bpy.ops.object.select_all(action='INVERT')
dump = len(bpy.context.selected_objects)
log.info(f'keeping {keep} objects, deleting {dump}')
bpy.ops.object.delete(confirm=False)
def main():
input_scene = Path(sys.argv[-1])
output_export = dest / 'serve' / input_scene.relative_to(src).parent / input_scene.name.replace('.blend', '.glb')
output_export.parent.mkdir(parents=True, exist_ok=True)
outData = {}
log.info(f'open collection {input_scene}')
bpy.ops.wm.open_mainfile(filepath=str(input_scene))
def dice_ground():
log.info('dice_ground')
for xsplit in range(-750, 750, 250):
for ysplit in range(-750, 750, 250):
separate_rect('gnd.001', -750, xsplit + 250, -750, ysplit + 250)
def separate_materials():
log.info('separate_materials')
for obj_name in all_mesh_objects():
if len(bpy.data.objects[obj_name].material_slots) > 1:
select_object(obj_name)
bpy.ops.mesh.separate(type='MATERIAL')
def lightmaps():
log.info('lightmaps')
for obj_name in all_mesh_objects():
# if not obj_name.startswith('sign_board'): continue
try:
obj = select_object(obj_name)
except Exception as exc:
log.warning(f'lightmap_pack failed on {obj_name}: {exc!r}')
continue
outData.setdefault('objs', {}).setdefault(obj_name, {})['worldBbox'] = objectBbox(obj)
storeExistingUvLayer(outData, obj)
lyr = make_lightmap_uv_layer(obj, outData)
obj.data.uv_layers.active = lyr
log.info(f'start lightmap_pack on {obj_name}; active uv is {obj.data.uv_layers.active.name}')
try:
bpy.ops.uv.lightmap_pack(
PREF_CONTEXT='ALL_FACES',
PREF_PACK_IN_ONE=True,
PREF_NEW_UVLAYER=False,
)
except Exception as exc:
log.warning(f'lightmap_pack failed on {obj_name}: {exc!r}')
def rel_paths():
log.info('rel_paths')
# bpy.ops.file.make_paths_relative() is not working; it makes like
# '//../../../home/drewp/own/proj_shared/megasecond/client/asset/wrap/gnd_dif.png'
for img in bpy.data.images.values():
prev = img.filepath
img.filepath = re.sub(r'.*/megasecond/client/asset/wrap/', '//', img.filepath)
if img.filepath != prev:
log.info(f'fix path from {prev} to {img.filepath}')
log.info(f' * image at {img.filepath}')
delete_extra_objs()
if 'gnd.001' in bpy.data.objects:
dice_ground()
separate_materials()
lightmaps()
rel_paths()
# write obj list so we can make deps?
saveScene(dest / 'stage/bake' / input_scene.relative_to(src))
writeGlb(output_export, select=None, with_materials=True)
main()