File size: 5,605 Bytes
1a682c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Blender Script that takes a folder of GLB files and processes them into PLY files
# strips textures and vertex colors leaving only vertex pos, vertex norm, index buffer
# 1. merges model into one contiguous mesh
# 2. scales the model to a unit sphere
# 3. centers the origin
# 4. centers the model
import bpy
import glob
import os
from os import mkdir
from os.path import isdir
import pathlib

# ---

importDir = "GLB/"
outputDir = "PLY0/"
if not isdir(outputDir): mkdir(outputDir)
#
for file in glob.glob(importDir + "*.glb"):
    model_name = pathlib.Path(file).stem
    if pathlib.Path(outputDir+model_name+'.ply').is_file() == True: continue
    try:
        bpy.ops.import_scene.gltf(filepath=file)
    except:
        continue
    bpy.ops.wm.ply_export(
                            filepath=outputDir+model_name+'.ply',
                            filter_glob='*.ply',
                            check_existing=False,
                            ascii_format=False,
                            export_selected_objects=True,
                            apply_modifiers=True,
                            export_triangulated_mesh=True,
                            export_normals=True,
                            export_uv=False,
                            export_colors='NONE',
                            global_scale=1.0,
                            forward_axis='Y',
                            up_axis='Z'
                        )
    
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete(use_global=False)
    bpy.ops.outliner.orphans_purge()
    bpy.ops.outliner.orphans_purge()
    bpy.ops.outliner.orphans_purge()

# ---

importDir = "PLY0/"
outputDir = "PLY1/"
if not isdir(outputDir): mkdir(outputDir)
#
counter = 0
for file in glob.glob(importDir + "*.ply"):
    model_name = pathlib.Path(file).stem
    if pathlib.Path(outputDir+model_name+'.ply').is_file() == True: continue
    bpy.ops.wm.ply_import(filepath=file)
    counter = counter + 1
    if counter > 10:
        bpy.ops.object.select_all(action='SELECT')
        
        mesh_obs = (o for o in bpy.context.selected_objects
        if o.type == 'MESH' and o.dimensions.length)
        for o in mesh_obs:
            o.scale *= 2 / max(o.dimensions)
            o.scale *= 0.55
            
        bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
        
        for obj in bpy.context.selected_objects: obj.location = (0,0,0)

        for obj in bpy.context.selected_objects:
            bpy.ops.object.select_all(action='DESELECT')
            obj.select_set(True)
            bpy.ops.wm.ply_export(
                                    filepath=outputDir+model_name+'.ply',
                                    filter_glob='*.ply',
                                    check_existing=False,
                                    ascii_format=False,
                                    export_selected_objects=True,
                                    apply_modifiers=True,
                                    export_triangulated_mesh=True,
                                    export_normals=True,
                                    export_uv=False,
                                    export_colors='NONE',
                                    global_scale=1.0,
                                    forward_axis='Y',
                                    up_axis='Z'
                                )
                                
        bpy.ops.object.select_all(action='SELECT')
        bpy.ops.object.delete(use_global=False)
        bpy.ops.outliner.orphans_purge()
        bpy.ops.outliner.orphans_purge()
        bpy.ops.outliner.orphans_purge()
        counter = 0

# ===
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
bpy.ops.outliner.orphans_purge()
bpy.ops.outliner.orphans_purge()
bpy.ops.outliner.orphans_purge()
# ===

for file in glob.glob(importDir + "*.ply"):
    model_name = pathlib.Path(file).stem
    if pathlib.Path(outputDir+model_name+'.ply').is_file() == True: continue
    bpy.ops.wm.ply_import(filepath=file)
    
    bpy.ops.object.select_all(action='SELECT')
    
    mesh_obs = (o for o in bpy.context.selected_objects
    if o.type == 'MESH' and o.dimensions.length)
    for o in mesh_obs:
        o.scale *= 2 / max(o.dimensions)
        o.scale *= 0.55
        
    bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='BOUNDS')
    
    for obj in bpy.context.selected_objects: obj.location = (0,0,0)
    
    for obj in bpy.context.selected_objects:
        bpy.ops.object.select_all(action='DESELECT')
        obj.select_set(True)
        bpy.ops.wm.ply_export(
                                filepath=outputDir+model_name+'.ply',
                                filter_glob='*.ply',
                                check_existing=False,
                                ascii_format=False,
                                export_selected_objects=True,
                                apply_modifiers=True,
                                export_triangulated_mesh=True,
                                export_normals=True,
                                export_uv=False,
                                export_colors='NONE',
                                global_scale=1.0,
                                forward_axis='Y',
                                up_axis='Z'
                            )
        
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete(use_global=False)
    bpy.ops.outliner.orphans_purge()
    bpy.ops.outliner.orphans_purge()
    bpy.ops.outliner.orphans_purge()