Testing for the need to quick cache was causing slowdowns on files with many duplis.
[blender.git] / source / blender / blenkernel / intern / object.c
index 64e22c85251ad3713b2ce74a867ce450e456c159..ef630a18ab4c4c39c90a44a47de0060aa404d28c 100644 (file)
@@ -17,7 +17,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
 #include "MEM_guardedalloc.h"
 
 #include "DNA_anim_types.h"
-#include "DNA_action_types.h"
 #include "DNA_armature_types.h"
-#include "DNA_boid_types.h"
 #include "DNA_camera_types.h"
 #include "DNA_constraint_types.h"
-#include "DNA_curve_types.h"
 #include "DNA_group_types.h"
+#include "DNA_key_types.h"
 #include "DNA_lamp_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_material_types.h"
-#include "DNA_mesh_types.h"
 #include "DNA_meta_types.h"
-#include "DNA_curve_types.h"
 #include "DNA_meshdata_types.h"
-#include "DNA_modifier_types.h"
-#include "DNA_nla_types.h"
-#include "DNA_object_types.h"
-#include "DNA_object_force.h"
-#include "DNA_object_fluidsim.h"
-#include "DNA_outliner_types.h"
-#include "DNA_particle_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
+#include "DNA_sequence_types.h"
 #include "DNA_space_types.h"
-#include "DNA_texture_types.h"
-#include "DNA_userdef_types.h"
 #include "DNA_view3d_types.h"
 #include "DNA_world_types.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_arithb.h"
 #include "BLI_editVert.h"
+#include "BLI_math.h"
+#include "BLI_pbvh.h"
 
 #include "BKE_utildefines.h"
 
 #include "BKE_colortools.h"
 #include "BKE_deform.h"
 #include "BKE_DerivedMesh.h"
-#include "BKE_nla.h"
 #include "BKE_animsys.h"
 #include "BKE_anim.h"
-#include "BKE_blender.h"
 #include "BKE_constraint.h"
 #include "BKE_curve.h"
 #include "BKE_displist.h"
 #include "BKE_property.h"
 #include "BKE_sca.h"
 #include "BKE_scene.h"
-#include "BKE_screen.h"
+#include "BKE_sequencer.h"
 #include "BKE_softbody.h"
 
 #include "LBM_fluidsim.h"
@@ -166,9 +153,9 @@ void object_free_particlesystems(Object *ob)
 {
        while(ob->particlesystem.first){
                ParticleSystem *psys = ob->particlesystem.first;
-
+               
                BLI_remlink(&ob->particlesystem,psys);
-
+               
                psys_free(ob,psys);
        }
 }
@@ -193,9 +180,9 @@ void object_free_modifiers(Object *ob)
 {
        while (ob->modifiers.first) {
                ModifierData *md = ob->modifiers.first;
-
+               
                BLI_remlink(&ob->modifiers, md);
-
+               
                modifier_free(md);
        }
 
@@ -206,6 +193,27 @@ void object_free_modifiers(Object *ob)
        object_free_softbody(ob);
 }
 
+void object_link_modifiers(struct Object *ob, struct Object *from)
+{
+       ModifierData *md;
+       object_free_modifiers(ob);
+
+       for (md=from->modifiers.first; md; md=md->next) {
+               ModifierData *nmd = NULL;
+
+               if(ELEM4(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance, eModifierType_Collision)) continue;
+
+               nmd = modifier_new(md->type);
+               modifier_copyData(md, nmd);
+               BLI_addtail(&ob->modifiers, nmd);
+       }
+
+       copy_object_particlesystems(from, ob);
+       copy_object_softbody(from, ob);
+
+       // TODO: smoke?, cloth?
+}
+
 /* here we will collect all local displist stuff */
 /* also (ab)used in depsgraph */
 void object_free_display(Object *ob)
@@ -224,31 +232,26 @@ void object_free_display(Object *ob)
        freedisplist(&ob->disp);
 }
 
-void free_sculptsession(SculptSession **ssp)
+void free_sculptsession(Object *ob)
 {
-       if(ssp && *ssp) {
-               SculptSession *ss = *ssp;
-               if(ss->projverts)
-                       MEM_freeN(ss->projverts);
-
-               if(ss->fmap)
-                       MEM_freeN(ss->fmap);
+       if(ob && ob->sculpt) {
+               SculptSession *ss = ob->sculpt;
+               DerivedMesh *dm= ob->derivedFinal;
 
-               if(ss->fmap_mem)
-                       MEM_freeN(ss->fmap_mem);
+               if(ss->pbvh)
+                       BLI_pbvh_free(ss->pbvh);
+               if(dm && dm->getPBVH)
+                       dm->getPBVH(NULL, dm); /* signal to clear */
 
                if(ss->texcache)
                        MEM_freeN(ss->texcache);
 
-               if(ss->layer_disps)
-                       MEM_freeN(ss->layer_disps);
-
-               if(ss->mesh_co_orig)
-                       MEM_freeN(ss->mesh_co_orig);
+               if(ss->layer_co)
+                       MEM_freeN(ss->layer_co);
 
                MEM_freeN(ss);
 
-               *ssp = NULL;
+               ob->sculpt = NULL;
        }
 }
 
@@ -290,6 +293,8 @@ void free_object(Object *ob)
                BLI_freelistN(&ob->defbase);
        if(ob->pose)
                free_pose(ob->pose);
+       if(ob->mpath)
+               animviz_free_motionpath(ob->mpath);
        free_properties(&ob->prop);
        object_free_modifiers(ob);
        
@@ -305,7 +310,7 @@ void free_object(Object *ob)
        if(ob->bsoft) bsbFree(ob->bsoft);
        if(ob->gpulamp.first) GPU_lamp_free(ob);
 
-       free_sculptsession(&ob->sculpt);
+       free_sculptsession(ob);
 
        if(ob->pc_ids.first) BLI_freelistN(&ob->pc_ids);
 }
@@ -339,6 +344,7 @@ void unlink_object(Scene *scene, Object *ob)
        unlink_actuators(&ob->actuators);
        
        /* check all objects: parents en bevels and fields, also from libraries */
+       // FIXME: need to check all animation blocks (drivers)
        obt= G.main->object.first;
        while(obt) {
                if(obt->proxy==ob)
@@ -355,11 +361,6 @@ void unlink_object(Scene *scene, Object *ob)
                        obt->recalc |= OB_RECALC;
                }
                
-               if(obt->track==ob) {
-                       obt->track= NULL;
-                       obt->recalc |= OB_RECALC_OB;
-               }
-               
                modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
                
                if ELEM(obt->type, OB_CURVE, OB_FONT) {
@@ -562,7 +563,28 @@ void unlink_object(Scene *scene, Object *ob)
                if(sce->id.lib==NULL) {
                        if(sce->camera==ob) sce->camera= NULL;
                        if(sce->toolsettings->skgen_template==ob) sce->toolsettings->skgen_template = NULL;
+                       if(sce->toolsettings->particle.object==ob) sce->toolsettings->particle.object= NULL;
+
+#ifdef DURIAN_CAMERA_SWITCH
+                       {
+                               TimeMarker *m;
+
+                               for (m= sce->markers.first; m; m= m->next) {
+                                       if(m->camera==ob)
+                                               m->camera= NULL;
+                               }
+                       }
+#endif
+                       if(sce->ed) {
+                               Sequence *seq;
+                               SEQ_BEGIN(sce->ed, seq)
+                                       if(seq->scene_camera==ob) {
+                                               seq->scene_camera= NULL;
+                                       }
+                               SEQ_END
+                       }
                }
+
                sce= sce->id.next;
        }
        
