-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
initializer.py
138 lines (111 loc) · 5.86 KB
/
initializer.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
import bpy
import numpy as np
import os
class Anton_OT_ForceUpdater(bpy.types.Operator):
bl_idname = "anton.forceupdate"
bl_label = ""
diffuse_library = [
(255/255, 0/255, 199/255, 1),
(255/255, 248/255, 0/255, 1),
(83/255, 255/255, 0/255, 1),
(13/255, 218/255, 134/255, 1),
(115/255, 255/255, 0/255, 1),
(0/255, 217/255, 255/255, 1),
(235/255, 255/255, 0/255, 1),
(255/255, 178/255, 83/255, 1),
(148/255, 13/255, 88/255, 1),
(255/255, 0/255, 141/255, 1),
(255/255, 139/255, 0/255, 1)]
def execute(self, context):
"""Adds ``NATIVE``, ``FIXED``, ``NODESIGNSPACE`` and ``FORCE_{}`` materials and
vertex groups ``DIRECTION_{}`` to the active object to facilitate problem definition.
:return: ``FINISHED`` if successful, ``CANCELLED`` otherwise
\\
"""
scene = context.scene
bpy.ops.anton.initialize()
active_object = bpy.context.active_object
if scene.anton.initialized:
if 'NATIVE' not in bpy.data.materials:
native_mat = bpy.data.materials.new(name='NATIVE')
active_object.data.materials.append(native_mat)
if 'FIXED' not in bpy.data.materials:
mat = bpy.data.materials.new(name='FIXED')
mat.diffuse_color = (1, 0, 0, 1)
active_object.data.materials.append(mat)
if 'NONDESIGNSPACE' not in bpy.data.materials:
nds_mat = bpy.data.materials.new(name='NONDESIGNSPACE')
nds_mat.diffuse_color = (0, 0, 1, 1)
active_object.data.materials.append(nds_mat)
for i in range(scene.anton.number_of_forces):
if str('FORCE_{}'.format(i+1)) not in bpy.data.materials:
# Take care of popping of excess forces
size = len(scene.forceprop)
new = scene.forceprop.add()
new.name = str(size+1)
new.direction_boolean = False
temp_mat = bpy.data.materials.new(name='FORCE_{}'.format(i+1))
temp_mat.diffuse_color = self.diffuse_library[i]
active_object.data.materials.append(temp_mat)
bpy.ops.object.vertex_group_add()
active_object.vertex_groups.active.name = 'DIRECTION_{}'.format(i+1)
scene.anton.forced = True
self.report({'INFO'}, 'FORCES: {}'.format(scene.anton.number_of_forces))
return{'FINISHED'}
else:
self.report({'ERROR'}, 'Initialize before problem definition.')
return{'CANCELLED'}
class Anton_OT_Initializer(bpy.types.Operator):
bl_idname = 'anton.initialize'
bl_label = 'Anton_Initializer'
bl_description = 'Initializes design space'
def execute(self, context):
"""Design space is defined with existing geometry.
:ivar objects: List of all the obstacle objects
:vartype objects: ``list``
:ivar points: Bounding points of all the obstacles
:vartype points: numpy array of ``floats``
:ivar hull: Convexhull of the bounding points
:vartype hull: numpy array of ``floats``
:return: ``FINISHED`` if successful, ``CANCELLED`` otherwise
"""
scene = context.scene
active_object = bpy.context.active_object
bpy.context.space_data.shading.type = 'MATERIAL'
scene.anton.filename = active_object.name
if not os.path.exists(os.path.join(scene.anton.workspace_path, scene.anton.filename)):
os.makedirs(os.path.join(scene.anton.workspace_path, scene.anton.filename))
# subprocess.call(["python3", os.path.join(scene.anton.taichi_path, "projects/spgrid_topo_opt/scripts/opt_anton.py")])
bpy.ops.object.modifier_add(type='TRIANGULATE')
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Triangulate")
bpy.ops.export_scene.obj(filepath=os.path.join(scene.anton.workspace_path, scene.anton.filename, scene.anton.filename + '.obj'),
check_existing=True,
axis_forward='Y',
axis_up='Z',
filter_glob="*.obj;*.mtl",
use_selection=False,
use_animation=False,
use_mesh_modifiers=True,
use_normals=False,
use_uvs=False,
use_materials=False,
use_triangles=True,
use_nurbs=False,
use_vertex_groups=False,
use_blen_objects=True,
group_by_object=False,
group_by_material=False,
keep_vertex_order=True,
global_scale=1, path_mode='AUTO')
active_object.select_set(True)
bpy.ops.object.delete()
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.import_scene.obj(filepath=os.path.join(scene.anton.workspace_path, scene.anton.filename, scene.anton.filename + '.obj'),
axis_forward='Y',
axis_up='Z')
_temp_ob = bpy.context.selected_objects[0]
_temp_ob.name = scene.anton.filename
bpy.context.view_layer.objects.active = _temp_ob
scene.anton.initialized = True
self.report({'INFO'}, 'Initialized design space.')
return {'FINISHED'}