Merging r59182 through r59257 from trunk into soc-2013-depsgraph_mt
[blender-staging.git] / source / blender / blenkernel / intern / object_deform.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/blenkernel/intern/object_deform.c
22  *  \ingroup bke
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_utildefines.h"
31 #include "BLI_ghash.h"
32
33 #include "BKE_action.h"
34 #include "BKE_object_deform.h"  /* own include */
35 #include "BKE_object.h"
36 #include "BKE_modifier.h"
37
38 #include "DNA_armature_types.h"
39 #include "DNA_modifier_types.h"
40 #include "DNA_object_types.h"
41
42 /* --- functions for getting vgroup aligned maps --- */
43
44 /**
45  * gets the status of "flag" for each bDeformGroup
46  * in ob->defbase and returns an array containing them
47  */
48 bool *BKE_objdef_lock_flags_get(Object *ob, const int defbase_tot)
49 {
50         bool is_locked = false;
51         int i;
52         //int defbase_tot = BLI_countlist(&ob->defbase);
53         bool *lock_flags = MEM_mallocN(defbase_tot * sizeof(bool), "defflags");
54         bDeformGroup *defgroup;
55
56         for (i = 0, defgroup = ob->defbase.first; i < defbase_tot && defgroup; defgroup = defgroup->next, i++) {
57                 lock_flags[i] = ((defgroup->flag & DG_LOCK_WEIGHT) != 0);
58                 is_locked |= lock_flags[i];
59         }
60         if (is_locked) {
61                 return lock_flags;
62         }
63
64         MEM_freeN(lock_flags);
65         return NULL;
66 }
67
68 bool *BKE_objdef_validmap_get(Object *ob, const int defbase_tot)
69 {
70         bDeformGroup *dg;
71         ModifierData *md;
72         bool *vgroup_validmap;
73         GHash *gh;
74         int i, step1 = 1;
75         //int defbase_tot = BLI_countlist(&ob->defbase);
76         VirtualModifierData virtualModifierData;
77
78         if (ob->defbase.first == NULL) {
79                 return NULL;
80         }
81
82         gh = BLI_ghash_str_new("BKE_objdef_validmap_get gh");
83
84         /* add all names to a hash table */
85         for (dg = ob->defbase.first; dg; dg = dg->next) {
86                 BLI_ghash_insert(gh, dg->name, NULL);
87         }
88
89         BLI_assert(BLI_ghash_size(gh) == defbase_tot);
90
91         /* now loop through the armature modifiers and identify deform bones */
92         for (md = ob->modifiers.first; md; md = !md->next && step1 ? (step1 = 0), modifiers_getVirtualModifierList(ob, &virtualModifierData) : md->next) {
93                 if (!(md->mode & (eModifierMode_Realtime | eModifierMode_Virtual)))
94                         continue;
95
96                 if (md->type == eModifierType_Armature) {
97                         ArmatureModifierData *amd = (ArmatureModifierData *) md;
98
99                         if (amd->object && amd->object->pose) {
100                                 bPose *pose = amd->object->pose;
101                                 bPoseChannel *chan;
102
103                                 for (chan = pose->chanbase.first; chan; chan = chan->next) {
104                                         void **val_p;
105                                         if (chan->bone->flag & BONE_NO_DEFORM)
106                                                 continue;
107
108                                         val_p = BLI_ghash_lookup_p(gh, chan->name);
109                                         if (val_p) {
110                                                 *val_p = SET_INT_IN_POINTER(1);
111                                         }
112                                 }
113                         }
114                 }
115         }
116
117         vgroup_validmap = MEM_mallocN(sizeof(*vgroup_validmap) * defbase_tot, "wpaint valid map");
118
119         /* add all names to a hash table */
120         for (dg = ob->defbase.first, i = 0; dg; dg = dg->next, i++) {
121                 vgroup_validmap[i] = (BLI_ghash_lookup(gh, dg->name) != NULL);
122         }
123
124         BLI_assert(i == BLI_ghash_size(gh));
125
126         BLI_ghash_free(gh, NULL, NULL);
127
128         return vgroup_validmap;
129 }
130
131 /* Returns total selected vgroups,
132  * wpi.defbase_sel is assumed malloc'd, all values are set */
133 bool *BKE_objdef_selected_get(Object *ob, int defbase_tot, int *r_dg_flags_sel_tot)
134 {
135         bool *dg_selection = MEM_mallocN(defbase_tot * sizeof(bool), __func__);
136         bDeformGroup *defgroup;
137         unsigned int i;
138         Object *armob = BKE_object_pose_armature_get(ob);
139         (*r_dg_flags_sel_tot) = 0;
140
141         if (armob) {
142                 bPose *pose = armob->pose;
143                 for (i = 0, defgroup = ob->defbase.first; i < defbase_tot && defgroup; defgroup = defgroup->next, i++) {
144                         bPoseChannel *pchan = BKE_pose_channel_find_name(pose, defgroup->name);
145                         if (pchan && (pchan->bone->flag & BONE_SELECTED)) {
146                                 dg_selection[i] = TRUE;
147                                 (*r_dg_flags_sel_tot) += 1;
148                         }
149                         else {
150                                 dg_selection[i] = FALSE;
151                         }
152                 }
153         }
154         else {
155                 memset(dg_selection, FALSE, sizeof(*dg_selection) * defbase_tot);
156         }
157
158         return dg_selection;
159 }