edits ontop of Alex's patch from r41292.
[blender.git] / source / blender / blenkernel / intern / object.c
index 6254103..819a982 100644 (file)
@@ -1,8 +1,4 @@
-/* object.c
- *
- * 
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/blenkernel/intern/object.c
+ *  \ingroup bke
+ */
+
+
 #include <string.h>
 #include <math.h>
 #include <stdio.h>                     
@@ -49,6 +50,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_sequence_types.h"
+#include "DNA_sound_types.h"
 #include "DNA_space_types.h"
 #include "DNA_view3d_types.h"
 #include "DNA_world_types.h"
@@ -91,7 +93,9 @@
 #include "BKE_sca.h"
 #include "BKE_scene.h"
 #include "BKE_sequencer.h"
+#include "BKE_speaker.h"
 #include "BKE_softbody.h"
+#include "BKE_material.h"
 
 #include "LBM_fluidsim.h"
 
@@ -203,8 +207,8 @@ void object_link_modifiers(struct Object *ob, struct Object *from)
                BLI_addtail(&ob->modifiers, nmd);
        }
 
-       copy_object_particlesystems(from, ob);
-       copy_object_softbody(from, ob);
+       copy_object_particlesystems(ob, from);
+       copy_object_softbody(ob, from);
 
        // TODO: smoke?, cloth?
 }
@@ -227,6 +231,17 @@ void object_free_display(Object *ob)
        freedisplist(&ob->disp);
 }
 
+void free_sculptsession_deformMats(SculptSession *ss)
+{
+       if(ss->orig_cos) MEM_freeN(ss->orig_cos);
+       if(ss->deform_cos) MEM_freeN(ss->deform_cos);
+       if(ss->deform_imats) MEM_freeN(ss->deform_imats);
+
+       ss->orig_cos = NULL;
+       ss->deform_cos = NULL;
+       ss->deform_imats = NULL;
+}
+
 void free_sculptsession(Object *ob)
 {
        if(ob && ob->sculpt) {
@@ -257,6 +272,7 @@ void free_sculptsession(Object *ob)
        }
 }
 
+
 /* do not free object itself */
 void free_object(Object *ob)
 {
@@ -273,7 +289,7 @@ void free_object(Object *ob)
                        else if(ob->type==OB_CURVE) unlink_curve(ob->data);
                        else if(ob->type==OB_MBALL) unlink_mball(ob->data);
                }
-               ob->data= 0;
+               ob->data= NULL;
        }
        
        for(a=0; a<ob->totcol; a++) {
@@ -281,12 +297,12 @@ void free_object(Object *ob)
        }
        if(ob->mat) MEM_freeN(ob->mat);
        if(ob->matbits) MEM_freeN(ob->matbits);
-       ob->mat= 0;
-       ob->matbits= 0;
+       ob->mat= NULL;
+       ob->matbits= NULL;
        if(ob->bb) MEM_freeN(ob->bb); 
-       ob->bb= 0;
+       ob->bb= NULL;
        if(ob->path) free_path(ob->path); 
-       ob->path= 0;
+       ob->path= NULL;
        if(ob->adt) BKE_free_animdata((ID *)ob);
        if(ob->poselib) ob->poselib->id.us--;
        if(ob->gpd) ((ID *)ob->gpd)->us--;
@@ -398,7 +414,7 @@ void unlink_object(Object *ob)
                                                for (ct= targets.first; ct; ct= ct->next) {
                                                        if (ct->tar == ob) {
                                                                ct->tar = NULL;
-                                                               strcpy(ct->subtarget, "");
+                                                               ct->subtarget[0]= '\0';
                                                                obt->recalc |= OB_RECALC_DATA;
                                                        }
                                                }
@@ -428,7 +444,7 @@ void unlink_object(Object *ob)
                                for (ct= targets.first; ct; ct= ct->next) {
                                        if (ct->tar == ob) {
                                                ct->tar = NULL;
-                                               strcpy(ct->subtarget, "");
+                                               ct->subtarget[0]= '\0';
                                                obt->recalc |= OB_RECALC_DATA;
                                        }
                                }
@@ -731,51 +747,41 @@ void make_local_camera(Camera *cam)
 {
        Main *bmain= G.main;
        Object *ob;
-       Camera *camn;
        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.lib==NULL) return;
        if(cam->id.us==1) {
-               cam->id.lib= 0;
-               cam->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)cam, 0);
+               id_clear_lib_data(bmain, (ID *)cam);
                return;
        }
        
