Skip to content

Commit 29dc1cc

Browse files
committed
refactor and fix bug of selected applying
1 parent a916635 commit 29dc1cc

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

__init__.py

+27-36
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,8 @@
3636

3737
######################################################
3838

39-
def select_object(obj, value):
40-
obj.select_set(value)
41-
42-
def get_active_object():
43-
return bpy.context.window.view_layer.objects.active
44-
45-
def set_active_object(obj):
46-
bpy.context.window.view_layer.objects.active = obj
47-
4839
def clear_shape_keys(Name):
49-
obj = get_active_object()
40+
obj = bpy.context.window.view_layer.objects.active
5041
if obj.data.shape_keys is None:
5142
return True
5243
obj.active_shape_key_index = len(obj.data.shape_keys.key_blocks) - 1
@@ -78,11 +69,17 @@ def delete_object(Obj):
7869

7970
def apply_modifier(target_object=None, target_modifiers=None):
8071
if target_object is None:
81-
obj_src = get_active_object()
72+
obj_src = bpy.context.window.view_layer.objects.active
8273
else:
8374
obj_src = target_object
8475

85-
if not obj_src.modifiers:
76+
if target_modifiers is None:
77+
target_modifiers = []
78+
for x in obj_src.modifiers:
79+
if x.show_viewport:
80+
target_modifiers.append(x.name)
81+
82+
if len(target_modifiers) == 0:
8683
# if object has no modifier then skip
8784
return True
8885

@@ -92,24 +89,18 @@ def apply_modifier(target_object=None, target_modifiers=None):
9289

9390
if obj_src.data.shape_keys is None:
9491
# if object has no shapekeys, just apply modifier
95-
for x in obj_src.modifiers:
92+
for x in target_modifiers:
9693
try:
97-
bpy.ops.object.modifier_apply(modifier=x.name)
94+
bpy.ops.object.modifier_apply(modifier=x)
9895
except RuntimeError:
9996
pass
10097
return True
10198

10299
obj_fin = clone_object(obj_src)
103100

104-
set_active_object(obj_fin)
101+
bpy.context.window.view_layer.objects.active = obj_fin
105102
clear_shape_keys('Basis')
106103

107-
if target_modifiers is None:
108-
target_modifiers = []
109-
for x in obj_fin.modifiers:
110-
if x.show_viewport:
111-
target_modifiers.append(x.name)
112-
113104
for x in target_modifiers:
114105
try:
115106
bpy.ops.object.modifier_apply(modifier=x)
@@ -123,7 +114,7 @@ def apply_modifier(target_object=None, target_modifiers=None):
123114
tmp_name = obj_src.data.shape_keys.key_blocks[i].name
124115
obj_tmp = clone_object(obj_src)
125116

126-
set_active_object(obj_tmp)
117+
bpy.context.window.view_layer.objects.active = obj_tmp
127118
clear_shape_keys(tmp_name)
128119

129120
for x in target_modifiers:
@@ -132,8 +123,10 @@ def apply_modifier(target_object=None, target_modifiers=None):
132123
except RuntimeError:
133124
pass
134125

135-
select_object(obj_tmp, True)
136-
set_active_object(obj_fin)
126+
obj_tmp.modifiers.clear()
127+
128+
obj_tmp.select_set(True)
129+
bpy.context.window.view_layer.objects.active = obj_fin
137130
try:
138131
bpy.ops.object.join_shapes()
139132
obj_fin.data.shape_keys.key_blocks[-1].name = tmp_name
@@ -152,20 +145,19 @@ def draw(self, context):
152145
bpy.context.window_manager.popup_menu(draw, title="Error", icon='INFO')
153146

154147
return False
155-
148+
156149
tmp_name = obj_src.name
157150
tmp_data_name = obj_src.data.name
158151
obj_fin.name = tmp_name + '.tmp'
159152

160-
161153
obj_src.data = obj_fin.data
162154
obj_src.data.name = tmp_data_name
163155

164156
for x in target_modifiers:
165157
obj_src.modifiers.remove(obj_src.modifiers[x])
166-
158+
167159
delete_object(obj_fin)
168-
set_active_object(obj_src)
160+
bpy.context.window.view_layer.objects.active = obj_src
169161

170162
class OBJECT_OT_apply_all_modifiers(bpy.types.Operator):
171163
"""Apply All Modifier to Selected Mesh Object"""
@@ -183,7 +175,7 @@ def execute(self, context):
183175
apply_modifier(target_object=bpy.data.objects[x])
184176

185177
for x in targets:
186-
select_object(bpy.data.objects[x], True)
178+
bpy.data.objects[x].select_set(True)
187179

188180
return {'FINISHED'}
189181

@@ -204,21 +196,20 @@ def poll(cls, context):
204196
return obj and obj.type == 'MESH'
205197

206198
def execute(self, context):
207-
obj = get_active_object()
208-
objname = obj.name
199+
obj = bpy.context.window.view_layer.objects.active
209200

210201
if self.modifier_names and len(self.modifier_names) > 0:
211202
bpy.ops.object.select_all(action='DESELECT')
212203
str_targets = []
213204
for i in range(len(self.modifier_names)):
214-
if self.flags[i]:
215-
str_targets.append(bpy.data.objects[objname].modifiers[i].name)
205+
if self.flags[i] and obj.modifiers[self.modifier_names[i]]:
206+
str_targets.append(self.modifier_names[i])
216207

217-
apply_modifier(target_object=bpy.data.objects[objname], target_modifiers=str_targets)
208+
apply_modifier(target_object=obj, target_modifiers=str_targets)
218209

219-
select_object(obj, True)
210+
obj.select_set(True)
220211
else:
221-
self.modifier_names = tuple(i.name for i in bpy.data.objects[objname].modifiers)
212+
self.modifier_names = tuple(i.name for i in obj.modifiers)
222213
self.flags = tuple(False for i in range(32))
223214
return {'FINISHED'}
224215

0 commit comments

Comments
 (0)