~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/addons_contrib/geodesic_domes/add_shape_geodesic.py

  • Committer: Reinhard Tartler
  • Date: 2014-05-31 01:50:05 UTC
  • mfrom: (14.2.27 sid)
  • Revision ID: siretart@tauware.de-20140531015005-ml6druahuj82nsav
mergeĀ fromĀ debian

Show diffs side-by-side

added added

removed removed

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