Pose library: fix asset creation operator poll when no object active
[blender-addons.git] / add_mesh_geodesic_domes / add_shape_geodesic.py
blobe647ba35980bdd9344f7c0fd94b076396e761f6e
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 import mathutils
7 def reset_transform(ob):
8 m = mathutils.Matrix()
9 ob.matrix_local = m
12 def func_add_pose_shape_fast(source, target):
13 result = ""
14 reset_transform(target)
15 # If target object doesn't have Basis shape key, create it.
16 try:
17 num_keys = len(target.data.shape_keys.key_blocks)
18 except:
19 basis = target.shape_key_add()
20 basis.name = "Basis"
21 target.data.update()
22 key_index = target.active_shape_key_index
23 if key_index == 0:
24 # Insert new shape key
25 new_shapekey = target.shape_key_add()
26 new_shapekey.name = "Shape_" + source.name
27 new_shapekey_name = new_shapekey.name
28 key_index = len(target.data.shape_keys.key_blocks) - 1
29 target.active_shape_key_index = key_index
30 # else, the active shape will be used (updated)
31 target.show_only_shape_key = True
32 shape_key_verts = target.data.shape_keys.key_blocks[key_index].data
33 try:
34 vgroup = target.active_shape_key.vertex_group
35 target.active_shape_key.vertex_group = ''
36 except:
37 print("blub")
38 result = "***ERROR*** blub"
39 pass
40 # copy the local vertex positions to the new shape
41 verts = source.data.vertices
42 try:
43 for n in range(len(verts)):
44 shape_key_verts[n].co = verts[n].co
45 # go to all armature modifies and unpose the shape
46 except:
47 message = "***ERROR***, meshes have different number of vertices"
48 result = message
49 for n in target.modifiers:
50 if n.type == 'ARMATURE' and n.show_viewport:
51 # print("got one")
52 n.use_bone_envelopes = False
53 n.use_deform_preserve_volume = False
54 n.use_vertex_groups = True
55 armature = n.object
56 unposeMesh(shape_key_verts, target, armature)
57 break
59 # set the new shape key value to 1.0, so we see the result instantly
60 target.data.shape_keys.key_blocks[ target.active_shape_key_index].value = 1.0
61 try:
62 target.active_shape_key.vertex_group = vgroup
63 except:
64 print("bluba")
65 result = result + "bluba"
66 pass
67 target.show_only_shape_key = False
68 target.data.update()
69 return result
72 class add_pose_shape_fast(bpy.types.Operator):
73 bl_idname = "object.add_pose_shape_fast"
74 bl_label = "Add object as corrective shape faster"
75 bl_description = "Adds 1st object as shape to 2nd object as pose shape (only 1 armature)"
77 @classmethod
78 def poll(cls, context):
79 return context.active_object is not None
81 def execute(self, context):
83 if len(context.selected_objects) > 2:
84 print("Select source and target objects please")
85 return {'FINISHED'}
87 selection = context.selected_objects
88 target = context.active_object
89 if context.active_object == selection[0]:
90 source = selection[1]
91 else:
92 source = selection[0]
93 print(source)
94 print(target)
95 func_add_pose_shape_fast(source, target)
97 return {'FINISHED'}