-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexGlb.py
304 lines (240 loc) · 9.07 KB
/
exGlb.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
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
import re
import bpy
from mathutils import Vector
# glb 保留变换信息,格式化为两层结构
def export_gltf(output_file):
ex_path = output_file
bpy.ops.export_scene.gltf(filepath=ex_path, export_colors=False, export_draco_mesh_compression_enable=True,
export_cameras=True, use_selection=True, use_visible=False, export_apply=True,
export_lights=True, export_selected=True)
return {'FINISHED'}
def remove_empty():
"""
删除空对象
"""
while True:
model = bpy.context.selected_objects[:]
# 删除空物体
objs = [
i
for i in model
if i.type in ["EMPTY", "LIGHT", "CAMERA"] and not i.children
]
bpy.ops.object.delete({"selected_objects": objs})
if not objs:
break
def clearObjects():
try:
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
except:
pass
def clearCollections():
try:
for c in bpy.context.scene.collection.children:
bpy.context.scene.collection.children.unlink(c)
for c in bpy.data.collections:
if not c.users:
bpy.data.collections.remove(c)
except:
pass
clearObjects()
clearCollections()
collection = bpy.data.collections.new('Protect')
bpy.context.scene.collection.children.link(collection)
collection = bpy.data.collections.new('Origin')
bpy.context.scene.collection.children.link(collection)
collection = bpy.data.collections.new('Join')
bpy.context.scene.collection.children.link(collection)
protect = bpy.data.collections['Protect']
originPd = bpy.data.collections['Origin']
joinPd = bpy.data.collections['Join']
protect.color_tag = 'COLOR_01'
originPd.color_tag = 'COLOR_02'
joinPd.color_tag = 'COLOR_03'
# input_file = r'C:\Users\thn\Desktop\coffee_maker_001.glb'
input_file = r'C:\Users\thn\Desktop\untitled.fbx'
layer_collection = bpy.context.view_layer.layer_collection
bpy.context.view_layer.active_layer_collection = layer_collection
if input_file.endswith('.glb') or input_file.endswith('.gltf'):
bpy.ops.import_scene.gltf(filepath=input_file)
if input_file.endswith('.fbx'):
bpy.ops.import_scene.fbx(filepath=input_file)
if input_file.endswith('.obj'):
bpy.ops.import_scene.obj(filepath=input_file)
bpy.ops.object.make_single_user(object=True, obdata=True, material=True)
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
# bpy.ops.object.transform_apply(location=True, rotation=True)
selected_objects = bpy.context.selected_objects
for o in selected_objects:
bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
bpy.context.scene.collection.objects.unlink(o)
originPd.objects.link(o)
remove_empty()
for o in originPd.objects:
if o.type == 'MESH' and o.hide_select == False:
od = o.copy()
od.data = o.data.copy()
joinPd.objects.link(od)
# join
bpy.ops.object.select_all(action='DESELECT')
for o in joinPd.objects:
if o.type == 'MESH':
o.select_set(True)
bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
bpy.context.view_layer.objects.active = o
bpy.ops.object.join()
# rename join object and mesh
for o in joinPd.objects:
o.name = "product2trans"
# for o in originPd.objects:
# if o.type == 'MESH':
# o.name = o.data.name
#
# i = 0
# for o in originPd.objects:
# if o.type == 'MESH':
# o.name = "部件." + str(i)
# o.data.name = o.name
# o.data.name = "uma_Mesh." + str(i)
# i += 1
pd = bpy.data.objects['product2trans']
dimensions = pd.dimensions
scale = pd.scale
# bpy.data.objects.remove(pd)
# 设定父级,并保留变换
bpy.ops.object.empty_add(type='PLAIN_AXES', align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.name = "ProductContainer"
pdc = bpy.data.objects['ProductContainer']
for o in originPd.objects:
if o.parent == None:
o.select_set(True)
bpy.context.view_layer.objects.active = pdc
bpy.ops.object.parent_set(type='OBJECT', keep_transform=True)
model = bpy.context.selected_objects[:]
rex = re.compile('\.\d+$')
for i in model:
# if file_kind == 'glb':
# 去除小数点
old_name = i.name
group = rex.search(old_name)
if group:
index = group.span()[0]
new_name = old_name[:index] + old_name[index + 1:]
i.name = new_name
i.name = i.name.replace(' - ', '_-_')
pd_box = [pd.matrix_world @ Vector(pdBvert) for pdBvert in pd.bound_box]
x = (pd_box[0][0] + pd_box[4][0]) / 2
y = (pd_box[0][1] + pd_box[2][1]) / 2
z = (pd_box[0][2] + pd_box[1][2]) / 2
for o in originPd.objects:
o.location.x -= x - pdc.location.x
o.location.y -= y - pdc.location.y
o.location.z -= z - pdc.location.z
# clearObjects()
pd.select_set(True)
bpy.context.view_layer.objects.active = pd
bpy.context.scene.cursor.location = x, y, z
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
pd.location = 0, 0, 0
bpy.context.scene.cursor.location = 0, 0, 0
bpy.ops.object.select_all(action='DESELECT')
import bpy
from mathutils import Vector
def add_glb_product(input_file):
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'CUDA'
bpy.context.preferences.addons["cycles"].preferences.get_devices()
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.device = 'GPU'
collection = bpy.data.collections.new('Protect')
bpy.context.scene.collection.children.link(collection)
collection = bpy.data.collections.new('Origin')
bpy.context.scene.collection.children.link(collection)
collection = bpy.data.collections.new('Join')
bpy.context.scene.collection.children.link(collection)
protect = bpy.data.collections['Protect']
originPd = bpy.data.collections['Origin']
joinPd = bpy.data.collections['Join']
protect.color_tag = 'COLOR_01'
originPd.color_tag = 'COLOR_02'
joinPd.color_tag = 'COLOR_03'
layer_collection = bpy.context.view_layer.layer_collection
bpy.context.view_layer.active_layer_collection = layer_collection
if input_file.endswith('.glb') or input_file.endswith('.gltf'):
bpy.ops.import_scene.gltf(filepath=input_file)
if input_file.endswith('.fbx'):
bpy.ops.import_scene.fbx(filepath=input_file)
if input_file.endswith('.obj'):
bpy.ops.import_scene.obj(filepath=input_file)
bpy.ops.object.make_single_user(object=True, obdata=True, material=True)
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
# bpy.ops.object.transform_apply(location=True, rotation=True)
selected_objects = bpy.context.selected_objects
for o in selected_objects:
# o.scale = o.scale * 2
bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
bpy.context.scene.collection.objects.unlink(o)
originPd.objects.link(o)
remove_empty()
for o in originPd.objects:
if o.type == 'MESH' and o.hide_select == False:
od = o.copy()
od.data = o.data.copy()
joinPd.objects.link(od)
# join
bpy.ops.object.select_all(action='DESELECT')
for o in joinPd.objects:
if o.type == 'MESH':
o.select_set(True)
bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
bpy.context.view_layer.objects.active = o
bpy.ops.object.join()
# rename join object and mesh
for o in joinPd.objects:
o.name = "product2trans"
def remove_empty():
"""
删除空对象
"""
while True:
model = bpy.context.selected_objects[:]
# 删除空物体
objs = [
i
for i in model
if i.type in ["EMPTY", "LIGHT", "CAMERA"] and not i.children
]
bpy.ops.object.delete({"selected_objects": objs})
if not objs:
break
def location_dimensions():
# 等比缩放 2, 设置物体原点为 物体外边框正中心, 设置坐标为0,0,0
appropriate_dimensions = 11
obj = bpy.data.objects['product2trans']
k1 = obj.dimensions.x
k2 = obj.dimensions.y
k3 = obj.dimensions.z
dimList = [k1, k2, k3]
k = max(dimList)
obj.scale *= appropriate_dimensions / k
bpy.context.view_layer.update()
pd_box = [obj.matrix_world @ Vector(pdBvert) for pdBvert in obj.bound_box]
x = (pd_box[0][0] + pd_box[4][0]) / 2
y = (pd_box[0][1] + pd_box[2][1]) / 2
z = (pd_box[0][2] + pd_box[1][2]) / 2
bpy.context.scene.cursor.location = x, y, z
obj.select_set(True)
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
obj.location = 0, 0, 0
obj.rotation_mode = 'XYZ'
obj.rotation_euler = (0.143621563911438, -0.013038791716098785, 0.608063817024231)
bpy.context.scene.cursor.location = 0, 0, 0
def render_image(path):
bpy.data.collections['Origin'].hide_render = True
bpy.context.scene.render.image_settings.file_format = "PNG"
bpy.context.scene.render.resolution_percentage = 100
bpy.context.scene.render.filepath = path
bpy.ops.render.render(write_still=True)
add_glb_product(r'C:\Users\thn\Desktop\coffee_maker_001.glb')
location_dimensions()
render_image(r'C:\Users\thn\Desktop\1.jpg')