~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/key.c

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*  key.c      
3
3
 *  
4
4
 * 
5
 
 * $Id: key.c,v 1.21 2006/04/17 17:35:19 ton Exp $
 
5
 * $Id: key.c 14444 2008-04-16 22:40:48Z hos $
6
6
 *
7
 
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
7
 * ***** BEGIN GPL LICENSE BLOCK *****
8
8
 *
9
9
 * This program is free software; you can redistribute it and/or
10
10
 * modify it under the terms of the GNU General Public License
11
11
 * as published by the Free Software Foundation; either version 2
12
 
 * of the License, or (at your option) any later version. The Blender
13
 
 * Foundation also sells licenses for use in proprietary software under
14
 
 * the Blender License.  See http://www.blender.org/BL/ for information
15
 
 * about this.
 
12
 * of the License, or (at your option) any later version.
16
13
 *
17
14
 * This program is distributed in the hope that it will be useful,
18
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
27
 *
31
28
 * Contributor(s): none yet.
32
29
 *
33
 
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
30
 * ***** END GPL LICENSE BLOCK *****
34
31
 */
35
32
 
36
33
#include <math.h>
63
60
 
64
61
#include "BLI_blenlib.h"
65
62
 
 
63
#include "blendef.h"
 
64
 
66
65
#ifdef HAVE_CONFIG_H
67
66
#include <config.h>
68
67
#endif
185
184
        make_local_ipo(key->ipo);
186
185
}
187
186
 
 
187
/*
 
188
 * Sort shape keys and Ipo curves after a change.  This assumes that at most
 
189
 * one key was moved, which is a valid assumption for the places it's
 
190
 * currently being called.
 
191
 */
188
192
 
