tfnn commited on
Commit
1a682c2
1 Parent(s): 8a1c8f5

Upload 2 files

Browse files
Files changed (2) hide show
  1. glb_to_ply.blend +0 -0
  2. glb_to_ply.py +148 -0
glb_to_ply.blend ADDED
Binary file (988 kB). View file
 
glb_to_ply.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Blender Script that takes a folder of GLB files and processes them into PLY files
2
+ # strips textures and vertex colors leaving only vertex pos, vertex norm, index buffer
3
+ # 1. merges model into one contiguous mesh
4
+ # 2. scales the model to a unit sphere
5
+ # 3. centers the origin
6
+ # 4. centers the model
7
+ import bpy
8
+ import glob
9
+ import os
10
+ from os import mkdir
11
+ from os.path import isdir
12
+ import pathlib
13
+
14
+ # ---
15
+
16
+ importDir = "GLB/"
17
+ outputDir = "PLY0/"
18
+ if not isdir(outputDir): mkdir(outputDir)
19
+ #
20
+ for file in glob.glob(importDir + "*.glb"):
21
+ model_name = pathlib.Path(file).stem
22
+ if pathlib.Path(outputDir+model_name+'.ply').is_file() == True: continue
23
+ try:
24
+ bpy.ops.import_scene.gltf(filepath=file)
25
+ except:
26
+ continue
27
+ bpy.ops.wm.ply_export(
28
+ filepath=outputDir+model_name+'.ply',
29
+ filter_glob='*.ply',
30
+ check_existing=False,
31
+ ascii_format=False,
32
+ export_selected_objects=True,
33
+ apply_modifiers=True,
34
+ export_triangulated_mesh=True,
35
+ export_normals=True,
36
+ export_uv=False,
37
+ export_colors='NONE',
38
+ global_scale=1.0,
39
+ forward_axis='Y',
40
+ up_axis='Z'
41
+ )
42
+
43
+ bpy.ops.object.select_all(action='SELECT')
44
+ bpy.ops.object.delete(use_global=False)
45
+ bpy.ops.outliner.orphans_purge()
46
+ bpy.ops.outliner.orphans_purge()
47
+ bpy.ops.outliner.orphans_purge()
48
+
49
+ # ---
50
+
51
+ importDir = "PLY0/"
52
+ outputDir = "PLY1/"
53
+ if not isdir(outputDir): mkdir(outputDir)
54
+ #
55
+ counter = 0
56
+ for file in glob.glob(importDir + "*.ply"):
57
+ model_name = pathlib.Path(file).stem
58
+ if pathlib.Path(outputDir+model_name+'.ply').is_file() == True: continue
59
+ bpy.ops.wm.ply_import(filepath=file)
60
+ counter = counter + 1
61
+ if counter > 10:
62
+ bpy.ops.object.select_all(action='SELECT')
63
+
64
+ mesh_obs = (o for o in bpy.context.selected_objects
65
+ if o.type == 'MESH' and o.dimensions.length)
66
+ for o in mesh_obs:
67
+ o.scale *= 2 / max(o.dimensions)
68
+ o.scale *= 0.55
69
+
70
+ bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
71
+
72
+ for obj in bpy.context.selected_objects: obj.location = (0,0,0)
73
+
74
+ for obj in bpy.context.selected_objects:
75
+ bpy.ops.object.select_all(action='DESELECT')
76
+ obj.select_set(True)
77
+ bpy.ops.wm.ply_export(
78
+ filepath=outputDir+model_name+'.ply',
79
+ filter_glob='*.ply',
80
+ check_existing=False,
81
+ ascii_format=False,
82
+ export_selected_objects=True,
83
+ apply_modifiers=True,
84
+ export_triangulated_mesh=True,
85
+ export_normals=True,
86
+ export_uv=False,
87
+ export_colors='NONE',
88
+ global_scale=1.0,
89
+ forward_axis='Y',
90
+ up_axis='Z'
91
+ )
92
+
93
+ bpy.ops.object.select_all(action='SELECT')
94
+ bpy.ops.object.delete(use_global=False)
95
+ bpy.ops.outliner.orphans_purge()
96
+ bpy.ops.outliner.orphans_purge()
97
+ bpy.ops.outliner.orphans_purge()
98
+ counter = 0
99
+
100
+ # ===
101
+ bpy.ops.object.select_all(action='SELECT')
102
+ bpy.ops.object.delete(use_global=False)
103
+ bpy.ops.outliner.orphans_purge()
104
+ bpy.ops.outliner.orphans_purge()
105
+ bpy.ops.outliner.orphans_purge()
106
+ # ===
107
+
108
+ for file in glob.glob(importDir + "*.ply"):
109
+ model_name = pathlib.Path(file).stem
110
+ if pathlib.Path(outputDir+model_name+'.ply').is_file() == True: continue
111
+ bpy.ops.wm.ply_import(filepath=file)
112
+
113
+ bpy.ops.object.select_all(action='SELECT')
114
+
115
+ mesh_obs = (o for o in bpy.context.selected_objects
116
+ if o.type == 'MESH' and o.dimensions.length)
117
+ for o in mesh_obs:
118
+ o.scale *= 2 / max(o.dimensions)
119
+ o.scale *= 0.55
120
+
121
+ bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
122
+
123
+ for obj in bpy.context.selected_objects: obj.location = (0,0,0)
124
+
125
+ for obj in bpy.context.selected_objects:
126
+ bpy.ops.object.select_all(action='DESELECT')
127
+ obj.select_set(True)
128
+ bpy.ops.wm.ply_export(
129
+ filepath=outputDir+model_name+'.ply',
130
+ filter_glob='*.ply',
131
+ check_existing=False,
132
+ ascii_format=False,
133
+ export_selected_objects=True,
134
+ apply_modifiers=True,
135
+ export_triangulated_mesh=True,
136
+ export_normals=True,
137
+ export_uv=False,
138
+ export_colors='NONE',
139
+ global_scale=1.0,
140
+ forward_axis='Y',
141
+ up_axis='Z'
142
+ )
143
+
144
+ bpy.ops.object.select_all(action='SELECT')
145
+ bpy.ops.object.delete(use_global=False)
146
+ bpy.ops.outliner.orphans_purge()
147
+ bpy.ops.outliner.orphans_purge()
148
+ bpy.ops.outliner.orphans_purge()