1 # SPDX-FileCopyrightText: 2016-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
9 def reset_transform(ob
):
10 m
= mathutils
.Matrix()
14 def func_add_pose_shape_fast(source
, target
):
16 reset_transform(target
)
17 # If target object doesn't have Basis shape key, create it.
19 num_keys
= len(target
.data
.shape_keys
.key_blocks
)
21 basis
= target
.shape_key_add()
24 key_index
= target
.active_shape_key_index
26 # Insert new shape key
27 new_shapekey
= target
.shape_key_add()
28 new_shapekey
.name
= "Shape_" + source
.name
29 new_shapekey_name
= new_shapekey
.name
30 key_index
= len(target
.data
.shape_keys
.key_blocks
) - 1
31 target
.active_shape_key_index
= key_index
32 # else, the active shape will be used (updated)
33 target
.show_only_shape_key
= True
34 shape_key_verts
= target
.data
.shape_keys
.key_blocks
[key_index
].data
36 vgroup
= target
.active_shape_key
.vertex_group
37 target
.active_shape_key
.vertex_group
= ''
40 result
= "***ERROR*** blub"
42 # copy the local vertex positions to the new shape
43 verts
= source
.data
.vertices
45 for n
in range(len(verts
)):
46 shape_key_verts
[n
].co
= verts
[n
].co
47 # go to all armature modifies and unpose the shape
49 message
= "***ERROR***, meshes have different number of vertices"
51 for n
in target
.modifiers
:
52 if n
.type == 'ARMATURE' and n
.show_viewport
:
54 n
.use_bone_envelopes
= False
55 n
.use_deform_preserve_volume
= False
56 n
.use_vertex_groups
= True
58 unposeMesh(shape_key_verts
, target
, armature
)
61 # set the new shape key value to 1.0, so we see the result instantly
62 target
.data
.shape_keys
.key_blocks
[ target
.active_shape_key_index
].value
= 1.0
64 target
.active_shape_key
.vertex_group
= vgroup
67 result
= result
+ "bluba"
69 target
.show_only_shape_key
= False
74 class add_pose_shape_fast(bpy
.types
.Operator
):
75 bl_idname
= "object.add_pose_shape_fast"
76 bl_label
= "Add object as corrective shape faster"
77 bl_description
= "Adds 1st object as shape to 2nd object as pose shape (only 1 armature)"
80 def poll(cls
, context
):
81 return context
.active_object
is not None
83 def execute(self
, context
):
85 if len(context
.selected_objects
) > 2:
86 print("Select source and target objects please")
89 selection
= context
.selected_objects
90 target
= context
.active_object
91 if context
.active_object
== selection
[0]:
97 func_add_pose_shape_fast(source
, target
)