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 #if 0 // XXX old animation system
84 static void default_key_ipo(Scene *scene, Key *key)
89 key->ipo= add_ipo(scene, "KeyIpo", ID_KE);
91 icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
93 icu->blocktype= ID_KE;
94 icu->adrcode= KEY_SPEED;
95 icu->flag= IPO_VISIBLE|IPO_SELECT|IPO_AUTO_HORIZ;
98 BLI_addtail( &(key->ipo->curve), icu);
100 icu->bezt= bezt= MEM_callocN(2*sizeof(BezTriple), "defaultipo");
104 bezt->f1=bezt->f2= bezt->f3= SELECT;
105 bezt->h1= bezt->h2= HD_AUTO;
107 bezt->vec[1][0]= 100.0;
108 bezt->vec[1][1]= 1.0;
110 bezt->f1=bezt->f2= bezt->f3= SELECT;
111 bezt->h1= bezt->h2= HD_AUTO;
113 calchandles_ipocurve(icu);
115 #endif // XXX old animation system
118 /************************* Mesh ************************/
120 void mesh_to_key(Mesh *me, KeyBlock *kb)
126 if(me->totvert==0) return;
128 if(kb->data) MEM_freeN(kb->data);
130 kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
131 kb->totelem= me->totvert;
135 for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
136 VECCOPY(fp, mvert->co);
141 void key_to_mesh(KeyBlock *kb, Mesh *me)
150 tot= MIN2(kb->totelem, me->totvert);
152 for(a=0; a<tot; a++, fp+=3, mvert++) {
153 VECCOPY(mvert->co, fp);
157 static KeyBlock *add_keyblock(Scene *scene, Key *key)
164 if(kb) curpos= kb->pos;
166 kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
167 BLI_addtail(&key->block, kb);
168 kb->type= KEY_CARDINAL;
170 tot= BLI_countlist(&key->block);
171 if(tot==1) strcpy(kb->name, "Basis");
172 else sprintf(kb->name, "Key %d", tot-1);
174 // XXX this is old anim system stuff? (i.e. the 'index' of the shapekey)
178 if(key->totkey==1) key->refkey= kb;
183 // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
184 if(key->type == KEY_RELATIVE)
187 #if 0 // XXX old animation system
188 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
189 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
195 #endif // XXX old animation system
200 void insert_meshkey(Scene *scene, Mesh *me, short rel)
206 me->key= add_key( (ID *)me);
209 me->key->type = KEY_RELATIVE;
211 // default_key_ipo(scene, me->key); // XXX old animation system
215 kb= add_keyblock(scene, key);
220 /************************* Lattice ************************/
222 void latt_to_key(Lattice *lt, KeyBlock *kb)
228 tot= lt->pntsu*lt->pntsv*lt->pntsw;
231 if(kb->data) MEM_freeN(kb->data);
233 kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
238 for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
239 VECCOPY(fp, bp->vec);
243 void key_to_latt(KeyBlock *kb, Lattice *lt)
252 tot= lt->pntsu*lt->pntsv*lt->pntsw;
253 tot= MIN2(kb->totelem, tot);
255 for(a=0; a<tot; a++, fp+=3, bp++) {
256 VECCOPY(bp->vec, fp);
261 /* exported to python... hrms, should not, use object levels! (ton) */
262 void insert_lattkey(Scene *scene, Lattice *lt, short rel)
268 lt->key= add_key( (ID *)lt);
269 // default_key_ipo(scene, lt->key); // XXX old animation system
273 kb= add_keyblock(scene, key);
278 /************************* Curve ************************/
280 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
289 tot= count_curveverts(nurb);
292 if(kb->data) MEM_freeN(kb->data);
294 kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
305 VECCOPY(fp, bezt->vec[0]);
307 VECCOPY(fp, bezt->vec[1]);
309 VECCOPY(fp, bezt->vec[2]);
318 a= nu->pntsu*nu->pntsv;
320 VECCOPY(fp, bp->vec);
331 void key_to_curve(KeyBlock *kb, Curve *cu, ListBase *nurb)
342 tot= count_curveverts(nurb);
344 tot= MIN2(kb->totelem, tot);
351 while(a-- && tot>0) {
352 VECCOPY(bezt->vec[0], fp);
354 VECCOPY(bezt->vec[1], fp);
356 VECCOPY(bezt->vec[2], fp);
367 a= nu->pntsu*nu->pntsv;
368 while(a-- && tot>0) {
369 VECCOPY(bp->vec, fp);
382 void insert_curvekey(Scene *scene, Curve *cu, short rel)
386 ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
389 cu->key= add_key( (ID *)cu);
392 cu->key->type = KEY_RELATIVE;
394 // default_key_ipo(scene, cu->key); // XXX old animation system
398 kb= add_keyblock(scene, key);
400 curve_to_key(cu, kb, lb);
403 /*********************** add shape key ***********************/
405 static void ED_object_shape_key_add(bContext *C, Scene *scene, Object *ob)
409 if(ob->type==OB_MESH) insert_meshkey(scene, ob->data, 1);
410 else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob->data, 1);
411 else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob->data, 1);
414 ob->shapenr= BLI_countlist(&key->block);
416 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
419 /*********************** remove shape key ***********************/
421 static int ED_object_shape_key_remove(bContext *C, Object *ob)
423 Main *bmain= CTX_data_main(C);
432 kb= BLI_findlink(&key->block, ob->shapenr-1);
435 for(rkb= key->block.first; rkb; rkb= rkb->next)
436 if(rkb->relative == ob->shapenr-1)
439 BLI_remlink(&key->block, kb);
442 key->refkey= key->block.first;
444 if(kb->data) MEM_freeN(kb->data);
447 for(kb= key->block.first; kb; kb= kb->next)
448 if(kb->adrcode>=ob->shapenr)
451 #if 0 // XXX old animation system
454 for(icu= key->ipo->curve.first; icu; icu= icu->next) {
455 if(icu->adrcode==ob->shapenr-1) {
456 BLI_remlink(&key->ipo->curve, icu);
461 for(icu= key->ipo->curve.first; icu; icu= icu->next)
462 if(icu->adrcode>=ob->shapenr)
465 #endif // XXX old animation system
467 if(ob->shapenr>1) ob->shapenr--;
471 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
472 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
473 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
475 free_libblock_us(&(bmain->key), key);
478 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
479 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
484 static int ED_object_shape_key_mirror(bContext *C, Scene *scene, Object *ob)
493 kb= BLI_findlink(&key->block, ob->shapenr-1);
499 char *tag_elem= MEM_callocN(sizeof(char) * kb->totelem, "shape_key_mirror");
502 if(ob->type==OB_MESH) {
506 mesh_octree_table(ob, NULL, NULL, 's');
508 for(i1=0, mv=me->mvert; i1<me->totvert; i1++, mv++) {
509 i2= mesh_get_x_mirror_vert(ob, i1);
511 fp1= ((float *)kb->data) + i1*3;
516 if(tag_elem[i1]==0 && tag_elem[i2]==0) {
517 fp1= ((float *)kb->data) + i1*3;
518 fp2= ((float *)kb->data) + i2*3;
528 tag_elem[i1]= tag_elem[i2]= 1;
532 mesh_octree_table(ob, NULL, NULL, 'e');
534 /* todo, other types? */
539 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
540 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
545 /********************** shape key operators *********************/
547 static int shape_key_poll(bContext *C)
549 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
550 ID *data= (ob)? ob->data: NULL;
551 return (ob && !ob->id.lib && data && !data->lib);
554 static int shape_key_add_exec(bContext *C, wmOperator *op)
556 Scene *scene= CTX_data_scene(C);
557 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
559 ED_object_shape_key_add(C, scene, ob);
561 return OPERATOR_FINISHED;
564 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
567 ot->name= "Add Shape Key";
568 ot->name= "Add shape key to the object.";
569 ot->idname= "OBJECT_OT_shape_key_add";
572 ot->poll= shape_key_poll;
573 ot->exec= shape_key_add_exec;
576 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
579 static int shape_key_remove_exec(bContext *C, wmOperator *op)
581 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
583 if(!ED_object_shape_key_remove(C, ob))
584 return OPERATOR_CANCELLED;
586 return OPERATOR_FINISHED;
589 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
592 ot->name= "Remove Shape Key";
593 ot->name= "Remove shape key from the object.";
594 ot->idname= "OBJECT_OT_shape_key_remove";
597 ot->poll= shape_key_poll;
598 ot->exec= shape_key_remove_exec;
601 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
604 static int shape_key_clear_exec(bContext *C, wmOperator *op)
606 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
607 Key *key= ob_get_key(ob);
608 KeyBlock *kb= ob_get_keyblock(ob);
611 return OPERATOR_CANCELLED;
613 for(kb=key->block.first; kb; kb=kb->next)
616 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
617 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
619 return OPERATOR_FINISHED;
622 void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
625 ot->name= "Clear Shape Keys";
626 ot->description= "Clear weights for all shape keys.";
627 ot->idname= "OBJECT_OT_shape_key_clear";
630 ot->poll= shape_key_poll;
631 ot->exec= shape_key_clear_exec;
634 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
637 static int shape_key_mirror_exec(bContext *C, wmOperator *op)
639 Scene *scene= CTX_data_scene(C);
640 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
642 if(!ED_object_shape_key_mirror(C, scene, ob))
643 return OPERATOR_CANCELLED;
645 return OPERATOR_FINISHED;
648 void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
651 ot->name= "Mirror Shape Key";
652 ot->idname= "OBJECT_OT_shape_key_mirror";
655 ot->poll= shape_key_poll;
656 ot->exec= shape_key_mirror_exec;
659 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
663 static int shape_key_move_exec(bContext *C, wmOperator *op)
665 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
667 int type= RNA_enum_get(op->ptr, "type");
668 Key *key= ob_get_key(ob);
671 KeyBlock *kb, *kb_other;
672 kb= BLI_findlink(&key->block, ob->shapenr-1);
678 BLI_remlink(&key->block, kb);
679 BLI_insertlinkbefore(&key->block, kb_other, kb);
687 BLI_remlink(&key->block, kb);
688 BLI_insertlinkafter(&key->block, kb_other, kb);
694 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
695 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
697 return OPERATOR_FINISHED;
700 void OBJECT_OT_shape_key_move(wmOperatorType *ot)
702 static EnumPropertyItem slot_move[] = {
703 {-1, "UP", 0, "Up", ""},
704 {1, "DOWN", 0, "Down", ""},
705 {0, NULL, 0, NULL, NULL}
709 ot->name= "Move Shape Key";
710 ot->idname= "OBJECT_OT_shape_key_move";
713 ot->poll= shape_key_poll;
714 ot->exec= shape_key_move_exec;
717 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
719 RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");