@@ -622,7 +644,7 @@ void unlink_object(Scene *scene, Object *ob)
        /* groups */
        group= G.main->group.first;
        while(group) {
-               rem_from_group(group, ob);
+               rem_from_group(group, ob, NULL, NULL);
                group= group->id.next;
        }
        
@@ -687,9 +709,9 @@ void make_local_camera(Camera *cam)
        int local=0, lib=0;
 
        /* - only lib users: do nothing
-           * - only local users: set flag
-           * - mixed: make copy
-           */
+               * - only local users: set flag
+               * - mixed: make copy
+               */
        
        if(cam->id.lib==0) return;
        if(cam->id.us==1) {
@@ -740,13 +762,13 @@ float dof_camera(Object *ob)
                return 0.0f;
        if (cam->dof_ob) {      
                /* too simple, better to return the distance on the view axis only
-                * return VecLenf(ob->obmat[3], cam->dof_ob->obmat[3]); */
-               float mat[4][4], obmat[4][4];
+                * return len_v3v3(ob->obmat[3], cam->dof_ob->obmat[3]); */
+               float mat[4][4], imat[4][4], obmat[4][4];
                
-               Mat4CpyMat4(obmat, ob->obmat);
-               Mat4Ortho(obmat);
-               Mat4Invert(ob->imat, obmat);
-               Mat4MulMat4(mat, cam->dof_ob->obmat, ob->imat);
+               copy_m4_m4(obmat, ob->obmat);
+               normalize_m4(obmat);
+               invert_m4_m4(imat, obmat);
+               mul_m4_m4m4(mat, cam->dof_ob->obmat, imat);
                return (float)fabs(mat[3][2]);
        }
        return cam->YF_dofdist;
@@ -772,6 +794,7 @@ void *add_lamp(char *name)
        la->samp= 3;
        la->bias= 1.0f;
        la->soft= 3.0f;
+       la->compressthresh= 0.05f;
        la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 
        la->area_size=la->area_sizey=la->area_sizez= 1.0f; 
        la->buffers= 1;
@@ -794,7 +817,7 @@ void *add_lamp(char *name)
        la->sun_intensity = 1.0f;
        la->skyblendtype= MA_RAMP_ADD;
        la->skyblendfac= 1.0f;
-       la->sky_colorspace= BLI_CS_CIE;
+       la->sky_colorspace= BLI_XYZ_CIE;
        la->sky_exposure= 1.0f;
        
        curvemapping_initialize(la->curfalloff);
@@ -834,9 +857,9 @@ void make_local_lamp(Lamp *la)
        int local=0, lib=0;
 
        /* - only lib users: do nothing
-           * - only local users: set flag
-           * - mixed: make copy
-           */
+               * - only local users: set flag
+               * - mixed: make copy
+               */
        
        if(la->id.lib==0) return;
        if(la->id.us==1) {
@@ -961,24 +984,27 @@ Object *add_only_object(int type, char *name)
 
        /* default object vars */
        ob->type= type;
-       /* ob->transflag= OB_QUAT; */
-
-#if 0 /* not used yet */
-       QuatOne(ob->quat);
-       QuatOne(ob->dquat);
-#endif 
-
+       
        ob->col[0]= ob->col[1]= ob->col[2]= 1.0;
        ob->col[3]= 1.0;
-
-       ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
-       ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
+       
        ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
-
-       Mat4One(ob->constinv);
-       Mat4One(ob->parentinv);
-       Mat4One(ob->obmat);
-       ob->dt= OB_SHADED;
+       
+       /* objects should default to having Euler XYZ rotations, 
+        * but rotations default to quaternions 
+        */
+       ob->rotmode= ROT_MODE_EUL;
+       /* axis-angle must not have a 0,0,0 axis, so set y-axis as default... */
+       ob->rotAxis[1]= ob->drotAxis[1]= 1.0f;
+       /* quaternions should be 1,0,0,0 by default.... */
+       ob->quat[0]= ob->dquat[0]= 1.0f;
+       /* rotation locks should be 4D for 4 component rotations by default... */
+       ob->protectflag = OB_LOCK_ROT4D;
+       
+       unit_m4(ob->constinv);
+       unit_m4(ob->parentinv);
+       unit_m4(ob->obmat);
+       ob->dt= OB_TEXTURE;
        ob->empty_drawtype= OB_ARROWS;
        ob->empty_drawsize= 1.0;
 
@@ -991,11 +1017,6 @@ Object *add_only_object(int type, char *name)
                ob->upflag= OB_POSZ;
        }
        
-#if 0 // XXX old animation system
-       ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
-       ob->ipowin= ID_OB;      /* the ipowin shown */
-#endif // XXX old animation system
-       
        ob->dupon= 1; ob->dupoff= 0;
        ob->dupsta= 1; ob->dupend= 100;
        ob->dupfacesca = 1.0;
@@ -1018,6 +1039,9 @@ Object *add_only_object(int type, char *name)
        ob->fluidsimSettings = NULL;
 
        ob->pc_ids.first = ob->pc_ids.last = NULL;
+       
+       /* Animation Visualisation defaults */
+       animviz_settings_init(&ob->avs);
 
        return ob;
 }
@@ -1037,13 +1061,6 @@ Object *add_object(struct Scene *scene, int type)
 
        ob->lay= scene->lay;
        
-       /* objects should default to having Euler XYZ rotations, 
-        * but rotations default to quaternions 
-        */
-       ob->rotmode= ROT_MODE_EUL;
-       /* axis-angle must not have a 0,0,0 axis, so set y-axis as default... */
-       ob->rotAxis[1]= ob->drotAxis[1]= 1.0f;
-
        base= scene_add_base(scene, ob);
        scene_select_base(scene, base);
        ob->recalc |= OB_RECALC;
@@ -1101,7 +1118,7 @@ ParticleSystem *copy_particlesystem(ParticleSystem *psys)
                        pa->hair = MEM_dupallocN(pa->hair);
        }
 
-       if(psysn->particles->keys || psysn->particles->boid) {
+       if(psysn->particles && (psysn->particles->keys || psysn->particles->boid)) {
                ParticleKey *key = psysn->particles->keys;
                BoidParticle *boid = psysn->particles->boid;
 
@@ -1122,8 +1139,8 @@ ParticleSystem *copy_particlesystem(ParticleSystem *psys)
        }
 
        if(psys->clmd) {
-               ClothModifierData *nclmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
-               modifier_copyData((ModifierData*)psys->clmd, (ModifierData*)nclmd);
+               psysn->clmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
+               modifier_copyData((ModifierData*)psys->clmd, (ModifierData*)psysn->clmd);
                psys->hair_in_dm = psys->hair_out_dm = NULL;
        }
 
@@ -1132,6 +1149,8 @@ ParticleSystem *copy_particlesystem(ParticleSystem *psys)
        psysn->pathcache= NULL;
        psysn->childcache= NULL;
        psysn->edit= NULL;
+       psysn->frand= NULL;
+       psysn->pdd= NULL;
        
        psysn->pathcachebufs.first = psysn->pathcachebufs.last = NULL;
        psysn->childcachebufs.first = psysn->childcachebufs.last = NULL;
@@ -1139,6 +1158,12 @@ ParticleSystem *copy_particlesystem(ParticleSystem *psys)
        
        psysn->pointcache= BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches);
 
