code cleanup: move file string defines into BLI_path_utils.h, BKE_utildefines is...
[blender.git] / source / blender / blenkernel / intern / anim.c
index 05171a5d9f556953a83792787ad0b10421ee1418..b4a3d905edb6732047a28990e81b87888400d41d 100644 (file)
@@ -63,7 +63,6 @@
 #include "BKE_object.h"
 #include "BKE_particle.h"
 #include "BKE_scene.h"
-#include "BKE_utildefines.h"
 #include "BKE_tessmesh.h"
 #include "BKE_depsgraph.h"
 #include "BKE_anim.h"
 /* --------------------- */
 /* forward declarations */
 
-static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated);
+static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBase *duplilist, float par_space_mat[][4], int par_index, int level, int animated);
 
 /* ******************************************************************** */
-/* Animation Visualisation */
+/* Animation Visualization */
 
-/* Initialize the default settings for animation visualisation */
+/* Initialize the default settings for animation visualization */
 void animviz_settings_init(bAnimVizSettings *avs)
 {
        /* sanity check */
        if (avs == NULL)
                return;
-               
+
        /* ghosting settings */
-       avs->ghost_bc= avs->ghost_ac= 10;
-       
-       avs->ghost_sf= 1; // xxx - take from scene instead?
-       avs->ghost_ef= 250; // xxx - take from scene instead?
-       
-       avs->ghost_step= 1;
-       
-       
+       avs->ghost_bc = avs->ghost_ac = 10;
+
+       avs->ghost_sf = 1; /* xxx - take from scene instead? */
+       avs->ghost_ef = 250; /* xxx - take from scene instead? */
+
+       avs->ghost_step = 1;
+
+
        /* path settings */
-       avs->path_bc= avs->path_ac= 10;
-       
-       avs->path_sf= 1; // xxx - take from scene instead?
-       avs->path_ef= 250; // xxx - take from scene instead?
-       
-       avs->path_viewflag= (MOTIONPATH_VIEW_KFRAS|MOTIONPATH_VIEW_KFNOS);
-       
-       avs->path_step= 1;
+       avs->path_bc = avs->path_ac = 10;
+
+       avs->path_sf = 1; /* xxx - take from scene instead? */
+       avs->path_ef = 250; /* xxx - take from scene instead? */
+
+       avs->path_viewflag = (MOTIONPATH_VIEW_KFRAS | MOTIONPATH_VIEW_KFNOS);
+
+       avs->path_step = 1;
 }
 
 /* ------------------- */
@@ -121,8 +120,8 @@ void animviz_free_motionpath_cache(bMotionPath *mpath)
                MEM_freeN(mpath->points);
        
        /* reset the relevant parameters */
