Testing for the need to quick cache was causing slowdowns on files with many duplis.
[blender.git] / source / blender / blenkernel / intern / object.c
index f363b7d9a0a4d12bbf050f33f0befec2ee9ec31a..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_math.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(ob && ob->sculpt) {
+               SculptSession *ss = ob->sculpt;
+               DerivedMesh *dm= ob->derivedFinal;
 
-               if(ss->fmap)
-                       MEM_freeN(ss->fmap);
-
-               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) {
@@ -741,12 +763,12 @@ float dof_camera(Object *ob)
        if (cam->dof_ob) {      
                /* too simple, better to return the distance on the view axis only
                 * return len_v3v3(ob->obmat[3], cam->dof_ob->obmat[3]); */
-               float mat[4][4], obmat[4][4];
+               float mat[4][4], imat[4][4], obmat[4][4];
                
                copy_m4_m4(obmat, ob->obmat);
                normalize_m4(obmat);
-               invert_m4_m4(ob->imat, obmat);
-               mul_m4_m4m4(mat, cam->dof_ob->obmat, ob->imat);
+               invert_m4_m4(imat, obmat);
+               mul_m4_m4m4(mat, cam->dof_ob->obmat, imat);
                return (float)fabs(mat[3][2]);
        }
        return cam->YF_dofdist;
@@ -795,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);
@@ -835,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) {
@@ -962,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 */
-       unit_qt(ob->quat);
-       unit_qt(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;
-
+       
+       /* 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_SHADED;
+       ob->dt= OB_TEXTURE;
        ob->empty_drawtype= OB_ARROWS;
        ob->empty_drawsize= 1.0;
 
@@ -992,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;
@@ -1019,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;
 }
@@ -1038,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;
@@ -1123,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;
        }
 
@@ -1142,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;
@@ -1264,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;
@@ -1333,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;
        
@@ -1407,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;
@@ -1431,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 */
@@ -1467,28 +1521,8 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
        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
        
@@ -1624,6 +1658,33 @@ void object_rot_to_mat3(Object *ob, float mat[][3])
        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 */
 {
        float smat[3][3];
@@ -1681,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 {
@@ -1704,7 +1765,7 @@ 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) {
                        vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
@@ -1786,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);
-                                               add_v3_v3v3(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++;
                                        }
                                }
@@ -1919,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 */
@@ -1940,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
@@ -1973,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);
@@ -1992,9 +2045,8 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
        }
        
        /* set negative scale flag in object */
-       cross_v3_v3v3(vec, ob->obmat[0], ob->obmat[1]);
-       if( dot_v3v3(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)
@@ -2074,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];
-       
-       sub_v3_v3v3(vec, ob->obmat[3], targetmat[3]);
-       vec_to_quat( quat,vec, ob->trackflag, ob->upflag);
-       quat_to_mat3( totmat,quat);
-       
-       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 copy_m4_m4(tmat, ob->obmat);
-       
-       mul_m4_m3m4(ob->obmat, totmat, tmat);
-
 }
 
 void where_is_object(struct Scene *scene, Object *ob)
@@ -2122,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;
                
@@ -2149,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;
@@ -2160,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 */
@@ -2175,7 +2187,6 @@ void what_does_parent(Scene *scene, Object *ob, Object *workob)
        unit_m4(workob->parentinv);
        unit_m4(workob->constinv);
        workob->parent= ob->parent;
-       workob->track= ob->track;
 
        workob->trackflag= ob->trackflag;
        workob->upflag= ob->upflag;
@@ -2224,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;
@@ -2242,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;
@@ -2308,27 +2357,97 @@ void minmax_object(Object *ob, float *min, float *max)
        }
 }
 
-/* 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 */
@@ -2340,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) {
@@ -2364,6 +2486,11 @@ void object_handle_update(Scene *scene, Object *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);
@@ -2371,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
@@ -2386,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);
                        }
@@ -2402,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);
@@ -2449,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 */
@@ -2574,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)
 {
@@ -2601,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;
+}
+