-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCONFIG_Symmetry.py
203 lines (162 loc) · 7.14 KB
/
DCONFIG_Symmetry.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# ------------------------------------------------------------
# Copyright(c) 2018-2020 Jesse Yurkovich
# Licensed under the MIT License <http://opensource.org/licenses/MIT>.
# See the LICENSE file in the repo root for full license information.
# ------------------------------------------------------------
#
# Better symmetry
#
import bpy
from mathutils import (Vector, Matrix)
from . import DCONFIG_Utils as dc
class DCONFIG_OT_mesh_symmetry(bpy.types.Operator):
bl_idname = "dconfig.mesh_symmetry"
bl_label = "DC Mesh Symmetry"
bl_description = "Symmetrize mesh along an axis"
bl_options = {'UNDO'}
direction: bpy.props.EnumProperty(
items=(
('POSITIVE_X', "+X to -X", "+X to -X", 0),
('NEGATIVE_X', "-X to +X", "-X to +X", 1),
('POSITIVE_Y', "+Y to -Y", "+Y to -Y", 2),
('NEGATIVE_Y', "-Y to +Y", "-Y to +Y", 3),
('POSITIVE_Z', "+Z to -Z", "+Z to -Z", 4),
('NEGATIVE_Z', "-Z to +Z", "-Z to +Z", 5),
),
name="Direction",
description="which side to copy from",
default='POSITIVE_X',
options={'ANIMATABLE'},
update=None,
get=None,
set=None)
dc_uses_symmetry_gizmo = True
@classmethod
def poll(cls, context):
return dc.active_mesh_selected(context)
def execute(self, context):
dc.trace_enter(self)
if context.mode == 'EDIT_MESH':
bpy.ops.mesh.select_linked()
bpy.ops.mesh.symmetrize(direction=self.direction)
else:
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.symmetrize(direction=self.direction)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
DCONFIG_GGT_symmetry_gizmo.destroy(context)
return dc.trace_exit(self)
def invoke(self, context, event):
dc.trace_enter(self)
if context.space_data.type == 'VIEW_3D':
DCONFIG_GGT_symmetry_gizmo.create(context)
return dc.trace_exit(self)
class DCONFIG_GT_symmetry_gizmo(bpy.types.Gizmo):
__slots__ = (
"custom_shape",
"op",
"direction",
"draw_offset",
)
cube_tri_verts = (
(-0.5, -0.5, 0.5), (-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),
(-0.5, 0.5, 0.5), (0.5, 0.5, -0.5), (-0.5, 0.5, -0.5),
(0.5, 0.5, 0.5), (0.5, -0.5, -0.5), (0.5, 0.5, -0.5),
(0.5, -0.5, 0.5), (-0.5, -0.5, -0.5), (0.5, -0.5, -0.5),
(0.5, 0.5, -0.5), (-0.5, -0.5, -0.5), (-0.5, 0.5, -0.5),
(-0.5, 0.5, 0.5), (0.5, -0.5, 0.5), (0.5, 0.5, 0.5),
(-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5), (-0.5, 0.5, -0.5),
(-0.5, 0.5, 0.5), (0.5, 0.5, 0.5), (0.5, 0.5, -0.5),
(0.5, 0.5, 0.5), (0.5, -0.5, 0.5), (0.5, -0.5, -0.5),
(0.5, -0.5, 0.5), (-0.5, -0.5, 0.5), (-0.5, -0.5, -0.5),
(0.5, 0.5, -0.5), (0.5, -0.5, -0.5), (-0.5, -0.5, -0.5),
(-0.5, 0.5, 0.5), (-0.5, -0.5, 0.5), (0.5, -0.5, 0.5),
)
def draw(self, context):
self.draw_custom_shape(self.custom_shape)
def draw_select(self, context, select_id):
self.draw_custom_shape(self.custom_shape, select_id=select_id)
def setup(self):
if not hasattr(self, "custom_shape"):
self.custom_shape = self.new_custom_shape('TRIS', self.cube_tri_verts)
def update(self, mat_target):
self.matrix_basis = mat_target
self.matrix_offset = Matrix.Translation(self.draw_offset * 4)
def invoke(self, context, event):
self.op.direction = self.direction
self.op.execute(context)
return {'FINISHED'}
def modal(self, context, event, tweak):
return {'RUNNING_MODAL'}
class DCONFIG_GGT_symmetry_gizmo(bpy.types.GizmoGroup):
bl_label = "Symmetry Gizmo Group"
bl_space_type = 'VIEW_3D'
bl_region_type = 'WINDOW'
bl_options = {'3D'}
gizmo_state = {}
use_local = True
@staticmethod
def my_target_operator(context):
wm = context.window_manager
op = wm.operators[-1] if wm.operators else None
return op if getattr(op, "dc_uses_symmetry_gizmo", False) else None
@classmethod
def poll(cls, context):
op = cls.my_target_operator(context)
if op is None:
DCONFIG_GGT_symmetry_gizmo.destroy(context)
return False
return True
@classmethod
def create(cls, context, use_local=True):
cls.gizmo_state['show_gizmo_object_translate'] = context.space_data.show_gizmo_object_translate
cls.gizmo_state['show_gizmo_object_rotate'] = context.space_data.show_gizmo_object_rotate
cls.gizmo_state['show_gizmo_object_scale'] = context.space_data.show_gizmo_object_scale
cls.gizmo_state['show_gizmo'] = context.space_data.show_gizmo
cls.use_local = use_local
context.space_data.show_gizmo_object_translate = False
context.space_data.show_gizmo_object_rotate = False
context.space_data.show_gizmo_object_scale = False
context.space_data.show_gizmo = True
wm = context.window_manager
wm.gizmo_group_type_ensure("DCONFIG_GGT_symmetry_gizmo")
@classmethod
def destroy(cls, context):
wm = context.window_manager
wm.gizmo_group_type_unlink_delayed("DCONFIG_GGT_symmetry_gizmo")
if cls.gizmo_state:
context.space_data.show_gizmo_object_translate = cls.gizmo_state['show_gizmo_object_translate']
context.space_data.show_gizmo_object_rotate = cls.gizmo_state['show_gizmo_object_rotate']
context.space_data.show_gizmo_object_scale = cls.gizmo_state['show_gizmo_object_scale']
context.space_data.show_gizmo = cls.gizmo_state['show_gizmo']
cls.gizmo_state.clear()
def setup(self, context):
def setup_widget(direction, draw_offset, color):
mpr = self.gizmos.new("DCONFIG_GT_symmetry_gizmo")
mpr.op = DCONFIG_GGT_symmetry_gizmo.my_target_operator(context)
mpr.direction = direction
mpr.draw_offset = draw_offset
mpr.color = color
mpr.alpha = 0.3
mpr.color_highlight = color
mpr.alpha_highlight = 0.5
mpr.select_bias = 0
mpr.scale_basis = 0.2
mpr.use_draw_offset_scale = True
mpr.use_select_background = True
mpr.use_event_handle_all = False
ui_theme_prefs = context.preferences.themes[0].user_interface
setup_widget("POSITIVE_X", Vector((-1, 0, 0)), ui_theme_prefs.axis_x)
setup_widget("NEGATIVE_X", Vector((1, 0, 0)), ui_theme_prefs.axis_x)
setup_widget("POSITIVE_Y", Vector((0, -1, 0)), ui_theme_prefs.axis_y)
setup_widget("NEGATIVE_Y", Vector((0, 1, 0)), ui_theme_prefs.axis_y)
setup_widget("POSITIVE_Z", Vector((0, 0, -1)), ui_theme_prefs.axis_z)
setup_widget("NEGATIVE_Z", Vector((0, 0, 1)), ui_theme_prefs.axis_z)
def refresh(self, context):
if not self.use_local:
mat_target = Matrix.Identity(4)
else:
target = context.active_object
mat_target = target.matrix_world.normalized()
for mpr in self.gizmos:
mpr.update(mat_target)