Sun Position: update translation
[blender-addons.git] / add_mesh_geodesic_domes / add_shape_geodesic.py
blob8786b298016d311d9da391944f84baa934ad9ccb
1 # SPDX-FileCopyrightText: 2016-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
6 import mathutils
9 def reset_transform(ob):
10 m = mathutils.Matrix()
11 ob.matrix_local = m
14 def func_add_pose_shape_fast(source, target):
15 result = ""
16 reset_transform(target)
17 # If target object doesn't have Basis shape key, create it.
18 try:
19 num_keys = len(target.data.shape_keys.key_blocks)
20 except:
21 basis = target.shape_key_add()
22 basis.name = "Basis"
23 target.data.update()
24 key_index = target.active_shape_key_index
25 if key_index == 0:
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
35 try:
36 vgroup = target.active_shape_key.vertex_group
37 target.active_shape_key.vertex_group = ''
38 except:
39 print("blub")
40 result = "***ERROR*** blub"
41 pass
42 # copy the local vertex positions to the new shape
43 verts = source.data.vertices
44 try:
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
48 except:
49 message = "***ERROR***, meshes have different number of vertices"
50 result = message
51 for n in target.modifiers:
52 if n.type == 'ARMATURE' and n.show_viewport:
53 # print("got one")
54 n.use_bone_envelopes = False
55 n.use_deform_preserve_volume = False
56 n.use_vertex_groups = True
57 armature = n.object
58 unposeMesh(shape_key_verts, target, armature)
59 break
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
63 try:
64 target.active_shape_key.vertex_group = vgroup
65 except:
66 print("bluba")
67 result = result + "bluba"
68 pass
69 target.show_only_shape_key = False
70 target.data.update()
71 return result
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)"
79 @classmethod
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")
87 return {'FINISHED'}
89 selection = context.selected_objects
90 target = context.active_object
91 if context.active_object == selection[0]:
92 source = selection[1]
93 else:
94 source = selection[0]
95 print(source)
96 print(target)
97 func_add_pose_shape_fast(source, target)
99 return {'FINISHED'}