+       /* XXX - from reading existing code this seems correct but intended usage of
+        * pointcache should /w cloth should be added in 'ParticleSystem' - campbell */
+       if(psysn->clmd) {
+               psysn->clmd->point_cache= psysn->pointcache;
+       }
+
        id_us_plus((ID *)psysn->part);
 
        return psysn;
@@ -1261,7 +1286,7 @@ Object *copy_object(Object *ob)
                if(ob->type==OB_ARMATURE)
                        armature_rebuild_pose(obn, obn->data);
        }
-       copy_defgroups(&obn->defbase, &ob->defbase);
+       defgroup_copy_list(&obn->defbase, &ob->defbase);
        copy_constraints(&obn->constraints, &ob->constraints);
 
        obn->mode = 0;
@@ -1330,9 +1355,9 @@ void make_local_object(Object *ob)
        int local=0, lib=0;
 
        /* - only lib users: do nothing
-           * - only local users: set flag
-           * - mixed: make copy
-           */
+               * - only local users: set flag
+               * - mixed: make copy
+               */
        
        if(ob->id.lib==NULL) return;
        
@@ -1404,9 +1429,9 @@ int object_is_libdata(Object *ob)
 int object_data_is_libdata(Object *ob)
 {
        if(!ob) return 0;
-       if(ob->proxy) return 0;
+       if(ob->proxy && (ob->data==NULL || ((ID *)ob->data)->lib==NULL)) return 0;
        if(ob->id.lib) return 1;
-       if(!ob->data) return 0;
+       if(ob->data==NULL) return 0;
        if(((ID *)ob->data)->lib) return 1;
 
        return 0;
@@ -1428,6 +1453,38 @@ static void armature_set_id_extern(Object *ob)
                        
 }
 
+void object_copy_proxy_drivers(Object *ob, Object *target)
+{
+       if ((target->adt) && (target->adt->drivers.first)) {
+               FCurve *fcu;
+               
+               /* add new animdata block */
+               if(!ob->adt)
+                       ob->adt= BKE_id_add_animdata(&ob->id);
+               
+               /* make a copy of all the drivers (for now), then correct any links that need fixing */
+               free_fcurves(&ob->adt->drivers);
+               copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
+               
+               for (fcu= ob->adt->drivers.first; fcu; fcu= fcu->next) {
+                       ChannelDriver *driver= fcu->driver;
+                       DriverVar *dvar;
+                       
+                       for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
+                               /* all drivers */
+                               DRIVER_TARGETS_LOOPER(dvar) 
+                               {
+                                       if ((Object *)dtar->id == target)
+                                               dtar->id= (ID *)ob;
+                                       else
+                                               id_lib_extern((ID *)dtar->id);
+                               }
+                               DRIVER_TARGETS_LOOPER_END
+                       }
+               }
+       }
+}
+
 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
 /*             local_object->proxy == pointer to library object, saved in files and read */
 /*             local_object->proxy_group == pointer to group dupli-object, saved in files and read */
@@ -1461,31 +1518,11 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
        }
        
        ob->parent= target->parent;     /* libdata */
-       Mat4CpyMat4(ob->parentinv, target->parentinv);
+       copy_m4_m4(ob->parentinv, target->parentinv);
        
        /* copy animdata stuff - drivers only for now... */
-       if ((target->adt) && (target->adt->drivers.first)) {
-               FCurve *fcu;
-               
-               /* add new animdata block */
-               ob->adt= BKE_id_add_animdata(&ob->id);
-               
-               /* make a copy of all the drivers (for now), then correct any links that need fixing */
-               copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
-               
-               for (fcu= ob->adt->drivers.first; fcu; fcu= fcu->next) {
-                       ChannelDriver *driver= fcu->driver;
-                       DriverTarget *dtar;
-                       
-                       for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
-                               if ((Object *)dtar->id == target)
-                                       dtar->id= (ID *)ob;
-                               else
-                                       id_lib_extern((ID *)dtar->id);
-                       }
-               }
-       }
-       
+       object_copy_proxy_drivers(ob, target);
+
        /* skip constraints? */
        // FIXME: this is considered by many as a bug
        
@@ -1585,7 +1622,7 @@ void object_scale_to_mat3(Object *ob, float mat[][3])
        vec[0]= ob->size[0]+ob->dsize[0];
        vec[1]= ob->size[1]+ob->dsize[1];
        vec[2]= ob->size[2]+ob->dsize[2];
-       SizeToMat3(vec, mat);
+       size_to_mat3( mat,vec);
 }
 
 // TODO: this should take rotation orders into account later...
@@ -1596,29 +1633,56 @@ void object_rot_to_mat3(Object *ob, float mat[][3])
        /* initialise the delta-rotation matrix, which will get (pre)multiplied 
         * with the rotation matrix to yield the appropriate rotation
         */
-       Mat3One(dmat);
+       unit_m3(dmat);
        
        /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
        if (ob->rotmode > 0) {
                /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
-               EulOToMat3(ob->rot, ob->rotmode, rmat);
-               EulOToMat3(ob->drot, ob->rotmode, dmat);
+               eulO_to_mat3( rmat,ob->rot, ob->rotmode);
+               eulO_to_mat3( dmat,ob->drot, ob->rotmode);
        }
        else if (ob->rotmode == ROT_MODE_AXISANGLE) {
                /* axis-angle -  not really that great for 3D-changing orientations */
-               AxisAngleToMat3(ob->rotAxis, ob->rotAngle, rmat);
-               AxisAngleToMat3(ob->drotAxis, ob->drotAngle, dmat);
+               axis_angle_to_mat3( rmat,ob->rotAxis, ob->rotAngle);
+               axis_angle_to_mat3( dmat,ob->drotAxis, ob->drotAngle);
        }
        else {
                /* quats are normalised before use to eliminate scaling issues */
-               NormalQuat(ob->quat);
-               QuatToMat3(ob->quat, rmat);
-               QuatToMat3(ob->dquat, dmat);
+               normalize_qt(ob->quat);
+               quat_to_mat3( rmat,ob->quat);
+               quat_to_mat3( dmat,ob->dquat);
        }
        
        /* combine these rotations */
        // XXX is this correct? if errors, change the order of multiplication...
-       Mat3MulMat3(mat, dmat, rmat);
+       mul_m3_m3m3(mat, dmat, rmat);
+}
+
+void object_mat3_to_rot(Object *ob, float mat[][3], int use_compat)
+{
+       if (ob->rotmode == ROT_MODE_QUAT)
+               mat3_to_quat(ob->quat, mat);
+       else if (ob->rotmode == ROT_MODE_AXISANGLE)
+               mat3_to_axis_angle(ob->rotAxis, &ob->rotAngle, mat);
+       else {
+               if(use_compat) {
+                       float eul[3];
+                       VECCOPY(eul, ob->rot);
+                       mat3_to_compatible_eulO(ob->rot, eul, ob->rotmode, mat);
+               }
+               else
+                       mat3_to_eulO(ob->rot, ob->rotmode, mat);
+       }
+}
+
+/* see pchan_apply_mat4() for the equivalent 'pchan' function */
+void object_apply_mat4(Object *ob, float mat[][4])
+{
+       float mat3[3][3];
+       VECCOPY(ob->loc, mat[3]);
+       mat4_to_size(ob->size, mat);
+       copy_m3_m4(mat3, mat);
+       object_mat3_to_rot(ob, mat3, 0);
 }
 
 void object_to_mat3(Object *ob, float mat[][3])        /* no parent */
