2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2002-2009 full recode.
23 * ***** END GPL LICENSE BLOCK *****
25 * API's for creating vertex groups from bones
26 * - Interfaces with heat weighting in meshlaplacian
29 /** \file blender/editors/armature/armature_skinning.c
33 #include "DNA_mesh_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
43 #include "BKE_action.h"
44 #include "BKE_armature.h"
45 #include "BKE_deform.h"
46 #include "BKE_report.h"
47 #include "BKE_subsurf.h"
48 #include "BKE_modifier.h"
50 #include "ED_armature.h"
54 #include "armature_intern.h"
55 #include "meshlaplacian.h"
61 /* ********************************** Bone Skinning *********************************************** */
63 static int bone_skinnable_cb(Object *ob, Bone *bone, void *datap)
65 /* Bones that are deforming
66 * are regarded to be "skinnable" and are eligible for
69 * This function performs 2 functions:
71 * a) It returns 1 if the bone is skinnable.
72 * If we loop over all bones with this
73 * function, we can count the number of
75 * b) If the pointer data is non null,
76 * it is treated like a handle to a
77 * bone pointer -- the bone pointer
78 * is set to point at this bone, and
79 * the pointer the handle points to
80 * is incremented to point to the
81 * next member of an array of pointers
82 * to bones. This way we can loop using
83 * this function to construct an array of
84 * pointers to bones that point to all
89 struct { Object *armob; void *list; int heat; } *data = datap;
91 if (!(ob->mode & OB_MODE_WEIGHT_PAINT) || !(bone->flag & BONE_HIDDEN_P)) {
92 if (!(bone->flag & BONE_NO_DEFORM)) {
93 if (data->heat && data->armob->pose && BKE_pose_channel_find_name(data->armob->pose, bone->name))
94 segments = bone->segments;
98 if (data->list != NULL) {
99 hbone = (Bone ***) &data->list;
101 for (a = 0; a < segments; a++) {
112 static int vgroup_add_unique_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr))
114 /* This group creates a vertex group to ob that has the
115 * same name as bone (provided the bone is skinnable).
116 * If such a vertex group already exist the routine exits.
118 if (!(bone->flag & BONE_NO_DEFORM)) {
119 if (!defgroup_find_name(ob, bone->name)) {
120 ED_vgroup_add_name(ob, bone->name);
127 static int dgroup_skinnable_cb(Object *ob, Bone *bone, void *datap)
129 /* Bones that are deforming
130 * are regarded to be "skinnable" and are eligible for
133 * This function performs 2 functions:
135 * a) If the bone is skinnable, it creates
136 * a vertex group for ob that has
137 * the name of the skinnable bone
138 * (if one doesn't exist already).
139 * b) If the pointer data is non null,
140 * it is treated like a handle to a
141 * bDeformGroup pointer -- the
142 * bDeformGroup pointer is set to point
143 * to the deform group with the bone's
144 * name, and the pointer the handle
145 * points to is incremented to point to the
146 * next member of an array of pointers
147 * to bDeformGroups. This way we can loop using
148 * this function to construct an array of
149 * pointers to bDeformGroups, all with names
150 * of skinnable bones.
152 bDeformGroup ***hgroup, *defgroup = NULL;
154 struct { Object *armob; void *list; int heat; } *data = datap;
155 int wpmode = (ob->mode & OB_MODE_WEIGHT_PAINT);
156 bArmature *arm = data->armob->data;
158 if (!wpmode || !(bone->flag & BONE_HIDDEN_P)) {
159 if (!(bone->flag & BONE_NO_DEFORM)) {
160 if (data->heat && data->armob->pose && BKE_pose_channel_find_name(data->armob->pose, bone->name))
161 segments = bone->segments;
165 if (!wpmode || ((arm->layer & bone->layer) && (bone->flag & BONE_SELECTED)))
166 if (!(defgroup = defgroup_find_name(ob, bone->name)))
167 defgroup = ED_vgroup_add_name(ob, bone->name);
169 if (data->list != NULL) {
170 hgroup = (bDeformGroup ***) &data->list;
172 for (a = 0; a < segments; a++) {
183 static void add_vgroups__mapFunc(void *userData, int index, const float co[3],
184 const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
186 /* DerivedMesh mapFunc for getting final coords in weight paint mode */
188 float (*verts)[3] = userData;
189 copy_v3_v3(verts[index], co);
192 static void envelope_bone_weighting(Object *ob, Mesh *mesh, float (*verts)[3], int numbones, Bone **bonelist,
193 bDeformGroup **dgrouplist, bDeformGroup **dgroupflip,
194 float (*root)[3], float (*tip)[3], int *selected, float scale)
196 /* Create vertex group weights from envelopes */
199 bDeformGroup *dgroup;
202 bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
204 /* for each vertex in the mesh */
205 for (i = 0; i < mesh->totvert; i++) {
206 iflip = (dgroupflip) ? mesh_get_x_mirror_vert(ob, i, use_topology) : 0;
208 /* for each skinnable bone */
209 for (j = 0; j < numbones; ++j) {
214 dgroup = dgrouplist[j];
216 /* store the distance-factor from the vertex to the bone */
217 distance = distfactor_to_bone(verts[i], root[j], tip[j],
218 bone->rad_head * scale, bone->rad_tail * scale, bone->dist * scale);
220 /* add the vert to the deform group if (weight != 0.0) */
221 if (distance != 0.0f)
222 ED_vgroup_vert_add(ob, dgroup, i, distance, WEIGHT_REPLACE);
224 ED_vgroup_vert_remove(ob, dgroup, i);
226 /* do same for mirror */
227 if (dgroupflip && dgroupflip[j] && iflip >= 0) {
228 if (distance != 0.0f)
229 ED_vgroup_vert_add(ob, dgroupflip[j], iflip, distance,
232 ED_vgroup_vert_remove(ob, dgroupflip[j], iflip);
238 static void add_verts_to_dgroups(ReportList *reports, Scene *scene, Object *ob, Object *par, int heat, bool mirror)
240 /* This functions implements the automatic computation of vertex group
241 * weights, either through envelopes or using a heat equilibrium.
243 * This function can be called both when parenting a mesh to an armature,
244 * or in weightpaint + posemode. In the latter case selection is taken
245 * into account and vertex weights can be mirrored.
247 * The mesh vertex positions used are either the final deformed coords
248 * from the derivedmesh in weightpaint mode, the final subsurf coords
249 * when parenting, or simply the original mesh coords.
252 bArmature *arm = par->data;
253 Bone **bonelist, *bone;
254 bDeformGroup **dgrouplist, **dgroupflip;
255 bDeformGroup *dgroup;
258 Mat4 bbone_array[MAX_BBONE_SUBDIV], *bbone = NULL;
259 float (*root)[3], (*tip)[3], (*verts)[3];
261 int numbones, vertsfilled = 0, i, j, segments = 0;
262 int wpmode = (ob->mode & OB_MODE_WEIGHT_PAINT);
263 struct { Object *armob; void *list; int heat; } looper_data;
265 looper_data.armob = par;
266 looper_data.heat = heat;
267 looper_data.list = NULL;
269 /* count the number of skinnable bones */
270 numbones = bone_looper(ob, arm->bonebase.first, &looper_data, bone_skinnable_cb);
275 if (ED_vgroup_data_create(ob->data) == FALSE)
278 /* create an array of pointer to bones that are skinnable
279 * and fill it with all of the skinnable bones */
280 bonelist = MEM_callocN(numbones * sizeof(Bone *), "bonelist");
281 looper_data.list = bonelist;
282 bone_looper(ob, arm->bonebase.first, &looper_data, bone_skinnable_cb);
284 /* create an array of pointers to the deform groups that
285 * correspond to the skinnable bones (creating them
287 dgrouplist = MEM_callocN(numbones * sizeof(bDeformGroup *), "dgrouplist");
288 dgroupflip = MEM_callocN(numbones * sizeof(bDeformGroup *), "dgroupflip");
290 looper_data.list = dgrouplist;
291 bone_looper(ob, arm->bonebase.first, &looper_data, dgroup_skinnable_cb);
293 /* create an array of root and tip positions transformed into
295 root = MEM_callocN(numbones * sizeof(float) * 3, "root");
296 tip = MEM_callocN(numbones * sizeof(float) * 3, "tip");
297 selected = MEM_callocN(numbones * sizeof(int), "selected");
299 for (j = 0; j < numbones; ++j) {
301 dgroup = dgrouplist[j];
309 if ((par->pose) && (pchan = BKE_pose_channel_find_name(par->pose, bone->name))) {
310 if (bone->segments > 1) {
311 segments = bone->segments;
312 b_bone_spline_setup(pchan, 1, bbone_array);
321 /* compute root and tip */
323 mul_v3_m4v3(root[j], bone->arm_mat, bbone[segments].mat[3]);
324 if ((segments + 1) < bone->segments) {
325 mul_v3_m4v3(tip[j], bone->arm_mat, bbone[segments + 1].mat[3]);
328 copy_v3_v3(tip[j], bone->arm_tail);
332 copy_v3_v3(root[j], bone->arm_head);
333 copy_v3_v3(tip[j], bone->arm_tail);
336 mul_m4_v3(par->obmat, root[j]);
337 mul_m4_v3(par->obmat, tip[j]);
341 if ((arm->layer & bone->layer) && (bone->flag & BONE_SELECTED))
347 /* find flipped group */
348 if (dgroup && mirror) {
349 char name_flip[MAXBONENAME];
351 BKE_deform_flip_side_name(name_flip, dgroup->name, false);
352 dgroupflip[j] = defgroup_find_name(ob, name_flip);
357 mesh = (Mesh *)ob->data;
358 verts = MEM_callocN(mesh->totvert * sizeof(*verts), "closestboneverts");
361 /* if in weight paint mode, use final verts from derivedmesh */
362 DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
364 if (dm->foreachMappedVert) {
365 dm->foreachMappedVert(dm, add_vgroups__mapFunc, (void *)verts, DM_FOREACH_NOP);
371 else if (modifiers_findByType(ob, eModifierType_Subsurf)) {
372 /* is subsurf on? Lets use the verts on the limit surface then.
373 * = same amount of vertices as mesh, but vertices moved to the
374 * subsurfed position, like for 'optimal'. */
375 subsurf_calculate_limit_positions(mesh, verts);
379 /* transform verts to global space */
380 for (i = 0; i < mesh->totvert; i++) {
382 copy_v3_v3(verts[i], mesh->mvert[i].co);
383 mul_m4_v3(ob->obmat, verts[i]);
386 /* compute the weights based on gathered vertices and bones */
388 const char *error = NULL;
391 heat_bone_weighting(ob, mesh, verts, numbones, dgrouplist, dgroupflip,
392 root, tip, selected, &error);
394 error = "Built without OpenNL";
397 BKE_report(reports, RPT_WARNING, error);
401 envelope_bone_weighting(ob, mesh, verts, numbones, bonelist, dgrouplist,
402 dgroupflip, root, tip, selected, mat4_to_scale(par->obmat));
405 /* only generated in some cases but can call anyway */
406 ED_mesh_mirror_spatial_table(ob, NULL, NULL, 'e');
408 /* free the memory allocated */
410 MEM_freeN(dgrouplist);
411 MEM_freeN(dgroupflip);
418 void create_vgroups_from_armature(ReportList *reports, Scene *scene, Object *ob, Object *par, int mode, bool mirror)
420 /* Lets try to create some vertex groups
421 * based on the bones of the parent armature.
423 bArmature *arm = par->data;
425 if (mode == ARM_GROUPS_NAME) {
426 const int defbase_tot = BLI_countlist(&ob->defbase);
428 /* Traverse the bone list, trying to create empty vertex
429 * groups corresponding to the bone.
431 defbase_add = bone_looper(ob, arm->bonebase.first, NULL, vgroup_add_unique_bone_cb);
434 /* its possible there are DWeight's outside the range of the current
435 * objects deform groups, in this case the new groups wont be empty [#33889] */
436 ED_vgroup_data_clamp_range(ob->data, defbase_tot);
439 else if (mode == ARM_GROUPS_ENVELOPE || mode == ARM_GROUPS_AUTO) {
440 /* Traverse the bone list, trying to create vertex groups
441 * that are populated with the vertices for which the
444 add_verts_to_dgroups(reports, scene, ob, par, (mode == ARM_GROUPS_AUTO), mirror);