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"
74 #include "RNA_access.h"
79 #include "object_intern.h"
82 static void BIF_undo_push() {}
85 #if 0 // XXX old animation system
86 static void default_key_ipo(Scene *scene, Key *key)
91 key->ipo= add_ipo(scene, "KeyIpo", ID_KE);
93 icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
95 icu->blocktype= ID_KE;
96 icu->adrcode= KEY_SPEED;
97 icu->flag= IPO_VISIBLE|IPO_SELECT|IPO_AUTO_HORIZ;
100 BLI_addtail( &(key->ipo->curve), icu);
102 icu->bezt= bezt= MEM_callocN(2*sizeof(BezTriple), "defaultipo");
106 bezt->f1=bezt->f2= bezt->f3= SELECT;
107 bezt->h1= bezt->h2= HD_AUTO;
109 bezt->vec[1][0]= 100.0;
110 bezt->vec[1][1]= 1.0;
112 bezt->f1=bezt->f2= bezt->f3= SELECT;
113 bezt->h1= bezt->h2= HD_AUTO;
115 calchandles_ipocurve(icu);
117 #endif // XXX old animation system
120 /* **************************************** */
122 void mesh_to_key(Mesh *me, KeyBlock *kb)
128 if(me->totvert==0) return;
130 if(kb->data) MEM_freeN(kb->data);
132 kb->data= MEM_callocN(me->key->elemsize*me->totvert, "kb->data");
133 kb->totelem= me->totvert;
137 for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
138 VECCOPY(fp, mvert->co);
143 void key_to_mesh(KeyBlock *kb, Mesh *me)
152 tot= MIN2(kb->totelem, me->totvert);
154 for(a=0; a<tot; a++, fp+=3, mvert++) {
155 VECCOPY(mvert->co, fp);
159 static KeyBlock *add_keyblock(Scene *scene, Key *key)
166 if(kb) curpos= kb->pos;
168 kb= MEM_callocN(sizeof(KeyBlock), "Keyblock");
169 BLI_addtail(&key->block, kb);
170 kb->type= KEY_CARDINAL;
171 tot= BLI_countlist(&key->block);
172 if(tot==1) strcpy(kb->name, "Basis");
173 else sprintf(kb->name, "Key %d", tot-1);
177 if(key->totkey==1) key->refkey= kb;
179 // XXX kb->pos is the confusing old horizontal-line RVK crap in old IPO Editor...
180 if(key->type == KEY_RELATIVE)
183 #if 0 // XXX old animation system
184 curpos= bsystem_time(scene, 0, (float)CFRA, 0.0);
185 if(calc_ipo_spec(key->ipo, KEY_SPEED, &curpos)==0) {
191 #endif // XXX old animation system
196 void insert_meshkey(Scene *scene, Mesh *me, short rel)
202 me->key= add_key( (ID *)me);
205 me->key->type = KEY_RELATIVE;
207 // default_key_ipo(scene, me->key); // XXX old animation system
211 kb= add_keyblock(scene, key);
216 /* ******************** */
218 void latt_to_key(Lattice *lt, KeyBlock *kb)
224 tot= lt->pntsu*lt->pntsv*lt->pntsw;
227 if(kb->data) MEM_freeN(kb->data);
229 kb->data= MEM_callocN(lt->key->elemsize*tot, "kb->data");
234 for(a=0; a<kb->totelem; a++, fp+=3, bp++) {
235 VECCOPY(fp, bp->vec);
239 void key_to_latt(KeyBlock *kb, Lattice *lt)
248 tot= lt->pntsu*lt->pntsv*lt->pntsw;
249 tot= MIN2(kb->totelem, tot);
251 for(a=0; a<tot; a++, fp+=3, bp++) {
252 VECCOPY(bp->vec, fp);
257 /* exported to python... hrms, should not, use object levels! (ton) */
258 void insert_lattkey(Scene *scene, Lattice *lt, short rel)
264 lt->key= add_key( (ID *)lt);
265 // default_key_ipo(scene, lt->key); // XXX old animation system
269 kb= add_keyblock(scene, key);
274 /* ******************************** */
276 void curve_to_key(Curve *cu, KeyBlock *kb, ListBase *nurb)
285 tot= count_curveverts(nurb);
288 if(kb->data) MEM_freeN(kb->data);
290 kb->data= MEM_callocN(cu->key->elemsize*tot, "kb->data");
301 VECCOPY(fp, bezt->vec[0]);
303 VECCOPY(fp, bezt->vec[1]);
305 VECCOPY(fp, bezt->vec[2]);
314 a= nu->pntsu*nu->pntsv;
316 VECCOPY(fp, bp->vec);
327 void key_to_curve(KeyBlock *kb, Curve *cu, ListBase *nurb)
338 tot= count_curveverts(nurb);
340 tot= MIN2(kb->totelem, tot);
347 while(a-- && tot>0) {
348 VECCOPY(bezt->vec[0], fp);
350 VECCOPY(bezt->vec[1], fp);
352 VECCOPY(bezt->vec[2], fp);
363 a= nu->pntsu*nu->pntsv;
364 while(a-- && tot>0) {
365 VECCOPY(bp->vec, fp);
378 void insert_curvekey(Scene *scene, Curve *cu, short rel)
384 cu->key= add_key( (ID *)cu);
387 cu->key->type = KEY_RELATIVE;
389 // default_key_ipo(scene, cu->key); // XXX old animation system
393 kb= add_keyblock(scene, key);
395 if(cu->editnurb->first) curve_to_key(cu, kb, cu->editnurb);
396 else curve_to_key(cu, kb, &cu->nurb);
400 /* ******************** */
402 void delete_key(Scene *scene, Object *ob)
409 if(key==NULL) return;
411 kb= BLI_findlink(&key->block, ob->shapenr-1);
414 for(rkb= key->block.first; rkb; rkb= rkb->next)
415 if(rkb->relative == ob->shapenr-1)
418 BLI_remlink(&key->block, kb);
420 if(key->refkey== kb) key->refkey= key->block.first;
422 if(kb->data) MEM_freeN(kb->data);
425 for(kb= key->block.first; kb; kb= kb->next) {
426 if(kb->adrcode>=ob->shapenr)
430 #if 0 // XXX old animation system
433 for(icu= key->ipo->curve.first; icu; icu= icu->next) {
434 if(icu->adrcode==ob->shapenr-1) {
435 BLI_remlink(&key->ipo->curve, icu);
440 for(icu= key->ipo->curve.first; icu; icu= icu->next)
441 if(icu->adrcode>=ob->shapenr)
444 #endif // XXX old animation system
446 if(ob->shapenr>1) ob->shapenr--;
450 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
451 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
452 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
454 free_libblock_us(&(G.main->key), key);
457 DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
459 BIF_undo_push("Delete Shapekey");
462 /********************** shape key operators *********************/
464 static int shape_key_add_exec(bContext *C, wmOperator *op)
466 Scene *scene= CTX_data_scene(C);
467 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
471 return OPERATOR_CANCELLED;
473 if(ob->type==OB_MESH) insert_meshkey(scene, ob->data, 1);
474 else if ELEM(ob->type, OB_CURVE, OB_SURF) insert_curvekey(scene, ob->data, 1);
475 else if(ob->type==OB_LATTICE) insert_lattkey(scene, ob->data, 1);
478 ob->shapenr= BLI_countlist(&key->block);
480 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
482 return OPERATOR_FINISHED;
485 void OBJECT_OT_shape_key_add(wmOperatorType *ot)
488 ot->name= "Add Shape Key";
489 ot->idname= "OBJECT_OT_shape_key_add";
492 ot->exec= shape_key_add_exec;
495 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
498 static int shape_key_remove_exec(bContext *C, wmOperator *op)
500 Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
501 Scene *scene= CTX_data_scene(C);
502 Main *bmain= CTX_data_main(C);
508 return OPERATOR_CANCELLED;
512 return OPERATOR_CANCELLED;
514 kb= BLI_findlink(&key->block, ob->shapenr-1);
517 for(rkb= key->block.first; rkb; rkb= rkb->next)
518 if(rkb->relative == ob->shapenr-1)
521 BLI_remlink(&key->block, kb);
524 key->refkey= key->block.first;
526 if(kb->data) MEM_freeN(kb->data);
529 for(kb= key->block.first; kb; kb= kb->next)
530 if(kb->adrcode>=ob->shapenr)
533 #if 0 // XXX old animation system
536 for(icu= key->ipo->curve.first; icu; icu= icu->next) {
537 if(icu->adrcode==ob->shapenr-1) {
538 BLI_remlink(&key->ipo->curve, icu);
543 for(icu= key->ipo->curve.first; icu; icu= icu->next)
544 if(icu->adrcode>=ob->shapenr)
547 #endif // XXX old animation system
549 if(ob->shapenr>1) ob->shapenr--;
553 if(GS(key->from->name)==ID_ME) ((Mesh *)key->from)->key= NULL;
554 else if(GS(key->from->name)==ID_CU) ((Curve *)key->from)->key= NULL;
555 else if(GS(key->from->name)==ID_LT) ((Lattice *)key->from)->key= NULL;
557 free_libblock_us(&(bmain->key), key);
560 DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
561 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
563 return OPERATOR_FINISHED;
566 void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
569 ot->name= "Remove Shape Key";
570 ot->idname= "OBJECT_OT_shape_key_remove";
573 ot->exec= shape_key_remove_exec;
576 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
579 void move_keys(Object *ob)
582 /* XXX probably goes away entirely */
585 float div, dy, oldpos, vec[3], dvec[3];
586 int afbreek=0, firsttime= 1;
587 unsigned short event = 0;
588 short mval[2], val, xo, yo;
591 if(G.sipo->blocktype!=ID_KE) return;
593 if(G.sipo->ipo && G.sipo->ipo->id.lib) return;
594 if(G.sipo->editipo==NULL) return;
597 if(key==NULL) return;
599 /* which kb is involved */
600 kb= BLI_findlink(&key->block, ob->shapenr-1);
605 getmouseco_areawin(mval);
608 dvec[0]=dvec[1]=dvec[2]= 0.0;
611 getmouseco_areawin(mval);
612 if(mval[0]!=xo || mval[1]!=yo || firsttime) {
615 dy= (float)(mval[1]- yo);
617 div= (float)(G.v2d->mask.ymax-G.v2d->mask.ymin);
618 dvec[1]+= (G.v2d->cur.ymax-G.v2d->cur.ymin)*(dy)/div;
622 apply_keyb_grid(vec, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
623 apply_keyb_grid(vec+1, 0.0, 1.0, 0.1, U.flag & USER_AUTOGRABGRID);
625 kb->pos= oldpos+vec[1];
627 sprintf(str, "Y: %.3f ", vec[1]);
635 else BIF_wait_for_statechange();
638 event= extern_qread(&val);
647 arrows_move_cursor(event);
658 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
661 editipo_changed(G.sipo, 0);
663 BIF_undo_push("Move Shapekey(s)");