36
36
37
37
######################################################
38
38
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
-
48
39
def clear_shape_keys (Name ):
49
- obj = get_active_object ()
40
+ obj = bpy . context . window . view_layer . objects . active
50
41
if obj .data .shape_keys is None :
51
42
return True
52
43
obj .active_shape_key_index = len (obj .data .shape_keys .key_blocks ) - 1
@@ -78,11 +69,17 @@ def delete_object(Obj):
78
69
79
70
def apply_modifier (target_object = None , target_modifiers = None ):
80
71
if target_object is None :
81
- obj_src = get_active_object ()
72
+ obj_src = bpy . context . window . view_layer . objects . active
82
73
else :
83
74
obj_src = target_object
84
75
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 :
86
83
# if object has no modifier then skip
87
84
return True
88
85
@@ -92,24 +89,18 @@ def apply_modifier(target_object=None, target_modifiers=None):
92
89
93
90
if obj_src .data .shape_keys is None :
94
91
# if object has no shapekeys, just apply modifier
95
- for x in obj_src . modifiers :
92
+ for x in target_modifiers :
96
93
try :
97
- bpy .ops .object .modifier_apply (modifier = x . name )
94
+ bpy .ops .object .modifier_apply (modifier = x )
98
95
except RuntimeError :
99
96
pass
100
97
return True
101
98
102
99
obj_fin = clone_object (obj_src )
103
100
104
- set_active_object ( obj_fin )
101
+ bpy . context . window . view_layer . objects . active = obj_fin
105
102
clear_shape_keys ('Basis' )
106
103
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
-
113
104
for x in target_modifiers :
114
105
try :
115
106
bpy .ops .object .modifier_apply (modifier = x )
@@ -123,7 +114,7 @@ def apply_modifier(target_object=None, target_modifiers=None):
123
114
tmp_name = obj_src .data .shape_keys .key_blocks [i ].name
124
115
obj_tmp = clone_object (obj_src )
125
116
126
- set_active_object ( obj_tmp )
117
+ bpy . context . window . view_layer . objects . active = obj_tmp
127
118
clear_shape_keys (tmp_name )
128
119
129
120
for x in target_modifiers :
@@ -132,8 +123,10 @@ def apply_modifier(target_object=None, target_modifiers=None):
132
123
except RuntimeError :
133
124
pass
134
125
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
137
130
try :
138
131
bpy .ops .object .join_shapes ()
139
132
obj_fin .data .shape_keys .key_blocks [- 1 ].name = tmp_name
@@ -152,20 +145,19 @@ def draw(self, context):
152
145
bpy .context .window_manager .popup_menu (draw , title = "Error" , icon = 'INFO' )
153
146
154
147
return False
155
-
148
+
156
149
tmp_name = obj_src .name
157
150
tmp_data_name = obj_src .data .name
158
151
obj_fin .name = tmp_name + '.tmp'
159
152
160
-
161
153
obj_src .data = obj_fin .data
162
154
obj_src .data .name = tmp_data_name
163
155
164
156
for x in target_modifiers :
165
157
obj_src .modifiers .remove (obj_src .modifiers [x ])
166
-
158
+
167
159
delete_object (obj_fin )
168
- set_active_object ( obj_src )
160
+ bpy . context . window . view_layer . objects . active = obj_src
169
161
170
162
class OBJECT_OT_apply_all_modifiers (bpy .types .Operator ):
171
163
"""Apply All Modifier to Selected Mesh Object"""
@@ -183,7 +175,7 @@ def execute(self, context):
183
175
apply_modifier (target_object = bpy .data .objects [x ])
184
176
185
177
for x in targets :
186
- select_object ( bpy .data .objects [x ], True )
178
+ bpy .data .objects [x ]. select_set ( True )
187
179
188
180
return {'FINISHED' }
189
181
@@ -204,21 +196,20 @@ def poll(cls, context):
204
196
return obj and obj .type == 'MESH'
205
197
206
198
def execute (self , context ):
207
- obj = get_active_object ()
208
- objname = obj .name
199
+ obj = bpy .context .window .view_layer .objects .active
209
200
210
201
if self .modifier_names and len (self .modifier_names ) > 0 :
211
202
bpy .ops .object .select_all (action = 'DESELECT' )
212
203
str_targets = []
213
204
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 ] )
216
207
217
- apply_modifier (target_object = bpy . data . objects [ objname ] , target_modifiers = str_targets )
208
+ apply_modifier (target_object = obj , target_modifiers = str_targets )
218
209
219
- select_object ( obj , True )
210
+ obj . select_set ( True )
220
211
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 )
222
213
self .flags = tuple (False for i in range (32 ))
223
214
return {'FINISHED' }
224
215
0 commit comments