4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, shapekey support
25 * ***** END GPL LICENSE BLOCK *****
37 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_arithb.h"
42 #include "DNA_action_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_ipo_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_lattice_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view2d_types.h"
56 #include "BKE_action.h"
58 #include "BKE_context.h"
59 #include "BKE_curve.h"
60 #include "BKE_depsgraph.h"
61 #include "BKE_global.h"
64 #include "BKE_library.h"
67 #include "BKE_object.h"
68 #include "BKE_utildefines.h"
70 #include "BLO_sys_types.h" // for intptr_t support
72 #include "ED_object.h"
75 #include "RNA_access.h"
76 #include "RNA_define.h"
81 #include "object_intern.h"
83 /************************* Mesh ************************/
85 void mesh_to_key(Mesh *me, KeyBlock *kb)
91 if(me->totvert==0) return;
93 if(kb->data) MEM_freeN(kb->data);
95 kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
96 kb->totelem= me->totvert;
100 for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
101 VECCOPY(fp, mvert->co);
106 void key_to_mesh(KeyBlock *kb, Mesh *me)
115 tot= MIN2(kb->totelem, me->totvert);
117 for(a=0; a<tot; a++, fp+=3, mvert++) {
118 VECCOPY(mvert->co, fp);
122 static KeyBlock *add_keyblock(Scene *scene, Key *key)
129 if(kb) curpos= kb->pos;
131 kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
132 BLI_addtail(&key->block, kb);
133 kb->type= KEY_CARDINAL;
135 tot= BLI_countlist(&key->block);
136 if(tot==1) strcpy(kb->name, "Basis");
137 else sprintf(kb->name, "Key %d", tot-1);
139 // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
143 if(key->totkey==1) key->refkey= kb;
148 // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
149 if(key->type == KEY_RELATIVE)
152 #if 0 // XXX old animation system
153 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
154 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
160 #endif // XXX old animation system
165 static void insert_meshkey(Scene *scene, Object *ob)
173 key= me->key= add_key((ID *)me);
174 key->type= KEY_RELATIVE;
178 kb= add_keyblock(scene, key);
181 /* create from mesh */
185 /* copy from current values */
186 kb->data= do_ob_key(scene, ob);
187 kb->totelem= me->totvert;
191 /************************* Lattice ************************/
193 void latt_to_key(Lattice *lt, KeyBlock *kb)
199 tot= lt->pntsu*lt->pntsv*lt->pntsw;
202 if(kb->data) MEM_freeN(kb->data);
204 kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
209 for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
210 VECCOPY(fp, bp->vec);
214 void key_to_latt(KeyBlock *kb, Lattice *lt)
223 tot= lt->pntsu*lt->pntsv*lt->pntsw;
224 tot= MIN2(kb->totelem, tot);
226 for(a=0; a<tot; a++, fp+=3, bp++) {
227 VECCOPY(bp->vec, fp);
231 static void insert_lattkey(Scene *scene, Object *ob)
233 Lattice *lt= ob->data;
239 key= lt->key= add_key( (ID *)lt);
240 key->type= KEY_RELATIVE;
243 kb= add_keyblock(scene, key);
246 /* create from lattice */
250 /* copy from current values */
251 kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
252 kb->data= do_ob_key(scene, ob);
256 /************************* Curve ************************/
258 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
267 tot= count_curveverts(nurb);
270 if(kb->data) MEM_freeN(kb->data);
272 kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
283 VECCOPY(fp, bezt->vec[0]);
285 VECCOPY(fp, bezt->vec[1]);
287 VECCOPY(fp, bezt->vec[2]);
296 a= nu->pntsu*nu->pntsv;
298 VECCOPY(fp, bp->vec);
309 void key_to_curve(KeyBlock *kb, Curve *cu, ListBase *nurb)
320 tot= count_curveverts(nurb);
322 tot= MIN2(kb->totelem, tot);
329 while(a-- && tot>0) {
330 VECCOPY(bezt->vec[0], fp);
332 VECCOPY(bezt->vec[1], fp);
334 VECCOPY(bezt->vec[2], fp);
345 a= nu->pntsu*nu->pntsv;
346 while(a-- && tot>0) {
347 VECCOPY(bp->vec, fp);
360 static void insert_curvekey(Scene *scene, Object *ob)
365 ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
369 key= cu->key= add_key( (ID *)cu);
370 key->type = KEY_RELATIVE;
374 kb= add_keyblock(scene, key);
377 /* create from curve */
378 curve_to_key(cu, kb, lb);
381 /* copy from current values */
382 kb->totelem= count_curveverts(lb);
383 kb->data= do_ob_key(scene, ob);
388 /*********************** add shape key ***********************/
390 static void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob)
394 if(ob->type==OB_MESH) insert_meshkey(scene, ob);
395 else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob);
396 else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob);
399 ob->shapenr= BLI_countlist(&key->block);
401 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
404 /*********************** remove shape key ***********************/
406 static int ED_object_shape_key_remove(bContext *C, Object *ob)
408 Main *bmain= CTX_data_main(C);
417 kb= BLI_findlink(&key->block, ob->shapenr-1);
420 for(rkb= key->block.first; rkb; rkb= rkb->next)
421 if(rkb->relative == ob->shapenr-1)
424 BLI_remlink(&key->block, kb);
427 key->refkey= key->block.first;
429 if(kb->data) MEM_freeN(kb->data);
432 for(kb= key->block.first; kb; kb= kb->next)
433 if(kb->adrcode>=ob->shapenr)
436 #if 0 // XXX old animation system
439 for(icu= key->ipo->curve.first; icu; icu= icu->next) {
440 if(icu->adrcode==ob->shapenr-1) {
441 BLI_remlink(&key->ipo->curve, icu);
446 for(icu= key->ipo->curve.first; icu; icu= icu->next)
447 if(icu->adrcode>=ob->shapenr)
450 #endif // XXX old animation system
452 if(ob->shapenr>1) ob->shapenr--;
456 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
457 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
458 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
460 free_libblock_us(&(bmain->key), key);
463 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
464 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
469 static int ED_object_shape_key_mirror(bContext *C, Scene *scene, Object *ob)
478 kb= BLI_findlink(&key->block, ob->shapenr-1);
484 char *tag_elem= MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
487 if(ob->type==OB_MESH) {
491 mesh_octree_table(ob, NULL, NULL, 's');
493 for(i1=0, mv=me->mvert; i1<me->totvert; i1++, mv++) {
494 i2= mesh_get_x_mirror_vert(ob, i1);
496 fp1= ((float *)kb->data) + i1*3;
501 if(tag_elem[i1]==0 && tag_elem[i2]==0) {
502 fp1= ((float *)kb->data) + i1*3;
503 fp2= ((float *)kb->data) + i2*3;
513 tag_elem[i1]= tag_elem[i2]= 1;
517 mesh_octree_table(ob, NULL, NULL, 'e');
519 /* todo, other types? */
524 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
525 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
530 /********************** shape key operators *********************/
532 static int shape_key_mode_poll(bContext *C)
534 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
535 ID *data= (ob)? ob->data: NULL;
536 return (ob && !ob->id.lib && data && !data->lib && ob->mode != OB_MODE_EDIT);
539 static int shape_key_poll(bContext *C)
541 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
542 ID *data= (ob)? ob->data: NULL;
543 return (ob && !ob->id.lib && data && !data->lib);
546 static int shape_key_add_exec(bContext *C, wmOperator *op)
548 Scene *scene= CTX_data_scene(C);
549 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
551 ED_object_shape_key_add(C, scene, ob);
553 return OPERATOR_FINISHED;
556 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
559 ot->name= "Add Shape Key";
560 ot->name= "Add shape key to the object.";
561 ot->idname= "OBJECT_OT_shape_key_add";
564 ot->poll= shape_key_mode_poll;
565 ot->exec= shape_key_add_exec;
568 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
571 static int shape_key_remove_exec(bContext *C, wmOperator *op)
573 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
575 if(!ED_object_shape_key_remove(C, ob))
576 return OPERATOR_CANCELLED;
578 return OPERATOR_FINISHED;
581 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
584 ot->name= "Remove Shape Key";
585 ot->name= "Remove shape key from the object.";
586 ot->idname= "OBJECT_OT_shape_key_remove";
589 ot->poll= shape_key_mode_poll;
590 ot->exec= shape_key_remove_exec;
593 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
596 static int shape_key_clear_exec(bContext *C, wmOperator *op)
598 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
599 Key *key= ob_get_key(ob);
600 KeyBlock *kb= ob_get_keyblock(ob);
603 return OPERATOR_CANCELLED;
605 for(kb=key->block.first; kb; kb=kb->next)
608 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
609 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
611 return OPERATOR_FINISHED;
614 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
617 ot->name= "Clear Shape Keys";
618 ot->description= "Clear weights for all shape keys.";
619 ot->idname= "OBJECT_OT_shape_key_clear";
622 ot->poll= shape_key_poll;
623 ot->exec= shape_key_clear_exec;
626 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
629 static int shape_key_mirror_exec(bContext *C, wmOperator *op)
631 Scene *scene= CTX_data_scene(C);
632 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
634 if(!ED_object_shape_key_mirror(C, scene, ob))
635 return OPERATOR_CANCELLED;
637 return OPERATOR_FINISHED;
640 void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
643 ot->name= "Mirror Shape Key";
644 ot->idname= "OBJECT_OT_shape_key_mirror";
647 ot->poll= shape_key_mode_poll;
648 ot->exec= shape_key_mirror_exec;
651 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
655 static int shape_key_move_exec(bContext *C, wmOperator *op)
657 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
659 int type= RNA_enum_get(op->ptr, "type");
660 Key *key= ob_get_key(ob);
663 KeyBlock *kb, *kb_other;
664 kb= BLI_findlink(&key->block, ob->shapenr-1);
670 BLI_remlink(&key->block, kb);
671 BLI_insertlinkbefore(&key->block, kb_other, kb);
679 BLI_remlink(&key->block, kb);
680 BLI_insertlinkafter(&key->block, kb_other, kb);
686 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
687 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
689 return OPERATOR_FINISHED;
692 void OBJECT_OT_shape_key_move(wmOperatorType *ot)
694 static EnumPropertyItem slot_move[] = {
695 {-1, "UP", 0, "Up", ""},
696 {1, "DOWN", 0, "Down", ""},
697 {0, NULL, 0, NULL, NULL}
701 ot->name= "Move Shape Key";
702 ot->idname= "OBJECT_OT_shape_key_move";
705 ot->poll= shape_key_mode_poll;
706 ot->exec= shape_key_move_exec;
709 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
711 RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");