6 * ***** BEGIN GPL LICENSE BLOCK *****
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.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
32 /** \file blender/blenkernel/intern/anim.c
41 #include "MEM_guardedalloc.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_editVert.h"
47 #include "BLI_utildefines.h"
49 #include "DNA_anim_types.h"
50 #include "DNA_armature_types.h"
51 #include "DNA_group_types.h"
52 #include "DNA_key_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_vfont_types.h"
57 #include "BKE_animsys.h"
58 #include "BKE_curve.h"
59 #include "BKE_DerivedMesh.h"
60 #include "BKE_depsgraph.h"
62 #include "BKE_group.h"
63 #include "BKE_global.h"
65 #include "BKE_lattice.h"
68 #include "BKE_object.h"
69 #include "BKE_particle.h"
70 #include "BKE_scene.h"
71 #include "BKE_utildefines.h"
72 #include "BKE_depsgraph.h"
76 // XXX bad level call...
78 /* --------------------- */
79 /* forward declarations */
81 static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated);
83 /* ******************************************************************** */
84 /* Animation Visualisation */
86 /* Initialise the default settings for animation visualisation */
87 void animviz_settings_init(bAnimVizSettings *avs)
93 /* ghosting settings */
94 avs->ghost_bc= avs->ghost_ac= 10;
96 avs->ghost_sf= 1; // xxx - take from scene instead?
97 avs->ghost_ef= 250; // xxx - take from scene instead?
103 avs->path_bc= avs->path_ac= 10;
105 avs->path_sf= 1; // xxx - take from scene instead?
106 avs->path_ef= 250; // xxx - take from scene instead?
108 avs->path_viewflag= (MOTIONPATH_VIEW_KFRAS|MOTIONPATH_VIEW_KFNOS);
113 /* ------------------- */
115 /* Free the given motion path's cache */
116 void animviz_free_motionpath_cache(bMotionPath *mpath)
122 /* free the path if necessary */
124 MEM_freeN(mpath->points);
126 /* reset the relevant parameters */
131 /* Free the given motion path instance and its data
132 * NOTE: this frees the motion path given!
134 void animviz_free_motionpath(bMotionPath *mpath)
140 /* free the cache first */
141 animviz_free_motionpath_cache(mpath);
143 /* now the instance itself */
147 /* ------------------- */
149 /* Setup motion paths for the given data
150 * - scene: current scene (for frame ranges, etc.)
151 * - ob: object to add paths for (must be provided)
152 * - pchan: posechannel to add paths for (optional; if not provided, object-paths are assumed)
154 bMotionPath *animviz_verify_motionpaths(Scene *scene, Object *ob, bPoseChannel *pchan)
156 bAnimVizSettings *avs;
157 bMotionPath *mpath, **dst;
160 if (ELEM(NULL, scene, ob))
163 /* get destination data */
165 /* paths for posechannel - assume that posechannel belongs to the object */
170 /* paths for object */
175 /* avoid 0 size allocs */
176 if(avs->path_sf >= avs->path_ef) {
180 /* if there is already a motionpath, just return that,
181 * but provided it's settings are ok
186 /* if range is not invalid, and/or length is set ok, just return */
187 if ((mpath->start_frame != mpath->end_frame) && (mpath->length > 0))
191 /* create a new motionpath, and assign it */
192 mpath= MEM_callocN(sizeof(bMotionPath), "bMotionPath");
196 /* set settings from the viz settings */
197 mpath->start_frame= avs->path_sf;
198 mpath->end_frame= avs->path_ef;
200 mpath->length= mpath->end_frame - mpath->start_frame;
202 if (avs->path_bakeflag & MOTIONPATH_BAKE_HEADS)
203 mpath->flag |= MOTIONPATH_FLAG_BHEAD;
205 mpath->flag &= ~MOTIONPATH_FLAG_BHEAD;
207 /* allocate a cache */
208 mpath->points= MEM_callocN(sizeof(bMotionPathVert)*mpath->length, "bMotionPathVerts");
210 /* tag viz settings as currently having some path(s) which use it */
211 avs->path_bakeflag |= MOTIONPATH_BAKE_HAS_PATHS;
217 /* ------------------- */
219 /* Motion path needing to be baked (mpt) */
220 typedef struct MPathTarget {
221 struct MPathTarget *next, *prev;
223 bMotionPath *mpath; /* motion path in question */
225 Object *ob; /* source object */
226 bPoseChannel *pchan; /* source posechannel (if applicable) */
231 /* get list of motion paths to be baked for the given object
232 * - assumes the given list is ready to be used
234 void animviz_get_object_motionpaths(Object *ob, ListBase *targets)
238 /* object itself first */
239 if ((ob->avs.recalc & ANIMVIZ_RECALC_PATHS) && (ob->mpath)) {
240 /* new target for object */
241 mpt= MEM_callocN(sizeof(MPathTarget), "MPathTarget Ob");
242 BLI_addtail(targets, mpt);
244 mpt->mpath= ob->mpath;
249 if ((ob->pose) && (ob->pose->avs.recalc & ANIMVIZ_RECALC_PATHS)) {
250 bArmature *arm= ob->data;
253 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
254 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && (pchan->mpath)) {
255 /* new target for bone */
256 mpt= MEM_callocN(sizeof(MPathTarget), "MPathTarget PoseBone");
257 BLI_addtail(targets, mpt);
259 mpt->mpath= pchan->mpath;
269 /* Note on evaluation optimisations:
270 * Optimisations currently used here play tricks with the depsgraph in order to try and
271 * evaluate as few objects as strictly necessary to get nicer performance under standard
272 * production conditions. For those people who really need the accurate version,
273 * disable the ifdef (i.e. 1 -> 0) and comment out the call to motionpaths_calc_optimise_depsgraph()
276 /* tweak the object ordering to trick depsgraph into making MotionPath calculations run faster */
277 static void motionpaths_calc_optimise_depsgraph(Scene *scene, ListBase *targets)
279 Base *base, *baseNext;
282 /* make sure our temp-tag isn't already in use */
283 for (base= scene->base.first; base; base= base->next)
284 base->object->flag &= ~BA_TEMP_TAG;
286 /* for each target, dump its object to the start of the list if it wasn't moved already */
287 for (mpt= targets->first; mpt; mpt= mpt->next) {
288 for (base=scene->base.first; base; base=baseNext) {
289 baseNext = base->next;
291 if ((base->object == mpt->ob) && !(mpt->ob->flag & BA_TEMP_TAG)) {
292 BLI_remlink(&scene->base, base);
293 BLI_addhead(&scene->base, base);
295 mpt->ob->flag |= BA_TEMP_TAG;
296 break; // we really don't need to continue anymore once this happens, but this line might really 'break'
301 /* "brew me a list that's sorted a bit faster now depsy" */
302 DAG_scene_sort(G.main, scene);
305 /* update scene for current frame */
306 static void motionpaths_calc_update_scene(Scene *scene)
308 #if 1 // 'production' optimisations always on
309 Base *base, *last=NULL;
311 /* only stuff that moves or needs display still */
312 DAG_scene_update_flags(G.main, scene, scene->lay, TRUE);
314 /* find the last object with the tag
315 * - all those afterwards are assumed to not be relevant for our calculations
317 // optimise further by moving out...
318 for (base=scene->base.first; base; base=base->next) {
319 if (base->object->flag & BA_TEMP_TAG)
323 /* perform updates for tagged objects */
324 // XXX: this will break if rigs depend on scene or other data that
325 // is animated but not attached to/updatable from objects
326 for (base=scene->base.first; base; base=base->next) {
327 /* update this object */
328 object_handle_update(scene, base->object);
330 /* if this is the last one we need to update, let's stop to save some time */
334 #else // original, 'always correct' version
336 * - if this is too slow, resort to using a more efficient way
337 * that doesn't force complete update, but for now, this is the
340 scene_update_for_newframe(G.main, scene, scene->lay); // XXX this is the best way we can get anything moving
346 /* perform baking for the targets on the current frame */
347 static void motionpaths_calc_bake_targets(Scene *scene, ListBase *targets)
351 /* for each target, check if it can be baked on the current frame */
352 for (mpt= targets->first; mpt; mpt= mpt->next) {
353 bMotionPath *mpath= mpt->mpath;
354 bMotionPathVert *mpv;
356 /* current frame must be within the range the cache works for
357 * - is inclusive of the first frame, but not the last otherwise we get buffer overruns
359 if ((CFRA < mpath->start_frame) || (CFRA >= mpath->end_frame))
362 /* get the relevant cache vert to write to */
363 mpv= mpath->points + (CFRA - mpath->start_frame);
365 /* pose-channel or object path baking? */
368 if (mpath->flag & MOTIONPATH_FLAG_BHEAD) {
369 VECCOPY(mpv->co, mpt->pchan->pose_head);
372 VECCOPY(mpv->co, mpt->pchan->pose_tail);
375 /* result must be in worldspace */
376 mul_m4_v3(mpt->ob->obmat, mpv->co);
379 /* worldspace object location */
380 VECCOPY(mpv->co, mpt->ob->obmat[3]);
385 /* Perform baking of the given object's and/or its bones' transforms to motion paths
386 * - scene: current scene
387 * - ob: object whose flagged motionpaths should get calculated
388 * - recalc: whether we need to
390 // TODO: include reports pointer?
391 void animviz_calc_motionpaths(Scene *scene, ListBase *targets)
398 if (ELEM(NULL, targets, targets->first))
401 /* set frame values */
405 // TODO: this method could be improved...
406 // 1) max range for standard baking
407 // 2) minimum range for recalc baking (i.e. between keyframes, but how?)
408 for (mpt= targets->first; mpt; mpt= mpt->next) {
409 /* try to increase area to do (only as much as needed) */
410 sfra= MIN2(sfra, mpt->mpath->start_frame);
411 efra= MAX2(efra, mpt->mpath->end_frame);
413 if (efra <= sfra) return;
415 /* optimise the depsgraph for faster updates */
416 // TODO: whether this is used should depend on some setting for the level of optimisations used
417 motionpaths_calc_optimise_depsgraph(scene, targets);
419 /* calculate path over requested range */
420 for (CFRA=sfra; CFRA<=efra; CFRA++) {
421 /* update relevant data for new frame */
422 motionpaths_calc_update_scene(scene);
424 /* perform baking for targets */
425 motionpaths_calc_bake_targets(scene, targets);
428 /* reset original environment */
430 motionpaths_calc_update_scene(scene);
432 /* clear recalc flags from targets */
433 for (mpt= targets->first; mpt; mpt= mpt->next) {
434 bAnimVizSettings *avs;
436 /* get pointer to animviz settings for each target */
438 avs= &mpt->ob->pose->avs;
442 /* clear the flag requesting recalculation of targets */
443 avs->recalc &= ~ANIMVIZ_RECALC_PATHS;
447 /* ******************************************************************** */
448 /* Curve Paths - for curve deforms and/or curve following */
450 /* free curve path data
451 * NOTE: frees the path itself!
452 * NOTE: this is increasingly innacurate with non-uniform BevPoint subdivisions [#24633]
454 void free_path(Path *path)
456 if(path->data) MEM_freeN(path->data);
460 /* calculate a curve-deform path for a curve
461 * - only called from displist.c -> do_makeDispListCurveTypes
463 void calc_curvepath(Object *ob)
466 BevPoint *bevp, *bevpn, *bevpfirst, *bevplast;
471 float *fp, *dist, *maxdist, xyz[3];
472 float fac, d=0, fac1, fac2;
476 /* in a path vertices are with equal differences: path->len = number of verts */
477 /* NOW WITH BEVELCURVE!!! */
479 if(ob==NULL || ob->type != OB_CURVE) return;
482 nurbs= BKE_curve_nurbs(cu);
485 if(cu->path) free_path(cu->path);
489 if(bl==NULL || !bl->nr) return;
491 cu->path=path= MEM_callocN(sizeof(Path), "calc_curvepath");
493 /* if POLY: last vertice != first vertice */
494 cycl= (bl->poly!= -1);
496 if(cycl) tot= bl->nr;
500 /* exception: vector handle paths and polygon paths should be subdivided at least a factor resolu */
501 if(path->len<nu->resolu*SEGMENTSU(nu)) path->len= nu->resolu*SEGMENTSU(nu);
503 dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
505 /* all lengths in *dist */
506 bevp= bevpfirst= (BevPoint *)(bl+1);
509 for(a=0; a<tot; a++) {
512 sub_v3_v3v3(xyz, bevpfirst->vec, bevp->vec);
514 sub_v3_v3v3(xyz, (bevp+1)->vec, bevp->vec);
516 *fp= *(fp-1)+len_v3(xyz);
522 /* the path verts in path->data */
523 /* now also with TILT value */
524 pp= path->data = (PathPoint *)MEM_callocN(sizeof(PathPoint)*path->len, "pathdata");
528 bevplast= bevpfirst + (bl->nr-1);
531 fac= 1.0f/((float)path->len-1.0f);
532 fac = fac * path->totdist;
534 for(a=0; a<path->len; a++) {
538 /* we're looking for location (distance) 'd' in the array */
539 while((d>= *fp) && fp<maxdist) {
541 if(bevp<bevplast) bevp++;
544 if(cycl) bevpn= bevpfirst;
545 else bevpn= bevplast;
549 fac1= *(fp)- *(fp-1);
554 interp_v3_v3v3(pp->vec, bevp->vec, bevpn->vec, fac2);
555 pp->vec[3]= fac1*bevp->alfa + fac2*bevpn->alfa;
556 pp->radius= fac1*bevp->radius + fac2*bevpn->radius;
557 pp->weight= fac1*bevp->weight + fac2*bevpn->weight;
558 interp_qt_qtqt(pp->quat, bevp->quat, bevpn->quat, fac2);
559 normalize_qt(pp->quat);
568 /* is this only used internally?*/
569 int interval_test(int min, int max, int p1, int cycl)
573 p1= ((p1 -min) % (max-min+1)) + max+1;
575 p1= ((p1 -min) % (max-min+1)) + min;
578 if(p1 < min) p1= min;
579 else if(p1 > max) p1= max;
585 /* calculate the deformation implied by the curve path at a given parametric position, and returns whether this operation succeeded
586 * - *vec needs FOUR items!
587 * - ctime is normalized range <0-1>
589 int where_on_path(Object *ob, float ctime, float *vec, float *dir, float *quat, float *radius, float *weight) /* returns OK */
595 PathPoint *pp, *p0, *p1, *p2, *p3;
598 int cycl=0, s0, s1, s2, s3;
600 if(ob==NULL || ob->type != OB_CURVE) return 0;
602 if(cu->path==NULL || cu->path->data==NULL) {
603 printf("no path!\n");
609 /* test for cyclic */
612 if (!bl->nr) return 0;
613 if(bl->poly> -1) cycl= 1;
615 ctime *= (path->len-1);
617 s1= (int)floor(ctime);
618 fac= (float)(s1+1)-ctime;
620 /* path->len is corected for cyclic */
621 s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
622 s1= interval_test(0, path->len-1-cycl, s1, cycl);
623 s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
624 s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
631 /* note, commented out for follow constraint */
632 //if(cu->flag & CU_FOLLOW) {
634 key_curve_tangent_weights(1.0f-fac, data, KEY_BSPLINE);
636 interp_v3_v3v3v3v3(dir, p0->vec, p1->vec, p2->vec, p3->vec, data);
638 /* make compatible with vectoquat */
644 /* make sure that first and last frame are included in the vectors here */
645 if(nu->type == CU_POLY) key_curve_position_weights(1.0f-fac, data, KEY_LINEAR);
646 else if(nu->type == CU_BEZIER) key_curve_position_weights(1.0f-fac, data, KEY_LINEAR);
647 else if(s0==s1 || p2==p3) key_curve_position_weights(1.0f-fac, data, KEY_CARDINAL);
648 else key_curve_position_weights(1.0f-fac, data, KEY_BSPLINE);
650 vec[0]= data[0]*p0->vec[0] + data[1]*p1->vec[0] + data[2]*p2->vec[0] + data[3]*p3->vec[0] ; /* X */
651 vec[1]= data[0]*p0->vec[1] + data[1]*p1->vec[1] + data[2]*p2->vec[1] + data[3]*p3->vec[1] ; /* Y */
652 vec[2]= data[0]*p0->vec[2] + data[1]*p1->vec[2] + data[2]*p2->vec[2] + data[3]*p3->vec[2] ; /* Z */
653 vec[3]= data[0]*p0->vec[3] + data[1]*p1->vec[3] + data[2]*p2->vec[3] + data[3]*p3->vec[3] ; /* Tilt, should not be needed since we have quat still used */
656 float totfac, q1[4], q2[4];
658 totfac= data[0]+data[3];
659 if(totfac>FLT_EPSILON) interp_qt_qtqt(q1, p0->quat, p3->quat, data[3] / totfac);
660 else QUATCOPY(q1, p1->quat);
662 totfac= data[1]+data[2];
663 if(totfac>FLT_EPSILON) interp_qt_qtqt(q2, p1->quat, p2->quat, data[2] / totfac);
664 else QUATCOPY(q2, p3->quat);
666 totfac = data[0]+data[1]+data[2]+data[3];
667 if(totfac>FLT_EPSILON) interp_qt_qtqt(quat, q1, q2, (data[1]+data[2]) / totfac);
668 else QUATCOPY(quat, q2);
672 *radius= data[0]*p0->radius + data[1]*p1->radius + data[2]*p2->radius + data[3]*p3->radius;
675 *weight= data[0]*p0->weight + data[1]*p1->weight + data[2]*p2->weight + data[3]*p3->weight;
680 /* ******************************************************************** */
683 static DupliObject *new_dupli_object(ListBase *lb, Object *ob, float mat[][4], int lay, int index, int type, int animated)
685 DupliObject *dob= MEM_callocN(sizeof(DupliObject), "dupliobject");
687 BLI_addtail(lb, dob);
689 copy_m4_m4(dob->mat, mat);
690 copy_m4_m4(dob->omat, ob->obmat);
691 dob->origlay= ob->lay;
694 dob->animated= (type == OB_DUPLIGROUP) && animated;
700 static void group_duplilist(ListBase *lb, Scene *scene, Object *ob, int level, int animated)
705 float mat[4][4], tmat[4][4];
707 if(ob->dup_group==NULL) return;
708 group= ob->dup_group;
710 /* simple preventing of too deep nested groups */
711 if(level>MAX_DUPLI_RECUR) return;
713 /* handles animated groups, and */
714 /* we need to check update for objects that are not in scene... */
715 group_handle_recalc_and_update(scene, ob, group);
716 animated= animated || group_is_animated(ob, group);
718 for(go= group->gobject.first; go; go= go->next) {
719 /* note, if you check on layer here, render goes wrong... it still deforms verts and uses parent imat */
722 /* Group Dupli Offset, should apply after everything else */
723 if (group->dupli_ofs[0] || group->dupli_ofs[1] || group->dupli_ofs[2]) {
724 copy_m4_m4(tmat, go->ob->obmat);
725 sub_v3_v3v3(tmat[3], tmat[3], group->dupli_ofs);
726 mul_m4_m4m4(mat, tmat, ob->obmat);
728 mul_m4_m4m4(mat, go->ob->obmat, ob->obmat);
731 dob= new_dupli_object(lb, go->ob, mat, ob->lay, 0, OB_DUPLIGROUP, animated);
733 /* check the group instance and object layers match, also that the object visible flags are ok. */
734 if( (dob->origlay & group->layer)==0 ||
735 (G.rendering==0 && dob->ob->restrictflag & OB_RESTRICT_VIEW) ||
736 (G.rendering && dob->ob->restrictflag & OB_RESTRICT_RENDER)
744 if(go->ob->transflag & OB_DUPLI) {
745 copy_m4_m4(dob->ob->obmat, dob->mat);
746 object_duplilist_recursive(&group->id, scene, go->ob, lb, ob->obmat, level+1, animated);
747 copy_m4_m4(dob->ob->obmat, dob->omat);
753 static void frames_duplilist(ListBase *lb, Scene *scene, Object *ob, int level, int animated)
755 extern int enable_cu_speed; /* object.c */
756 Object copyob = {{NULL}};
757 int cfrao = scene->r.cfra;
759 /* simple prevention of too deep nested groups */
760 if (level > MAX_DUPLI_RECUR) return;
762 /* if we don't have any data/settings which will lead to object movement,
763 * don't waste time trying, as it will all look the same...
765 if (ob->parent==NULL && ob->constraints.first==NULL && ob->adt==NULL)
768 /* make a copy of the object's original data (before any dupli-data overwrites it)
769 * as we'll need this to keep track of unkeyed data
770 * - this doesn't take into account other data that can be reached from the object,
771 * for example it's shapekeys or bones, hence the need for an update flush at the end
775 /* duplicate over the required range */
776 if (ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
778 for (scene->r.cfra= ob->dupsta; scene->r.cfra<=ob->dupend; scene->r.cfra++) {
781 /* - dupoff = how often a frames within the range shouldn't be made into duplis
782 * - dupon = the length of each "skipping" block in frames
785 ok= scene->r.cfra - ob->dupsta;
786 ok= ok % (ob->dupon+ob->dupoff);
787 ok= (ok < ob->dupon);
793 /* WARNING: doing animation updates in this way is not terribly accurate, as the dependencies
794 * and/or other objects which may affect this object's transforms are not updated either.
795 * However, this has always been the way that this worked (i.e. pre 2.5), so I guess that it'll be fine!
797 BKE_animsys_evaluate_animdata(&ob->id, ob->adt, (float)scene->r.cfra, ADT_RECALC_ANIM); /* ob-eval will do drivers, so we don't need to do them */
798 where_is_object_time(scene, ob, (float)scene->r.cfra);
800 dob= new_dupli_object(lb, ob, ob->obmat, ob->lay, scene->r.cfra, OB_DUPLIFRAMES, animated);
801 copy_m4_m4(dob->omat, copyob.obmat);
807 /* reset frame to original frame, then re-evaluate animation as above
808 * as 2.5 animation data may have far-reaching consequences
810 scene->r.cfra= cfrao;
812 BKE_animsys_evaluate_animdata(&ob->id, ob->adt, (float)scene->r.cfra, ADT_RECALC_ANIM); /* ob-eval will do drivers, so we don't need to do them */
813 where_is_object_time(scene, ob, (float)scene->r.cfra);
815 /* but, to make sure unkeyed object transforms are still sane,
816 * let's copy object's original data back over
821 typedef struct vertexDupliData {
822 ID *id; /* scene or group, for recursive loops */
827 float obmat[4][4]; /* Only used for dupliverts inside dupligroups, where the ob->obmat is modified */
835 static void vertex_dupli__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
838 vertexDupliData *vdd= userData;
839 float vec[3], q2[4], mat[3][3], tmat[4][4], obmat[4][4];
842 mul_v3_m4v3(vec, vdd->pmat, co);
843 sub_v3_v3(vec, vdd->pmat[3]);
844 add_v3_v3(vec, vdd->obmat[3]);
846 copy_m4_m4(obmat, vdd->obmat);
847 VECCOPY(obmat[3], vec);
849 if(vdd->par->transflag & OB_DUPLIROT) {
851 vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
854 vec[0]= -no_s[0]; vec[1]= -no_s[1]; vec[2]= -no_s[2];
857 vec_to_quat( q2,vec, vdd->ob->trackflag, vdd->ob->upflag);
859 quat_to_mat3( mat,q2);
860 copy_m4_m4(tmat, obmat);
861 mul_m4_m4m3(obmat, tmat, mat);
864 origlay = vdd->ob->lay;
866 dob= new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index, OB_DUPLIVERTS, vdd->animated);
868 /* restore the original layer so that each dupli will have proper dob->origlay */
869 vdd->ob->lay = origlay;
872 VECCOPY(dob->orco, vdd->orco[index]);
874 if(vdd->ob->transflag & OB_DUPLI) {
876 copy_m4_m4(tmpmat, vdd->ob->obmat);
877 copy_m4_m4(vdd->ob->obmat, obmat); /* pretend we are really this mat */
878 object_duplilist_recursive((ID *)vdd->id, vdd->scene, vdd->ob, vdd->lb, obmat, vdd->level+1, vdd->animated);
879 copy_m4_m4(vdd->ob->obmat, tmpmat);
883 static void vertex_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int level, int animated)
885 Object *ob, *ob_iter;
892 GroupObject * go = NULL;
894 float vec[3], no[3], pmat[4][4];
895 int totvert, a, oblay;
898 copy_m4_m4(pmat, par->obmat);
900 /* simple preventing of too deep nested groups */
901 if(level>MAX_DUPLI_RECUR) return;
903 em = BKE_mesh_get_editmesh(me);
906 dm= editmesh_get_derived_cage(scene, par, em, CD_MASK_BAREMESH);
907 BKE_mesh_end_editmesh(me, em);
909 dm= mesh_get_derived_deform(scene, par, CD_MASK_BAREMESH);
912 vdd.orco= (float(*)[3])get_mesh_orco_verts(par);
913 transform_mesh_orco_verts(me, vdd.orco, me->totvert, 0);
918 totvert = dm->getNumVerts(dm);
920 /* having to loop on scene OR group objects is NOT FUN */
921 if (GS(id->name) == ID_SCE) {
924 base= sce->base.first;
928 go = group->gobject.first;
931 /* Start looping on Scene OR Group objects */
934 ob_iter= base->object;
938 oblay = ob_iter->lay;
941 if (lay & oblay && scene->obedit!=ob_iter) {
946 /* End Scene/Group object loop, below is generic */
949 /* par_space_mat - only used for groups so we can modify the space dupli's are in
950 when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
953 mul_m4_m4m4(vdd.obmat, ob->obmat, par_space_mat);
955 copy_m4_m4(vdd.obmat, ob->obmat);
959 vdd.animated= animated;
964 copy_m4_m4(vdd.pmat, pmat);
966 /* mballs have a different dupli handling */
967 if(ob->type!=OB_MBALL) ob->flag |= OB_DONE; /* doesnt render */
970 dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
973 for(a=0; a<totvert; a++) {
974 dm->getVertCo(dm, a, vec);
975 dm->getVertNo(dm, a, no);
977 vertex_dupli__mapFunc(&vdd, a, vec, no, NULL);
981 /* Set proper layer in case of scene looping,
982 * in case of groups the object layer will be
983 * changed when it's duplicated due to the
986 ob->lay = vdd.par->lay;
994 if (sce) base= base->next; /* scene loop */
995 else go= go->next; /* group loop */
1003 static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int level, int animated)
1005 Object *ob, *ob_iter;
1009 Mesh *me= par->data;
1013 float pmat[4][4], imat[3][3], (*orco)[3] = NULL, w;
1014 int lay, oblay, totface, a;
1016 Group *group = NULL;
1017 GroupObject *go = NULL;
1019 float ob__obmat[4][4]; /* needed for groups where the object matrix needs to be modified */
1021 /* simple preventing of too deep nested groups */
1022 if(level>MAX_DUPLI_RECUR) return;
1024 copy_m4_m4(pmat, par->obmat);
1026 em = BKE_mesh_get_editmesh(me);
1030 dm= editmesh_get_derived_cage(scene, par, em, CD_MASK_BAREMESH);
1032 totface= dm->getNumFaces(dm);
1033 mface= MEM_mallocN(sizeof(MFace)*totface, "mface temp");
1034 dm->copyFaceArray(dm, mface);
1035 totvert= dm->getNumVerts(dm);
1036 mvert= MEM_mallocN(sizeof(MVert)*totvert, "mvert temp");
1037 dm->copyVertArray(dm, mvert);
1039 BKE_mesh_end_editmesh(me, em);
1042 dm = mesh_get_derived_deform(scene, par, CD_MASK_BAREMESH);
1044 totface= dm->getNumFaces(dm);
1045 mface= dm->getFaceArray(dm);
1046 mvert= dm->getVertArray(dm);
1051 orco= (float(*)[3])get_mesh_orco_verts(par);
1052 transform_mesh_orco_verts(me, orco, me->totvert, 0);
1060 /* having to loop on scene OR group objects is NOT FUN */
1061 if (GS(id->name) == ID_SCE) {
1064 base= sce->base.first;
1066 group = (Group *)id;
1068 go = group->gobject.first;
1071 /* Start looping on Scene OR Group objects */
1072 while (base || go) {
1074 ob_iter= base->object;
1078 oblay = ob_iter->lay;
1081 if (lay & oblay && scene->obedit!=ob_iter) {
1086 /* End Scene/Group object loop, below is generic */
1088 /* par_space_mat - only used for groups so we can modify the space dupli's are in
1089 when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
1092 mul_m4_m4m4(ob__obmat, ob->obmat, par_space_mat);
1094 copy_m4_m4(ob__obmat, ob->obmat);
1096 copy_m3_m4(imat, ob->parentinv);
1098 /* mballs have a different dupli handling */
1099 if(ob->type!=OB_MBALL) ob->flag |= OB_DONE; /* doesnt render */
1101 for(a=0; a<totface; a++) {
1102 int mv1 = mface[a].v1;
1103 int mv2 = mface[a].v2;
1104 int mv3 = mface[a].v3;
1105 int mv4 = mface[a].v4;
1106 float *v1= mvert[mv1].co;
1107 float *v2= mvert[mv2].co;
1108 float *v3= mvert[mv3].co;
1109 float *v4= (mv4)? mvert[mv4].co: NULL;
1110 float cent[3], quat[4], mat[3][3], mat3[3][3], tmat[4][4], obmat[4][4];
1114 cent_quad_v3(cent, v1, v2, v3, v4);
1116 cent_tri_v3(cent, v1, v2, v3);
1117 mul_m4_v3(pmat, cent);
1119 sub_v3_v3v3(cent, cent, pmat[3]);
1120 add_v3_v3(cent, ob__obmat[3]);
1122 copy_m4_m4(obmat, ob__obmat);
1124 VECCOPY(obmat[3], cent);
1127 tri_to_quat( quat,v1, v2, v3);
1128 quat_to_mat3( mat,quat);
1131 if(par->transflag & OB_DUPLIFACES_SCALE) {
1132 float size= v4? area_quad_v3(v1, v2, v3, v4): area_tri_v3(v1, v2, v3);
1133 size= sqrtf(size) * par->dupfacesca;
1134 mul_m3_fl(mat, size);
1137 copy_m3_m3(mat3, mat);
1138 mul_m3_m3m3(mat, imat, mat3);
1140 copy_m4_m4(tmat, obmat);
1141 mul_m4_m4m3(obmat, tmat, mat);
1143 dob= new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIFACES, animated);
1145 w= (mv4)? 0.25f: 1.0f/3.0f;
1148 VECADDFAC(dob->orco, dob->orco, orco[mv1], w);
1149 VECADDFAC(dob->orco, dob->orco, orco[mv2], w);
1150 VECADDFAC(dob->orco, dob->orco, orco[mv3], w);
1152 VECADDFAC(dob->orco, dob->orco, orco[mv4], w);
1156 dob->uv[0] += w*mtface[a].uv[0][0];
1157 dob->uv[1] += w*mtface[a].uv[0][1];
1158 dob->uv[0] += w*mtface[a].uv[1][0];
1159 dob->uv[1] += w*mtface[a].uv[1][1];
1160 dob->uv[0] += w*mtface[a].uv[2][0];
1161 dob->uv[1] += w*mtface[a].uv[2][1];
1164 dob->uv[0] += w*mtface[a].uv[3][0];
1165 dob->uv[1] += w*mtface[a].uv[3][1];
1170 if(ob->transflag & OB_DUPLI) {
1172 copy_m4_m4(tmpmat, ob->obmat);
1173 copy_m4_m4(ob->obmat, obmat); /* pretend we are really this mat */
1174 object_duplilist_recursive((ID *)id, scene, ob, lb, ob->obmat, level+1, animated);
1175 copy_m4_m4(ob->obmat, tmpmat);
1184 if (sce) base= base->next; /* scene loop */
1185 else go= go->next; /* group loop */
1199 static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], ParticleSystem *psys, int level, int animated)
1202 Object *ob=NULL, **oblist=NULL, obcopy, *obcopylist=NULL;
1204 ParticleDupliWeight *dw;
1205 ParticleSettings *part;
1207 ChildParticle *cpa=NULL;
1209 ParticleCacheKey *cache;
1210 float ctime, pa_time, scale = 1.0f;
1211 float tmat[4][4], mat[4][4], pamat[4][4], vec[3], size=0.0;
1212 float (*obmat)[4], (*oldobmat)[4];
1213 int a, b, counter, hair = 0;
1214 int totpart, totchild, totgroup=0 /*, pa_num */;
1216 int no_draw_flag = PARS_UNEXIST;
1218 if(psys==NULL) return;
1220 /* simple preventing of too deep nested groups */
1221 if(level>MAX_DUPLI_RECUR) return;
1228 if(!psys_check_enabled(par, psys))
1231 if(G.rendering == 0)
1232 no_draw_flag |= PARS_NO_DISP;
1234 ctime = bsystem_time(scene, par, (float)scene->r.cfra, 0.0);
1236 totpart = psys->totpart;
1237 totchild = psys->totchild;
1239 BLI_srandom(31415926 + psys->seed);
1241 if((psys->renderdata || part->draw_as==PART_DRAW_REND) && ELEM(part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
1242 ParticleSimulationData sim= {NULL};
1246 sim.psmd= psys_get_modifier(par, psys);
1248 /* first check for loops (particle system object used as dupli object) */
1249 if(part->ren_as == PART_DRAW_OB) {
1250 if(ELEM(part->dup_ob, NULL, par))
1253 else { /*PART_DRAW_GR */
1254 if(part->dup_group == NULL || part->dup_group->gobject.first == NULL)
1257 for(go=part->dup_group->gobject.first; go; go=go->next)
1262 /* if we have a hair particle system, use the path cache */
1263 if(part->type == PART_HAIR) {
1264 if(psys->flag & PSYS_HAIR_DONE)
1265 hair= (totchild == 0 || psys->childcache) && psys->pathcache;
1269 /* we use cache, update totchild according to cached data */
1270 totchild = psys->totchildcache;
1271 totpart = psys->totcached;
1274 psys_check_group_weights(part);
1276 psys->lattice = psys_get_lattice(&sim);
1278 /* gather list of objects or single object */
1279 if(part->ren_as==PART_DRAW_GR) {
1280 group_handle_recalc_and_update(scene, par, part->dup_group);
1282 if(part->draw & PART_DRAW_COUNT_GR) {
1283 for(dw=part->dupliweights.first; dw; dw=dw->next)
1284 totgroup += dw->count;
1287 for(go=part->dup_group->gobject.first; go; go=go->next)
1291 /* we also copy the actual objects to restore afterwards, since
1292 * where_is_object_time will change the object which breaks transform */
1293 oblist = MEM_callocN(totgroup*sizeof(Object *), "dupgroup object list");
1294 obcopylist = MEM_callocN(totgroup*sizeof(Object), "dupgroup copy list");
1297 if(part->draw & PART_DRAW_COUNT_GR && totgroup) {
1298 dw = part->dupliweights.first;
1300 for(a=0; a<totgroup; dw=dw->next) {
1301 for(b=0; b<dw->count; b++, a++) {
1303 obcopylist[a] = *dw->ob;
1308 go = part->dup_group->gobject.first;
1309 for(a=0; a<totgroup; a++, go=go->next) {
1311 obcopylist[a] = *go->ob;
1320 if(totchild==0 || part->draw & PART_DRAW_PARENT)
1325 for(pa=psys->particles,counter=0; a<totpart+totchild; a++,pa++,counter++) {
1327 /* handle parent particle */
1328 if(pa->flag & no_draw_flag)
1331 /* pa_num = pa->num; */ /* UNUSED */
1336 /* handle child particle */
1337 cpa = &psys->child[a - totpart];
1339 /* pa_num = a; */ /* UNUSED */
1340 pa_time = psys->particles[cpa->parent].time;
1341 size = psys_get_child_size(psys, cpa, ctime, NULL);
1344 /* some hair paths might be non-existent so they can't be used for duplication */
1346 ((a < totpart && psys->pathcache[a]->steps < 0) ||
1347 (a >= totpart && psys->childcache[a-totpart]->steps < 0)))
1350 if(part->ren_as==PART_DRAW_GR) {
1351 /* for groups, pick the object based on settings */
1352 if(part->draw&PART_DRAW_RAND_GR)
1353 b= BLI_rand() % totgroup;
1358 obmat = oblist[b]->obmat;
1359 oldobmat = obcopylist[b].obmat;
1363 oldobmat= obcopy.obmat;
1367 /* hair we handle separate and compute transform based on hair keys */
1369 cache = psys->pathcache[a];
1370 psys_get_dupli_path_transform(&sim, pa, NULL, cache, pamat, &scale);
1373 cache = psys->childcache[a-totpart];
1374 psys_get_dupli_path_transform(&sim, NULL, cpa, cache, pamat, &scale);
1377 VECCOPY(pamat[3], cache->co);
1384 if(psys_get_particle_state(&sim, a, &state, 0) == 0) {
1389 normalize_qt_qt(tquat, state.rot);
1390 quat_to_mat4(pamat, tquat);
1391 copy_v3_v3(pamat[3], state.co);
1396 if(part->ren_as==PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
1397 for(go= part->dup_group->gobject.first, b=0; go; go= go->next, b++) {
1398 mul_m4_m4m4(tmat, oblist[b]->obmat, pamat);
1399 mul_mat3_m4_fl(tmat, size*scale);
1401 mul_m4_m4m4(mat, tmat, par_space_mat);
1403 copy_m4_m4(mat, tmat);
1405 dob= new_dupli_object(lb, go->ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
1406 copy_m4_m4(dob->omat, obcopylist[b].obmat);
1408 psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
1412 /* to give ipos in object correct offset */
1413 where_is_object_time(scene, ob, ctime-pa_time);
1415 VECCOPY(vec, obmat[3]);
1416 obmat[3][0] = obmat[3][1] = obmat[3][2] = 0.0f;
1418 /* Normal particles and cached hair live in global space so we need to
1419 * remove the real emitter's transformation before 2nd order duplication.
1421 if(par_space_mat && GS(id->name) != ID_GR)
1422 mul_m4_m4m4(mat, pamat, psys->imat);
1424 copy_m4_m4(mat, pamat);
1426 mul_m4_m4m4(tmat, obmat, mat);
1427 mul_mat3_m4_fl(tmat, size*scale);
1430 mul_m4_m4m4(mat, tmat, par_space_mat);
1432 copy_m4_m4(mat, tmat);
1434 if(part->draw & PART_DRAW_GLOBAL_OB)
1435 VECADD(mat[3], mat[3], vec);
1437 dob= new_dupli_object(lb, ob, mat, ob->lay, counter, GS(id->name) == ID_GR ? OB_DUPLIGROUP : OB_DUPLIPARTS, animated);
1438 copy_m4_m4(dob->omat, oldobmat);
1440 psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
1444 /* restore objects since they were changed in where_is_object_time */
1445 if(part->ren_as==PART_DRAW_GR) {
1446 for(a=0; a<totgroup; a++)
1447 *(oblist[a])= obcopylist[a];
1457 MEM_freeN(obcopylist);
1460 end_latt_deform(psys->lattice);
1461 psys->lattice = NULL;
1465 static Object *find_family_object(Object **obar, char *family, char ch)
1470 if( obar[(int)ch] ) return obar[(int)ch];
1472 flen= strlen(family);
1474 ob= G.main->object.first;
1476 if( ob->id.name[flen+2]==ch ) {
1477 if( strncmp(ob->id.name+2, family, flen)==0 ) break;
1488 static void font_duplilist(ListBase *lb, Scene *scene, Object *par, int level, int animated)
1490 Object *ob, *obar[256]= {NULL};
1492 struct chartrans *ct, *chartransdata;
1493 float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
1496 /* simple preventing of too deep nested groups */
1497 if(level>MAX_DUPLI_RECUR) return;
1499 copy_m4_m4(pmat, par->obmat);
1501 /* in par the family name is stored, use this to find the other objects */
1503 chartransdata= BKE_text_to_curve(scene, par, FO_DUPLI);
1504 if(chartransdata==NULL) return;
1507 slen= strlen(cu->str);
1514 for(a=0; a<slen; a++, ct++) {
1516 ob= find_family_object(obar, cu->family, cu->str[a]);
1518 vec[0]= fsize*(ct->xof - xof);
1519 vec[1]= fsize*(ct->yof - yof);
1522 mul_m4_v3(pmat, vec);
1524 copy_m4_m4(obmat, par->obmat);
1525 VECCOPY(obmat[3], vec);
1527 new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIVERTS, animated);
1531 MEM_freeN(chartransdata);
1536 static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated)
1538 if((ob->transflag & OB_DUPLI)==0)
1541 /* Should the dupli's be generated for this object? - Respect restrict flags */
1543 if (ob->restrictflag & OB_RESTRICT_RENDER) {
1547 if (ob->restrictflag & OB_RESTRICT_VIEW) {
1552 if(ob->transflag & OB_DUPLIPARTS) {
1553 ParticleSystem *psys = ob->particlesystem.first;
1554 for(; psys; psys=psys->next)
1555 new_particle_duplilist(duplilist, id, scene, ob, par_space_mat, psys, level+1, animated);
1557 else if(ob->transflag & OB_DUPLIVERTS) {
1558 if(ob->type==OB_MESH) {
1559 vertex_duplilist(duplilist, id, scene, ob, par_space_mat, level+1, animated);
1561 else if(ob->type==OB_FONT) {
1562 if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1563 font_duplilist(duplilist, scene, ob, level+1, animated);
1567 else if(ob->transflag & OB_DUPLIFACES) {
1568 if(ob->type==OB_MESH)
1569 face_duplilist(duplilist, id, scene, ob, par_space_mat, level+1, animated);
1571 else if(ob->transflag & OB_DUPLIFRAMES) {
1572 if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
1573 frames_duplilist(duplilist, scene, ob, level+1, animated);
1575 } else if(ob->transflag & OB_DUPLIGROUP) {
1578 group_duplilist(duplilist, scene, ob, level+1, animated); /* now recursive */
1581 for(dob= duplilist->first; dob; dob= dob->next)
1582 if(dob->type == OB_DUPLIGROUP)
1583 copy_m4_m4(dob->ob->obmat, dob->mat);
1588 /* Returns a list of DupliObject
1589 * note; group dupli's already set transform matrix. see note in group_duplilist() */
1590 ListBase *object_duplilist(Scene *sce, Object *ob)
1592 ListBase *duplilist= MEM_mallocN(sizeof(ListBase), "duplilist");
1593 duplilist->first= duplilist->last= NULL;
1594 object_duplilist_recursive((ID *)sce, sce, ob, duplilist, NULL, 0, 0);
1598 void free_object_duplilist(ListBase *lb)
1602 /* loop in reverse order, if object is instanced multiple times
1603 the original layer may not really be original otherwise, proper
1604 solution is more complicated */
1605 for(dob= lb->last; dob; dob= dob->prev) {
1606 dob->ob->lay= dob->origlay;
1607 copy_m4_m4(dob->ob->obmat, dob->omat);
1614 int count_duplilist(Object *ob)
1616 if(ob->transflag & OB_DUPLI) {
1617 if(ob->transflag & OB_DUPLIVERTS) {
1618 if(ob->type==OB_MESH) {
1619 if(ob->transflag & OB_DUPLIVERTS) {
1620 ParticleSystem *psys = ob->particlesystem.first;
1623 for(; psys; psys=psys->next)
1624 pdup += psys->totpart;
1635 else if(ob->transflag & OB_DUPLIFRAMES) {
1636 int tot= ob->dupend - ob->dupsta;
1637 tot/= (ob->dupon+ob->dupoff);
1638 return tot*ob->dupon;