@@ -1632,7 +1696,7 @@ void object_to_mat3(Object *ob, float mat[][3])   /* no parent */
 
        /* rot */
        object_rot_to_mat3(ob, rmat);
-       Mat3MulMat3(mat, rmat, smat);
+       mul_m3_m3m3(mat, rmat, smat);
 }
 
 void object_to_mat4(Object *ob, float mat[][4])
@@ -1641,7 +1705,7 @@ void object_to_mat4(Object *ob, float mat[][4])
        
        object_to_mat3(ob, tmat);
        
-       Mat4CpyMat3(mat, tmat);
+       copy_m4_m3(mat, tmat);
        
        mat[3][0]= ob->loc[0] + ob->dloc[0];
        mat[3][1]= ob->loc[1] + ob->dloc[1];
@@ -1656,7 +1720,7 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
        float q[4], vec[4], dir[3], quat[4], radius, x1, ctime;
        float timeoffs = 0.0, sf_orig = 0.0;
        
-       Mat4One(mat);
+       unit_m4(mat);
        
        cu= par->data;
        if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
@@ -1678,10 +1742,10 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
                /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
                 * but this will only work if it actually is animated... 
                 *
-                * we firstly calculate the modulus of cu->ctime/cu->pathlen to clamp ctime within the 0.0 to 1.0 times pathlen
-                * range, then divide this (the modulus) by pathlen to get a value between 0.0 and 1.0
+                * we divide the curvetime calculated in the previous step by the length of the path, to get a time
+                * factor, which then gets clamped to lie within 0.0 - 1.0 range
                 */
