-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl_to_blender_gunma.py
94 lines (74 loc) · 2.81 KB
/
stl_to_blender_gunma.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
import os
import bpy
from pathlib import Path
from stat import ST_SIZE
# input parent dir path
dir_path = str(r"I:\20220923_Gunma_Kimura_WhaleDataBase")
# get whole file names specimen number
def search_stl(path,list_stl):
"""
output list of dir of stl under the folder.
"""
p = Path(path)
i = 0
for file in p.iterdir():
if file.is_dir():
search_stl(file,list_stl)
elif file.is_file():
base, ext = os.path.splitext(file) #make taple
if ext == ".stl":
# resolve()を使って絶対パスを表示する
list_stl.append(file.resolve())
return list_stl
def translate(vector):
bpy.ops.transform.translate(value=vector, constraint_axis=(True,True,True))
# del whole models
def delete_whole_models():
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
# save blender file
def save_blender(path, specimen_name):
p = Path(path + "\\" + specimen_name + ".blend")
bpy.ops.wm.save_as_mainfile(filepath = str(p))
# make list whole stl path
list_stl = search_stl(dir_path,[])
# make set of specimen numbers
list_spnum = list()
for i in range(len(list_stl)):
list_spnum.append(str(list_stl[i]).split("_")[-1]) # listing spnum like a "GMNH-VM-474.stl"
set_spnum = set(list_spnum)
# import stl files with spnum
for i in set_spnum:
basename = str() # name for Title and Blender file name
for stl in list_stl:
spname = str(stl).split("_")
if spname[-1] == i:
basename = spname[-4] + "_" + spname[-3] + "_" + spname[-1].split(".")[0]
importpath = str(stl)
# import stl
bpy.ops.import_mesh.stl(filepath=importpath,
axis_forward='-Z',
axis_up='Y',
filter_glob="*.stl")
# geometry to origin
bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN')
bpy.ops.object.location_clear(clear_delta=False)
dim_x = bpy.context.object.dimensions.x
# pe or tyb
if str(stl).split("_")[-2] == "pe":
translate((dim_x,0,0))
elif str(stl).split("_")[-2] == "tyb":
translate((-(dim_x),0,0))
else:
print("There is unknown part. Please named pe or tyb.") # Unknown object
break
else:
continue
# add sphere at origin
bpy.ops.mesh.primitive_uv_sphere_add(radius=(5))
# save blender file with specimen number
save_blender(dir_path, basename)
# Sketchfab upload
bpy.data.window_managers["WinMan"].sketchfab_export.title = basename
bpy.ops.wm.sketchfab_export()
delete_whole_models()