-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcenterObjectsInBlender.py
More file actions
138 lines (112 loc) · 4.4 KB
/
Copy pathcenterObjectsInBlender.py
File metadata and controls
138 lines (112 loc) · 4.4 KB
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
import sys
import argparse
parser = argparse.ArgumentParser(description='Process arguments')
parser.add_argument("--folder")
parser.add_argument("--newModelName")
# parse all args after "--"
args = parser.parse_args(sys.argv[sys.argv.index("--") + 1:])
import os
# Define constants
organ_name = args.newModelName
folder = args.folder
new_model_files_dir = os.path.join(folder, "generated")
centered_model_files_dir = os.path.join(new_model_files_dir, "centered")
metadata_filename = os.path.join(new_model_files_dir, "organ_metadata.json")
organ_model_filename = os.path.join(new_model_files_dir, organ_name + ".obj")
print(organ_name)
print(folder)
print(new_model_files_dir)
print(centered_model_files_dir)
print(metadata_filename)
print(organ_model_filename)
if not os.path.exists(new_model_files_dir):
os.makedirs(new_model_files_dir)
if not os.path.exists(centered_model_files_dir):
os.makedirs(centered_model_files_dir)
def location_to_json (location):
return {
"x": location.x,
"y": location.y,
"z": location.z,
}
import bpy
# Remove all objects from the scene
# NOTE:
# An exception is thrown if no objets exist
# In that case just ignore the exception
try:
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete(use_global=False)
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
except:
pass
# get list of all files in directory
file_list = os.listdir(folder)
# reduce the list to files ending in 'obj'
# using 'list comprehensions'
obj_list = [item for item in file_list if item[-3:] == 'obj']
# loop through the strings in obj_list.
for item in obj_list:
full_path_to_file = os.path.join(folder, item)
bpy.ops.import_scene.obj(filepath=full_path_to_file)
# Give the object it's name
imported_objects = [obj for obj in bpy.context.selected_objects[:]]
for imported_obj in imported_objects:
imported_obj.name = item[:-4]
imported_obj.data.name = imported_obj.name + "_MESH"
# Select all objects then center the entire model
# gather list of items of interest and select only them
organ_parts = [item.name for item in bpy.data.objects if item.type == "MESH"]
for object_name in organ_parts:
bpy.data.objects[object_name].select = True
# Get the 3D view context so we can begin moving things around
context = None
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
context = bpy.context.copy()
context['area'] = area
break;
# Store metadata about this organ
organ_metadata = {};
# Move the origin to the center of the object
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
# Move the 3D cursor to the center of the geometry and record it's position
# This is the organ's relative position in the body
bpy.ops.view3d.snap_cursor_to_selected(context)
organ_metadata["bodyOffset"] = location_to_json(bpy.context.scene.cursor_location)
# Center the whole model by:
# 1) Snap the 3D cursor to the center
# 2) Snap the selected objects to the cursor
bpy.ops.view3d.snap_cursor_to_center(context)
bpy.ops.view3d.snap_selected_to_cursor(context, use_offset=True)
# Save whole model
bpy.ops.export_scene.obj(filepath=organ_model_filename, use_selection=True)
# Save the offset of individual parts
organ_part_offsets = organ_metadata["parts"] = [];
# Deselct all objects
for selected_obj in bpy.context.selected_objects[:]:
selected_obj.select = False
# Save the object parts
for object_name in organ_parts:
organ_part = bpy.data.objects[object_name]
organ_part.select = True
# Save each model file after it's been moved to the center, but still has it's offset
# based on the center of the whole model
organ_part_filename = os.path.join(new_model_files_dir, object_name + ".obj")
bpy.ops.export_scene.obj(filepath=organ_part_filename, use_selection=True)
# Save each model file after it's origin has been moved to the center
bpy.ops.view3d.snap_cursor_to_center(context)
bpy.ops.view3d.snap_selected_to_cursor(context, use_offset=True)
organ_part_offsets.append({
"name": object_name,
"file": object_name + ".obj",
})
organ_part_filename = os.path.join(centered_model_files_dir, object_name + ".obj")
bpy.ops.export_scene.obj(filepath=organ_part_filename, use_selection=True)
organ_part.select = False
# Save the organ metadata to a json file
import json
with open(metadata_filename, 'w') as outfile:
json.dump(organ_metadata, outfile)