This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
forked from przemir/ApplyModifierForObjectWithShapeKeys
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApplyModifierForObjectWithShapeKeys.py
150 lines (124 loc) · 6.28 KB
/
ApplyModifierForObjectWithShapeKeys.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
# ------------------------------------------------------------------------------
# The MIT License (MIT)
#
# Copyright (c) 2015 Przemysław Bągard
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ------------------------------------------------------------------------------
# Date: 24 Jun 2019
# Blender script
# Description: Apply modifier and remove from the stack for object with shape keys
# (Pushing 'Apply' button in 'Object modifiers' tab result in an error 'Modifier cannot be applied to a mesh with shape keys').
import math
import bpy
from bpy.props import *
bl_info = {
"name": "Apply modifier for object with shape keys",
"author": "Przemysław Bągard",
"blender": (2, 80, 0),
"version": (0, 2, 5),
"location": "Context menu",
"description": "Apply modifier and remove from the stack for object with shape keys (Pushing 'Apply' button in 'Object modifiers' tab result in an error 'Modifier cannot be applied to a mesh with shape keys').",
"category": "Object Tools > Multi Shape Keys"
}
# Algorithm:
# - Duplicate active object as many times as the number of shape keys
# - For each copy remove all shape keys except one
# - Removing last shape does not change geometry data of object
# - Apply modifier for each copy
# - Join objects as shapes and restore shape keys names
# - Delete all duplicated object except one
# - Delete old object
# - Restore name of object and object data
def applyModifierForObjectWithShapeKeys(context, modifierName):
list_names = []
modifier_list = []
list_shapes = []
if context.object.data.shape_keys:
list_shapes = [o for o in context.object.data.shape_keys.key_blocks]
if(list_shapes == []):
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modifierName)
return context.view_layer.objects.active
modifier_list.append(context.view_layer.objects.active)
for i in range(1, len(list_shapes)):
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked": False, "mode": 'TRANSLATION'}, TRANSFORM_OT_translate={
"value": (0, 0, 0), "release_confirm": False})
modifier_list.append(context.view_layer.objects.active)
for i, o in enumerate(modifier_list):
context.view_layer.objects.active = o
list_names.append(o.data.shape_keys.key_blocks[i].name)
for j in range(i+1, len(modifier_list))[::-1]:
context.object.active_shape_key_index = j
bpy.ops.object.shape_key_remove()
for j in range(0, i):
context.object.active_shape_key_index = 0
bpy.ops.object.shape_key_remove()
# last deleted shape doesn't change object shape
context.object.active_shape_key_index = 0
bpy.ops.object.shape_key_remove()
# time to apply modifiers
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modifierName)
# TODO: Safeguards - If Edge Modifier, make sure angle is turned off, otherwise warn user First
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = modifier_list[0]
modifier_list[0].select_set(True)
bpy.ops.object.shape_key_add(from_mix=False)
context.view_layer.objects.active.data.shape_keys.key_blocks[0].name = list_names[0]
for i in range(1, len(modifier_list)):
modifier_list[i].select_set(True)
bpy.ops.object.join_shapes()
modifier_list[i].select_set(False)
context.view_layer.objects.active.data.shape_keys.key_blocks[i].name = list_names[i]
bpy.ops.object.select_all(action='DESELECT')
for o in modifier_list[1:]:
o.select_set(True)
bpy.ops.object.delete(use_global=False)
context.view_layer.objects.active = modifier_list[0]
context.view_layer.objects.active.select_set(True)
return context.view_layer.objects.active
class ApplyModifierForObjectWithShapeKeysOperator(bpy.types.Operator):
bl_idname = "object.apply_modifier_for_object_with_shape_keys"
bl_label = "Apply modifier for object with shape keys"
def item_list(self, context):
return [(modifier.name, modifier.name, modifier.name) for modifier in bpy.context.active_object.modifiers]
my_enum: EnumProperty(name="Modifier name",
items=item_list)
def execute(self, context):
ob = context.view_layer.objects.active
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = ob
context.view_layer.objects.active.select_set(True)
applyModifierForObjectWithShapeKeys(context, self.my_enum)
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
class MESH_PT_ApplyModifierDialogPanel(bpy.types.Panel):
bl_label = "Multi Shape Keys"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
self.layout.operator(
"object.apply_modifier_for_object_with_shape_keys")
def menu_func(self, context):
self.layout.operator("object.apply_modifier_for_object_with_shape_keys",
text="Apply modifier for object with shape keys")
register, unregister = bpy.utils.register_classes_factory(
(ApplyModifierForObjectWithShapeKeysOperator, MESH_PT_ApplyModifierDialogPanel))
if __name__ == "__main__":
register()