-       mpath->points= NULL;
-       mpath->length= 0;
+       mpath->points = NULL;
+       mpath->length = 0;
 }
 
 /* Free the given motion path instance and its data 
@@ -144,9 +143,11 @@ void animviz_free_motionpath(bMotionPath *mpath)
 /* ------------------- */
 
 /* Setup motion paths for the given data
- *     - scene: current scene (for frame ranges, etc.)
- *     - ob: object to add paths for (must be provided)
- *     - pchan: posechannel to add paths for (optional; if not provided, object-paths are assumed)
+ * - Only used when explicitly calculating paths on bones which may/may not be consider already
+ *
+ * < scene: current scene (for frame ranges, etc.)
+ * < ob: object to add paths for (must be provided)
+ * < pchan: posechannel to add paths for (optional; if not provided, object-paths are assumed)
  */
 bMotionPath *animviz_verify_motionpaths(ReportList *reports, Scene *scene, Object *ob, bPoseChannel *pchan)
 {
@@ -160,46 +161,57 @@ bMotionPath *animviz_verify_motionpaths(ReportList *reports, Scene *scene, Objec
        /* get destination data */
        if (pchan) {
                /* paths for posechannel - assume that posechannel belongs to the object */
-               avs= &ob->pose->avs;
-               dst= &pchan->mpath;
+               avs = &ob->pose->avs;
+               dst = &pchan->mpath;
        }
        else {
                /* paths for object */
-               avs= &ob->avs;
-               dst= &ob->mpath;
+               avs = &ob->avs;
+               dst = &ob->mpath;
        }
 
        /* avoid 0 size allocs */
        if (avs->path_sf >= avs->path_ef) {
                BKE_reportf(reports, RPT_ERROR,
-                                       "Motion Path frame extents invalid for %s (%d to %d).%s\n",
-                                       (pchan)? pchan->name : ob->id.name,
-                                       avs->path_sf, avs->path_ef,
-                                       (avs->path_sf == avs->path_ef)? " Cannot have single-frame paths." : "");
+                           "Motion Path frame extents invalid for %s (%d to %d).%s\n",
+                           (pchan) ? pchan->name : ob->id.name,
+                           avs->path_sf, avs->path_ef,
+                           (avs->path_sf == avs->path_ef) ? " Cannot have single-frame paths." : "");
                return NULL;
        }
 
        /* if there is already a motionpath, just return that,
-        * but provided it's settings are ok 
+        * provided it's settings are ok (saves extra free+alloc)
         */
        if (*dst != NULL) {
-               mpath= *dst;
+               int expected_length = avs->path_ef - avs->path_sf;
                
-               /* if range is not invalid, and/or length is set ok, just return */
-               if ((mpath->start_frame != mpath->end_frame) && (mpath->length > 0))
-                       return mpath;
+               mpath = *dst;
+               
+               /* path is "valid" if length is valid, but must also be of the same length as is being requested */
+               if ((mpath->start_frame != mpath->end_frame) && (mpath->length > 0)) {
+                       /* outer check ensures that we have some curve data for this path */
+                       if (mpath->length == expected_length) {
+                               /* return/use this as it is already valid length */
+                               return mpath;
+                       }
+                       else {
+                               /* clear the existing path (as the range has changed), and reallocate below */
+                               animviz_free_motionpath_cache(mpath);
+                       }
+               }
        }
        else {
                /* create a new motionpath, and assign it */
-               mpath= MEM_callocN(sizeof(bMotionPath), "bMotionPath");
-               *dst= mpath;
+               mpath = MEM_callocN(sizeof(bMotionPath), "bMotionPath");
+               *dst = mpath;
        }
        
        /* set settings from the viz settings */
-       mpath->start_frame= avs->path_sf;
-       mpath->end_frame= avs->path_ef;
+       mpath->start_frame = avs->path_sf;
+       mpath->end_frame = avs->path_ef;
        
-       mpath->length= mpath->end_frame - mpath->start_frame;
+       mpath->length = mpath->end_frame - mpath->start_frame;
        
        if (avs->path_bakeflag & MOTIONPATH_BAKE_HEADS)
                mpath->flag |= MOTIONPATH_FLAG_BHEAD;
@@ -207,7 +219,7 @@ bMotionPath *animviz_verify_motionpaths(ReportList *reports, Scene *scene, Objec
                mpath->flag &= ~MOTIONPATH_FLAG_BHEAD;
        
        /* allocate a cache */
-       mpath->points= MEM_callocN(sizeof(bMotionPathVert)*mpath->length, "bMotionPathVerts");
+       mpath->points = MEM_callocN(sizeof(bMotionPathVert) * mpath->length, "bMotionPathVerts");
        
        /* tag viz settings as currently having some path(s) which use it */
        avs->path_bakeflag |= MOTIONPATH_BAKE_HAS_PATHS;
@@ -222,18 +234,18 @@ bMotionPath *animviz_verify_motionpaths(ReportList *reports, Scene *scene, Objec
 typedef struct MPathTarget {
        struct MPathTarget *next, *prev;
        
-       bMotionPath *mpath;                     /* motion path in question */
+       bMotionPath *mpath;         /* motion path in question */
        
-       Object *ob;                                     /* source object */
-       bPoseChannel *pchan;            /* source posechannel (if applicable) */
+       Object *ob;                 /* source object */
+       bPoseChannel *pchan;        /* source posechannel (if applicable) */
 } MPathTarget;
 
 /* ........ */
 
 /* get list of motion paths to be baked for the given object
- *     - assumes the given list is ready to be used
+ *  - assumes the given list is ready to be used
  */
-// TODO: it would be nice in future to be able to update objects dependent on these bones too?
+/* TODO: it would be nice in future to be able to update objects dependent on these bones too? */
 void animviz_get_object_motionpaths(Object *ob, ListBase *targets)
 {
        MPathTarget *mpt;
@@ -241,27 +253,27 @@ void animviz_get_object_motionpaths(Object *ob, ListBase *targets)
        /* object itself first */
        if ((ob->avs.recalc & ANIMVIZ_RECALC_PATHS) && (ob->mpath)) {
                /* new target for object */
-               mpt= MEM_callocN(sizeof(MPathTarget), "MPathTarget Ob");
+               mpt = MEM_callocN(sizeof(MPathTarget), "MPathTarget Ob");
                BLI_addtail(targets, mpt);
                
-               mpt->mpath= ob->mpath;
-               mpt->ob= ob;
+               mpt->mpath = ob->mpath;
+               mpt->ob = ob;
        }
        
        /* bones */
        if ((ob->pose) && (ob->pose->avs.recalc & ANIMVIZ_RECALC_PATHS)) {
-               bArmature *arm= ob->data;
+               bArmature *arm = ob->data;
                bPoseChannel *pchan;
                
-               for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
+               for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
                        if ((pchan->bone) && (arm->layer & pchan->bone->layer) && (pchan->mpath)) {
                                /* new target for bone */
-                               mpt= MEM_callocN(sizeof(MPathTarget), "MPathTarget PoseBone");
+                               mpt = MEM_callocN(sizeof(MPathTarget), "MPathTarget PoseBone");
                                BLI_addtail(targets, mpt);
                                
-                               mpt->mpath= pchan->mpath;
-                               mpt->ob= ob;
-                               mpt->pchan= pchan;
+                               mpt->mpath = pchan->mpath;
+                               mpt->ob = ob;
+                               mpt->pchan = pchan;
                        }
                }
        }
@@ -269,7 +281,7 @@ void animviz_get_object_motionpaths(Object *ob, ListBase *targets)
 
 /* ........ */
 
-/* Note on evaluation optimisations:
+/* Note on evaluation optimizations:
  * Optimisations currently used here play tricks with the depsgraph in order to try and 
  * evaluate as few objects as strictly necessary to get nicer performance under standard
  * production conditions. For those people who really need the accurate version, 
@@ -283,12 +295,12 @@ static void motionpaths_calc_optimise_depsgraph(Scene *scene, ListBase *targets)
        MPathTarget *mpt;
        
        /* make sure our temp-tag isn't already in use */
-       for (base= scene->base.first; base; base= base->next)
+       for (base = scene->base.first; base; base = base->next)
                base->object->flag &= ~BA_TEMP_TAG;
        
        /* for each target, dump its object to the start of the list if it wasn't moved already */
-       for (mpt= targets->first; mpt; mpt= mpt->next) {
-               for (base=scene->base.first; base; base=baseNext) {
+       for (mpt = targets->first; mpt; mpt = mpt->next) {
+               for (base = scene->base.first; base; base = baseNext) {
                        baseNext = base->next;
                        
                        if ((base->object == mpt->ob) && !(mpt->ob->flag & BA_TEMP_TAG)) {
@@ -296,7 +308,9 @@ static void motionpaths_calc_optimise_depsgraph(Scene *scene, ListBase *targets)
                                BLI_addhead(&scene->base, base);
                                
                                mpt->ob->flag |= BA_TEMP_TAG;
-                               break; // we really don't need to continue anymore once this happens, but this line might really 'break'
+
+                               /* we really don't need to continue anymore once this happens, but this line might really 'break' */
+                               break;
                        }
                }
        }
@@ -308,39 +322,39 @@ static void motionpaths_calc_optimise_depsgraph(Scene *scene, ListBase *targets)
 /* update scene for current frame */
 static void motionpaths_calc_update_scene(Scene *scene)
 {
-#if 1 // 'production' optimisations always on
-       Base *base, *last=NULL;
+#if 1 // 'production' optimizations always on
+       Base *base, *last = NULL;
        
        /* only stuff that moves or needs display still */
        DAG_scene_update_flags(G.main, scene, scene->lay, TRUE);
        
        /* find the last object with the tag 
-        *      - all those afterwards are assumed to not be relevant for our calculations
+        * - all those afterwards are assumed to not be relevant for our calculations
         */
-       // optimize further by moving out...
-       for (base=scene->base.first; base; base=base->next) {
+       /* optimize further by moving out... */
+       for (base = scene->base.first; base; base = base->next) {
                if (base->object->flag & BA_TEMP_TAG)
                        last = base;
        }
        
        /* perform updates for tagged objects */
-       // XXX: this will break if rigs depend on scene or other data that 
-       // is animated but not attached to/updatable from objects
-       for (base=scene->base.first; base; base=base->next) {
+       /* XXX: this will break if rigs depend on scene or other data that
+        * is animated but not attached to/updatable from objects */
+       for (base = scene->base.first; base; base = base->next) {
                /* update this object */
-               object_handle_update(scene, base->object);
+               BKE_object_handle_update(scene, base->object);
                
                /* if this is the last one we need to update, let's stop to save some time */
                if (base == last)
                        break;
        }
 #else // original, 'always correct' version
-       /* do all updates 
-        *      - if this is too slow, resort to using a more efficient way 
-        *        that doesn't force complete update, but for now, this is the
-        *        most accurate way!
-        */
-       scene_update_for_newframe(G.main, scene, scene->lay); // XXX this is the best way we can get anything moving
+         /* do all updates
+          *  - if this is too slow, resort to using a more efficient way
+          *    that doesn't force complete update, but for now, this is the
+          *    most accurate way!
+          */
+       BKE_scene_update_for_newframe(G.main, scene, scene->lay); /* XXX this is the best way we can get anything moving */
 #endif
 }
 
@@ -352,8 +366,8 @@ static void motionpaths_calc_bake_targets(Scene *scene, ListBase *targets)
        MPathTarget *mpt;
        
        /* for each target, check if it can be baked on the current frame */
-       for (mpt= targets->first; mpt; mpt= mpt->next) {        
-               bMotionPath *mpath= mpt->mpath;
+       for (mpt = targets->first; mpt; mpt = mpt->next) {
+               bMotionPath *mpath = mpt->mpath;
                bMotionPathVert *mpv;
                
                /* current frame must be within the range the cache works for 
@@ -363,7 +377,7 @@ static void motionpaths_calc_bake_targets(Scene *scene, ListBase *targets)
                        continue;
                
                /* get the relevant cache vert to write to */
-               mpv= mpath->points + (CFRA - mpath->start_frame);
+               mpv = mpath->points + (CFRA - mpath->start_frame);
                
                /* pose-channel or object path baking? */
                if (mpt->pchan) {
@@ -388,9 +402,9 @@ static void motionpaths_calc_bake_targets(Scene *scene, ListBase *targets)
 /* Perform baking of the given object's and/or its bones' transforms to motion paths 
  *     - scene: current scene
  *     - ob: object whose flagged motionpaths should get calculated
- *     - recalc: whether we need to 
+ *     - recalc: whether we need to
  */
-// TODO: include reports pointer?
+/* TODO: include reports pointer? */
 void animviz_calc_motionpaths(Scene *scene, ListBase *targets)
 {
        MPathTarget *mpt;
@@ -405,22 +419,22 @@ void animviz_calc_motionpaths(Scene *scene, ListBase *targets)
        cfra = CFRA;
        sfra = efra = cfra;
        
-       // TODO: this method could be improved...
-       //      1) max range for standard baking
-       //      2) minimum range for recalc baking (i.e. between keyframes, but how?)
-       for (mpt= targets->first; mpt; mpt= mpt->next) {
+       /* TODO: this method could be improved...
+        * 1) max range for standard baking
+        * 2) minimum range for recalc baking (i.e. between keyframes, but how?) */
+       for (mpt = targets->first; mpt; mpt = mpt->next) {
                /* try to increase area to do (only as much as needed) */
-               sfra= MIN2(sfra, mpt->mpath->start_frame);
-               efra= MAX2(efra, mpt->mpath->end_frame);
+               sfra = MIN2(sfra, mpt->mpath->start_frame);
+               efra = MAX2(efra, mpt->mpath->end_frame);
        }
        if (efra <= sfra) return;
        
        /* optimize the depsgraph for faster updates */
-       // TODO: whether this is used should depend on some setting for the level of optimisations used
+       /* TODO: whether this is used should depend on some setting for the level of optimizations used */
        motionpaths_calc_optimise_depsgraph(scene, targets);
        
        /* calculate path over requested range */
-       for (CFRA=sfra; CFRA<=efra; CFRA++) {
+       for (CFRA = sfra; CFRA <= efra; CFRA++) {
                /* update relevant data for new frame */
                motionpaths_calc_update_scene(scene);
                
@@ -429,18 +443,18 @@ void animviz_calc_motionpaths(Scene *scene, ListBase *targets)
        }
        
        /* reset original environment */
-       CFRA= cfra;
+       CFRA = cfra;
        motionpaths_calc_update_scene(scene);
        
        /* clear recalc flags from targets */
-       for (mpt= targets->first; mpt; mpt= mpt->next) {
+       for (mpt = targets->first; mpt; mpt = mpt->next) {
                bAnimVizSettings *avs;
                
                /* get pointer to animviz settings for each target */
                if (mpt->pchan)
-                       avs= &mpt->ob->pose->avs;
-               else    
-                       avs= &mpt->ob->avs;
+                       avs = &mpt->ob->pose->avs;
+               else
+                       avs = &mpt->ob->avs;
                
                /* clear the flag requesting recalculation of targets */
                avs->recalc &= ~ANIMVIZ_RECALC_PATHS;
@@ -461,7 +475,7 @@ void free_path(Path *path)
 }
 
 /* calculate a curve-deform path for a curve 
- *     - only called from displist.c -> do_makeDispListCurveTypes
+ *  - only called from displist.c -> do_makeDispListCurveTypes
  */
 void calc_curvepath(Object *ob)
 {
@@ -472,92 +486,92 @@ void calc_curvepath(Object *ob)
        Nurb *nu;
        Path *path;
        float *fp, *dist, *maxdist, xyz[3];
-       float fac, d=0, fac1, fac2;
-       int a, tot, cycl=0;
+       float fac, d = 0, fac1, fac2;
+       int a, tot, cycl = 0;
        ListBase *nurbs;
        
        /* in a path vertices are with equal differences: path->len = number of verts */
        /* NOW WITH BEVELCURVE!!! */
        
-       if (ob==NULL || ob->type != OB_CURVE) return;
-       cu= ob->data;
+       if (ob == NULL || ob->type != OB_CURVE) return;
+       cu = ob->data;
 
-       nurbs= BKE_curve_nurbs(cu);
-       nu= nurbs->first;
+       nurbs = BKE_curve_nurbs_get(cu);
+       nu = nurbs->first;
 
        if (cu->path) free_path(cu->path);
-       cu->path= NULL;
+       cu->path = NULL;
        
-       bl= cu->bev.first;
-       if (bl==NULL || !bl->nr) return;
+       bl = cu->bev.first;
+       if (bl == NULL || !bl->nr) return;
 
-       cu->path=path= MEM_callocN(sizeof(Path), "calc_curvepath");
+       cu->path = path = MEM_callocN(sizeof(Path), "calc_curvepath");
        
        /* if POLY: last vertice != first vertice */
-       cycl= (bl->poly!= -1);
+       cycl = (bl->poly != -1);
        
-       if (cycl) tot= bl->nr;
-       else tot= bl->nr-1;
+       if (cycl) tot = bl->nr;
+       else tot = bl->nr - 1;
        
-       path->len= tot+1;
+       path->len = tot + 1;
        /* exception: vector handle paths and polygon paths should be subdivided at least a factor resolu */
-       if (path->len<nu->resolu*SEGMENTSU(nu)) path->len= nu->resolu*SEGMENTSU(nu);
+       if (path->len < nu->resolu * SEGMENTSU(nu)) path->len = nu->resolu * SEGMENTSU(nu);
        
-       dist= (float *)MEM_mallocN((tot+1)*4, "calcpathdist");
+       dist = (float *)MEM_mallocN((tot + 1) * 4, "calcpathdist");
 
-               /* all lengths in *dist */
-       bevp= bevpfirst= (BevPoint *)(bl+1);
-       fp= dist;
-       *fp= 0;
-       for (a=0; a<tot; a++) {
+       /* all lengths in *dist */
+       bevp = bevpfirst = (BevPoint *)(bl + 1);
+       fp = dist;
+       *fp = 0;
+       for (a = 0; a < tot; a++) {
                fp++;
-               if (cycl && a==tot-1)
+               if (cycl && a == tot - 1)
                        sub_v3_v3v3(xyz, bevpfirst->vec, bevp->vec);
                else
-                       sub_v3_v3v3(xyz, (bevp+1)->vec, bevp->vec);
+                       sub_v3_v3v3(xyz, (bevp + 1)->vec, bevp->vec);
                
-               *fp= *(fp-1)+len_v3(xyz);
+               *fp = *(fp - 1) + len_v3(xyz);
                bevp++;
        }
        
-       path->totdist= *fp;
+       path->totdist = *fp;
        
-               /* the path verts  in path->data */
-               /* now also with TILT value */
-       pp= path->data = (PathPoint *)MEM_callocN(sizeof(PathPoint)*path->len, "pathdata");
-       
-       bevp= bevpfirst;
-       bevpn= bevp+1;
-       bevplast= bevpfirst + (bl->nr-1);
-       fp= dist+1;
-       maxdist= dist+tot;
-       fac= 1.0f/((float)path->len-1.0f);
-               fac = fac * path->totdist;
+       /* the path verts  in path->data */
+       /* now also with TILT value */
+       pp = path->data = (PathPoint *)MEM_callocN(sizeof(PathPoint) * path->len, "pathdata");
+
+       bevp = bevpfirst;
+       bevpn = bevp + 1;
+       bevplast = bevpfirst + (bl->nr - 1);
+       fp = dist + 1;
+       maxdist = dist + tot;
+       fac = 1.0f / ((float)path->len - 1.0f);
+       fac = fac * path->totdist;
        
-       for (a=0; a<path->len; a++) {
+       for (a = 0; a < path->len; a++) {
                
-               d= ((float)a)*fac;
+               d = ((float)a) * fac;
                
                /* we're looking for location (distance) 'd' in the array */
-               while ((d>= *fp) && fp<maxdist) {
+               while ((d >= *fp) && fp < maxdist) {
                        fp++;
-                       if (bevp<bevplast) bevp++;
-                       bevpn= bevp+1;
-                       if (bevpn>bevplast) {
-                               if (cycl) bevpn= bevpfirst;
-                               else bevpn= bevplast;
+                       if (bevp < bevplast) bevp++;
+                       bevpn = bevp + 1;
+                       if (bevpn > bevplast) {
+                               if (cycl) bevpn = bevpfirst;
+                               else bevpn = bevplast;
                        }
                }
                
-               fac1= *(fp)- *(fp-1);
-               fac2= *(fp)-d;
-               fac1= fac2/fac1;
-               fac2= 1.0f-fac1;
+               fac1 = *(fp) - *(fp - 1);
+               fac2 = *(fp) - d;
+               fac1 = fac2 / fac1;
+               fac2 = 1.0f - fac1;
                
                interp_v3_v3v3(pp->vec, bevp->vec, bevpn->vec, fac2);
-               pp->vec[3]= fac1*bevp->alfa + fac2*bevpn->alfa;
-               pp->radius= fac1*bevp->radius + fac2*bevpn->radius;
-               pp->weight= fac1*bevp->weight + fac2*bevpn->weight;
+               pp->vec[3] = fac1 * bevp->alfa + fac2 * bevpn->alfa;
+               pp->radius = fac1 * bevp->radius + fac2 * bevpn->radius;
+               pp->weight = fac1 * bevp->weight + fac2 * bevpn->weight;
                interp_qt_qtqt(pp->quat, bevp->quat, bevpn->quat, fac2);
                normalize_qt(pp->quat);
                
@@ -573,13 +587,13 @@ int interval_test(int min, int max, int p1, int cycl)
 {
        if (cycl) {
                if (p1 < min) 
-                       p1=  ((p1 -min) % (max-min+1)) + max+1;
+                       p1 =  ((p1 - min) % (max - min + 1)) + max + 1;
                else if (p1 > max)
-                       p1=  ((p1 -min) % (max-min+1)) + min;
+                       p1 =  ((p1 - min) % (max - min + 1)) + min;
        }
        else {
-               if (p1 < min) p1= min;
-               else if (p1 > max) p1= max;
+               if (p1 < min) p1 = min;
+               else if (p1 > max) p1 = max;
        }
        return p1;
 }
@@ -601,84 +615,84 @@ int where_on_path(Object *ob, float ctime, float vec[4], float dir[3], float qua
        PathPoint *pp, *p0, *p1, *p2, *p3;
        float fac;
        float data[4];
-       int cycl=0, s0, s1, s2, s3;
+       int cycl = 0, s0, s1, s2, s3;
 
-       if (ob==NULL || ob->type != OB_CURVE) return 0;
-       cu= ob->data;
-       if (cu->path==NULL || cu->path->data==NULL) {
+       if (ob == NULL || ob->type != OB_CURVE) return 0;
+       cu = ob->data;
+       if (cu->path == NULL || cu->path->data == NULL) {
                printf("no path!\n");
                return 0;
        }
-       path= cu->path;
-       pp= path->data;
+       path = cu->path;
+       pp = path->data;
        
        /* test for cyclic */
-       bl= cu->bev.first;
+       bl = cu->bev.first;
        if (!bl) return 0;
        if (!bl->nr) return 0;
-       if (bl->poly> -1) cycl= 1;
+       if (bl->poly > -1) cycl = 1;
 
-       ctime *= (path->len-1);
+       ctime *= (path->len - 1);
        
-       s1= (int)floor(ctime);
-       fac= (float)(s1+1)-ctime;
+       s1 = (int)floor(ctime);
+       fac = (float)(s1 + 1) - ctime;
 
        /* path->len is corected for cyclic */
-       s0= interval_test(0, path->len-1-cycl, s1-1, cycl);
-       s1= interval_test(0, path->len-1-cycl, s1, cycl);
-       s2= interval_test(0, path->len-1-cycl, s1+1, cycl);
-       s3= interval_test(0, path->len-1-cycl, s1+2, cycl);
+       s0 = interval_test(0, path->len - 1 - cycl, s1 - 1, cycl);
+       s1 = interval_test(0, path->len - 1 - cycl, s1, cycl);
+       s2 = interval_test(0, path->len - 1 - cycl, s1 + 1, cycl);
+       s3 = interval_test(0, path->len - 1 - cycl, s1 + 2, cycl);
 
-       p0= pp + s0;
-       p1= pp + s1;
-       p2= pp + s2;
-       p3= pp + s3;
+       p0 = pp + s0;
+       p1 = pp + s1;
+       p2 = pp + s2;
+       p3 = pp + s3;
 
        /* note, commented out for follow constraint */
        //if (cu->flag & CU_FOLLOW) {
 
-               key_curve_tangent_weights(1.0f-fac, data, KEY_BSPLINE);
+       key_curve_tangent_weights(1.0f - fac, data, KEY_BSPLINE);
 
-               interp_v3_v3v3v3v3(dir, p0->vec, p1->vec, p2->vec, p3->vec, data);
+       interp_v3_v3v3v3v3(dir, p0->vec, p1->vec, p2->vec, p3->vec, data);
 
-               /* make compatible with vectoquat */
-               negate_v3(dir);
+       /* make compatible with vectoquat */
+       negate_v3(dir);
        //}
        
-       nu= cu->nurb.first;
+       nu = cu->nurb.first;
 
        /* make sure that first and last frame are included in the vectors here  */
-       if (nu->type == CU_POLY) key_curve_position_weights(1.0f-fac, data, KEY_LINEAR);
-       else if (nu->type == CU_BEZIER) key_curve_position_weights(1.0f-fac, data, KEY_LINEAR);
-       else if (s0==s1 || p2==p3) key_curve_position_weights(1.0f-fac, data, KEY_CARDINAL);
-       else key_curve_position_weights(1.0f-fac, data, KEY_BSPLINE);
+       if (nu->type == CU_POLY) key_curve_position_weights(1.0f - fac, data, KEY_LINEAR);
+       else if (nu->type == CU_BEZIER) key_curve_position_weights(1.0f - fac, data, KEY_LINEAR);
+       else if (s0 == s1 || p2 == p3) key_curve_position_weights(1.0f - fac, data, KEY_CARDINAL);
+       else key_curve_position_weights(1.0f - fac, data, KEY_BSPLINE);
 
-       vec[0]= data[0]*p0->vec[0] + data[1]*p1->vec[0] + data[2]*p2->vec[0] + data[3]*p3->vec[0] ; /* X */
-       vec[1]= data[0]*p0->vec[1] + data[1]*p1->vec[1] + data[2]*p2->vec[1] + data[3]*p3->vec[1] ; /* Y */
-       vec[2]= data[0]*p0->vec[2] + data[1]*p1->vec[2] + data[2]*p2->vec[2] + data[3]*p3->vec[2] ; /* Z */
-       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 */
+       vec[0] = data[0] * p0->vec[0] + data[1] * p1->vec[0] + data[2] * p2->vec[0] + data[3] * p3->vec[0]; /* X */
+       vec[1] = data[0] * p0->vec[1] + data[1] * p1->vec[1] + data[2] * p2->vec[1] + data[3] * p3->vec[1]; /* Y */
+       vec[2] = data[0] * p0->vec[2] + data[1] * p1->vec[2] + data[2] * p2->vec[2] + data[3] * p3->vec[2]; /* Z */
+       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 */
 
        if (quat) {
                float totfac, q1[4], q2[4];
 
-               totfac= data[0]+data[3];
-               if (totfac>FLT_EPSILON) interp_qt_qtqt(q1, p0->quat, p3->quat, data[3] / totfac);
-               else                                    copy_qt_qt(q1, p1->quat);
+               totfac = data[0] + data[3];
+               if (totfac > FLT_EPSILON) interp_qt_qtqt(q1, p0->quat, p3->quat, data[3] / totfac);
+               else copy_qt_qt(q1, p1->quat);
 
-               totfac= data[1]+data[2];
-               if (totfac>FLT_EPSILON) interp_qt_qtqt(q2, p1->quat, p2->quat, data[2] / totfac);
-               else                                    copy_qt_qt(q2, p3->quat);
+               totfac = data[1] + data[2];
+               if (totfac > FLT_EPSILON) interp_qt_qtqt(q2, p1->quat, p2->quat, data[2] / totfac);
+               else copy_qt_qt(q2, p3->quat);
 
-               totfac = data[0]+data[1]+data[2]+data[3];
-               if (totfac>FLT_EPSILON) interp_qt_qtqt(quat, q1, q2, (data[1]+data[2]) / totfac);
-               else                                    copy_qt_qt(quat, q2);
+               totfac = data[0] + data[1] + data[2] + data[3];
+               if (totfac > FLT_EPSILON) interp_qt_qtqt(quat, q1, q2, (data[1] + data[2]) / totfac);
+               else copy_qt_qt(quat, q2);
        }
 
        if (radius)
-               *radius= data[0]*p0->radius + data[1]*p1->radius + data[2]*p2->radius + data[3]*p3->radius;
+               *radius = data[0] * p0->radius + data[1] * p1->radius + data[2] * p2->radius + data[3] * p3->radius;
 
        if (weight)
-               *weight= data[0]*p0->weight + data[1]*p1->weight + data[2]*p2->weight + data[3]*p3->weight;
+               *weight = data[0] * p0->weight + data[1] * p1->weight + data[2] * p2->weight + data[3] * p3->weight;
 
        return 1;
 }
@@ -686,44 +700,45 @@ int where_on_path(Object *ob, float ctime, float vec[4], float dir[3], float qua
 /* ******************************************************************** */
 /* Dupli-Geometry */
 
-static DupliObject *new_dupli_object(ListBase *lb, Object *ob, float mat[][4], int lay, int index, int type, int animated)
+static DupliObject *new_dupli_object(ListBase *lb, Object *ob, float mat[][4], int lay, int index, int par_index, int type, int animated)
 {
-       DupliObject *dob= MEM_callocN(sizeof(DupliObject), "dupliobject");
+       DupliObject *dob = MEM_callocN(sizeof(DupliObject), "dupliobject");
        
        BLI_addtail(lb, dob);
-       dob->ob= ob;
+       dob->ob = ob;
        copy_m4_m4(dob->mat, mat);
        copy_m4_m4(dob->omat, ob->obmat);
-       dob->origlay= ob->lay;
-       dob->index= index;
-       dob->type= type;
-       dob->animated= (type == OB_DUPLIGROUP) && animated;
-       ob->lay= lay;
+       dob->origlay = ob->lay;
+       dob->index = index;
+       dob->particle_index = par_index;
+       dob->type = type;
+       dob->animated = (type == OB_DUPLIGROUP) && animated;
+       ob->lay = lay;
        
        return dob;
 }
 
-static void group_duplilist(ListBase *lb, Scene *scene, Object *ob, int level, int animated)
+static void group_duplilist(ListBase *lb, Scene *scene, Object *ob, int par_index, int level, int animated)
 {
        DupliObject *dob;
        Group *group;
        GroupObject *go;
        float mat[4][4], tmat[4][4];
        
-       if (ob->dup_group==NULL) return;
-       group= ob->dup_group;
+       if (ob->dup_group == NULL) return;
+       group = ob->dup_group;
        
        /* simple preventing of too deep nested groups */
-       if (level>MAX_DUPLI_RECUR) return;
+       if (level > MAX_DUPLI_RECUR) return;
        
        /* handles animated groups, and */
        /* we need to check update for objects that are not in scene... */
        group_handle_recalc_and_update(scene, ob, group);
-       animated= animated || group_is_animated(ob, group);
+       animated = animated || group_is_animated(ob, group);
        
-       for (go= group->gobject.first; go; go= go->next) {
+       for (go = group->gobject.first; go; go = go->next) {
                /* note, if you check on layer here, render goes wrong... it still deforms verts and uses parent imat */
-               if (go->ob!=ob) {
+               if (go->ob != ob) {
                        
                        /* group dupli offset, should apply after everything else */
                        if (!is_zero_v3(group->dupli_ofs)) {
@@ -735,31 +750,31 @@ static void group_duplilist(ListBase *lb, Scene *scene, Object *ob, int level, i
                                mult_m4_m4m4(mat, ob->obmat, go->ob->obmat);
                        }
                        
-                       dob= new_dupli_object(lb, go->ob, mat, ob->lay, 0, OB_DUPLIGROUP, animated);
+                       dob = new_dupli_object(lb, go->ob, mat, ob->lay, 0, par_index, OB_DUPLIGROUP, animated);
 
                        /* check the group instance and object layers match, also that the object visible flags are ok. */
-                       if (    (dob->origlay & group->layer)==0 ||
-                               (G.rendering==0 && dob->ob->restrictflag & OB_RESTRICT_VIEW) ||
-                               (G.rendering && dob->ob->restrictflag & OB_RESTRICT_RENDER)
-                       {
-                               dob->no_draw= 1;
+                       if ((dob->origlay & group->layer) == 0 ||
+                           ((G.is_rendering == FALSE) && dob->ob->restrictflag & OB_RESTRICT_VIEW) ||
+                           ((G.is_rendering == TRUE)  && dob->ob->restrictflag & OB_RESTRICT_RENDER))
+                       {
+                               dob->no_draw = TRUE;
                        }
                        else {
-                               dob->no_draw= 0;
+                               dob->no_draw = FALSE;
                        }
 
                        if (go->ob->transflag & OB_DUPLI) {
                                copy_m4_m4(dob->ob->obmat, dob->mat);
-                               object_duplilist_recursive(&group->id, scene, go->ob, lb, ob->obmat, level+1, animated);
+                               object_duplilist_recursive(&group->id, scene, go->ob, lb, ob->obmat, par_index, level + 1, animated);
                                copy_m4_m4(dob->ob->obmat, dob->omat);
                        }
                }
        }
 }
 
-static void frames_duplilist(ListBase *lb, Scene *scene, Object *ob, int level, int animated)
+static void frames_duplilist(ListBase *lb, Scene *scene, Object *ob, int par_index, int level, int animated)
 {
-       extern int enable_cu_speed;     /* object.c */
+       extern int enable_cu_speed; /* object.c */
        Object copyob;
        int cfrao = scene->r.cfra;
        int dupend = ob->dupend;
@@ -770,7 +785,7 @@ static void frames_duplilist(ListBase *lb, Scene *scene, Object *ob, int level,
        /* if we don't have any data/settings which will lead to object movement,
         * don't waste time trying, as it will all look the same...
         */
-       if (ob->parent==NULL && ob->constraints.first==NULL && ob->adt==NULL) 
+       if (ob->parent == NULL && ob->constraints.first == NULL && ob->adt == NULL)
                return;
        
        /* make a copy of the object's original data (before any dupli-data overwrites it) 
@@ -781,18 +796,18 @@ static void frames_duplilist(ListBase *lb, Scene *scene, Object *ob, int level,
        copyob = *ob;
        
        /* duplicate over the required range */
-       if (ob->transflag & OB_DUPLINOSPEED) enable_cu_speed= 0;
+       if (ob->transflag & OB_DUPLINOSPEED) enable_cu_speed = 0;
        
-       for (scene->r.cfra= ob->dupsta; scene->r.cfra<=dupend; scene->r.cfra++) {
-               short ok= 1;
+       for (scene->r.cfra = ob->dupsta; scene->r.cfra <= dupend; scene->r.cfra++) {
+               short ok = 1;
                
                /* - dupoff = how often a frames within the range shouldn't be made into duplis
                 * - dupon = the length of each "skipping" block in frames
                 */
                if (ob->dupoff) {
-                       ok= scene->r.cfra - ob->dupsta;
-                       ok= ok % (ob->dupon+ob->dupoff);
-                       ok= (ok < ob->dupon);
+                       ok = scene->r.cfra - ob->dupsta;
+                       ok = ok % (ob->dupon + ob->dupoff);
+                       ok = (ok < ob->dupon);
                }
                
                if (ok) {       
@@ -803,22 +818,22 @@ static void frames_duplilist(ListBase *lb, Scene *scene, Object *ob, int level,
                         * However, this has always been the way that this worked (i.e. pre 2.5), so I guess that it'll be fine!
                         */
                        BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, (float)scene->r.cfra, ADT_RECALC_ANIM); /* ob-eval will do drivers, so we don't need to do them */
-                       where_is_object_time(scene, ob, (float)scene->r.cfra);
+                       BKE_object_where_is_calc_time(scene, ob, (float)scene->r.cfra);
                        
-                       dob= new_dupli_object(lb, ob, ob->obmat, ob->lay, scene->r.cfra, OB_DUPLIFRAMES, animated);
+                       dob = new_dupli_object(lb, ob, ob->obmat, ob->lay, scene->r.cfra, par_index, OB_DUPLIFRAMES, animated);
                        copy_m4_m4(dob->omat, copyob.obmat);
                }
        }
 
-       enable_cu_speed= 1;
+       enable_cu_speed = 1;
        
        /* reset frame to original frame, then re-evaluate animation as above 
         * as 2.5 animation data may have far-reaching consequences
         */
-       scene->r.cfra= cfrao;
+       scene->r.cfra = cfrao;
        
        BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, (float)scene->r.cfra, ADT_RECALC_ANIM); /* ob-eval will do drivers, so we don't need to do them */
-       where_is_object_time(scene, ob, (float)scene->r.cfra);
+       BKE_object_where_is_calc_time(scene, ob, (float)scene->r.cfra);
        
        /* but, to make sure unkeyed object transforms are still sane, 
         * let's copy object's original data back over
@@ -826,7 +841,7 @@ static void frames_duplilist(ListBase *lb, Scene *scene, Object *ob, int level,
        *ob = copyob;
 }
 
-typedef struct vertexDupliData {
+typedef struct VertexDupliData {
        ID *id; /* scene or group, for recursive loops */
        int level;
        int animated;
@@ -836,7 +851,8 @@ typedef struct vertexDupliData {
        Scene *scene;
        Object *ob, *par;
        float (*orco)[3];
-} vertexDupliData;
+       int par_index;
+} VertexDupliData;
 
 /* ------------- */
 
@@ -844,7 +860,7 @@ static void vertex_dupli__mapFunc(void *userData, int index, const float co[3],
                                   const float no_f[3], const short no_s[3])
 {
        DupliObject *dob;
-       vertexDupliData *vdd= userData;
+       VertexDupliData *vdd = userData;
        float vec[3], q2[4], mat[3][3], tmat[4][4], obmat[4][4];
        int origlay;
        
@@ -857,22 +873,22 @@ static void vertex_dupli__mapFunc(void *userData, int index, const float co[3],
        
        if (vdd->par->transflag & OB_DUPLIROT) {
                if (no_f) {
-                       vec[0]= -no_f[0]; vec[1]= -no_f[1]; vec[2]= -no_f[2];
+                       vec[0] = -no_f[0]; vec[1] = -no_f[1]; vec[2] = -no_f[2];
                }
                else if (no_s) {
-                       vec[0]= -no_s[0]; vec[1]= -no_s[1]; vec[2]= -no_s[2];
+                       vec[0] = -no_s[0]; vec[1] = -no_s[1]; vec[2] = -no_s[2];
                }
                
-               vec_to_quat( q2,vec, vdd->ob->trackflag, vdd->ob->upflag);
+               vec_to_quat(q2, vec, vdd->ob->trackflag, vdd->ob->upflag);
                
-               quat_to_mat3( mat,q2);
+               quat_to_mat3(mat, q2);
                copy_m4_m4(tmat, obmat);
                mul_m4_m4m3(obmat, tmat, mat);
        }
 
        origlay = vdd->ob->lay;
        
-       dob= new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index, OB_DUPLIVERTS, vdd->animated);
+       dob = new_dupli_object(vdd->lb, vdd->ob, obmat, vdd->par->lay, index, vdd->par_index, OB_DUPLIVERTS, vdd->animated);
 
        /* restore the original layer so that each dupli will have proper dob->origlay */
        vdd->ob->lay = origlay;
@@ -884,21 +900,21 @@ static void vertex_dupli__mapFunc(void *userData, int index, const float co[3],
                float tmpmat[4][4];
                copy_m4_m4(tmpmat, vdd->ob->obmat);
                copy_m4_m4(vdd->ob->obmat, obmat); /* pretend we are really this mat */
-               object_duplilist_recursive((ID *)vdd->id, vdd->scene, vdd->ob, vdd->lb, obmat, vdd->level+1, vdd->animated);
+               object_duplilist_recursive((ID *)vdd->id, vdd->scene, vdd->ob, vdd->lb, obmat, vdd->par_index, vdd->level + 1, vdd->animated);
                copy_m4_m4(vdd->ob->obmat, tmpmat);
        }
 }
 
-static void vertex_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int level, int animated)
+static void vertex_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int par_index, int level, int animated)
 {
        Object *ob, *ob_iter;
-       Mesh *me= par->data;
+       Mesh *me = par->data;
        Base *base = NULL;
        DerivedMesh *dm;
-       vertexDupliData vdd;
+       VertexDupliData vdd;
        Scene *sce = NULL;
        Group *group = NULL;
-       GroupObject * go = NULL;
+       GroupObject *go = NULL;
        BMEditMesh *em;
        float vec[3], no[3], pmat[4][4];
        int totvert, a, oblay;
@@ -907,54 +923,54 @@ static void vertex_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, fl
        copy_m4_m4(pmat, par->obmat);
        
        /* simple preventing of too deep nested groups */
-       if (level>MAX_DUPLI_RECUR) return;
+       if (level > MAX_DUPLI_RECUR) return;
        
        em = me->edit_btmesh;
        
        if (em) {
-               dm= editbmesh_get_derived_cage(scene, par, em, CD_MASK_BAREMESH);
+               dm = editbmesh_get_derived_cage(scene, par, em, CD_MASK_BAREMESH);
        }
        else
-               dm= mesh_get_derived_deform(scene, par, CD_MASK_BAREMESH);
+               dm = mesh_get_derived_deform(scene, par, CD_MASK_BAREMESH);
        
-       if (G.rendering) {
-               vdd.orco= (float(*)[3])get_mesh_orco_verts(par);
-               transform_mesh_orco_verts(me, vdd.orco, me->totvert, 0);
+       if (G.is_rendering) {
+               vdd.orco = (float(*)[3])BKE_mesh_orco_verts_get(par);
+               BKE_mesh_orco_verts_transform(me, vdd.orco, me->totvert, 0);
        }
        else
-               vdd.orco= NULL;
+               vdd.orco = NULL;
        
        totvert = dm->getNumVerts(dm);
 
        /* having to loop on scene OR group objects is NOT FUN */
        if (GS(id->name) == ID_SCE) {
                sce = (Scene *)id;
-               lay= sce->lay;
-               base= sce->base.first;
+               lay = sce->lay;
+               base = sce->base.first;
        }
        else {
                group = (Group *)id;
-               lay= group->layer;
+               lay = group->layer;
                go = group->gobject.first;
        }
        
        /* Start looping on Scene OR Group objects */
        while (base || go) { 
                if (sce) {
-                       ob_iter= base->object;
+                       ob_iter = base->object;
                        oblay = base->lay;
                }
                else {
-                       ob_iter= go->ob;
+                       ob_iter = go->ob;
                        oblay = ob_iter->lay;
                }
                
-               if (lay & oblay && scene->obedit!=ob_iter) {
-                       ob=ob_iter->parent;
+               if (lay & oblay && scene->obedit != ob_iter) {
+                       ob = ob_iter->parent;
                        while (ob) {
-                               if (ob==par) {
+                               if (ob == par) {
                                        ob = ob_iter;
-       /* End Scene/Group object loop, below is generic */
+                                       /* End Scene/Group object loop, below is generic */
                                        
                                        
                                        /* par_space_mat - only used for groups so we can modify the space dupli's are in
@@ -965,23 +981,24 @@ static void vertex_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, fl
                                        else
                                                copy_m4_m4(vdd.obmat, ob->obmat);
 
-                                       vdd.id= id;
-                                       vdd.level= level;
-                                       vdd.animated= animated;
-                                       vdd.lb= lb;
-                                       vdd.ob= ob;
-                                       vdd.scene= scene;
-                                       vdd.par= par;
+                                       vdd.id = id;
+                                       vdd.level = level;
+                                       vdd.animated = animated;
+                                       vdd.lb = lb;
+                                       vdd.ob = ob;
+                                       vdd.scene = scene;
+                                       vdd.par = par;
                                        copy_m4_m4(vdd.pmat, pmat);
+                                       vdd.par_index = par_index;
                                        
                                        /* mballs have a different dupli handling */
-                                       if (ob->type!=OB_MBALL) ob->flag |= OB_DONE;    /* doesnt render */
+                                       if (ob->type != OB_MBALL) ob->flag |= OB_DONE;  /* doesnt render */
 
                                        if (me->edit_btmesh) {
-                                               dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void*) &vdd);
+                                               dm->foreachMappedVert(dm, vertex_dupli__mapFunc, (void *) &vdd);
                                        }
                                        else {
-                                               for (a=0; a<totvert; a++) {
+                                               for (a = 0; a < totvert; a++) {
                                                        dm->getVertCo(dm, a, vec);
                                                        dm->getVertNo(dm, a, no);
                                                        
@@ -999,11 +1016,11 @@ static void vertex_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, fl
                                        
                                        break;
                                }
-                               ob= ob->parent;
+                               ob = ob->parent;
                        }
                }
-               if (sce)        base= base->next;       /* scene loop */
-               else            go= go->next;           /* group loop */
+               if (sce) base = base->next;     /* scene loop */
+               else go = go->next;             /* group loop */
        }
 
        if (vdd.orco)
@@ -1011,13 +1028,13 @@ static void vertex_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, fl
        dm->release(dm);
 }
 
-static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int level, int animated)
+static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int par_index, int level, int animated)
 {
        Object *ob, *ob_iter;
        Base *base = NULL;
        DupliObject *dob;
        DerivedMesh *dm;
-       Mesh *me= par->data;
+       Mesh *me = par->data;
        MLoopUV *mloopuv;
        MPoly *mpoly, *mp;
        MLoop *mloop;
@@ -1031,63 +1048,63 @@ static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, floa
        float ob__obmat[4][4]; /* needed for groups where the object matrix needs to be modified */
        
        /* simple preventing of too deep nested groups */
-       if (level>MAX_DUPLI_RECUR) return;
+       if (level > MAX_DUPLI_RECUR) return;
        
        copy_m4_m4(pmat, par->obmat);
        em = me->edit_btmesh;
 
        if (em) {
-               dm= editbmesh_get_derived_cage(scene, par, em, CD_MASK_BAREMESH);
+               dm = editbmesh_get_derived_cage(scene, par, em, CD_MASK_BAREMESH);
        }
        else {
                dm = mesh_get_derived_deform(scene, par, CD_MASK_BAREMESH);
        }
 
-       totface= dm->getNumPolys(dm);
-       mpoly= dm->getPolyArray(dm);
-       mloop= dm->getLoopArray(dm);
-       mvert= dm->getVertArray(dm);
+       totface = dm->getNumPolys(dm);
+       mpoly = dm->getPolyArray(dm);
+       mloop = dm->getLoopArray(dm);
+       mvert = dm->getVertArray(dm);
 
-       if (G.rendering) {
+       if (G.is_rendering) {
 
-               orco= (float(*)[3])get_mesh_orco_verts(par);
-               transform_mesh_orco_verts(me, orco, me->totvert, 0);
-               mloopuv= me->mloopuv;
+               orco = (float(*)[3])BKE_mesh_orco_verts_get(par);
+               BKE_mesh_orco_verts_transform(me, orco, me->totvert, 0);
+               mloopuv = me->mloopuv;
        }
        else {
-               orco= NULL;
-               mloopuv= NULL;
+               orco = NULL;
+               mloopuv = NULL;
        }
        
        /* having to loop on scene OR group objects is NOT FUN */
        if (GS(id->name) == ID_SCE) {
                sce = (Scene *)id;
-               lay= sce->lay;
-               base= sce->base.first;
+               lay = sce->lay;
+               base = sce->base.first;
        }
        else {
                group = (Group *)id;
-               lay= group->layer;
+               lay = group->layer;
                go = group->gobject.first;
        }
        
        /* Start looping on Scene OR Group objects */
        while (base || go) { 
                if (sce) {
-                       ob_iter= base->object;
+                       ob_iter = base->object;
                        oblay = base->lay;
                }
                else {
-                       ob_iter= go->ob;
+                       ob_iter = go->ob;
                        oblay = ob_iter->lay;
                }
                
-               if (lay & oblay && scene->obedit!=ob_iter) {
-                       ob=ob_iter->parent;
+               if (lay & oblay && scene->obedit != ob_iter) {
+                       ob = ob_iter->parent;
                        while (ob) {
-                               if (ob==par) {
+                               if (ob == par) {
                                        ob = ob_iter;
-       /* End Scene/Group object loop, below is generic */
+                                       /* End Scene/Group object loop, below is generic */
                                        
                                        /* par_space_mat - only used for groups so we can modify the space dupli's are in
                                         * when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
@@ -1100,9 +1117,9 @@ static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, floa
                                        copy_m3_m4(imat, ob->parentinv);
                                                
                                        /* mballs have a different dupli handling */
-                                       if (ob->type!=OB_MBALL) ob->flag |= OB_DONE;    /* doesnt render */
+                                       if (ob->type != OB_MBALL) ob->flag |= OB_DONE;  /* doesnt render */
 
-                                       for (a=0, mp= mpoly; a<totface; a++, mp++) {
+                                       for (a = 0, mp = mpoly; a < totface; a++, mp++) {
                                                int mv1;
                                                int mv2;
                                                int mv3;
@@ -1112,25 +1129,25 @@ static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, floa
                                                float *v3;
                                                /* float *v4; */ /* UNUSED */
                                                float cent[3], quat[4], mat[3][3], mat3[3][3], tmat[4][4], obmat[4][4];
-                                               MLoop *loopstart= mloop + mp->loopstart;
+                                               MLoop *loopstart = mloop + mp->loopstart;
 
                                                if (mp->totloop < 3) {
                                                        /* highly unlikely but to be safe */
                                                        continue;
                                                }
                                                else {
-                                                       v1= mvert[(mv1= loopstart[0].v)].co;
-                                                       v2= mvert[(mv2= loopstart[1].v)].co;
-                                                       v3= mvert[(mv3= loopstart[2].v)].co;
+                                                       v1 = mvert[(mv1 = loopstart[0].v)].co;
+                                                       v2 = mvert[(mv2 = loopstart[1].v)].co;
+                                                       v3 = mvert[(mv3 = loopstart[2].v)].co;
 #if 0
                                                        if (mp->totloop > 3) {
-                                                               v4= mvert[(mv4= loopstart[3].v)].co;
+                                                               v4 = mvert[(mv4 = loopstart[3].v)].co;
                                                        }
 #endif
                                                }
 
                                                /* translation */
-                                               mesh_calc_poly_center(mp, loopstart, mvert, cent);
+                                               BKE_mesh_calc_poly_center(mp, loopstart, mvert, cent);
 
                                                mul_m4_v3(pmat, cent);
                                                
@@ -1142,13 +1159,13 @@ static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, floa
                                                copy_v3_v3(obmat[3], cent);
                                                
                                                /* rotation */
-                                               tri_to_quat( quat,v1, v2, v3);
-                                               quat_to_mat3( mat,quat);
+                                               tri_to_quat(quat, v1, v2, v3);
+                                               quat_to_mat3(mat, quat);
                                                
                                                /* scale */
                                                if (par->transflag & OB_DUPLIFACES_SCALE) {
-                                                       float sizemesh_calc_poly_area(mp, loopstart, mvert, NULL);
-                                                       size= sqrtf(size) * par->dupfacesca;
+                                                       float size = BKE_mesh_calc_poly_area(mp, loopstart, mvert, NULL);
+                                                       size = sqrtf(size) * par->dupfacesca;
                                                        mul_m3_fl(mat, size);
                                                }
                                                
@@ -1158,9 +1175,9 @@ static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, floa
                                                copy_m4_m4(tmat, obmat);
                                                mul_m4_m4m3(obmat, tmat, mat);
                                                
-                                               dob= new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIFACES, animated);
-                                               if (G.rendering) {
-                                                       w= 1.0f / (float)mp->totloop;
+                                               dob = new_dupli_object(lb, ob, obmat, par->lay, a, par_index, OB_DUPLIFACES, animated);
+                                               if (G.is_rendering) {
+                                                       w = 1.0f / (float)mp->totloop;
 
                                                        if (orco) {
                                                                int j;
@@ -1181,18 +1198,18 @@ static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, floa
                                                        float tmpmat[4][4];
                                                        copy_m4_m4(tmpmat, ob->obmat);
                                                        copy_m4_m4(ob->obmat, obmat); /* pretend we are really this mat */
-                                                       object_duplilist_recursive((ID *)id, scene, ob, lb, ob->obmat, level+1, animated);
+                                                       object_duplilist_recursive((ID *)id, scene, ob, lb, ob->obmat, par_index, level + 1, animated);
                                                        copy_m4_m4(ob->obmat, tmpmat);
                                                }
                                        }
                                        
                                        break;
                                }
-                               ob= ob->parent;
+                               ob = ob->parent;
                        }
                }
-               if (sce)        base= base->next;       /* scene loop */
-               else            go= go->next;           /* group loop */
+               if (sce) base = base->next;     /* scene loop */
+               else go = go->next;             /* group loop */
        }
 
        if (orco)
@@ -1201,54 +1218,54 @@ static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, floa
        dm->release(dm);
 }
 
-static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], ParticleSystem *psys, int level, int animated)
+static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int UNUSED(par_index), ParticleSystem *psys, int level, int animated)
 {
        GroupObject *go;
-       Object *ob=NULL, **oblist=NULL, obcopy, *obcopylist=NULL;
+       Object *ob = NULL, **oblist = NULL, obcopy, *obcopylist = NULL;
        DupliObject *dob;
        ParticleDupliWeight *dw;
        ParticleSettings *part;
        ParticleData *pa;
-       ChildParticle *cpa=NULL;
+       ChildParticle *cpa = NULL;
        ParticleKey state;
        ParticleCacheKey *cache;
        float ctime, pa_time, scale = 1.0f;
-       float tmat[4][4], mat[4][4], pamat[4][4], vec[3], size=0.0;
+       float tmat[4][4], mat[4][4], pamat[4][4], vec[3], size = 0.0;
        float (*obmat)[4], (*oldobmat)[4];
-       int a, b, counter, hair = 0;
-       int totpart, totchild, totgroup=0 /*, pa_num */;
+       int a, b, counter, index, hair = 0;
+       int totpart, totchild, totgroup = 0 /*, pa_num */;
 
        int no_draw_flag = PARS_UNEXIST;
 
-       if (psys==NULL) return;
+       if (psys == NULL) return;
        
        /* simple preventing of too deep nested groups */
-       if (level>MAX_DUPLI_RECUR) return;
+       if (level > MAX_DUPLI_RECUR) return;
        
-       part=psys->part;
+       part = psys->part;
 
-       if (part==NULL)
+       if (part == NULL)
                return;
 
        if (!psys_check_enabled(par, psys))
                return;
 
-       if (G.rendering == 0)
+       if (G.is_rendering == FALSE)
                no_draw_flag |= PARS_NO_DISP;
        
-       ctime = BKE_curframe(scene); /* NOTE: in old animsys, used parent object's timeoffset... */
+       ctime = BKE_scene_frame_get(scene); /* NOTE: in old animsys, used parent object's timeoffset... */
 
        totpart = psys->totpart;
        totchild = psys->totchild;
 
        BLI_srandom(31415926 + psys->seed);
 
-       if ((psys->renderdata || part->draw_as==PART_DRAW_REND) && ELEM(part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
-               ParticleSimulationData sim= {NULL};
-               sim.scene= scene;
-               sim.ob= par;
-               sim.psys= psys;
-               sim.psmd= psys_get_modifier(par, psys);
+       if ((psys->renderdata || part->draw_as == PART_DRAW_REND) && ELEM(part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
+               ParticleSimulationData sim = {NULL};
+               sim.scene = scene;
+               sim.ob = par;
+               sim.psys = psys;
+               sim.psmd = psys_get_modifier(par, psys);
                /* make sure emitter imat is in global coordinates instead of render view coordinates */
                invert_m4_m4(par->imat, par->obmat);
 
@@ -1261,15 +1278,15 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                        if (part->dup_group == NULL || part->dup_group->gobject.first == NULL)
                                return;
 
-                       for (go=part->dup_group->gobject.first; go; go=go->next)
-                               if (go->ob == par)
-                                       return;
+                       if (BLI_findptr(&part->dup_group->gobject, par, offsetof(GroupObject, ob))) {
+                               return;
+                       }
                }
 
                /* if we have a hair particle system, use the path cache */
                if (part->type == PART_HAIR) {
                        if (psys->flag & PSYS_HAIR_DONE)
-                               hair= (totchild == 0 || psys->childcache) && psys->pathcache;
+                               hair = (totchild == 0 || psys->childcache) && psys->pathcache;
                        if (!hair)
                                return;
                        
@@ -1283,29 +1300,29 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                psys->lattice = psys_get_lattice(&sim);
 
                /* gather list of objects or single object */
-               if (part->ren_as==PART_DRAW_GR) {
+               if (part->ren_as == PART_DRAW_GR) {
                        group_handle_recalc_and_update(scene, par, part->dup_group);
 
                        if (part->draw & PART_DRAW_COUNT_GR) {
-                               for (dw=part->dupliweights.first; dw; dw=dw->next)
+                               for (dw = part->dupliweights.first; dw; dw = dw->next)
                                        totgroup += dw->count;
                        }
                        else {
-                               for (go=part->dup_group->gobject.first; go; go=go->next)
+                               for (go = part->dup_group->gobject.first; go; go = go->next)
                                        totgroup++;
                        }
 
                        /* we also copy the actual objects to restore afterwards, since
-                        * where_is_object_time will change the object which breaks transform */
-                       oblist = MEM_callocN(totgroup*sizeof(Object *), "dupgroup object list");
-                       obcopylist = MEM_callocN(totgroup*sizeof(Object), "dupgroup copy list");
+                        * BKE_object_where_is_calc_time will change the object which breaks transform */
+                       oblist = MEM_callocN(totgroup * sizeof(Object *), "dupgroup object list");
+                       obcopylist = MEM_callocN(totgroup * sizeof(Object), "dupgroup copy list");
 
                        
                        if (part->draw & PART_DRAW_COUNT_GR && totgroup) {
                                dw = part->dupliweights.first;
 
-                               for (a=0; a<totgroup; dw=dw->next) {
-                                       for (b=0; b<dw->count; b++, a++) {
+                               for (a = 0; a < totgroup; dw = dw->next) {
+                                       for (b = 0; b < dw->count; b++, a++) {
                                                oblist[a] = dw->ob;
                                                obcopylist[a] = *dw->ob;
                                        }
@@ -1313,7 +1330,7 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                        }
                        else {
                                go = part->dup_group->gobject.first;
-                               for (a=0; a<totgroup; a++, go=go->next) {
+                               for (a = 0; a < totgroup; a++, go = go->next) {
                                        oblist[a] = go->ob;
                                        obcopylist[a] = *go->ob;
                                }
@@ -1324,13 +1341,14 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                        obcopy = *ob;
                }
 
-               if (totchild==0 || part->draw & PART_DRAW_PARENT)
+               if (totchild == 0 || part->draw & PART_DRAW_PARENT)
                        a = 0;
                else
                        a = totpart;
 
-               for (pa=psys->particles,counter=0; a<totpart+totchild; a++,pa++,counter++) {
-                       if (a<totpart) {
+               index = 0;
+               for (pa = psys->particles, counter = 0; a < totpart + totchild; a++, pa++, counter++) {
+                       if (a < totpart) {
                                /* handle parent particle */
                                if (pa->flag & no_draw_flag)
                                        continue;
@@ -1351,29 +1369,29 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                        /* some hair paths might be non-existent so they can't be used for duplication */
                        if (hair &&
                            ((a < totpart && psys->pathcache[a]->steps < 0) ||
-                            (a >= totpart && psys->childcache[a-totpart]->steps < 0)))
+                            (a >= totpart && psys->childcache[a - totpart]->steps < 0)))
                        {
                                continue;
                        }
 
-                       if (part->ren_as==PART_DRAW_GR) {
+                       if (part->ren_as == PART_DRAW_GR) {
                                /* prevent divide by zero below [#28336] */
                                if (totgroup == 0)
                                        continue;
 
                                /* for groups, pick the object based on settings */
-                               if (part->draw&PART_DRAW_RAND_GR)
-                                       b= BLI_rand() % totgroup;
+                               if (part->draw & PART_DRAW_RAND_GR)
+                                       b = BLI_rand() % totgroup;
                                else
-                                       b= a % totgroup;
+                                       b = a % totgroup;
 
                                ob = oblist[b];
                                obmat = oblist[b]->obmat;
                                oldobmat = obcopylist[b].obmat;
                        }
                        else {
-                               obmat= ob->obmat;
-                               oldobmat= obcopy.obmat;
+                               obmat = ob->obmat;
+                               oldobmat = obcopy.obmat;
                        }
 
                        if (hair) {
@@ -1383,12 +1401,12 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                                        psys_get_dupli_path_transform(&sim, pa, NULL, cache, pamat, &scale);
                                }
                                else {
-                                       cache = psys->childcache[a-totpart];
+                                       cache = psys->childcache[a - totpart];
                                        psys_get_dupli_path_transform(&sim, NULL, cpa, cache, pamat, &scale);
                                }
 
                                copy_v3_v3(pamat[3], cache->co);
-                               pamat[3][3]= 1.0f;
+                               pamat[3][3] = 1.0f;
                                
                        }
                        else {
@@ -1402,17 +1420,17 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                                        normalize_qt_qt(tquat, state.rot);
                                        quat_to_mat4(pamat, tquat);
                                        copy_v3_v3(pamat[3], state.co);
-                                       pamat[3][3]= 1.0f;
+                                       pamat[3][3] = 1.0f;
                                }
                        }
 
-                       if (part->ren_as==PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
-                               for (go= part->dup_group->gobject.first, b=0; go; go= go->next, b++) {
+                       if (part->ren_as == PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
+                               for (go = part->dup_group->gobject.first, b = 0; go; go = go->next, b++) {
 
                                        copy_m4_m4(tmat, oblist[b]->obmat);
                                        /* apply particle scale */
-                                       mul_mat3_m4_fl(tmat, size*scale);
-                                       mul_v3_fl(tmat[3], size*scale);
+                                       mul_mat3_m4_fl(tmat, size * scale);
+                                       mul_v3_fl(tmat[3], size * scale);
                                        /* group dupli offset, should apply after everything else */
                                        if (!is_zero_v3(part->dup_group->dupli_ofs))
                                                sub_v3_v3v3(tmat[3], tmat[3], part->dup_group->dupli_ofs);
@@ -1424,27 +1442,45 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                                        else
                                                copy_m4_m4(mat, tmat);
 
-                                       dob= new_dupli_object(lb, go->ob, mat, par->lay, counter, OB_DUPLIPARTS, animated);
+                                       dob = new_dupli_object(lb, go->ob, mat, par->lay, counter, index, OB_DUPLIPARTS, animated);
                                        copy_m4_m4(dob->omat, obcopylist[b].obmat);
-                                       if (G.rendering)
+                                       if (G.is_rendering)
                                                psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
                                }
                        }
                        else {
                                /* to give ipos in object correct offset */
-                               where_is_object_time(scene, ob, ctime-pa_time);
+                               BKE_object_where_is_calc_time(scene, ob, ctime - pa_time);
 
                                copy_v3_v3(vec, obmat[3]);
                                obmat[3][0] = obmat[3][1] = obmat[3][2] = 0.0f;
 
                                /* particle rotation uses x-axis as the aligned axis, so pre-rotate the object accordingly */
                                if ((part->draw & PART_DRAW_ROTATE_OB) == 0) {
-                                       float xvec[3], q[4];
+                                       float xvec[3], q[4], size_mat[4][4], original_size[3];
+
+                                       mat4_to_size(original_size, obmat);
+                                       size_to_mat4(size_mat, original_size);
+
                                        xvec[0] = -1.f;
                                        xvec[1] = xvec[2] = 0;
                                        vec_to_quat(q, xvec, ob->trackflag, ob->upflag);
                                        quat_to_mat4(obmat, q);
-                                       obmat[3][3]= 1.0f;
+                                       obmat[3][3] = 1.0f;
+                                       
+                                       /* add scaling if requested */
+                                       if ((part->draw & PART_DRAW_NO_SCALE_OB) == 0)
+                                               mult_m4_m4m4(obmat, obmat, size_mat);
+                               }
+                               else if (part->draw & PART_DRAW_NO_SCALE_OB) {
+                                       /* remove scaling */
+                                       float size_mat[4][4], original_size[3];
+
+                                       mat4_to_size(original_size, obmat);
+                                       size_to_mat4(size_mat, original_size);
+                                       invert_m4(size_mat);
+
+                                       mult_m4_m4m4(obmat, obmat, size_mat);
                                }
                                
                                /* Normal particles and cached hair live in global space so we need to
@@ -1456,7 +1492,7 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                                        copy_m4_m4(mat, pamat);
 
                                mult_m4_m4m4(tmat, mat, obmat);
-                               mul_mat3_m4_fl(tmat, size*scale);
+                               mul_mat3_m4_fl(tmat, size * scale);
 
                                if (par_space_mat)
                                        mult_m4_m4m4(mat, par_space_mat, tmat);
@@ -1466,20 +1502,23 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p
                                if (part->draw & PART_DRAW_GLOBAL_OB)
                                        add_v3_v3v3(mat[3], mat[3], vec);
 
-                               dob= new_dupli_object(lb, ob, mat, ob->lay, counter, GS(id->name) == ID_GR ? OB_DUPLIGROUP : OB_DUPLIPARTS, animated);
+                               dob = new_dupli_object(lb, ob, mat, ob->lay, counter, index, GS(id->name) == ID_GR ? OB_DUPLIGROUP : OB_DUPLIPARTS, animated);
                                copy_m4_m4(dob->omat, oldobmat);
-                               if (G.rendering)
+                               if (G.is_rendering)
                                        psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
                        }
+
+                       /* only counts visible particles */
+                       index++;
                }
 
-               /* restore objects since they were changed in where_is_object_time */
-               if (part->ren_as==PART_DRAW_GR) {
-                       for (a=0; a<totgroup; a++)
-                               *(oblist[a])= obcopylist[a];
+               /* restore objects since they were changed in BKE_object_where_is_calc_time */
+               if (part->ren_as == PART_DRAW_GR) {
+                       for (a = 0; a < totgroup; a++)
+                               *(oblist[a]) = obcopylist[a];
                }
                else
-                       *ob= obcopy;
+                       *ob = obcopy;
        }
 
        /* clean up */
@@ -1499,64 +1538,64 @@ static Object *find_family_object(Object **obar, char *family, char ch)
        Object *ob;
        int flen;
        
-       if ( obar[(int)ch] ) return obar[(int)ch];
+       if (obar[(int)ch]) return obar[(int)ch];
        
-       flen= strlen(family);
+       flen = strlen(family);
        
-       ob= G.main->object.first;
+       ob = G.main->object.first;
        while (ob) {
-               if ( ob->id.name[flen+2]==ch ) {
-                       if ( strncmp(ob->id.name+2, family, flen)==0 ) break;
+               if (ob->id.name[flen + 2] == ch) {
+                       if (strncmp(ob->id.name + 2, family, flen) == 0) break;
                }
-               ob= ob->id.next;
+               ob = ob->id.next;
        }
        
-       obar[(int)ch]= ob;
+       obar[(int)ch] = ob;
        
        return ob;
 }
 
 
-static void font_duplilist(ListBase *lb, Scene *scene, Object *par, int level, int animated)
+static void font_duplilist(ListBase *lb, Scene *scene, Object *par, int par_index, int level, int animated)
 {
-       Object *ob, *obar[256]= {NULL};
+       Object *ob, *obar[256] = {NULL};
        Curve *cu;
-       struct chartrans *ct, *chartransdata;
+       struct CharTrans *ct, *chartransdata;
        float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
        int slen, a;
        
        /* simple preventing of too deep nested groups */
-       if (level>MAX_DUPLI_RECUR) return;
+       if (level > MAX_DUPLI_RECUR) return;
        
        copy_m4_m4(pmat, par->obmat);
        
        /* in par the family name is stored, use this to find the other objects */
        
-       chartransdata= BKE_text_to_curve(G.main, scene, par, FO_DUPLI);
-       if (chartransdata==NULL) return;
+       chartransdata = BKE_vfont_to_curve(G.main, scene, par, FO_DUPLI);
+       if (chartransdata == NULL) return;
 
-       cu= par->data;
-       slen= strlen(cu->str);
-       fsize= cu->fsize;
-       xof= cu->xof;
-       yof= cu->yof;
+       cu = par->data;
+       slen = strlen(cu->str);
+       fsize = cu->fsize;
+       xof = cu->xof;
+       yof = cu->yof;
        
-       ct= chartransdata;
+       ct = chartransdata;
        
-       for (a=0; a<slen; a++, ct++) {
+       for (a = 0; a < slen; a++, ct++) {
                
-               ob= find_family_object(obar, cu->family, cu->str[a]);
+               ob = find_family_object(obar, cu->family, cu->str[a]);
                if (ob) {
-                       vec[0]= fsize*(ct->xof - xof);
-                       vec[1]= fsize*(ct->yof - yof);
-                       vec[2]= 0.0;
+                       vec[0] = fsize * (ct->xof - xof);
+                       vec[1] = fsize * (ct->yof - yof);
+                       vec[2] = 0.0;
                        
                        mul_m4_v3(pmat, vec);
                        
                        copy_m4_m4(obmat, par->obmat);
                        copy_v3_v3(obmat[3], vec);
                        
-                       new_dupli_object(lb, ob, obmat, par->lay, a, OB_DUPLIVERTS, animated);
+                       new_dupli_object(lb, ob, obmat, par->lay, a, par_index, OB_DUPLIVERTS, animated);
                }
        }
        
@@ -1565,13 +1604,13 @@ static void font_duplilist(ListBase *lb, Scene *scene, Object *par, int level, i
 
 /* ------------- */
 
-static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBase *duplilist, float par_space_mat[][4], int level, int animated)
+static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBase *duplilist, float par_space_mat[][4], int par_index, int level, int animated)
 {      
-       if ((ob->transflag & OB_DUPLI)==0)
+       if ((ob->transflag & OB_DUPLI) == 0)
                return;
        
        /* Should the dupli's be generated for this object? - Respect restrict flags */
-       if (G.rendering) {
+       if (G.is_rendering) {
                if (ob->restrictflag & OB_RESTRICT_RENDER) {
                        return;
                }
@@ -1584,35 +1623,35 @@ static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBas
 
        if (ob->transflag & OB_DUPLIPARTS) {
                ParticleSystem *psys = ob->particlesystem.first;
-               for (; psys; psys=psys->next)
-                       new_particle_duplilist(duplilist, id, scene, ob, par_space_mat, psys, level+1, animated);
+               for (; psys; psys = psys->next)
+                       new_particle_duplilist(duplilist, id, scene, ob, par_space_mat, par_index, psys, level + 1, animated);
        }
        else if (ob->transflag & OB_DUPLIVERTS) {
-               if (ob->type==OB_MESH) {
-                       vertex_duplilist(duplilist, id, scene, ob, par_space_mat, level+1, animated);
+               if (ob->type == OB_MESH) {
+                       vertex_duplilist(duplilist, id, scene, ob, par_space_mat, par_index, level + 1, animated);
                }
-               else if (ob->type==OB_FONT) {
-                       if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
-                               font_duplilist(duplilist, scene, ob, level+1, animated);
+               else if (ob->type == OB_FONT) {
+                       if (GS(id->name) == ID_SCE) { /* TODO - support dupligroups */
+                               font_duplilist(duplilist, scene, ob, par_index, level + 1, animated);
                        }
                }
        }
        else if (ob->transflag & OB_DUPLIFACES) {
-               if (ob->type==OB_MESH)
-                       face_duplilist(duplilist, id, scene, ob, par_space_mat, level+1, animated);
+               if (ob->type == OB_MESH)
+                       face_duplilist(duplilist, id, scene, ob, par_space_mat, par_index, level + 1, animated);
        }
        else if (ob->transflag & OB_DUPLIFRAMES) {
-               if (GS(id->name)==ID_SCE) { /* TODO - support dupligroups */
-                       frames_duplilist(duplilist, scene, ob, level+1, animated);
+               if (GS(id->name) == ID_SCE) { /* TODO - support dupligroups */
+                       frames_duplilist(duplilist, scene, ob, par_index, level + 1, animated);
                }
        }
        else if (ob->transflag & OB_DUPLIGROUP) {
                DupliObject *dob;
                
-               group_duplilist(duplilist, scene, ob, level+1, animated); /* now recursive */
+               group_duplilist(duplilist, scene, ob, par_index, level + 1, animated); /* now recursive */
 
-               if (level==0) {
-                       for (dob= duplilist->first; dob; dob= dob->next)
+               if (level == 0) {
+                       for (dob = duplilist->first; dob; dob = dob->next)
                                if (dob->type == OB_DUPLIGROUP)
                                        copy_m4_m4(dob->ob->obmat, dob->mat);
                }
@@ -1623,9 +1662,9 @@ static void object_duplilist_recursive(ID *id, Scene *scene, Object *ob, ListBas
  * note; group dupli's already set transform matrix. see note in group_duplilist() */
 ListBase *object_duplilist(Scene *sce, Object *ob)
 {
-       ListBase *duplilist= MEM_mallocN(sizeof(ListBase), "duplilist");
-       duplilist->first= duplilist->last= NULL;
-       object_duplilist_recursive((ID *)sce, sce, ob, duplilist, NULL, 0, 0);
+       ListBase *duplilist = MEM_mallocN(sizeof(ListBase), "duplilist");
+       duplilist->first = duplilist->last = NULL;
+       object_duplilist_recursive((ID *)sce, sce, ob, duplilist, NULL, 0, 0, 0);
        return duplilist;
 }
 
@@ -1636,8 +1675,8 @@ void free_object_duplilist(ListBase *lb)
        /* loop in reverse order, if object is instanced multiple times
         * the original layer may not really be original otherwise, proper
         * solution is more complicated */
-       for (dob= lb->last; dob; dob= dob->prev) {
-               dob->ob->lay= dob->origlay;
+       for (dob = lb->last; dob; dob = dob->prev) {
+               dob->ob->lay = dob->origlay;
                copy_m4_m4(dob->ob->obmat, dob->omat);
        }
        
@@ -1649,16 +1688,16 @@ int count_duplilist(Object *ob)
 {
        if (ob->transflag & OB_DUPLI) {
                if (ob->transflag & OB_DUPLIVERTS) {
-                       if (ob->type==OB_MESH) {
+                       if (ob->type == OB_MESH) {
                                if (ob->transflag & OB_DUPLIVERTS) {
                                        ParticleSystem *psys = ob->particlesystem.first;
-                                       int pdup=0;
+                                       int pdup = 0;
 
-                                       for (; psys; psys=psys->next)
+                                       for (; psys; psys = psys->next)
                                                pdup += psys->totpart;
 
-                                       if (pdup==0) {
-                                               Mesh *me= ob->data;
+                                       if (pdup == 0) {
+                                               Mesh *me = ob->data;
                                                return me->totvert;
                                        }
                                        else
@@ -1667,9 +1706,9 @@ int count_duplilist(Object *ob)
                        }
                }
                else if (ob->transflag & OB_DUPLIFRAMES) {
-                       int tot= ob->dupend - ob->dupsta; 
-                       tot/= (ob->dupon+ob->dupoff);
-                       return tot*ob->dupon;
+                       int tot = ob->dupend - ob->dupsta;
+                       tot /= (ob->dupon + ob->dupoff);
+                       return tot * ob->dupon;
                }
        }
        return 1;