-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMirrorMirrorTool.py
222 lines (168 loc) · 7.24 KB
/
MirrorMirrorTool.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
bl_info = {
"name": "Mirror Mirror Tool",
"author": "Robert Fornof",
"version": (1, 4),
"blender": (2, 71, 0),
"location": "View3D > Tool_Tab> Mirror",
"description": "Set mirror",
"warning": "",
"wiki_url": "",
"category": "Object"}
import bpy
from bpy.app.handlers import persistent
#------------------- FUNCTIONS------------------------------
# Do the Basic Union, Difference and Intersection Operations
def Operation(context,_operation):
''' select the object, then select what you want it's mirror object to be '''
#select 2 context object
try:
# select objects
if(len(bpy.context.selected_objects)) == 1 : # one is selected , add mirror mod immediately to that object#
modifier_ob = bpy.context.active_object
print("one is selected")
mirror_mod = modifier_ob.modifiers.new("mirror_mirror","MIRROR")
# if modifier_ob.type !='MESH' and modifier_ob.type !="CURVE":
# mirror_ob = modifier_ob # set to mirror_ob , hope the other one is a mesh
# mirror_ob.select = False
# modifier_ob = bpy.context.selected_objects[0]
else:
#mirror_ob
mirror_ob = bpy.context.active_object # last ob selected
mirror_ob.select_set(False) # pop modifier_ob from sel_stack
print("popped")
#modifier_ob
modifier_ob = bpy.context.selected_objects[0]
print("Modifier object:" +str(modifier_ob.name))
#modifier_ob.select=1
print("mirror_ob",mirror_ob)
print("modifier_ob",modifier_ob)
# put mirror modifier on modifier_ob
mirror_mod = modifier_ob.modifiers.new("mirror_mirror","MIRROR")
# set mirror object to mirror_ob
mirror_mod.mirror_object = mirror_ob
if _operation == "MIRROR_X":
mirror_mod.use_x = True
mirror_mod.use_y = False
mirror_mod.use_z = False
elif _operation == "MIRROR_Y":
mirror_mod.use_x = False
mirror_mod.use_y = True
mirror_mod.use_z = False
elif _operation == "MIRROR_Z":
mirror_mod.use_x = False
mirror_mod.use_y = False
mirror_mod.use_z = True
#selection at the end -add back the deselected mirror modifier object
mirror_ob.select_set(True)
modifier_ob.select_set(True)
##bpy.context.scene.objects.active = modifier_ob
bpy.context.view_layer.objects.active = modifier_ob
print("Selected" + str(modifier_ob)) # modifier ob is the active ob
#mirror_ob.select = 0
#one = bpy.context.selected_objects[0]
#bpy.data.objects[one.name].select = 1
except Exception as e:
print("error!" + str(e))
print("please select exactly two objects, the last one gets the modifier unless its not a mesh")
#------------------- OPERATOR CLASSES ------------------------------
# Mirror Tool
class MirrorX(bpy.types.Operator):
"""This adds an X mirror to the selected object"""
bl_idname = "object.mirror_mirror_x"
bl_label = "Mirror X"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
Operation(context,"MIRROR_X")
return {'FINISHED'}
class MirrorY(bpy.types.Operator):
"""This adds a Y mirror modifier"""
bl_idname = "object.mirror_mirror_y"
bl_label = "Mirror Y"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
Operation(context,"MIRROR_Y")
return {'FINISHED'}
class MirrorZ(bpy.types.Operator):
"""This add a Z mirror modifier"""
bl_idname = "object.mirror_mirror_z"
bl_label = "Mirror Z"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
Operation(context,"MIRROR_Z")
return {'FINISHED'}
#------------------- MENU CLASSES ------------------------------
class MirrorMenu(bpy.types.Menu):
bl_label = "Mirror_Mirror_Tool_MT_"
bl_idname = "OBJECT_MT_mirror"
def draw(self, context):
layout = self.layout
self.layout.operator(MirrorTool.bl_idname,icon = "ZOOMIN")
class MirrorTab(bpy.types.Panel):
#"[note]: Add a mirror on the x, y , or z axis using : ALT+SHIFT+X , ALT+SHIFT+Y, ALT+SHIFT+Z in object mode"
bl_label = "Mirror"
bl_idname = "Mirror_Mirror_Tool_PT_"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Mirror"
bl_context = "objectmode"
def draw(self,context):
layout = self.layout
box = layout.box()
box.label(text="Mirror Axis:",icon = "MODIFIER")
box.operator(MirrorX.bl_idname ,icon = "ZOOM_IN")
box.operator(MirrorY.bl_idname ,icon = "ZOOM_IN")
box.operator(MirrorZ.bl_idname ,icon = "ZOOM_IN")
box.separator()
#---------- Tree Viewer--------------
def VIEW3D_MirrorMenu(self, context):
self.layout.menu(MirrorMenu.bl_idname)
#------------------- REGISTER ------------------------------
addon_keymaps = []
def register():
# Operators
bpy.utils.register_class(MirrorX)
bpy.utils.register_class(MirrorY)
bpy.utils.register_class(MirrorZ)
#Append 3DVIEW Menu
#bpy.utils.register_class(MirrorMenu)
#bpy.types.VIEW3D_MT_object.append(VIEW3D_MirrorMenu)
# Append 3DVIEW Tab
bpy.utils.register_class(MirrorTab)
# handle the keymap
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(MirrorX.bl_idname, 'X', 'PRESS', alt=True, shift = True)
addon_keymaps.append((km, kmi))
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(MirrorY.bl_idname, 'Y', 'PRESS', alt=True, shift = True)
addon_keymaps.append((km, kmi))
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(MirrorZ.bl_idname, 'Z', 'PRESS', alt=True, shift = True)
addon_keymaps.append((km, kmi))
def unregister():
#bpy.utils.unregister_class(MirrorMenu)
bpy.utils.unregister_class(MirrorTab)
#bpy.types.VIEW3D_MT_object.remove(VIEW3D_MirrorMenu)
#Operators
bpy.utils.unregister_class(MirrorX)
bpy.utils.unregister_class(MirrorY)
bpy.utils.unregister_class(MirrorZ)
# bpy.app.handlers.scene_update_post.remove(HandleScene)
#bpy.types.VIEW3D_MT_object.remove(VIEW3D_MirrorMenu)
# Keymapping
# handle the keymap
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
try:
unregister()
except:
pass
register()