-       ob= bmain->object.first;
-       while(ob) {
+       for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) {
                if(ob->data==cam) {
                        if(ob->id.lib) lib= 1;
                        else local= 1;
                }
-               ob= ob->id.next;
        }
        
        if(local && lib==0) {
-               cam->id.lib= 0;
-               cam->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)cam, 0);
+               id_clear_lib_data(bmain, (ID *)cam);
        }
        else if(local && lib) {
-               camn= copy_camera(cam);
+               Camera *camn= copy_camera(cam);
                camn->id.us= 0;
                
-               ob= bmain->object.first;
-               while(ob) {
-                       if(ob->data==cam) {
-                               
-                               if(ob->id.lib==0) {
+               for(ob= bmain->object.first; ob; ob= ob->id.next) {
+                       if(ob->data == cam) {
+                               if(ob->id.lib==NULL) {
                                        ob->data= camn;
                                        camn->id.us++;
                                        cam->id.us--;
                                }
                        }
-                       ob= ob->id.next;
                }
        }
 }
@@ -867,11 +873,32 @@ Lamp *copy_lamp(Lamp *la)
        
        lan->curfalloff = curvemapping_copy(la->curfalloff);
        
-#if 0 // XXX old animation system
-       id_us_plus((ID *)lan->ipo);
-#endif // XXX old animation system
+       if(la->preview)
+               lan->preview = BKE_previewimg_copy(la->preview);
+       
+       return lan;
+}
+
+Lamp *localize_lamp(Lamp *la)
+{
+       Lamp *lan;
+       int a;
+       
+       lan= copy_libblock(la);
+       BLI_remlink(&G.main->lamp, lan);
 
-       if (la->preview) lan->preview = BKE_previewimg_copy(la->preview);
+       for(a=0; a<MAX_MTEX; a++) {
+               if(lan->mtex[a]) {
+                       lan->mtex[a]= MEM_mallocN(sizeof(MTex), "localize_lamp");
+                       memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
+                       /* free lamp decrements */
+                       id_us_plus((ID *)lan->mtex[a]->tex);
+               }
+       }
+       
+       lan->curfalloff = curvemapping_copy(la->curfalloff);
+
+       lan->preview= NULL;
        
        return lan;
 }
@@ -888,11 +915,9 @@ void make_local_lamp(Lamp *la)
                * - mixed: make copy
                */
        
-       if(la->id.lib==0) return;
+       if(la->id.lib==NULL) return;
        if(la->id.us==1) {
-               la->id.lib= 0;
-               la->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)la, 0);
+               id_clear_lib_data(bmain, (ID *)la);
                return;
        }
        
