Bugfix #4051
[blender.git] / source / blender / blenkernel / intern / anim.c
1 /** anim.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <math.h>
36 #include <string.h>
37
38 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_arithb.h"
41 #include "DNA_listBase.h"
42
43 #include "DNA_curve_types.h"
44 #include "DNA_effect_types.h"
45 #include "DNA_group_types.h"
46 #include "DNA_key_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_view3d_types.h"
52 #include "DNA_vfont_types.h"
53
54 #include "BKE_anim.h"
55 #include "BKE_DerivedMesh.h"
56 #include "BKE_displist.h"
57 #include "BKE_effect.h"
58 #include "BKE_font.h"
59 #include "BKE_group.h"
60 #include "BKE_global.h"
61 #include "BKE_ipo.h"
62 #include "BKE_key.h"
63 #include "BKE_main.h"
64 #include "BKE_object.h"
65 #include "BKE_utildefines.h"
66
67 #include "BKE_bad_level_calls.h"
68
69 #ifdef HAVE_CONFIG_H
70 #include <config.h>
71 #endif
72
73 ListBase duplilist= {0, 0}; 
74
75 void free_path(Path *path)
76 {
77         if(path->data) MEM_freeN(path->data);
78         MEM_freeN(path);
79 }
80
81
82 void calc_curvepath(Object *ob)
83 {
84         BevList *bl;
85         BevPoint *bevp, *bevpn, *bevpfirst, *bevplast, *tempbevp;
86         Curve *cu;
87         Nurb *nu;
88         Path *path;
89         float *fp, *dist, *maxdist, x, y, z;
90         float fac, d=0, fac1, fac2;
91         int a, tot, cycl=0;
92         float *ft;
93         
94         /* in a path vertices are with equal differences: path->len = number of verts */
95         /* NOW WITH BEVELCURVE!!! */
96         
97         if(ob==NULL || ob->type != OB_CURVE) return;
98         cu= ob->data;
99         if(ob==G.obedit) nu= editNurb.first;
100         else nu= cu->nurb.first;
101         
102         if(cu->path) free_path(cu->path);
103         cu->path= NULL;
104         
105         bl= cu->bev.first;
106         if(bl==NULL) return;
107
108         cu->path=path= MEM_callocN(sizeof(Path), "path");
109         
110         /* if POLY: last vertice != first vertice */
111         cycl= (bl->poly!= -1);
112         
113         if(cycl) tot= bl->nr;
114         else tot= bl->nr-1;
115         
116         path->len= tot+1;
117         /* exception: vector handle paths and polygon paths should be subdivided at least a factor 6 (or more?) */
118         if(path->len<6*nu->pntsu) path->len= 6*nu->pntsu;
119         
120         dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
121
122                 /* all lengths in *dist */
123         bevp= bevpfirst= (BevPoint *)(bl+1);
124         fp= dist;
125         *fp= 0;
126         for(a=0; a<tot; a++) {
127                 fp++;
128                 if(cycl && a==tot-1) {
129                         x= bevpfirst->x - bevp->x;
130                         y= bevpfirst->y - bevp->y;
131                         z= bevpfirst->z - bevp->z;
132                 }
133                 else {
134                         tempbevp = bevp+1;
135                         x= (tempbevp)->x - bevp->x;
136                         y= (tempbevp)->y - bevp->y;
137                         z= (tempbevp)->z - bevp->z;
138                 }
139                 *fp= *(fp-1)+ (float)sqrt(x*x+y*y+z*z);
140                 
141                 bevp++;
142         }
143         
144         path->totdist= *fp;
145
146                 /* the path verts  in path->data */
147                 /* now also with TILT value */
148         ft= path->data = (float *)MEM_callocN(16*path->len, "pathdata");
149         
150         bevp= bevpfirst;
151         bevpn= bevp+1;
152         bevplast= bevpfirst + (bl->nr-1);
153         fp= dist+1;
154         maxdist= dist+tot;
155         fac= 1.0f/((float)path->len-1.0f);
156         fac = fac * path->totdist;
157
158         for(a=0; a<path->len; a++) {
159                 
160                 d= ((float)a)*fac;
161                 
162                 /* we're looking for location (distance) 'd' in the array */
163                 while((d>= *fp) && fp<maxdist) {
164                         fp++;
165                         if(bevp<bevplast) bevp++;
166                         bevpn= bevp+1;
167                         if(bevpn>bevplast) {
168                                 if(cycl) bevpn= bevpfirst;
169                                 else bevpn= bevplast;
170                         }
171                 }
172                 
173                 fac1= *(fp)- *(fp-1);
174                 fac2= *(fp)-d;
175                 fac1= fac2/fac1;
176                 fac2= 1.0f-fac1;
177
178                 ft[0]= fac1*bevp->x+ fac2*(bevpn)->x;
179                 ft[1]= fac1*bevp->y+ fac2*(bevpn)->y;
180                 ft[2]= fac1*bevp->z+ fac2*(bevpn)->z;
181                 ft[3]= fac1*bevp->alfa+ fac2*(bevpn)->alfa;
182                 
183                 ft+= 4;
184
185         }
186         
187         MEM_freeN(dist);
188 }
189
190 int interval_test(int min, int max, int p1, int cycl)
191 {
192         
193         if(cycl) {
194                 if( p1 < min) 
195                         p1=  ((p1 -min) % (max-min+1)) + max+1;
196                 else if(p1 > max)
197                         p1=  ((p1 -min) % (max-min+1)) + min;
198         }
199         else {
200                 if(p1 < min) p1= min;
201                 else if(p1 > max) p1= max;
202         }
203         return p1;
204 }
205
206 /* warning, *vec needs FOUR items! */
207 /* ctime is normalized range <0-1> */
208 int where_on_path(Object *ob, float ctime, float *vec, float *dir)      /* returns OK */
209 {
210         Curve *cu;
211         Nurb *nu;
212         BevList *bl;
213         Path *path;
214         float *fp, *p0, *p1, *p2, *p3, fac;
215         float data[4];
216         int cycl=0, s0, s1, s2, s3;
217
218         if(ob==NULL || ob->type != OB_CURVE) return 0;
219         cu= ob->data;
220         if(cu->path==NULL || cu->path->data==NULL) {
221                 printf("no path!\n");
222         }
223         path= cu->path;
224         fp= path->data;
225         
226         /* test for cyclic */
227         bl= cu->bev.first;
228         if(bl && bl->poly> -1) cycl= 1;
229
230         ctime *= (path->len-1);
231         
232         s1= (int)floor(ctime);
233         fac= (float)(s1+1)-ctime;
234
235         /* path->len is corected for cyclic */
236         s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
237         s1= interval_test(0, path->len-1-cycl, s1, cycl);
238         s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
239         s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
240
241         p0= fp + 4*s0;
242         p1= fp + 4*s1;
243         p2= fp + 4*s2;
244         p3= fp + 4*s3;
245
246         /* note, commented out for follow constraint */
247         //if(cu->flag & CU_FOLLOW) {
248                 
249                 set_afgeleide_four_ipo(1.0f-fac, data, KEY_BSPLINE);
250                 
251                 dir[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
252                 dir[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
253                 dir[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
254                 
255                 /* make compatible with vectoquat */
256                 dir[0]= -dir[0];
257                 dir[1]= -dir[1];
258                 dir[2]= -dir[2];
259         //}
260         
261         nu= cu->nurb.first;
262
263         /* make sure that first and last frame are included in the vectors here  */
264         if((nu->type & 7)==CU_POLY) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
265         else if((nu->type & 7)==CU_BEZIER) set_four_ipo(1.0f-fac, data, KEY_LINEAR);
266         else if(s0==s1 || p2==p3) set_four_ipo(1.0f-fac, data, KEY_CARDINAL);
267         else set_four_ipo(1.0f-fac, data, KEY_BSPLINE);
268
269         vec[0]= data[0]*p0[0] + data[1]*p1[0] + data[2]*p2[0] + data[3]*p3[0] ;
270         vec[1]= data[0]*p0[1] + data[1]*p1[1] + data[2]*p2[1] + data[3]*p3[1] ;
271         vec[2]= data[0]*p0[2] + data[1]*p1[2] + data[2]*p2[2] + data[3]*p3[2] ;
272
273         vec[3]= data[0]*p0[3] + data[1]*p1[3] + data[2]*p2[3] + data[3]*p3[3] ;
274
275         return 1;
276 }
277
278 /* ****************** DUPLICATOR ************** */
279
280 static void new_dupli_object(ListBase *lb, Object *ob, float mat[][4], int lay, int index)
281 {
282         DupliObject *dob= MEM_mallocN(sizeof(DupliObject), "dupliobject");
283         BLI_addtail(lb, dob);
284         dob->ob= ob;
285         Mat4CpyMat4(dob->mat, mat);
286         Mat4CpyMat4(dob->omat, ob->obmat);
287         dob->origlay= ob->lay;
288         dob->index= index;
289         ob->lay= lay;
290 }
291
292 static void group_duplilist(ListBase *lb, Object *ob)
293 {
294         DupliObject *dob;
295         GroupObject *go;
296         float mat[4][4];
297         
298         if(ob->dup_group==NULL) return;
299         
300         /* handles animated groups, and */
301         /* we need to check update for objects that are not in scene... */
302         group_handle_recalc_and_update(ob, ob->dup_group);
303         
304         for(go= ob->dup_group->gobject.first; go; go= go->next) {
305                 if(go->ob!=ob) {
306                         Mat4MulMat4(mat, go->ob->obmat, ob->obmat);
307                         new_dupli_object(lb, go->ob, mat, ob->lay, 0);
308                 }
309         }
310         
311         /* make copy already, because in group dupli's deform displists can be makde, requiring parent matrices */
312         for(dob= lb->first; dob; dob= dob->next)
313                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
314 }
315
316 static void frames_duplilist(ListBase *lb, Object *ob)
317 {
318         extern int enable_cu_speed;     /* object.c */
319         Object copyob;
320         int cfrao, ok;
321         
322         cfrao= G.scene->r.cfra;
323         if(ob->parent==NULL && ob->track==NULL && ob->ipo==NULL && ob->constraints.first==NULL) return;
324
325         if(ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
326         copyob= *ob;    /* store transform info */
327
328         for(G.scene->r.cfra= ob->dupsta; G.scene->r.cfra<=ob->dupend; G.scene->r.cfra++) {
329
330                 ok= 1;
331                 if(ob->dupoff) {
332                         ok= G.scene->r.cfra - ob->dupsta;
333                         ok= ok % (ob->dupon+ob->dupoff);
334                         if(ok < ob->dupon) ok= 1;
335                         else ok= 0;
336                 }
337                 if(ok) {
338                         do_ob_ipo(ob);
339                         where_is_object_time(ob, (float)G.scene->r.cfra);
340                         new_dupli_object(lb, ob, ob->obmat, ob->lay, G.scene->r.cfra);
341                 }
342         }
343
344         *ob= copyob;    /* restore transform info */
345         G.scene->r.cfra= cfrao;
346         enable_cu_speed= 1;
347 }
348
349 struct vertexDupliData {
350         ListBase *lb;
351         float pmat[4][4];
352         Object *ob, *par;
353 };
354
355 static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
356 {
357         struct vertexDupliData *vdd= userData;
358         float vec[3], *q2, mat[3][3], tmat[4][4], obmat[4][4];
359         
360         VECCOPY(vec, co);
361         Mat4MulVecfl(vdd->pmat, vec);
362         VecSubf(vec, vec, vdd->pmat[3]);
363         VecAddf(vec, vec, vdd->ob->obmat[3]);
364         
365         Mat4CpyMat4(obmat, vdd->ob->obmat);
366         VECCOPY(obmat[3], vec);
367         
368         if(vdd->par->transflag & OB_DUPLIROT) {
369                 
370                 vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
371                 
372                 q2= vectoquat(vec, vdd->ob->trackflag, vdd->ob->upflag);
373                 
374                 QuatToMat3(q2, mat);
375                 Mat4CpyMat4(tmat, obmat);
376                 Mat4MulMat43(obmat, tmat, mat);
377         }
378         new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index);
379 }
380
381 static void vertex_duplilist(ListBase *lb, Scene *sce, Object *par)
382 {
383         Object *ob;
384         Base *base;
385         float vec[3], no[3], pmat[4][4];
386         int lay, totvert, a;
387         int dmNeedsFree;
388         DerivedMesh *dm;
389         
390         Mat4CpyMat4(pmat, par->obmat);
391         
392         lay= G.scene->lay;
393         
394         if(par==G.obedit)
395                 dm= editmesh_get_derived_cage(&dmNeedsFree);
396         else
397                 dm = mesh_get_derived_deform(par, &dmNeedsFree);
398         
399         totvert = dm->getNumVerts(dm);
400
401         base= sce->base.first;
402         while(base) {
403
404                 if(base->object->type>0 && (lay & base->lay) && G.obedit!=base->object) {
405                         ob= base->object->parent;
406                         while(ob) {
407                                 if(ob==par) {
408                                         struct vertexDupliData vdd;
409                                         
410                                         ob= base->object;
411                                         vdd.lb= lb;
412                                         vdd.ob= ob;
413                                         vdd.par= par;
414                                         Mat4CpyMat4(vdd.pmat, pmat);
415                                         
416                                         /* mballs have a different dupli handling */
417                                         if(ob->type!=OB_MBALL) ob->flag |= OB_DONE;     /* doesnt render */
418
419                                         if(par==G.obedit) {
420                                                 dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
421                                         }
422                                         else {
423                                                 for(a=0; a<totvert; a++) {
424                                                         dm->getVertCo(dm, a, vec);
425                                                         dm->getVertNo(dm, a, no);
426                                                         
427                                                         vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
428                                                 }
429                                         }
430                                         
431                                         break;
432                                 }
433                                 ob= ob->parent;
434                         }
435                 }
436                 base= base->next;
437         }
438
439         if (dmNeedsFree)
440                 dm->release(dm);
441 }
442
443 static void particle_duplilist(ListBase *lb, Scene *sce, Object *par, PartEff *paf)
444 {
445         Object *ob, copyob;
446         Base *base;
447         Particle *pa;
448         float ctime, vec1[3];
449         float vec[3], tmat[4][4], mat[3][3];
450         float *q2;
451         int lay, a, counter;    /* counter is used to find in render the indexed object */
452         
453         pa= paf->keys;
454         if(pa==0) {
455                 build_particle_system(par);
456                 pa= paf->keys;
457                 if(pa==0) return;
458         }
459         
460         ctime= bsystem_time(par, 0, (float)G.scene->r.cfra, 0.0);
461         
462         lay= G.scene->lay;
463
464         for(base= sce->base.first; base; base= base->next) {
465                 if(base->object->type>0 && (base->lay & lay) && G.obedit!=base->object) {
466                         ob= base->object->parent;
467                         while(ob) {
468                                 if(ob==par) {
469                                 
470                                         ob= base->object;
471                                         /* temp copy, to have ipos etc to work OK */
472                                         copyob= *ob;
473                                         
474                                         /* don't want parent animation to apply on past object positions */
475                                         if(!(paf->flag & PAF_STATIC))
476                                                 ob->parent= NULL;
477                                         
478                                         for(a=0, pa= paf->keys, counter=0; a<paf->totpart; a++, pa+=paf->totkey, counter++) {
479                                                 
480                                                 if(paf->flag & PAF_STATIC) {
481                                                         float mtime;
482                                                         
483                                                         where_is_particle(paf, pa, pa->time, vec1);
484                                                         mtime= pa->time+pa->lifetime;
485                                                         
486                                                         for(ctime= pa->time; ctime<mtime; ctime+=paf->staticstep, counter++) {
487                                                                 
488                                                                 /* make sure hair grows until the end.. */ 
489                                                                 if(ctime>pa->time+pa->lifetime) ctime= pa->time+pa->lifetime;
490                                                                 
491                                                                 /* to give ipos in object correct offset */
492                                                                 where_is_object_time(ob, ctime-pa->time);
493
494                                                                 where_is_particle(paf, pa, ctime, vec); // makes sure there's always a vec
495                                                                 Mat4MulVecfl(par->obmat, vec);
496                                                                 
497                                                                 if(paf->stype==PAF_VECT) {
498                                                                         where_is_particle(paf, pa, ctime+1.0, vec1); // makes sure there's always a vec
499                                                                         Mat4MulVecfl(par->obmat, vec1);
500
501                                                                         VecSubf(vec1, vec1, vec);
502                                                                         q2= vectoquat(vec1, ob->trackflag, ob->upflag);
503                                                                         
504                                                                         QuatToMat3(q2, mat);
505                                                                         Mat4CpyMat4(tmat, ob->obmat);
506                                                                         Mat4MulMat43(ob->obmat, tmat, mat);
507                                                                 }
508                                                                 
509                                                                 VECCOPY(ob->obmat[3], vec);
510                                                                 /* put object back in original state, so it cam be restored OK */
511                                                                 Mat4CpyMat4(tmat, ob->obmat);
512                                                                 Mat4CpyMat4(ob->obmat, copyob.obmat);
513                                                                 new_dupli_object(lb, ob, tmat, par->lay, counter);
514                                                         }
515                                                 }
516                                                 else { // non static particles
517                                                         
518                                                         if((paf->flag & PAF_UNBORN)==0 && ctime < pa->time) continue;
519                                                         if((paf->flag & PAF_DIED)==0 && ctime > pa->time+pa->lifetime) continue;
520
521                                                         //if(ctime < pa->time+pa->lifetime) {
522
523                                                         /* to give ipos in object correct offset, ob->parent is NULLed */
524                                                         where_is_object_time(ob, ctime-pa->time);
525                                                         
526                                                         where_is_particle(paf, pa, ctime, vec);
527                                                         if(paf->stype==PAF_VECT) {
528                                                                 
529                                                                 /* if particle died, we use previous position */
530                                                                 if(ctime > pa->time+pa->lifetime) {
531                                                                         where_is_particle(paf, pa, pa->time+pa->lifetime-1.0f, vec1);
532                                                                         VecSubf(vec1, vec, vec1);
533                                                                 }
534                                                                 else {
535                                                                         where_is_particle(paf, pa, ctime+1.0f, vec1);
536                                                                         VecSubf(vec1, vec1, vec);
537                                                                 }
538                                                                 q2= vectoquat(vec1, ob->trackflag, ob->upflag);
539                                         
540                                                                 QuatToMat3(q2, mat);
541                                                                 Mat4CpyMat4(tmat, ob->obmat);
542                                                                 Mat4MulMat43(ob->obmat, tmat, mat);
543                                                         }
544
545                                                         VECCOPY(ob->obmat[3], vec);
546                                                         
547                                                         /* put object back in original state, so it can be restored OK */
548                                                         Mat4CpyMat4(tmat, ob->obmat);
549                                                         Mat4CpyMat4(ob->obmat, copyob.obmat);
550                                                         new_dupli_object(lb, ob, tmat, par->lay, counter);
551                                                 }                                       
552                                         }
553                                         /* temp copy, to have ipos etc to work OK */
554                                         *ob= copyob;
555                                         
556                                         break;
557                                 }
558                                 ob= ob->parent;
559                         }
560                 }
561         }
562 }
563
564 static Object *find_family_object(Object **obar, char *family, char ch)
565 {
566         Object *ob;
567         int flen;
568         
569         if( obar[ch] ) return obar[ch];
570         
571         flen= strlen(family);
572         
573         ob= G.main->object.first;
574         while(ob) {
575                 if( ob->id.name[flen+2]==ch ) {
576                         if( strncmp(ob->id.name+2, family, flen)==0 ) break;
577                 }
578                 ob= ob->id.next;
579         }
580         
581         obar[ch]= ob;
582         
583         return ob;
584 }
585
586
587 static void font_duplilist(ListBase *lb, Object *par)
588 {
589         Object *ob, *obar[256];
590         Curve *cu;
591         struct chartrans *ct, *chartransdata;
592         float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
593         int slen, a;
594         
595         Mat4CpyMat4(pmat, par->obmat);
596         
597         /* in par the family name is stored, use this to find the other objects */
598         
599         chartransdata= text_to_curve(par, FO_DUPLI);
600         if(chartransdata==0) return;
601         
602         memset(obar, 0, 256*sizeof(void *));
603         
604         cu= par->data;
605         slen= strlen(cu->str);
606         fsize= cu->fsize;
607         xof= cu->xof;
608         yof= cu->yof;
609         
610         ct= chartransdata;
611         
612         for(a=0; a<slen; a++, ct++) {
613                 
614                 ob= find_family_object(obar, cu->family, cu->str[a]);
615                 if(ob) {
616                         vec[0]= fsize*(ct->xof - xof);
617                         vec[1]= fsize*(ct->yof - yof);
618                         vec[2]= 0.0;
619                         
620                         Mat4MulVecfl(pmat, vec);
621                         
622                         Mat4CpyMat4(obmat, par->obmat);
623                         VECCOPY(obmat[3], vec);
624                         
625                         new_dupli_object(lb, ob, obmat, par->lay, a);
626                 }
627                 
628         }
629         
630         MEM_freeN(chartransdata);
631 }
632
633 /* ***************************** */
634
635 /* note; group dupli's already set transform matrix. see note in group_duplilist() */
636 ListBase *object_duplilist(Scene *sce, Object *ob)
637 {
638         static ListBase duplilist={NULL, NULL};
639         
640         if(duplilist.first) {
641                 printf("wrong call to object_duplilist\n");
642                 return &duplilist;
643         }
644         duplilist.first= duplilist.last= NULL;
645         
646         if(ob->transflag & OB_DUPLI) {
647                 if(ob->transflag & OB_DUPLIVERTS) {
648                         if(ob->type==OB_MESH) {
649                                 if(ob->transflag & OB_DUPLIVERTS) {
650                                         PartEff *paf;
651                                         if( (paf=give_parteff(ob)) ) particle_duplilist(&duplilist, sce, ob, paf);
652                                         else vertex_duplilist(&duplilist, sce, ob);
653                                 }
654                         }
655                         else if(ob->type==OB_FONT) {
656                                 font_duplilist(&duplilist, ob);
657                         }
658                 }
659                 else if(ob->transflag & OB_DUPLIFRAMES) 
660                         frames_duplilist(&duplilist, ob);
661                 else if(ob->transflag & OB_DUPLIGROUP)
662                         group_duplilist(&duplilist, ob);
663         }
664         
665         return &duplilist;
666 }
667
668 void free_object_duplilist(ListBase *lb)
669 {
670         DupliObject *dob;
671         
672         for(dob= lb->first; dob; dob= dob->next) {
673                 dob->ob->lay= dob->origlay;
674                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
675         }
676         
677         BLI_freelistN(lb);
678 }
679
680 int count_duplilist(Object *ob)
681 {
682         if(ob->transflag & OB_DUPLI) {
683                 if(ob->transflag & OB_DUPLIVERTS) {
684                         if(ob->type==OB_MESH) {
685                                 if(ob->transflag & OB_DUPLIVERTS) {
686                                         PartEff *paf;
687                                         if( (paf=give_parteff(ob)) ) {
688                                                 return paf->totpart;
689                                         }
690                                         else {
691                                                 Mesh *me= ob->data;
692                                                 return me->totvert;
693                                         }
694                                 }
695                         }
696                 }
697                 else if(ob->transflag & OB_DUPLIFRAMES) {
698                         int tot= ob->dupend - ob->dupsta; 
699                         tot/= (ob->dupon+ob->dupoff);
700                         return tot*ob->dupon;
701                 }
702         }
703         return 1;
704 }