-               ctime= fmod(cu->ctime, cu->pathlen) / cu->pathlen;
+               ctime= cu->ctime / cu->pathlen;
                CLAMP(ctime, 0.0, 1.0);
        }
        else {
@@ -1701,28 +1765,28 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
        
        
        /* vec: 4 items! */
-       if( where_on_path(par, ctime, vec, dir, NULL, &radius) ) {
+        if( where_on_path(par, ctime, vec, dir, NULL, &radius) ) {
 
                if(cu->flag & CU_FOLLOW) {
-                       vectoquat(dir, ob->trackflag, ob->upflag, quat);
+                       vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
                        
                        /* the tilt */
-                       Normalize(dir);
+                       normalize_v3(dir);
                        q[0]= (float)cos(0.5*vec[3]);
                        x1= (float)sin(0.5*vec[3]);
                        q[1]= -x1*dir[0];
                        q[2]= -x1*dir[1];
                        q[3]= -x1*dir[2];
-                       QuatMul(quat, q, quat);
+                       mul_qt_qtqt(quat, q, quat);
                        
-                       QuatToMat4(quat, mat);
+                       quat_to_mat4( mat,quat);
                }
                
                if(cu->flag & CU_PATH_RADIUS) {
                        float tmat[4][4], rmat[4][4];
-                       Mat4Scale(tmat, radius);
-                       Mat4MulMat4(rmat, mat, tmat);
-                       Mat4CpyMat4(mat, rmat);
+                       scale_m4_fl(tmat, radius);
+                       mul_m4_m4m4(rmat, mat, tmat);
+                       copy_m4_m4(mat, rmat);
                }
 
                VECCOPY(mat[3], vec);
@@ -1736,7 +1800,7 @@ static void ob_parbone(Object *ob, Object *par, float mat[][4])
        float vec[3];
        
        if (par->type!=OB_ARMATURE) {
-               Mat4One(mat);
+               unit_m4(mat);
                return;
        }
        
@@ -1744,17 +1808,17 @@ static void ob_parbone(Object *ob, Object *par, float mat[][4])
        pchan= get_pose_channel(par->pose, ob->parsubstr);
        if (!pchan){
                printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
-               Mat4One(mat);
+               unit_m4(mat);
                return;
        }
 
        /* get bone transform */
-       Mat4CpyMat4(mat, pchan->pose_mat);
+       copy_m4_m4(mat, pchan->pose_mat);
 
        /* but for backwards compatibility, the child has to move to the tail */
        VECCOPY(vec, mat[1]);
-       VecMulf(vec, pchan->bone->length);
-       VecAddf(mat[3], mat[3], vec);
+       mul_v3_fl(vec, pchan->bone->length);
+       add_v3_v3v3(mat[3], mat[3], vec);
 }
 
 static void give_parvert(Object *par, int nr, float *vec)
@@ -1783,15 +1847,16 @@ static void give_parvert(Object *par, int nr, float *vec)
                        DerivedMesh *dm = par->derivedFinal;
                        
                        if(dm) {
-                               int i, count = 0, numVerts = dm->getNumVerts(dm);
+                               MVert *mvert= dm->getVertArray(dm);
                                int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
-                               float co[3];
+                               int i, count = 0, vindex, numVerts = dm->getNumVerts(dm);
 
                                /* get the average of all verts with (original index == nr) */
-                               for(i = 0; i < numVerts; ++i, ++index) {
-                                       if(*index == nr) {
-                                               dm->getVertCo(dm, i, co);
-                                               VecAddf(vec, vec, co);
+                               for(i = 0; i < numVerts; i++) {
+                                       vindex= (index)? index[i]: i;
+
+                                       if(vindex == nr) {
+                                               add_v3_v3v3(vec, vec, mvert[i].co);
                                                count++;
                                        }
                                }
@@ -1799,7 +1864,7 @@ static void give_parvert(Object *par, int nr, float *vec)
                                if (count==0) {
                                        /* keep as 0,0,0 */
                                } else if(count > 0) {
-                                       VecMulf(vec, 1.0f / count);
+                                       mul_v3_fl(vec, 1.0f / count);
                                } else {
                                        /* use first index if its out of range */
                                        dm->getVertCo(dm, 0, vec);
@@ -1883,7 +1948,7 @@ static void ob_parvert3(Object *ob, Object *par, float mat[][4])
        float cmat[3][3], v1[3], v2[3], v3[3], q[4];
 
        /* in local ob space */
-       Mat4One(mat);
+       unit_m4(mat);
        
        if (ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE)) {
                
@@ -1891,17 +1956,17 @@ static void ob_parvert3(Object *ob, Object *par, float mat[][4])
                give_parvert(par, ob->par2, v2);
                give_parvert(par, ob->par3, v3);
                                
-               triatoquat(v1, v2, v3, q);
-               QuatToMat3(q, cmat);
-               Mat4CpyMat3(mat, cmat);
+               tri_to_quat( q,v1, v2, v3);
+               quat_to_mat3( cmat,q);
+               copy_m4_m3(mat, cmat);
                
                if(ob->type==OB_CURVE) {
                        VECCOPY(mat[3], v1);
                }
                else {
-                       VecAddf(mat[3], v1, v2);
-                       VecAddf(mat[3], mat[3], v3);
-                       VecMulf(mat[3], 0.3333333f);
+                       add_v3_v3v3(mat[3], v1, v2);
+                       add_v3_v3v3(mat[3], mat[3], v3);
+                       mul_v3_fl(mat[3], 0.3333333f);
                }
        }
 }
@@ -1916,9 +1981,8 @@ void set_no_parent_ipo(int val)
 void where_is_object_time(Scene *scene, Object *ob, float ctime)
 {
        float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
-       float stime=ctime, fac1, fac2, vec[3];
+       float stime=ctime, fac1, fac2;
        int a;
-       int pop; 
        
        /* new version: correct parent+vertexparent and track+parent */
        /* this one only calculates direct attached parent and track */
@@ -1937,21 +2001,19 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
                
                /* hurms, code below conflicts with depgraph... (ton) */
                /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
-               pop= 0;
                if(no_parent_ipo==0 && stime != par->ctime) {
                        // only for ipo systems? 
-                       pushdata(par, sizeof(Object));
-                       pop= 1;
+                       Object tmp= *par;
                        
                        if(par->proxy_from);    // was a copied matrix, no where_is! bad...
                        else where_is_object_time(scene, par, ctime);
+
+                       solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
+
+                       *par= tmp;
                }
-               
-               solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
-               
-               if(pop) {
-                       poplast(par);
-               }
+               else
+                       solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
                
                if(ob->partype & PARSLOW) {
                        // include framerate
@@ -1970,14 +2032,8 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
                object_to_mat4(ob, ob->obmat);
        }
 
-       /* Handle tracking */
-       if(ob->track) {
-               if( ctime != ob->track->ctime) where_is_object_time(scene, ob->track, ctime);
-               solve_tracking (ob, ob->track->obmat);
-       }
-
        /* solve constraints */
-       if (ob->constraints.first) {
+       if (ob->constraints.first && !(ob->flag & OB_NO_CONSTRAINTS)) {
                bConstraintOb *cob;
                
                cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
@@ -1989,9 +2045,8 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
        }
        
        /* set negative scale flag in object */
-       Crossf(vec, ob->obmat[0], ob->obmat[1]);
-       if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
-       else ob->transflag &= ~OB_NEG_SCALE;
+       if(is_negative_m4(ob->obmat))   ob->transflag |= OB_NEG_SCALE;
+       else                                                    ob->transflag &= ~OB_NEG_SCALE;
 }
 
 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
@@ -2004,7 +2059,7 @@ static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[
        
        object_to_mat4(ob, locmat);
        
-       if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, obmat);
+       if(ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
 
        switch(ob->partype & PARTYPE) {
        case PAROBJECT:
@@ -2016,43 +2071,43 @@ static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[
                        }
                }
                
-               if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
+               if(ok) mul_serie_m4(totmat, par->obmat, tmat, 
                        NULL, NULL, NULL, NULL, NULL, NULL);
-               else Mat4CpyMat4(totmat, par->obmat);
+               else copy_m4_m4(totmat, par->obmat);
                
                break;
        case PARBONE:
                ob_parbone(ob, par, tmat);
-               Mat4MulSerie(totmat, par->obmat, tmat,         
+               mul_serie_m4(totmat, par->obmat, tmat,         
                        NULL, NULL, NULL, NULL, NULL, NULL);
                break;
                
        case PARVERT1:
-               Mat4One(totmat);
+               unit_m4(totmat);
                if (simul){
                        VECCOPY(totmat[3], par->obmat[3]);
                }
                else{
                        give_parvert(par, ob->par1, vec);
-                       VecMat4MulVecfl(totmat[3], par->obmat, vec);
+                       mul_v3_m4v3(totmat[3], par->obmat, vec);
                }
                break;
        case PARVERT3:
                ob_parvert3(ob, par, tmat);
                
-               Mat4MulSerie(totmat, par->obmat, tmat,         
+               mul_serie_m4(totmat, par->obmat, tmat,         
                        NULL, NULL, NULL, NULL, NULL, NULL);
                break;
                
        case PARSKEL:
-               Mat4CpyMat4(totmat, par->obmat);
+               copy_m4_m4(totmat, par->obmat);
                break;
        }
        
        // total 
-       Mat4MulSerie(tmat, totmat, ob->parentinv,         
+       mul_serie_m4(tmat, totmat, ob->parentinv,         
                NULL, NULL, NULL, NULL, NULL, NULL);
-       Mat4MulSerie(obmat, tmat, locmat,         
+       mul_serie_m4(obmat, tmat, locmat,         
                NULL, NULL, NULL, NULL, NULL, NULL);
        
        if (simul) {
@@ -2060,7 +2115,7 @@ static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[
        }
        else{
                // external usable originmat 
-               Mat3CpyMat4(originmat, tmat);
+               copy_m3_m4(originmat, tmat);
                
                // origin, voor help line
                if( (ob->partype & 15)==PARSKEL ) {
@@ -2071,33 +2126,6 @@ static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[
                }
        }
 
-}
-void solve_tracking (Object *ob, float targetmat[][4])
-{
-       float quat[4];
-       float vec[3];
-       float totmat[3][3];
-       float tmat[4][4];
-       
-       VecSubf(vec, ob->obmat[3], targetmat[3]);
-       vectoquat(vec, ob->trackflag, ob->upflag, quat);
-       QuatToMat3(quat, totmat);
-       
-       if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
-               /* 'temporal' : clear parent info */
-               object_to_mat4(ob, tmat);
-               tmat[0][3]= ob->obmat[0][3];
-               tmat[1][3]= ob->obmat[1][3];
-               tmat[2][3]= ob->obmat[2][3];
-               tmat[3][0]= ob->obmat[3][0];
-               tmat[3][1]= ob->obmat[3][1];
-               tmat[3][2]= ob->obmat[3][2];
-               tmat[3][3]= ob->obmat[3][3];
-       }
-       else Mat4CpyMat4(tmat, ob->obmat);
-       
-       Mat4MulMat34(ob->obmat, totmat, tmat);
-
 }
 
 void where_is_object(struct Scene *scene, Object *ob)
@@ -2119,12 +2147,6 @@ for a lamp that is the child of another object */
        int a;
        
        /* NO TIMEOFFS */
-       
-       /* no ipo! (because of dloc and realtime-ipos) */
-               // XXX old animation system
-       //ipo= ob->ipo;
-       //ob->ipo= NULL;
-
        if(ob->parent) {
                par= ob->parent;
                
@@ -2146,9 +2168,6 @@ for a lamp that is the child of another object */
                object_to_mat4(ob, ob->obmat);
        }
        
-       if(ob->track) 
-               solve_tracking(ob, ob->track->obmat);
-
        /* solve constraints */
        if (ob->constraints.first) {
                bConstraintOb *cob;
@@ -2157,10 +2176,6 @@ for a lamp that is the child of another object */
                solve_constraints(&ob->constraints, cob, (float)scene->r.cfra);
                constraints_clear_evalob(cob);
        }
-       
-       /*  WATCH IT!!! */
-               // XXX old animation system
-       //ob->ipo= ipo;
 }
 
 /* for calculation of the inverse parent transform, only used for editor */
@@ -2168,11 +2183,10 @@ void what_does_parent(Scene *scene, Object *ob, Object *workob)
 {
        clear_workob(workob);
        
-       Mat4One(workob->obmat);
-       Mat4One(workob->parentinv);
-       Mat4One(workob->constinv);
+       unit_m4(workob->obmat);
+       unit_m4(workob->parentinv);
+       unit_m4(workob->constinv);
        workob->parent= ob->parent;
-       workob->track= ob->track;
 
        workob->trackflag= ob->trackflag;
        workob->upflag= ob->upflag;
@@ -2221,7 +2235,7 @@ BoundBox *object_get_boundbox(Object *ob)
                bb = mesh_get_bb(ob);
        }
        else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
-               bb= ( (Curve *)ob->data )->bb;
+               bb= ob->bb ? ob->bb : ( (Curve *)ob->data )->bb;
        }
        else if(ob->type==OB_MBALL) {
                bb= ob->bb;
@@ -2239,6 +2253,44 @@ void object_boundbox_flag(Object *ob, int flag, int set)
        }
 }
 
+void object_get_dimensions(Object *ob, float *value)
+{
+       BoundBox *bb = NULL;
+       
+       bb= object_get_boundbox(ob);
+       if (bb) {
+               float scale[3];
+               
+               mat4_to_size( scale,ob->obmat);
+               
+               value[0] = fabs(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
+               value[1] = fabs(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
+               value[2] = fabs(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
+       } else {
+               value[0] = value[1] = value[2] = 0.f;
+       }
+}
+
+void object_set_dimensions(Object *ob, const float *value)
+{
+       BoundBox *bb = NULL;
+       
+       bb= object_get_boundbox(ob);
+       if (bb) {
+               float scale[3], len[3];
+               
+               mat4_to_size( scale,ob->obmat);
+               
+               len[0] = bb->vec[4][0] - bb->vec[0][0];
+               len[1] = bb->vec[2][1] - bb->vec[0][1];
+               len[2] = bb->vec[1][2] - bb->vec[0][2];
+               
+               if (len[0] > 0.f) ob->size[0] = value[0] / len[0];
+               if (len[1] > 0.f) ob->size[1] = value[1] / len[1];
+               if (len[2] > 0.f) ob->size[2] = value[2] / len[2];
+       }
+}
+
 void minmax_object(Object *ob, float *min, float *max)
 {
        BoundBox bb;
@@ -2258,7 +2310,7 @@ void minmax_object(Object *ob, float *min, float *max)
                bb= *(cu->bb);
                
                for(a=0; a<8; a++) {
-                       Mat4MulVecfl(ob->obmat, bb.vec[a]);
+                       mul_m4_v3(ob->obmat, bb.vec[a]);
                        DO_MINMAX(bb.vec[a], min, max);
                }
                break;
@@ -2267,10 +2319,10 @@ void minmax_object(Object *ob, float *min, float *max)
                        bPoseChannel *pchan;
                        for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
                                VECCOPY(vec, pchan->pose_head);
-                               Mat4MulVecfl(ob->obmat, vec);
+                               mul_m4_v3(ob->obmat, vec);
                                DO_MINMAX(vec, min, max);
                                VECCOPY(vec, pchan->pose_tail);
-                               Mat4MulVecfl(ob->obmat, vec);
+                               mul_m4_v3(ob->obmat, vec);
                                DO_MINMAX(vec, min, max);
                        }
                        break;
@@ -2283,7 +2335,7 @@ void minmax_object(Object *ob, float *min, float *max)
                        bb = *mesh_get_bb(ob);
                        
                        for(a=0; a<8; a++) {
-                               Mat4MulVecfl(ob->obmat, bb.vec[a]);
+                               mul_m4_v3(ob->obmat, bb.vec[a]);
                                DO_MINMAX(bb.vec[a], min, max);
                        }
                }
@@ -2295,37 +2347,107 @@ void minmax_object(Object *ob, float *min, float *max)
                DO_MINMAX(ob->obmat[3], min, max);
 
                VECCOPY(vec, ob->obmat[3]);
-               VecAddf(vec, vec, ob->size);
+               add_v3_v3v3(vec, vec, ob->size);
                DO_MINMAX(vec, min, max);
 
                VECCOPY(vec, ob->obmat[3]);
-               VecSubf(vec, vec, ob->size);
+               sub_v3_v3v3(vec, vec, ob->size);
                DO_MINMAX(vec, min, max);
                break;
        }
 }
 
-/* TODO - use dupli objects bounding boxes */
-void minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
+int minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
 {
+       int ok= 0;
        if ((ob->transflag & OB_DUPLI)==0) {
-               return;
+               return ok;
        } else {
                ListBase *lb;
                DupliObject *dob;
                
                lb= object_duplilist(scene, ob);
                for(dob= lb->first; dob; dob= dob->next) {
-                       if(dob->no_draw);
-                       else {
-                               /* should really use bound box of dup object */
-                               DO_MINMAX(dob->mat[3], min, max);
+                       if(dob->no_draw == 0) {
+                               BoundBox *bb= object_get_boundbox(dob->ob);
+
+                               if(bb) {
+                                       int i;
+                                       for(i=0; i<8; i++) {
+                                               float vec[3];
+                                               mul_v3_m4v3(vec, dob->mat, bb->vec[i]);
+                                               DO_MINMAX(vec, min, max);
+                                       }
+
+                                       ok= 1;
+                               }
                        }
                }
                free_object_duplilist(lb);      /* does restore */
        }
-}
 
+       return ok;
+}
+
+/* copied from DNA_object_types.h */
+typedef struct ObTfmBack {
+       float loc[3], dloc[3], orig[3];
+       float size[3], dsize[3];        /* scale and delta scale */
+       float rot[3], drot[3];          /* euler rotation */
+       float quat[4], dquat[4];        /* quaternion rotation */
+       float rotAxis[3], drotAxis[3];  /* axis angle rotation - axis part */
+       float rotAngle, drotAngle;      /* axis angle rotation - angle part */
+       float obmat[4][4];              /* final worldspace matrix with constraints & animsys applied */
+       float parentinv[4][4]; /* inverse result of parent, so that object doesn't 'stick' to parent */
+       float constinv[4][4]; /* inverse result of constraints. doesn't include effect of parent or object local transform */
+       float imat[4][4];       /* inverse matrix of 'obmat' for during render, old game engine, temporally: ipokeys of transform  */
+} ObTfmBack;
+
+void *object_tfm_backup(Object *ob)
+{
+       ObTfmBack *obtfm= MEM_mallocN(sizeof(ObTfmBack), "ObTfmBack");
+       copy_v3_v3(obtfm->loc, ob->loc);
+       copy_v3_v3(obtfm->dloc, ob->dloc);
+       copy_v3_v3(obtfm->orig, ob->orig);
+       copy_v3_v3(obtfm->size, ob->size);
+       copy_v3_v3(obtfm->dsize, ob->dsize);
+       copy_v3_v3(obtfm->rot, ob->rot);
+       copy_v3_v3(obtfm->drot, ob->drot);
+       copy_qt_qt(obtfm->quat, ob->quat);
+       copy_qt_qt(obtfm->dquat, ob->dquat);
+       copy_v3_v3(obtfm->rotAxis, ob->rotAxis);
+       copy_v3_v3(obtfm->drotAxis, ob->drotAxis);
+       obtfm->rotAngle= ob->rotAngle;
+       obtfm->drotAngle= ob->drotAngle;
+       copy_m4_m4(obtfm->obmat, ob->obmat);
+       copy_m4_m4(obtfm->parentinv, ob->parentinv);
+       copy_m4_m4(obtfm->constinv, ob->constinv);
+       copy_m4_m4(obtfm->imat, ob->imat);
+
+       return (void *)obtfm;
+}
+
+void object_tfm_restore(Object *ob, void *obtfm_pt)
+{
+       ObTfmBack *obtfm= (ObTfmBack *)obtfm_pt;
+       copy_v3_v3(ob->loc, obtfm->loc);
+       copy_v3_v3(ob->dloc, obtfm->dloc);
+       copy_v3_v3(ob->orig, obtfm->orig);
+       copy_v3_v3(ob->size, obtfm->size);
+       copy_v3_v3(ob->dsize, obtfm->dsize);
+       copy_v3_v3(ob->rot, obtfm->rot);
+       copy_v3_v3(ob->drot, obtfm->drot);
+       copy_qt_qt(ob->quat, obtfm->quat);
+       copy_qt_qt(ob->dquat, obtfm->dquat);
+       copy_v3_v3(ob->rotAxis, obtfm->rotAxis);
+       copy_v3_v3(ob->drotAxis, obtfm->drotAxis);
+       ob->rotAngle= obtfm->rotAngle;
+       ob->drotAngle= obtfm->drotAngle;
+       copy_m4_m4(ob->obmat, obtfm->obmat);
+       copy_m4_m4(ob->parentinv, obtfm->parentinv);
+       copy_m4_m4(ob->constinv, obtfm->constinv);
+       copy_m4_m4(ob->imat, obtfm->imat);
+}
 
 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
 /*           local_object->proxy      == pointer to library object, saved in files and read */
@@ -2337,7 +2459,10 @@ void minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
 void object_handle_update(Scene *scene, Object *ob)
 {
        if(ob->recalc & OB_RECALC) {
-               
+               /* speed optimization for animation lookups */
+               if(ob->pose)
+                       make_pose_channels_hash(ob->pose);
+
                /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers, 
                   which is only in where_is_object now */
                if(ob->recalc & OB_RECALC) {
@@ -2350,17 +2475,22 @@ void object_handle_update(Scene *scene, Object *ob)
                                // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
                                if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
                                        Object *obg= ob->proxy_from->proxy_group;
-                                       Mat4Invert(obg->imat, obg->obmat);
-                                       Mat4MulMat4(ob->obmat, ob->proxy_from->obmat, obg->imat);
+                                       invert_m4_m4(obg->imat, obg->obmat);
+                                       mul_m4_m4m4(ob->obmat, ob->proxy_from->obmat, obg->imat);
                                }
                                else
-                                       Mat4CpyMat4(ob->obmat, ob->proxy_from->obmat);
+                                       copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
                        }
                        else
                                where_is_object(scene, ob);
                }
                
                if(ob->recalc & OB_RECALC_DATA) {
+                       ID *data_id= (ID *)ob->data;
+                       AnimData *adt= BKE_animdata_from_id(data_id);
+                       float ctime= (float)scene->r.cfra; // XXX this is bad...
+                       ListBase pidlist;
+                       PTCacheID *pid;
                        
                        if (G.f & G_DEBUG)
                                printf("recalcdata %s\n", ob->id.name+2);
@@ -2368,9 +2498,13 @@ void object_handle_update(Scene *scene, Object *ob)
                        /* includes all keys and modifiers */
                        if(ob->type==OB_MESH) {
                                EditMesh *em = BKE_mesh_get_editmesh(ob->data);
-
+                               
+                               /* evaluate drivers */
+                               // XXX: should we push this to derivedmesh instead?
+                               BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
+                               
                                        // here was vieweditdatamask? XXX
-                               if(ob->mode & OB_MODE_EDIT) {
+                               if(em) {
                                        makeDerivedMesh(scene, ob, em, CD_MASK_BAREMESH);
                                        BKE_mesh_end_editmesh(ob->data, em);
                                } else
@@ -2383,10 +2517,6 @@ void object_handle_update(Scene *scene, Object *ob)
                                makeDispListCurveTypes(scene, ob, 0);
                        }
                        else if(ELEM(ob->type, OB_CAMERA, OB_LAMP)) {
-                               ID *data_id= (ID *)ob->data;
-                               AnimData *adt= BKE_animdata_from_id(data_id);
-                               float ctime= (float)scene->r.cfra; // XXX this is bad...
-                               
                                /* evaluate drivers */
                                BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
                        }
@@ -2399,6 +2529,9 @@ void object_handle_update(Scene *scene, Object *ob)
                                if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
                                        armature_rebuild_pose(ob, ob->data);
                                
+                               /* evaluate drivers */
+                               BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
+                               
                                if(ob->id.lib && ob->proxy_from) {
                                        copy_pose_result(ob->pose, ob->proxy_from->pose);
                                        // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
@@ -2446,6 +2579,24 @@ void object_handle_update(Scene *scene, Object *ob)
                                                psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
                                }
                        }
+
+                       /* check if quick cache is needed */
+                       BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
+
+                       for(pid=pidlist.first; pid; pid=pid->next) {
+                               if((pid->cache->flag & PTCACHE_BAKED)
+                                       || (pid->cache->flag & PTCACHE_QUICK_CACHE)==0)
+                                       continue;
+
+                               if(pid->cache->flag & PTCACHE_OUTDATED || (pid->cache->flag & PTCACHE_SIMULATION_VALID)==0) {
+                                       scene->physics_settings.quick_cache_step =
+                                               scene->physics_settings.quick_cache_step ?
+                                               MIN2(scene->physics_settings.quick_cache_step, pid->cache->step) :
+                                               pid->cache->step;
+                               }
+                       }
+
+                       BLI_freelistN(&pidlist);
                }
 
                /* the no-group proxy case, we call update */
@@ -2474,7 +2625,7 @@ float give_timeoffset(Object *ob) {
        }
 }
 
-int give_obdata_texspace(Object *ob, int **texflag, float **loc, float **size, float **rot) {
+int give_obdata_texspace(Object *ob, short **texflag, float **loc, float **size, float **rot) {
        
        if (ob->data==NULL)
                return 0;
@@ -2535,7 +2686,7 @@ int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3
                v1 = triangle_indexes[i][0];
                v2 = triangle_indexes[i][1];
                v3 = triangle_indexes[i][2];
-               result = RayIntersectsTriangle(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
+               result = isect_ray_tri_v3(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
        }
        
        return result;
@@ -2571,6 +2722,117 @@ int object_insert_ptcache(Object *ob)
        return i;
 }
 
+/* 'lens' may be set for envmap only */
+void object_camera_matrix(
+               RenderData *rd, Object *camera, int winx, int winy, short field_second,
+               float winmat[][4], rctf *viewplane, float *clipsta, float *clipend, float *lens, float *ycor,
+               float *viewdx, float *viewdy
+) {
+       Camera *cam=NULL;
+       float pixsize;
+       float shiftx=0.0, shifty=0.0, winside, viewfac;
+
+       rd->mode &= ~R_ORTHO;
+
+       /* question mark */
+       (*ycor)= rd->yasp / rd->xasp;
+       if(rd->mode & R_FIELDS)
+               (*ycor) *= 2.0f;
+
+       if(camera->type==OB_CAMERA) {
+               cam= camera->data;
+
+               if(cam->type==CAM_ORTHO) rd->mode |= R_ORTHO;
+               if(cam->flag & CAM_PANORAMA) rd->mode |= R_PANORAMA;
+
+               /* solve this too... all time depending stuff is in convertblender.c?
+                * Need to update the camera early because it's used for projection matrices
+                * and other stuff BEFORE the animation update loop is done
+                * */
+#if 0 // XXX old animation system
+               if(cam->ipo) {
+                       calc_ipo(cam->ipo, frame_to_float(re->scene, re->r.cfra));
+                       execute_ipo(&cam->id, cam->ipo);
+               }
+#endif // XXX old animation system
+               shiftx=cam->shiftx;
+               shifty=cam->shifty;
+               (*lens)= cam->lens;
+               (*clipsta)= cam->clipsta;
+               (*clipend)= cam->clipend;
+       }
+       else if(camera->type==OB_LAMP) {
+               Lamp *la= camera->data;
+               float fac= cos( M_PI*la->spotsize/360.0 );
+               float phi= acos(fac);
+
+               (*lens)= 16.0*fac/sin(phi);
+               if((*lens)==0.0f)
+                       (*lens)= 35.0;
+               (*clipsta)= la->clipsta;
+               (*clipend)= la->clipend;
+       }
+       else {  /* envmap exception... */;
+               if((*lens)==0.0f)
+                       (*lens)= 16.0;
+
+               if((*clipsta)==0.0f || (*clipend)==0.0f) {
+                       (*clipsta)= 0.1f;
+                       (*clipend)= 1000.0f;
+               }
+       }
+
+       /* ortho only with camera available */
+       if(cam && rd->mode & R_ORTHO) {
+               if(rd->xasp*winx >= rd->yasp*winy) {
+                       viewfac= winx;
+               }
+               else {
+                       viewfac= (*ycor) * winy;
+               }
+               /* ortho_scale == 1.0 means exact 1 to 1 mapping */
+               pixsize= cam->ortho_scale/viewfac;
+       }
+       else {
+               if(rd->xasp*winx >= rd->yasp*winy)      viewfac= ((*lens) * winx)/32.0;
+               else                                                            viewfac= (*ycor) * ((*lens) * winy)/32.0;
+               pixsize= (*clipsta) / viewfac;
+       }
+
+       /* viewplane fully centered, zbuffer fills in jittered between -.5 and +.5 */
+       winside= MAX2(winx, winy);
+       viewplane->xmin= -0.5f*(float)winx + shiftx*winside;
+       viewplane->ymin= -0.5f*(*ycor)*(float)winy + shifty*winside;
+       viewplane->xmax=  0.5f*(float)winx + shiftx*winside;
+       viewplane->ymax=  0.5f*(*ycor)*(float)winy + shifty*winside;
+
+       if(field_second) {
+               if(rd->mode & R_ODDFIELD) {
+                       viewplane->ymin-= 0.5 * (*ycor);
+                       viewplane->ymax-= 0.5 * (*ycor);
+               }
+               else {
+                       viewplane->ymin+= 0.5 * (*ycor);
+                       viewplane->ymax+= 0.5 * (*ycor);
+               }
+       }
+       /* the window matrix is used for clipping, and not changed during OSA steps */
+       /* using an offset of +0.5 here would give clip errors on edges */
+       viewplane->xmin *= pixsize;
+       viewplane->xmax *= pixsize;
+       viewplane->ymin *= pixsize;
+       viewplane->ymax *= pixsize;
+
+       (*viewdx)= pixsize;
+       (*viewdy)= (*ycor) * pixsize;
+
+       if(rd->mode & R_ORTHO)
+               orthographic_m4(winmat, viewplane->xmin, viewplane->xmax, viewplane->ymin, viewplane->ymax, *clipsta, *clipend);
+       else
+               perspective_m4(winmat, viewplane->xmin, viewplane->xmax, viewplane->ymin, viewplane->ymax, *clipsta, *clipend);
+
+}
+
 #if 0
 static int pc_findindex(ListBase *listbase, int index)
 {
@@ -2598,3 +2860,100 @@ void object_delete_ptcache(Object *ob, int index)
        BLI_freelinkN(&ob->pc_ids, link);
 }
 #endif
+
+/* shape key utility function */
+
+/************************* Mesh ************************/
+static KeyBlock *insert_meshkey(Scene *scene, Object *ob, char *name, int from_mix)
+{
+       Mesh *me= ob->data;
+       Key *key= me->key;
+       KeyBlock *kb;
+       int newkey= 0;
+
+       if(key == NULL) {
+               key= me->key= add_key((ID *)me);
+               key->type= KEY_RELATIVE;
+               newkey= 1;
+       }
+
+       kb= add_keyblock(key, name);
+
+       if(newkey || from_mix==FALSE) {
+               /* create from mesh */
+               mesh_to_key(me, kb);
+       }
+       else {
+               /* copy from current values */
+               kb->data= do_ob_key(scene, ob);
+               kb->totelem= me->totvert;
+       }
+
+       return kb;
+}
+/************************* Lattice ************************/
+static KeyBlock *insert_lattkey(Scene *scene, Object *ob, char *name, int from_mix)
+{
+       Lattice *lt= ob->data;
+       Key *key= lt->key;
+       KeyBlock *kb;
+       int newkey= 0;
+
+       if(key==NULL) {
+               key= lt->key= add_key( (ID *)lt);
+               key->type= KEY_RELATIVE;
+               newkey= 1;
+       }
+
+       kb= add_keyblock(key, name);
+
+       if(newkey || from_mix==FALSE) {
+               /* create from lattice */
+               latt_to_key(lt, kb);
+       }
+       else {
+               /* copy from current values */
+               kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
+               kb->data= do_ob_key(scene, ob);
+       }
+
+       return kb;
+}
+/************************* Curve ************************/
+static KeyBlock *insert_curvekey(Scene *scene, Object *ob, char *name, int from_mix)
+{
+       Curve *cu= ob->data;
+       Key *key= cu->key;
+       KeyBlock *kb;
+       ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
+       int newkey= 0;
+
+       if(key==NULL) {
+               key= cu->key= add_key( (ID *)cu);
+               key->type = KEY_RELATIVE;
+               newkey= 1;
+       }
+
+       kb= add_keyblock(key, name);
+
+       if(newkey || from_mix==FALSE) {
+               /* create from curve */
+               curve_to_key(cu, kb, lb);
+       }
+       else {
+               /* copy from current values */
+               kb->totelem= count_curveverts(lb);
+               kb->data= do_ob_key(scene, ob);
+       }
+
+       return kb;
+}
+
+KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, char *name, int from_mix)
+{
+       if(ob->type==OB_MESH)                                    return insert_meshkey(scene, ob, name, from_mix);
+       else if ELEM(ob->type, OB_CURVE, OB_SURF)return insert_curvekey(scene, ob, name, from_mix);
+       else if(ob->type==OB_LATTICE)                    return insert_lattkey(scene, ob, name, from_mix);
+       else                                                                     return NULL;
+}
+