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, shapekey support
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/object/object_shapekey.c
40 #include "MEM_guardedalloc.h"
42 #include "BLI_blenlib.h"
44 #include "BLI_utildefines.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_key_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_mesh_types.h"
50 #include "DNA_meshdata_types.h"
51 #include "DNA_object_types.h"
53 #include "BKE_context.h"
54 #include "BKE_depsgraph.h"
56 #include "BKE_library.h"
58 #include "BKE_object.h"
59 #include "BKE_lattice.h"
60 #include "BKE_curve.h"
62 #include "BLI_sys_types.h" // for intptr_t support
64 #include "ED_object.h"
67 #include "RNA_access.h"
68 #include "RNA_define.h"
73 #include "object_intern.h"
75 /*********************** add shape key ***********************/
77 static void ED_object_shape_key_add(bContext *C, Object *ob, const bool from_mix)
80 if ((kb = BKE_object_shapekey_insert(ob, NULL, from_mix))) {
81 Key *key = BKE_key_from_object(ob);
82 /* for absolute shape keys, new keys may not be added last */
83 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
85 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
89 /*********************** remove shape key ***********************/
91 static bool object_shapekey_remove(Main *bmain, Object *ob)
94 Key *key = BKE_key_from_object(ob);
100 kb = BLI_findlink(&key->block, ob->shapenr - 1);
102 return BKE_object_shapekey_remove(bmain, ob, kb);
108 static bool object_shape_key_mirror(bContext *C, Object *ob,
109 int *r_totmirr, int *r_totfail, bool use_topology)
113 int totmirr = 0, totfail = 0;
115 *r_totmirr = *r_totfail = 0;
117 key = BKE_key_from_object(ob);
121 kb = BLI_findlink(&key->block, ob->shapenr - 1);
124 char *tag_elem = MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
127 if (ob->type == OB_MESH) {
134 ED_mesh_mirror_spatial_table(ob, NULL, NULL, 's');
136 for (i1 = 0, mv = me->mvert; i1 < me->totvert; i1++, mv++) {
137 i2 = mesh_get_x_mirror_vert(ob, i1, use_topology);
139 fp1 = ((float *)kb->data) + i1 * 3;
145 if (tag_elem[i1] == 0 && tag_elem[i2] == 0) {
146 fp1 = ((float *)kb->data) + i1 * 3;
147 fp2 = ((float *)kb->data) + i2 * 3;
149 copy_v3_v3(tvec, fp1);
150 copy_v3_v3(fp1, fp2);
151 copy_v3_v3(fp2, tvec);
158 tag_elem[i1] = tag_elem[i2] = 1;
165 ED_mesh_mirror_spatial_table(ob, NULL, NULL, 'e');
167 else if (ob->type == OB_LATTICE) {
168 Lattice *lt = ob->data;
172 /* half but found up odd value */
173 const int pntsu_half = (lt->pntsu / 2) + (lt->pntsu % 2);
175 /* currently editmode isn't supported by mesh so
176 * ignore here for now too */
178 /* if (lt->editlatt) lt = lt->editlatt->latt; */
180 for (w = 0; w < lt->pntsw; w++) {
181 for (v = 0; v < lt->pntsv; v++) {
182 for (u = 0; u < pntsu_half; u++) {
183 int u_inv = (lt->pntsu - 1) - u;
186 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
187 fp1 = ((float *)kb->data) + i1 * 3;
192 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
193 i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
195 fp1 = ((float *)kb->data) + i1 * 3;
196 fp2 = ((float *)kb->data) + i2 * 3;
198 copy_v3_v3(tvec, fp1);
199 copy_v3_v3(fp1, fp2);
200 copy_v3_v3(fp2, tvec);
213 *r_totmirr = totmirr;
214 *r_totfail = totfail;
216 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
217 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
222 /********************** shape key operators *********************/
224 static int shape_key_mode_poll(bContext *C)
226 Object *ob = ED_object_context(C);
227 ID *data = (ob) ? ob->data : NULL;
228 return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT);
231 static int shape_key_mode_exists_poll(bContext *C)
233 Object *ob = ED_object_context(C);
234 ID *data = (ob) ? ob->data : NULL;
236 /* same as shape_key_mode_poll */
237 return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT) &&
238 /* check a keyblock exists */
239 (BKE_keyblock_from_object(ob) != NULL);
242 static int shape_key_move_poll(bContext *C)
244 /* Same as shape_key_mode_exists_poll above, but ensure we have at least two shapes! */
245 Object *ob = ED_object_context(C);
246 ID *data = (ob) ? ob->data : NULL;
247 Key *key = BKE_key_from_object(ob);
249 return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT && key && key->totkey > 1);
252 static int shape_key_poll(bContext *C)
254 Object *ob = ED_object_context(C);
255 ID *data = (ob) ? ob->data : NULL;
256 return (ob && !ob->id.lib && data && !data->lib);
259 static int shape_key_add_exec(bContext *C, wmOperator *op)
261 Object *ob = ED_object_context(C);
262 const bool from_mix = RNA_boolean_get(op->ptr, "from_mix");
264 ED_object_shape_key_add(C, ob, from_mix);
266 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
267 DAG_relations_tag_update(CTX_data_main(C));
269 return OPERATOR_FINISHED;
272 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
275 ot->name = "Add Shape Key";
276 ot->idname = "OBJECT_OT_shape_key_add";
277 ot->description = "Add shape key to the object";
280 ot->poll = shape_key_mode_poll;
281 ot->exec = shape_key_add_exec;
284 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
287 RNA_def_boolean(ot->srna, "from_mix", true, "From Mix", "Create the new shape key from the existing mix of keys");
290 static int shape_key_remove_exec(bContext *C, wmOperator *op)
292 Main *bmain = CTX_data_main(C);
293 Object *ob = ED_object_context(C);
294 bool changed = false;
296 if (RNA_boolean_get(op->ptr, "all")) {
297 changed = BKE_object_shapekey_free(bmain, ob);
300 changed = object_shapekey_remove(bmain, ob);
304 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
305 DAG_relations_tag_update(CTX_data_main(C));
306 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
308 return OPERATOR_FINISHED;
311 return OPERATOR_CANCELLED;
315 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
318 ot->name = "Remove Shape Key";
319 ot->idname = "OBJECT_OT_shape_key_remove";
320 ot->description = "Remove shape key from the object";
323 ot->poll = shape_key_mode_exists_poll;
324 ot->exec = shape_key_remove_exec;
327 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
330 RNA_def_boolean(ot->srna, "all", 0, "All", "Remove all shape keys");
333 static int shape_key_clear_exec(bContext *C, wmOperator *UNUSED(op))
335 Object *ob = ED_object_context(C);
336 Key *key = BKE_key_from_object(ob);
337 KeyBlock *kb = BKE_keyblock_from_object(ob);
340 return OPERATOR_CANCELLED;
342 for (kb = key->block.first; kb; kb = kb->next)
345 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
346 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
348 return OPERATOR_FINISHED;
351 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
354 ot->name = "Clear Shape Keys";
355 ot->description = "Clear weights for all shape keys";
356 ot->idname = "OBJECT_OT_shape_key_clear";
359 ot->poll = shape_key_poll;
360 ot->exec = shape_key_clear_exec;
363 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
366 /* starting point and step size could be optional */
367 static int shape_key_retime_exec(bContext *C, wmOperator *UNUSED(op))
369 Object *ob = ED_object_context(C);
370 Key *key = BKE_key_from_object(ob);
371 KeyBlock *kb = BKE_keyblock_from_object(ob);
375 return OPERATOR_CANCELLED;
377 for (kb = key->block.first; kb; kb = kb->next)
378 kb->pos = (cfra += 0.1f);
380 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
381 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
383 return OPERATOR_FINISHED;
386 void OBJECT_OT_shape_key_retime(wmOperatorType *ot)
389 ot->name = "Re-Time Shape Keys";
390 ot->description = "Resets the timing for absolute shape keys";
391 ot->idname = "OBJECT_OT_shape_key_retime";
394 ot->poll = shape_key_poll;
395 ot->exec = shape_key_retime_exec;
398 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
401 static int shape_key_mirror_exec(bContext *C, wmOperator *op)
403 Object *ob = ED_object_context(C);
404 int totmirr = 0, totfail = 0;
405 bool use_topology = RNA_boolean_get(op->ptr, "use_topology");
407 if (!object_shape_key_mirror(C, ob, &totmirr, &totfail, use_topology))
408 return OPERATOR_CANCELLED;
410 ED_mesh_report_mirror(op, totmirr, totfail);
412 return OPERATOR_FINISHED;
415 void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
418 ot->name = "Mirror Shape Key";
419 ot->idname = "OBJECT_OT_shape_key_mirror";
420 ot->description = "Mirror the current shape key along the local X axis";
423 ot->poll = shape_key_mode_poll;
424 ot->exec = shape_key_mirror_exec;
427 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
430 RNA_def_boolean(ot->srna, "use_topology", 0, "Topology Mirror",
431 "Use topology based mirroring (for when both sides of mesh have matching, unique topology)");
442 static int shape_key_move_exec(bContext *C, wmOperator *op)
444 Object *ob = ED_object_context(C);
446 Key *key = BKE_key_from_object(ob);
447 const int type = RNA_enum_get(op->ptr, "type");
448 const int totkey = key->totkey;
449 const int act_index = ob->shapenr - 1;
454 /* Replace the ref key only if we're at the top already (only for relative keys) */
455 new_index = (ELEM(act_index, 0, 1) || key->type == KEY_NORMAL) ? 0 : 1;
458 new_index = totkey - 1;
463 new_index = (totkey + act_index + type) % totkey;
467 if (!BKE_keyblock_move(ob, act_index, new_index)) {
468 return OPERATOR_CANCELLED;
471 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
472 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
474 return OPERATOR_FINISHED;
477 void OBJECT_OT_shape_key_move(wmOperatorType *ot)
479 static EnumPropertyItem slot_move[] = {
480 {KB_MOVE_TOP, "TOP", 0, "Top", "Top of the list"},
481 {KB_MOVE_UP, "UP", 0, "Up", ""},
482 {KB_MOVE_DOWN, "DOWN", 0, "Down", ""},
483 {KB_MOVE_BOTTOM, "BOTTOM", 0, "Bottom", "Bottom of the list"},
484 { 0, NULL, 0, NULL, NULL }
488 ot->name = "Move Shape Key";
489 ot->idname = "OBJECT_OT_shape_key_move";
490 ot->description = "Move the active shape key up/down in the list";
493 ot->poll = shape_key_move_poll;
494 ot->exec = shape_key_move_exec;
497 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
499 RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");