@@ -906,9 +931,7 @@ void make_local_lamp(Lamp *la)
        }
        
        if(local && lib==0) {
-               la->id.lib= 0;
-               la->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)la, 0);
+               id_clear_lib_data(bmain, (ID *)la);
        }
        else if(local && lib) {
                lan= copy_lamp(la);
@@ -918,7 +941,7 @@ void make_local_lamp(Lamp *la)
                while(ob) {
                        if(ob->data==la) {
                                
-                               if(ob->id.lib==0) {
+                               if(ob->id.lib==NULL) {
                                        ob->data= lan;
                                        lan->id.us++;
                                        la->id.us--;
@@ -968,6 +991,7 @@ static void *add_obdata_from_type(int type)
        case OB_LAMP: return add_lamp("Lamp");
        case OB_LATTICE: return add_lattice("Lattice");
        case OB_ARMATURE: return add_armature("Armature");
+       case OB_SPEAKER: return add_speaker("Speaker");
        case OB_EMPTY: return NULL;
        default:
                printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
@@ -987,6 +1011,7 @@ static const char *get_obdata_defname(int type)
        case OB_LAMP: return "Lamp";
        case OB_LATTICE: return "Lattice";
        case OB_ARMATURE: return "Armature";
+       case OB_SPEAKER: return "Speaker";
        case OB_EMPTY: return "Empty";
        default:
                printf("get_obdata_defname: Internal error, bad type: %d\n", type);
@@ -1013,10 +1038,13 @@ Object *add_only_object(int type, const char *name)
         * 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;
+
+       unit_axis_angle(ob->rotAxis, &ob->rotAngle);
+       unit_axis_angle(ob->drotAxis, &ob->drotAngle);
+
+       unit_qt(ob->quat);
+       unit_qt(ob->dquat);
+
        /* rotation locks should be 4D for 4 component rotations by default... */
        ob->protectflag = OB_LOCK_ROT4D;
        
@@ -1027,7 +1055,7 @@ Object *add_only_object(int type, const char *name)
        ob->empty_drawtype= OB_PLAINAXES;
        ob->empty_drawsize= 1.0;
 
-       if(type==OB_CAMERA || type==OB_LAMP) {
+       if(type==OB_CAMERA || type==OB_LAMP || type==OB_SPEAKER) {
                ob->trackflag= OB_NEGZ;
                ob->upflag= OB_POSY;
        }
@@ -1054,6 +1082,7 @@ Object *add_only_object(int type, const char *name)
        ob->state=1;
        /* ob->pad3 == Contact Processing Threshold */
        ob->m_contactProcessingThreshold = 1.;
+       ob->obstacleRad = 1.;
        
        /* NT fluid sim defaults */
        ob->fluidsimFlag = 0;
@@ -1075,7 +1104,7 @@ Object *add_object(struct Scene *scene, int type)
        Base *base;
        char name[32];
 
-       strcpy(name, get_obdata_defname(type));
+       BLI_strncpy(name, get_obdata_defname(type), sizeof(name));
        ob = add_only_object(type, name);
 
        ob->data= add_obdata_from_type(type);
@@ -1124,7 +1153,7 @@ BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
        return bsbn;
 }
 
-ParticleSystem *copy_particlesystem(ParticleSystem *psys)
+static ParticleSystem *copy_particlesystem(ParticleSystem *psys)
 {
        ParticleSystem *psysn;
        ParticleData *pa;
@@ -1269,6 +1298,37 @@ static void copy_object_pose(Object *obn, Object *ob)
        }
 }
 
+static int object_pose_context(Object *ob)
+{
+       if(     (ob) &&
+               (ob->type == OB_ARMATURE) &&
+               (ob->pose) &&
+               (ob->mode & OB_MODE_POSE)
+       ) {
+               return 1;
+       }
+       else {
+               return 0;
+       }
+}
+
+//Object *object_pose_armature_get(Object *ob)
+Object *object_pose_armature_get(struct Object *ob)
+{
+       if(ob==NULL)
+               return NULL;
+
+       if(object_pose_context(ob))
+               return ob;
+
+       ob= modifiers_isDeformedByArmature(ob);
+
+       if(object_pose_context(ob))
+               return ob;
+
+       return NULL;
+}
+
 static void copy_object_transform(Object *ob_tar, Object *ob_src)
 {
        copy_v3_v3(ob_tar->loc, ob_src->loc);
@@ -1358,11 +1418,10 @@ Object *copy_object(Object *ob)
        return obn;
 }
 
-void expand_local_object(Object *ob)
+static void extern_local_object(Object *ob)
 {
        //bActionStrip *strip;
        ParticleSystem *psys;
-       int a;
 
 #if 0 // XXX old animation system
        id_lib_extern((ID *)ob->action);
@@ -1370,10 +1429,11 @@ void expand_local_object(Object *ob)
 #endif // XXX old animation system
        id_lib_extern((ID *)ob->data);
        id_lib_extern((ID *)ob->dup_group);
-       
-       for(a=0; a<ob->totcol; a++) {
-               id_lib_extern((ID *)ob->mat[a]);
-       }
+       id_lib_extern((ID *)ob->poselib);
+       id_lib_extern((ID *)ob->gpd);
+
+       extern_local_matarar(ob->mat, ob->totcol);
+
 #if 0 // XXX old animation system
        for (strip=ob->nlastrips.first; strip; strip=strip->next) {
                id_lib_extern((ID *)strip->act);
@@ -1386,53 +1446,42 @@ void expand_local_object(Object *ob)
 void make_local_object(Object *ob)
 {
        Main *bmain= G.main;
-       Object *obn;
        Scene *sce;
        Base *base;
        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;
        
        ob->proxy= ob->proxy_from= NULL;
        
        if(ob->id.us==1) {
-               ob->id.lib= NULL;
-               ob->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)ob, 0);
-
+               id_clear_lib_data(bmain, (ID *)ob);
+               extern_local_object(ob);
        }
        else {
-               sce= bmain->scene.first;
-               while(sce) {
-                       base= sce->base.first;
-                       while(base) {
-                               if(base->object==ob) {
-                                       if(sce->id.lib) lib++;
-                                       else local++;
-                                       break;
-                               }
-                               base= base->next;
+               for(sce= bmain->scene.first; sce && ELEM(0, lib, local); sce= sce->id.next) {
+                       if(object_in_scene(ob, sce)) {
+                               if(sce->id.lib) lib= 1;
+                               else local= 1;
                        }
-                       sce= sce->id.next;
                }
-               
+
                if(local && lib==0) {
-                       ob->id.lib= 0;
-                       ob->id.flag= LIB_LOCAL;
-                       new_id(0, (ID *)ob, 0);
+                       id_clear_lib_data(bmain, (ID *)ob);
+                       extern_local_object(ob);
                }
                else if(local && lib) {
-                       obn= copy_object(ob);
+                       Object *obn= copy_object(ob);
                        obn->id.us= 0;
                        
                        sce= bmain->scene.first;
                        while(sce) {
-                               if(sce->id.lib==0) {
+                               if(sce->id.lib==NULL) {
                                        base= sce->base.first;
                                        while(base) {
                                                if(base->object==ob) {
@@ -1447,8 +1496,6 @@ void make_local_object(Object *ob)
                        }
                }
        }
-       
-       expand_local_object(ob);
 }
 
 /*
@@ -1555,6 +1602,12 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
        if(gob) {
                ob->rotmode= target->rotmode;
                mul_m4_m4m4(ob->obmat, target->obmat, gob->obmat);
+               if(gob->dup_group) { /* should always be true */
+                       float tvec[3];
+                       copy_v3_v3(tvec, gob->dup_group->dupli_ofs);
+                       mul_mat3_m4_v3(ob->obmat, tvec);
+                       sub_v3_v3(ob->obmat[3], tvec);
+               }
                object_apply_mat4(ob, ob->obmat, FALSE, TRUE);
        }
        else {
@@ -1580,7 +1633,7 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
        if(ob->matbits) MEM_freeN(ob->matbits);
        ob->mat = NULL;
        ob->matbits= NULL;
-       if ((target->totcol) && (target->mat) && ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) { //XXX OB_SUPPORT_MATERIAL
+       if ((target->totcol) && (target->mat) && OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
                int i;
                ob->colbits = target->colbits;
                
@@ -1603,6 +1656,10 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
                
                armature_set_id_extern(ob);
        }
+       else if (target->type == OB_EMPTY) {
+               ob->empty_drawtype = target->empty_drawtype;
+               ob->empty_drawsize = target->empty_drawsize;
+       }
 
        /* copy IDProperties */
        if(ob->id.properties) {
@@ -1623,12 +1680,6 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
 
 /* there is also a timing calculation in drawobject() */
 
-int no_speed_curve= 0;
-
-void disable_speed_curve(int val)
-{
-       no_speed_curve= val;
-}
 
 // XXX THIS CRUFT NEEDS SERIOUS RECODING ASAP!
 /* ob can be NULL */
@@ -1666,29 +1717,28 @@ void object_rot_to_mat3(Object *ob, float mat[][3])
 {
        float rmat[3][3], dmat[3][3];
        
-       /* initialise the delta-rotation matrix, which will get (pre)multiplied 
+       /* 'dmat' is the delta-rotation matrix, which will get (pre)multiplied
         * with the rotation matrix to yield the appropriate rotation
         */
-       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) */
-               eulO_to_mat3( rmat,ob->rot, ob->rotmode);
-               eulO_to_mat3( dmat,ob->drot, ob->rotmode);
+               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 */
-               axis_angle_to_mat3( rmat,ob->rotAxis, ob->rotAngle);
-               axis_angle_to_mat3( dmat,ob->drotAxis, ob->drotAngle);
+               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 */
                float tquat[4];
-
+               
                normalize_qt_qt(tquat, ob->quat);
                quat_to_mat3(rmat, tquat);
-
+               
                normalize_qt_qt(tquat, ob->dquat);
                quat_to_mat3(dmat, tquat);
        }
@@ -1701,8 +1751,13 @@ void object_mat3_to_rot(Object *ob, float mat[][3], short use_compat)
 {
        switch(ob->rotmode) {
        case ROT_MODE_QUAT:
-               mat3_to_quat(ob->quat, mat);
-               sub_v4_v4(ob->quat, ob->dquat);
+               {
+                       float dquat[4];
+                       mat3_to_quat(ob->quat, mat);
+                       normalize_qt_qt(dquat, ob->dquat);
+                       invert_qt(dquat);
+                       mul_qt_qtqt(ob->quat, dquat, ob->quat);
+               }
                break;
        case ROT_MODE_AXISANGLE:
                mat3_to_axis_angle(ob->rotAxis, &ob->rotAngle, mat);
@@ -1710,9 +1765,22 @@ void object_mat3_to_rot(Object *ob, float mat[][3], short use_compat)
                ob->rotAngle -= ob->drotAngle;
                break;
        default: /* euler */
-               if(use_compat)  mat3_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, mat);
-               else                    mat3_to_eulO(ob->rot, ob->rotmode, mat);
-               sub_v3_v3(ob->rot, ob->drot);
+               {
+                       float quat[4];
+                       float dquat[4];
+                       float tmat[3][3];
+
+                       /* without drot we could apply 'mat' directly */
+                       mat3_to_quat(quat, mat);
+                       eulO_to_quat(dquat, ob->drot, ob->rotmode);
+                       invert_qt(dquat);
+                       mul_qt_qtqt(quat, dquat, quat);
+                       quat_to_mat3(tmat, quat);
+                       /* end drot correction */
+
+                       if(use_compat)  mat3_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, tmat);
+                       else                    mat3_to_eulO(ob->rot, ob->rotmode, tmat);
+               }
        }
 }
 
@@ -1727,8 +1795,8 @@ void object_apply_mat4(Object *ob, float mat[][4], const short use_compat, const
                invert_m4_m4(imat, diff_mat);
                mul_m4_m4m4(rmat, mat, imat); /* get the parent relative matrix */
                object_apply_mat4(ob, rmat, use_compat, FALSE);
-
-               /* same as below, use rmat rather then mat */
+               
+               /* same as below, use rmat rather than mat */
                mat4_to_loc_rot_size(ob->loc, rot, ob->size, rmat);
                object_mat3_to_rot(ob, rot, use_compat);
        }
@@ -1767,6 +1835,7 @@ void object_to_mat4(Object *ob, float mat[][4])
        add_v3_v3v3(mat[3], ob->loc, ob->dloc);
 }
 
+/* extern */
 int enable_cu_speed= 1;
 
 static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
@@ -1800,14 +1869,19 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
                 * 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= cu->ctime / cu->pathlen;
-               CLAMP(ctime, 0.0, 1.0);
+               if (IS_EQF(cu->pathlen, 0.0f) == 0)
+                       ctime= cu->ctime / cu->pathlen;
+               else
+                       ctime= cu->ctime;
+
+               CLAMP(ctime, 0.0f, 1.0f);
        }
        else {
                ctime= scene->r.cfra - give_timeoffset(ob);
-               ctime /= cu->pathlen;
+               if (IS_EQF(cu->pathlen, 0.0f) == 0)
+                       ctime /= cu->pathlen;
                
-               CLAMP(ctime, 0.0, 1.0);
+               CLAMP(ctime, 0.0f, 1.0f);
        }
        
        /* time calculus is correct, now apply distance offset */
@@ -1898,9 +1972,10 @@ static void give_parvert(Object *par, int nr, float *vec)
                if(dm) {
                        MVert *mvert= dm->getVertArray(dm);
                        int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
-                       int i, count = 0, vindex, numVerts = dm->getNumVerts(dm);
+                       int i, vindex, numVerts = dm->getNumVerts(dm);
 
                        /* get the average of all verts with (original index == nr) */
+                       count= 0;
                        for(i = 0; i < numVerts; i++) {
                                vindex= (index)? index[i]: i;
 
@@ -2041,7 +2116,7 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
        if(ob==NULL) return;
        
        /* execute drivers only, as animation has already been done */
-       BKE_animsys_evaluate_animdata(&ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
+       BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
        
        if(ob->parent) {
                Object *par= ob->parent;
@@ -2249,17 +2324,17 @@ void what_does_parent(Scene *scene, Object *ob, Object *workob)
        workob->constraints.first = ob->constraints.first;
        workob->constraints.last = ob->constraints.last;
 
-       strcpy(workob->parsubstr, ob->parsubstr); 
+       BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
 
        where_is_object(scene, workob);
 }
 
-BoundBox *unit_boundbox()
+BoundBox *unit_boundbox(void)
 {
        BoundBox *bb;
        float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
 
-       bb= MEM_callocN(sizeof(BoundBox), "bb");
+       bb= MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
        boundbox_set_from_min_max(bb, min, max);
        
        return bb;
@@ -2313,9 +2388,9 @@ void object_get_dimensions(Object *ob, float *value)
                
                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]);
+               value[0] = fabsf(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
+               value[1] = fabsf(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
+               value[2] = fabsf(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
        } else {
                value[0] = value[1] = value[2] = 0.f;
        }
@@ -2344,24 +2419,42 @@ void object_set_dimensions(Object *ob, const float *value)
 void minmax_object(Object *ob, float *min, float *max)
 {
        BoundBox bb;
-       Mesh *me;
-       Curve *cu;
        float vec[3];
        int a;
+       short change= FALSE;
        
        switch(ob->type) {
-               
        case OB_CURVE:
        case OB_FONT:
        case OB_SURF:
-               cu= ob->data;
-               
-               if(cu->bb==NULL) tex_space_curve(cu);
-               bb= *(cu->bb);
-               
-               for(a=0; a<8; a++) {
-                       mul_m4_v3(ob->obmat, bb.vec[a]);
-                       DO_MINMAX(bb.vec[a], min, max);
+               {
+                       Curve *cu= ob->data;
+
+                       if(cu->bb==NULL) tex_space_curve(cu);
+                       bb= *(cu->bb);
+
+                       for(a=0; a<8; a++) {
+                               mul_m4_v3(ob->obmat, bb.vec[a]);
+                               DO_MINMAX(bb.vec[a], min, max);
+                       }
+                       change= TRUE;
+               }
+               break;
+       case OB_LATTICE:
+               {
+                       Lattice *lt= ob->data;
+                       BPoint *bp= lt->def;
+                       int u, v, w;
+
+                       for(w=0; w<lt->pntsw; w++) {
+                               for(v=0; v<lt->pntsv; v++) {
+                                       for(u=0; u<lt->pntsu; u++, bp++) {
+                                               mul_v3_m4v3(vec, ob->obmat, bp->vec);
+                                               DO_MINMAX(vec, min, max);
+                                       }
+                               }
+                       }
+                       change= TRUE;
                }
                break;
        case OB_ARMATURE:
@@ -2373,25 +2466,27 @@ void minmax_object(Object *ob, float *min, float *max)
                                mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
                                DO_MINMAX(vec, min, max);
                        }
-                       break;
+                       change= TRUE;
                }
-               /* no break, get_mesh will give NULL and it passes on to default */
+               break;
        case OB_MESH:
-               me= get_mesh(ob);
-               
-               if(me) {
-                       bb = *mesh_get_bb(ob);
-                       
-                       for(a=0; a<8; a++) {
-                               mul_m4_v3(ob->obmat, bb.vec[a]);
-                               DO_MINMAX(bb.vec[a], min, max);
+               {
+                       Mesh *me= get_mesh(ob);
+
+                       if(me) {
+                               bb = *mesh_get_bb(ob);
+
+                               for(a=0; a<8; a++) {
+                                       mul_m4_v3(ob->obmat, bb.vec[a]);
+                                       DO_MINMAX(bb.vec[a], min, max);
+                               }
+                               change= TRUE;
                        }
                }
-               if(min[0] < max[0] ) break;
-               
-               /* else here no break!!!, mesh can be zero sized */
-               
-       default:
+               break;
+       }
+
+       if(change == FALSE) {
                DO_MINMAX(ob->obmat[3], min, max);
 
                copy_v3_v3(vec, ob->obmat[3]);
@@ -2401,7 +2496,6 @@ void minmax_object(Object *ob, float *min, float *max)
                copy_v3_v3(vec, ob->obmat[3]);
                sub_v3_v3(vec, ob->size);
                DO_MINMAX(vec, min, max);
-               break;
        }
 }
 
@@ -2537,6 +2631,9 @@ void object_handle_update(Scene *scene, Object *ob)
                                        Object *obg= ob->proxy_from->proxy_group;
                                        invert_m4_m4(obg->imat, obg->obmat);
                                        mul_m4_m4m4(ob->obmat, ob->proxy_from->obmat, obg->imat);
+                                       if(obg->dup_group) { /* should always be true */
+                                               add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
+                                       }
                                }
                                else
                                        copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
@@ -2558,7 +2655,7 @@ void object_handle_update(Scene *scene, Object *ob)
                        if(adt) {
                                /* evaluate drivers */
                                // XXX: for mesh types, should we push this to derivedmesh instead?
-                               BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
+                               BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
                        }
 
                        /* includes all keys and modifiers */
@@ -2576,11 +2673,12 @@ void object_handle_update(Scene *scene, Object *ob)
 
 #else                          /* ensure CD_MASK_BAREMESH for now */
                                        EditMesh *em = (ob == scene->obedit)? BKE_mesh_get_editmesh(ob->data): NULL;
+                                       unsigned int data_mask= scene->customdata_mask | ob->customdata_mask | CD_MASK_BAREMESH;
                                        if(em) {
-                                               makeDerivedMesh(scene, ob, em,  scene->customdata_mask | CD_MASK_BAREMESH); /* was CD_MASK_BAREMESH */
+                                               makeDerivedMesh(scene, ob, em,  data_mask); /* was CD_MASK_BAREMESH */
                                                BKE_mesh_end_editmesh(ob->data, em);
                                        } else
-                                               makeDerivedMesh(scene, ob, NULL, scene->customdata_mask | CD_MASK_BAREMESH);
+                                               makeDerivedMesh(scene, ob, NULL, data_mask);
 #endif
 
                                }
@@ -2688,7 +2786,35 @@ void object_handle_update(Scene *scene, Object *ob)
        }
 }
 
-float give_timeoffset(Object *ob) {
+void object_sculpt_modifiers_changed(Object *ob)
+{
+       SculptSession *ss= ob->sculpt;
+
+       if(!ss->cache) {
+               /* we free pbvh on changes, except during sculpt since it can't deal with
+                  changing PVBH node organization, we hope topology does not change in
+                  the meantime .. weak */
+               if(ss->pbvh) {
+                               BLI_pbvh_free(ss->pbvh);
+                               ss->pbvh= NULL;
+               }
+
+               free_sculptsession_deformMats(ob->sculpt);
+       } else {
+               PBVHNode **nodes;
+               int n, totnode;
+
+               BLI_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
+
+               for(n = 0; n < totnode; n++)
+                       BLI_pbvh_node_mark_update(nodes[n]);
+
+               MEM_freeN(nodes);
+       }
+}
+
+float give_timeoffset(Object *ob)
+{
        if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
                return ob->sf + give_timeoffset(ob->parent);
        } else {
@@ -2696,7 +2822,8 @@ float give_timeoffset(Object *ob) {
        }
 }
 
-int give_obdata_texspace(Object *ob, short **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;
@@ -2793,6 +2920,16 @@ int object_insert_ptcache(Object *ob)
        return i;
 }
 
+void object_camera_mode(RenderData *rd, Object *camera)
+{
+       rd->mode &= ~(R_ORTHO|R_PANORAMA);
+       if(camera && camera->type==OB_CAMERA) {
+               Camera *cam= camera->data;
+               if(cam->type == CAM_ORTHO) rd->mode |= R_ORTHO;
+               if(cam->flag & CAM_PANORAMA) rd->mode |= R_PANORAMA;
+       }
+}
+
 /* 'lens' may be set for envmap only */
 void object_camera_matrix(
                RenderData *rd, Object *camera, int winx, int winy, short field_second,
@@ -2802,8 +2939,7 @@ void object_camera_matrix(
        Camera *cam=NULL;
        float pixsize;
        float shiftx=0.0, shifty=0.0, winside, viewfac;
-
-       rd->mode &= ~(R_ORTHO|R_PANORAMA);
+       short is_ortho= FALSE;
 
        /* question mark */
        (*ycor)= rd->yasp / rd->xasp;
@@ -2813,8 +2949,9 @@ void object_camera_matrix(
        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;
+               if(cam->type == CAM_ORTHO) {
+                       is_ortho= TRUE;
+               }
 
                /* solve this too... all time depending stuff is in convertblender.c?
                 * Need to update the camera early because it's used for projection matrices
@@ -2834,18 +2971,18 @@ void object_camera_matrix(
        }
        else if(camera->type==OB_LAMP) {
                Lamp *la= camera->data;
-               float fac= cos( M_PI*la->spotsize/360.0 );
+               float fac= cosf((float)M_PI*la->spotsize/360.0f);
                float phi= acos(fac);
 
-               (*lens)= 16.0*fac/sin(phi);
+               (*lens)= 16.0f*fac/sinf(phi);
                if((*lens)==0.0f)
-                       (*lens)= 35.0;
+                       (*lens)= 35.0f;
                (*clipsta)= la->clipsta;
                (*clipend)= la->clipend;
        }
        else {  /* envmap exception... */;
                if((*lens)==0.0f)
-                       (*lens)= 16.0;
+                       (*lens)= 16.0f;
 
                if((*clipsta)==0.0f || (*clipend)==0.0f) {
                        (*clipsta)= 0.1f;
@@ -2854,7 +2991,7 @@ void object_camera_matrix(
        }
 
        /* ortho only with camera available */
-       if(cam && rd->mode & R_ORTHO) {
+       if(cam && is_ortho) {
                if(rd->xasp*winx >= rd->yasp*winy) {
                        viewfac= winx;
                }
@@ -2865,8 +3002,8 @@ void object_camera_matrix(
                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;
+               if(rd->xasp*winx >= rd->yasp*winy)      viewfac= ((*lens) * winx)/32.0f;
+               else                                                            viewfac= (*ycor) * ((*lens) * winy)/32.0f;
                pixsize= (*clipsta) / viewfac;
        }
 
@@ -2879,12 +3016,12 @@ void object_camera_matrix(
 
        if(field_second) {
                if(rd->mode & R_ODDFIELD) {
-                       viewplane->ymin-= 0.5 * (*ycor);
-                       viewplane->ymax-= 0.5 * (*ycor);
+                       viewplane->ymin-= 0.5f * (*ycor);
+                       viewplane->ymax-= 0.5f * (*ycor);
                }
                else {
-                       viewplane->ymin+= 0.5 * (*ycor);
-                       viewplane->ymax+= 0.5 * (*ycor);
+                       viewplane->ymin+= 0.5f * (*ycor);
+                       viewplane->ymax+= 0.5f * (*ycor);
                }
        }
        /* the window matrix is used for clipping, and not changed during OSA steps */
@@ -2897,13 +3034,86 @@ void object_camera_matrix(
        (*viewdx)= pixsize;
        (*viewdy)= (*ycor) * pixsize;
 
-       if(rd->mode & R_ORTHO)
+       if(is_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);
 
 }
 
+void camera_view_frame_ex(Scene *scene, Camera *camera, float drawsize, const short do_clip, const float scale[3],
+                          float r_asp[2], float r_shift[2], float *r_drawsize, float r_vec[4][3])
+{
+       float facx, facy;
+       float depth;
+
+       /* aspect correcton */
+       if (scene) {
+               float aspx= (float) scene->r.xsch*scene->r.xasp;
+               float aspy= (float) scene->r.ysch*scene->r.yasp;
+
+               if(aspx < aspy) {
+                       r_asp[0]= aspx / aspy;
+                       r_asp[1]= 1.0;
+               }
+               else {
+                       r_asp[0]= 1.0;
+                       r_asp[1]= aspy / aspx;
+               }
+       }
+       else {
+               r_asp[0]= 1.0f;
+               r_asp[1]= 1.0f;
+       }
+
+       if(camera->type==CAM_ORTHO) {
+               facx= 0.5f * camera->ortho_scale * r_asp[0] * scale[0];
+               facy= 0.5f * camera->ortho_scale * r_asp[1] * scale[1];
+               r_shift[0]= camera->shiftx * camera->ortho_scale * scale[0];
+               r_shift[1]= camera->shifty * camera->ortho_scale * scale[1];
+               depth= do_clip ? -((camera->clipsta * scale[2]) + 0.1f) : - drawsize * camera->ortho_scale * scale[2];
+
+               *r_drawsize= 0.5f * camera->ortho_scale;
+       }
+       else {
+               /* that way it's always visible - clipsta+0.1 */
+               float fac;
+               *r_drawsize= drawsize / ((scale[0] + scale[1] + scale[2]) / 3.0f);
+
+               if(do_clip) {
+                       /* fixed depth, variable size (avoids exceeding clipping range) */
+                       depth = -(camera->clipsta + 0.1f);
+                       fac = depth / (camera->lens/-16.0f * scale[2]);
+               }
+               else {
+                       /* fixed size, variable depth (stays a reasonable size in the 3D view) */
+                       depth= *r_drawsize * camera->lens/-16.0f * scale[2];
+                       fac= *r_drawsize;
+               }
+
+               facx= fac * r_asp[0] * scale[0];
+               facy= fac * r_asp[1] * scale[1];
+               r_shift[0]= camera->shiftx*fac*2 * scale[0];
+               r_shift[1]= camera->shifty*fac*2 * scale[1];
+       }
+
+       r_vec[0][0]= r_shift[0] + facx; r_vec[0][1]= r_shift[1] + facy; r_vec[0][2]= depth;
+       r_vec[1][0]= r_shift[0] + facx; r_vec[1][1]= r_shift[1] - facy; r_vec[1][2]= depth;
+       r_vec[2][0]= r_shift[0] - facx; r_vec[2][1]= r_shift[1] - facy; r_vec[2][2]= depth;
+       r_vec[3][0]= r_shift[0] - facx; r_vec[3][1]= r_shift[1] + facy; r_vec[3][2]= depth;
+}
+
+void camera_view_frame(Scene *scene, Camera *camera, float r_vec[4][3])
+{
+       float dummy_asp[2];
+       float dummy_shift[2];
+       float dummy_drawsize;
+       const float dummy_scale[3]= {1.0f, 1.0f, 1.0f};
+
+       camera_view_frame_ex(scene, camera, FALSE, 1.0, dummy_scale,
+                            dummy_asp, dummy_shift, &dummy_drawsize, r_vec);
+}
+
 #if 0
 static int pc_findindex(ListBase *listbase, int index)
 {
@@ -2981,9 +3191,14 @@ static KeyBlock *insert_lattkey(Scene *scene, Object *ob, const char *name, int
 
        if(newkey || from_mix==FALSE) {
                kb= add_keyblock(key, name);
-
-               /* create from lattice */
-               latt_to_key(lt, kb);
+               if (!newkey) {
+                       KeyBlock *basekb= (KeyBlock *)key->block.first;
+                       kb->data= MEM_dupallocN(basekb->data);
+                       kb->totelem= basekb->totelem;
+               }
+               else {
+                       latt_to_key(lt, kb);
+               }
        }
        else {
                /* copy from current values */
@@ -3019,7 +3234,10 @@ static KeyBlock *insert_curvekey(Scene *scene, Object *ob, const char *name, int
                        KeyBlock *basekb= (KeyBlock *)key->block.first;
                        kb->data= MEM_dupallocN(basekb->data);
                        kb->totelem= basekb->totelem;
-               } else curve_to_key(cu, kb, lb);
+               }
+               else {
+                       curve_to_key(cu, kb, lb);
+               }
        }
        else {
                /* copy from current values */
@@ -3042,3 +3260,53 @@ KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, const char *name, in
        else                                                                     return NULL;
 }
 
+/* most important if this is modified it should _always_ return True, in certain
+ * cases false positives are hard to avoid (shape keys for eg)
+ */
+int object_is_modified(Scene *scene, Object *ob)
+{
+       int flag= 0;
+
+       if(ob_get_key(ob)) {
+               flag |= eModifierMode_Render;
+       }
+       else {
+               ModifierData *md;
+               /* cloth */
+               for(md=modifiers_getVirtualModifierList(ob); md && (flag != (eModifierMode_Render | eModifierMode_Realtime)); md=md->next) {
+                       if((flag & eModifierMode_Render) == 0   && modifier_isEnabled(scene, md, eModifierMode_Render))         flag |= eModifierMode_Render;
+                       if((flag & eModifierMode_Realtime) == 0 && modifier_isEnabled(scene, md, eModifierMode_Realtime))       flag |= eModifierMode_Realtime;
+               }
+       }
+
+       return flag;
+}
+
+static void copy_object__forwardModifierLinks(void *UNUSED(userData), Object *UNUSED(ob), ID **idpoin)
+{
+       /* this is copied from ID_NEW; it might be better to have a macro */
+       if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid;
+}
+
+void object_relink(Object *ob)
+{
+       if(ob->id.lib)
+               return;
+
+       relink_constraints(&ob->constraints);
+       if (ob->pose){
+               bPoseChannel *chan;
+               for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
+                       relink_constraints(&chan->constraints);
+               }
+       }
+       modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL);
+
+       if(ob->adt)
+               BKE_relink_animdata(ob->adt);
+
+       ID_NEW(ob->parent);
+
+       ID_NEW(ob->proxy);
+       ID_NEW(ob->proxy_group);
+}