189
193
void sort_keys(Key *key)
190
194
{
191
195
        KeyBlock *kb;
192
 
        int doit=1;
193
 
        
194
 
        while(doit) {
195
 
                doit= 0;
196
 
                
197
 
                for(kb= key->block.first; kb; kb= kb->next) {
198
 
                        if(kb->next) {
199
 
                                if(kb->pos > kb->next->pos) {
200
 
                                        BLI_remlink(&key->block, kb);
201
 
                                        
202
 
                                        BLI_insertlink(&key->block, kb->next, kb);
203
 
                                        
204
 
                                        doit= 1;
 
196
        short i, adrcode;
 
197
        IpoCurve *icu = NULL;
 
198
        KeyBlock *kb2;
 
199
 
 
200
        /* locate the key which is out of position */ 
 
201
        for( kb= key->block.first; kb; kb= kb->next )
 
202
                if( kb->next && kb->pos > kb->next->pos )
 
203
                        break;
 
204
 
 
205
        /* if we find a key, move it */
 
206
        if( kb ) {
 
207
                kb = kb->next; /* next key is the out-of-order one */
 
208
                BLI_remlink(&key->block, kb);
 
209
 
 
210
                /* find the right location and insert before */
 
211
                for( kb2=key->block.first; kb2; kb2= kb2->next ) {
 
212
                        if( kb2->pos > kb->pos ) {
 
213
                                BLI_insertlink(&key->block, kb2->prev, kb);
 
214
                                break;
 
215
                        }
 
216
                }
 
217
 
 
218
                /* if more than one Ipo curve, see if this key had a curve */
 
219
 
 
220
                if(key->ipo && key->ipo->curve.first != key->ipo->curve.last ) {
 
221
                        for(icu= key->ipo->curve.first; icu; icu= icu->next) {
 
222
                                /* if we find the curve, remove it and reinsert in the 
 
223
                                 right place */
 
224
                                if(icu->adrcode==kb->adrcode) {
 
225
                                        IpoCurve *icu2;
 
226
                                        BLI_remlink(&key->ipo->curve, icu);
 
227
                                        for(icu2= key->ipo->curve.first; icu2; icu2= icu2->next) {
 
228
                                                if(icu2->adrcode >= kb2->adrcode) {
 
229
                                                        BLI_insertlink(&key->ipo->curve, icu2->prev, icu);
 
230
                                                        break;
 
231
                                                }
 
232
                                        }
205
233
                                        break;
206
234
                                }
207
235
                        }
208
 
                }       
 
236
                }
 
237
 
 
238
                /* kb points at the moved key, icu at the moved ipo (if it exists).
 
239
                 * go back now and renumber adrcodes */
 
240
 
 
241
                /* first new code */
 
242
                adrcode = kb2->adrcode;
 
243
                for( i = kb->adrcode - adrcode; i >= 0; --i, ++adrcode ) {
 
244
                        /* if the next ipo curve matches the current key, renumber it */
 
245
                        if(icu && icu->adrcode == kb->adrcode ) {
 
246
                                icu->adrcode = adrcode;
 
247
                                icu = icu->next;
 
248
                        }
 
249
                        /* renumber the shape key */
 
250
                        kb->adrcode = adrcode;
 
251
                        kb = kb->next;
 
252
                }
209
253
        }
210
254
 
211
255
        /* new rule; first key is refkey, this to match drawing channels... */
498
542
                        case IPO_FLOAT:
499
543
                                
500
544
                                if(weights) {
501
 
                                        memcpy(poin, kref, 4*cp[0]);
 
545
                                        memcpy(poin, kref, sizeof(float)*cp[0]);
502
546
                                        if(*weights!=0.0f)
503
547
                                                rel_flerp(cp[0], (float *)poin, (float *)kref, (float *)k1, *weights);
504
548
                                        weights++;
505
549
                                }
506
550
                                else 
507
 
                                        memcpy(poin, k1, 4*cp[0]);
 
551
                                        memcpy(poin, k1, sizeof(float)*cp[0]);
508
552
 
509
553
                                poin+= ofsp[0];
510
554
 
511
555
                                break;
512
556
                        case IPO_BPOINT:
513
 
                                memcpy(poin, k1, 3*4);
514
 
                                memcpy(poin+16, k1+12, 4);
 
557
                                memcpy(poin, k1, 3*sizeof(float));
 
558
                                memcpy(poin+4*sizeof(float), k1+3*sizeof(float), sizeof(float));
515
559
                                
516
560
                                poin+= ofsp[0];                         
517
561
 
518
562
                                break;
519
563
                        case IPO_BEZTRIPLE:
520
 
                                memcpy(poin, k1, 4*12);
 
564
                                memcpy(poin, k1, sizeof(float)*10);
521
565
                                poin+= ofsp[0]; 
522
566
 
523
567
                                break;
625
669
        
626
670
        /* step 2: do it */
627
671
        
628
 
        kb= key->block.first;
629
 
        while(kb) {
630
 
                
 
672
        for(kb=key->block.first; kb; kb=kb->next) {
631
673
                if(kb!=key->refkey) {
632
674
                        float icuval= kb->curval;
633
675
                        
634
676
                        /* only with value, and no difference allowed */
635
 
                        if(icuval!=0.0f && kb->totelem==tot) {
 
677
                        if(!(kb->flag & KEYBLOCK_MUTE) && icuval!=0.0f && kb->totelem==tot) {
 
678
                                KeyBlock *refb;
636
679
                                float weight, *weights= kb->weights;
637
680
                                
638
681
                                poin= basispoin;
639
 
                                reffrom= key->refkey->data;
640
682
                                from= kb->data;
 
683
                                /* reference now can be any block */
 
684
                                refb= BLI_findlink(&key->block, kb->relative);
 
685
                                if(refb==NULL) continue;
 
686
                                reffrom= refb->data;
641
687
                                
642
688
                                poin+= start*ofs[0];
643
689
                                reffrom+= key->elemsize*start;  // key elemsize yes!
687
733
                                }
688
734
                        }
689
735
                }
690
 
                kb= kb->next;
691
736
        }
692
737
}
693
738
 
903
948
        }
904
949
}
905
950
 
906
 
static float *get_weights_array(Object *ob, Mesh *me, char *vgroup)
 
951
static float *get_weights_array(Object *ob, char *vgroup)
907
952
{
908
953
        bDeformGroup *curdef;
909
 
        int index= 0;
 
954
        MDeformVert *dvert= NULL;
 
955
        int totvert= 0, index= 0;
910
956
        
 
957
        /* no vgroup string set? */
911
958
        if(vgroup[0]==0) return NULL;
912
 
        if(me->dvert==NULL) return NULL;
 
959
        
 
960
        /* gather dvert and totvert */
 
961
        if(ob->type==OB_MESH) {
 
962
                Mesh *me= ob->data;
 
963
                dvert= me->dvert;
 
964
                totvert= me->totvert;
 
965
        }
 
966
        else if(ob->type==OB_LATTICE) {
 
967
                Lattice *lt= ob->data;
 
968
                dvert= lt->dvert;
 
969
                totvert= lt->pntsu*lt->pntsv*lt->pntsw;
 
970
        }
 
971
        
 
972
        if(dvert==NULL) return NULL;
913
973
        
914
974
        /* find the group (weak loop-in-loop) */
915
975
        for (curdef = ob->defbase.first; curdef; curdef=curdef->next, index++)
917
977
                        break;
918
978
 
919
979
        if(curdef) {
920
 
                MDeformVert *dvert= me->dvert;
921
980
                float *weights;
922
981
                int i, j;
923
982
                
924
 
                weights= MEM_callocN(me->totvert*sizeof(float), "weights");
 
983
                weights= MEM_callocN(totvert*sizeof(float), "weights");
925
984
                
926
 
                for (i=0; i < me->totvert; i++, dvert++) {
 
985
                for (i=0; i < totvert; i++, dvert++) {
927
986
                        for(j=0; j<dvert->totweight; j++) {
928
987
                                if (dvert->dw[j].def_nr == index) {
929
988
                                        weights[i]= dvert->dw[j].weight;
964
1023
                
965
1024
                for(a=0; a<me->totvert; a+=step, cfra+= delta) {
966
1025
                        
967
 
                        ctime= bsystem_time(0, 0, cfra, 0.0);
 
1026
                        ctime= bsystem_time(0, cfra, 0.0);
968
1027
                        if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
969
1028
                                ctime /= 100.0;
970
1029
                                CLAMP(ctime, 0.0, 1.0);
989
1048
                        KeyBlock *kb;
990
1049
                        
991
1050
                        for(kb= me->key->block.first; kb; kb= kb->next)
992
 
                                kb->weights= get_weights_array(ob, me, kb->vgroup);
 
1051
                                kb->weights= get_weights_array(ob, kb->vgroup);
993
1052
 
994
1053
                        do_rel_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, 0);
995
1054
                        
999
1058
                        }
1000
1059
                }
1001
1060
                else {
1002
 
                        ctime= bsystem_time(ob, 0, G.scene->r.cfra, 0.0);
 
1061
                        ctime= bsystem_time(ob, G.scene->r.cfra, 0.0);
1003
1062
 
1004
1063
                        if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
1005
1064
                                ctime /= 100.0;
1119
1178
                
1120
1179
                for(a=0; a<tot; a+=step, cfra+= delta) {
1121
1180
                        
1122
 
                        ctime= bsystem_time(0, 0, cfra, 0.0);
 
1181
                        ctime= bsystem_time(0, cfra, 0.0);
1123
1182
                        if(calc_ipo_spec(cu->key->ipo, KEY_SPEED, &ctime)==0) {
1124
1183
                                ctime /= 100.0;
1125
1184
                                CLAMP(ctime, 0.0, 1.0);
1141
1200
        }
1142
1201
        else {
1143
1202
                
1144
 
                ctime= bsystem_time(NULL, 0, (float)G.scene->r.cfra, 0.0);
 
1203
                ctime= bsystem_time(NULL, (float)G.scene->r.cfra, 0.0);
1145
1204
                
1146
1205
                if(cu->key->type==KEY_RELATIVE) {
1147
1206
                        do_rel_cu_key(cu, ctime);
1164
1223
        return 1;
1165
1224
}
1166
1225
 
1167
 
static int do_latt_key(Lattice *lt)
 
1226
static int do_latt_key(Object *ob, Lattice *lt)
1168
1227
{
1169
1228
        KeyBlock *k[4];
1170
1229
        float delta, cfra, ctime, t[4];
1183
1242
                
1184
1243
                for(a=0; a<tot; a++, cfra+= delta) {
1185
1244
                        
1186
 
                        ctime= bsystem_time(0, 0, cfra, 0.0);
 
1245
                        ctime= bsystem_time(0, cfra, 0.0);
1187
1246
                        if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1188
1247
                                ctime /= 100.0;
1189
1248
                                CLAMP(ctime, 0.0, 1.0);
1200
1259
                }               
1201
1260
        }
1202
1261
        else {
1203
 
                ctime= bsystem_time(NULL, 0, (float)G.scene->r.cfra, 0.0);
 
1262
                ctime= bsystem_time(NULL, (float)G.scene->r.cfra, 0.0);
1204
1263
        
1205
1264
                if(lt->key->type==KEY_RELATIVE) {
 
1265
                        KeyBlock *kb;
 
1266
                        
 
1267
                        for(kb= lt->key->block.first; kb; kb= kb->next)
 
1268
                                kb->weights= get_weights_array(ob, kb->vgroup);
 
1269
                        
1206
1270
                        do_rel_key(0, tot, tot, (char *)lt->def->vec, lt->key, 0);
 
1271
                        
 
1272
                        for(kb= lt->key->block.first; kb; kb= kb->next) {
 
1273
                                if(kb->weights) MEM_freeN(kb->weights);
 
1274
                                kb->weights= NULL;
 
1275
                        }
1207
1276
                }
1208
1277
                else {
1209
1278
                        if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1233
1302
        
1234
1303
        if(key==NULL)
1235
1304
                return 0;
1236
 
        
 
1305
                
1237
1306
        if(ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)) {
1238
1307
                KeyBlock *kb= BLI_findlink(&key->block, ob->shapenr-1);
1239
1308
 
 
1309
                if(kb && (kb->flag & KEYBLOCK_MUTE))
 
1310
                        kb= key->refkey;
 
1311
 
1240
1312
                if(kb==NULL) {
1241
1313
                        kb= key->block.first;
1242
1314
                        ob->shapenr= 1;
1244
1316
                
1245
1317
                if(ob->type==OB_MESH) {
1246
1318
                        Mesh *me= ob->data;
1247
 
                        float *weights= get_weights_array(ob, me, kb->vgroup);
 
1319
                        float *weights= get_weights_array(ob, kb->vgroup);
1248
1320
 
1249
1321
                        cp_key(0, me->totvert, me->totvert, (char *)me->mvert->co, key, kb, weights, 0);
1250
1322
                        
1252
1324
                }
1253
1325
                else if(ob->type==OB_LATTICE) {
1254
1326
                        Lattice *lt= ob->data;
 
1327
                        float *weights= get_weights_array(ob, kb->vgroup);
1255
1328
                        int tot= lt->pntsu*lt->pntsv*lt->pntsw;
1256
1329
                        
1257
 
                        cp_key(0, tot, tot, (char *)lt->def->vec, key, kb, NULL, 0);
 
1330
                        cp_key(0, tot, tot, (char *)lt->def->vec, key, kb, weights, 0);
 
1331
                        
 
1332
                        if(weights) MEM_freeN(weights);
1258
1333
                }
1259
1334
                else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1260
1335
                        Curve *cu= ob->data;
1268
1343
                if(ob->ipoflag & OB_ACTION_KEY)
1269
1344
                        do_all_object_actions(ob);
1270
1345
                else {
1271
 
                        calc_ipo(key->ipo, bsystem_time(ob, 0, G.scene->r.cfra, 0.0));
 
1346
                        calc_ipo(key->ipo, bsystem_time(ob, G.scene->r.cfra, 0.0));
1272
1347
                        execute_ipo((ID *)key, key->ipo);
1273
1348
                }
1274
1349
                
1275
1350
                if(ob->type==OB_MESH) return do_mesh_key(ob, ob->data);
1276
1351
                else if(ob->type==OB_CURVE) return do_curve_key( ob->data);
1277
1352
                else if(ob->type==OB_SURF) return do_curve_key( ob->data);
1278
 
                else if(ob->type==OB_LATTICE) return do_latt_key( ob->data);
 
1353
                else if(ob->type==OB_LATTICE) return do_latt_key(ob, ob->data);
1279
1354
        }
1280
1355
        
1281
1356
        return 0;
1312
1387
 
1313
1388
        return NULL;
1314
1389
}
 
1390
 
 
1391
/* get the appropriate KeyBlock given an index */
 
1392
KeyBlock *key_get_keyblock(Key *key, int index)
 
1393
{
 
1394
        KeyBlock *kb;
 
1395
        int i;
 
1396
        
 
1397
        if (key) {
 
1398
                kb= key->block.first;
 
1399
                
 
1400
                for (i= 1; i < key->totkey; i++) {
 
1401
                        kb= kb->next;
 
1402
                        
 
1403
                        if (index==i)
 
1404
                                return kb;
 
1405
                }
 
1406
        }
 
1407
        
 
1408
        return NULL;
 
1409
}