svn merge -r36583:36603 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / blenkernel / intern / object.c
index 6d264b3fed245bc50c4f5ff001e7f169e8815b0c..caae8763f8354910431d52f9acb35852abd0afb6 100644 (file)
  * ***** END GPL LICENSE BLOCK *****
  */
 
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/blenkernel/intern/object.c
+ *  \ingroup bke
+ */
+
+
 #include <string.h>
 #include <math.h>
 #include <stdio.h>                     
 #include <string.h>
 #include <math.h>
 #include <stdio.h>                     
 #include "BLI_editVert.h"
 #include "BLI_math.h"
 #include "BLI_pbvh.h"
 #include "BLI_editVert.h"
 #include "BLI_math.h"
 #include "BLI_pbvh.h"
-
-#include "BKE_utildefines.h"
+#include "BLI_utildefines.h"
 
 #include "BKE_main.h"
 #include "BKE_global.h"
 
 #include "BKE_main.h"
 #include "BKE_global.h"
-
+#include "BKE_idprop.h"
 #include "BKE_armature.h"
 #include "BKE_action.h"
 #include "BKE_bullet.h"
 #include "BKE_armature.h"
 #include "BKE_action.h"
 #include "BKE_bullet.h"
@@ -82,6 +86,7 @@
 #include "BKE_lattice.h"
 #include "BKE_library.h"
 #include "BKE_mesh.h"
 #include "BKE_lattice.h"
 #include "BKE_library.h"
 #include "BKE_mesh.h"
+#include "BKE_tessmesh.h"
 #include "BKE_mball.h"
 #include "BKE_modifier.h"
 #include "BKE_object.h"
 #include "BKE_mball.h"
 #include "BKE_modifier.h"
 #include "BKE_object.h"
 #include "BKE_scene.h"
 #include "BKE_sequencer.h"
 #include "BKE_softbody.h"
 #include "BKE_scene.h"
 #include "BKE_sequencer.h"
 #include "BKE_softbody.h"
+#include "BKE_material.h"
 
 #include "LBM_fluidsim.h"
 
 
 #include "LBM_fluidsim.h"
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
 #include "BPY_extern.h"
 #endif
 
 #include "BPY_extern.h"
 #endif
 
@@ -204,8 +210,8 @@ void object_link_modifiers(struct Object *ob, struct Object *from)
                BLI_addtail(&ob->modifiers, nmd);
        }
 
                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?
 }
 
        // TODO: smoke?, cloth?
 }
@@ -245,6 +251,13 @@ void free_sculptsession(Object *ob)
                if(ss->layer_co)
                        MEM_freeN(ss->layer_co);
 
                if(ss->layer_co)
                        MEM_freeN(ss->layer_co);
 
+               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);
+
                MEM_freeN(ss);
 
                ob->sculpt = NULL;
                MEM_freeN(ss);
 
                ob->sculpt = NULL;
@@ -267,7 +280,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);
                }
                        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++) {
        }
        
        for(a=0; a<ob->totcol; a++) {
@@ -275,16 +288,15 @@ void free_object(Object *ob)
        }
        if(ob->mat) MEM_freeN(ob->mat);
        if(ob->matbits) MEM_freeN(ob->matbits);
        }
        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); 
        if(ob->bb) MEM_freeN(ob->bb); 
-       ob->bb= 0;
+       ob->bb= NULL;
        if(ob->path) free_path(ob->path); 
        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->adt) BKE_free_animdata((ID *)ob);
        if(ob->poselib) ob->poselib->id.us--;
-       if(ob->dup_group) ob->dup_group->id.us--;
-       if(ob->gpd) ob->gpd->id.us--;
+       if(ob->gpd) ((ID *)ob->gpd)->us--;
        if(ob->defbase.first)
                BLI_freelistN(&ob->defbase);
        if(ob->pose)
        if(ob->defbase.first)
                BLI_freelistN(&ob->defbase);
        if(ob->pose)
@@ -317,12 +329,13 @@ static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Objec
 
        if (*obpoin==unlinkOb) {
                *obpoin = NULL;
 
        if (*obpoin==unlinkOb) {
                *obpoin = NULL;
-               ob->recalc |= OB_RECALC_ALL; // XXX: should this just be OB_RECALC_DATA?
+               ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME; // XXX: should this just be OB_RECALC_DATA?
        }
 }
 
        }
 }
 
-void unlink_object(Scene *scene, Object *ob)
+void unlink_object(Object *ob)
 {
 {
+       Main *bmain= G.main;
        Object *obt;
        Material *mat;
        World *wrld;
        Object *obt;
        Material *mat;
        World *wrld;
@@ -344,7 +357,7 @@ void unlink_object(Scene *scene, Object *ob)
        
        /* check all objects: parents en bevels and fields, also from libraries */
        // FIXME: need to check all animation blocks (drivers)
        
        /* check all objects: parents en bevels and fields, also from libraries */
        // FIXME: need to check all animation blocks (drivers)
-       obt= G.main->object.first;
+       obt= bmain->object.first;
        while(obt) {
                if(obt->proxy==ob)
                        obt->proxy= NULL;
        while(obt) {
                if(obt->proxy==ob)
                        obt->proxy= NULL;
@@ -357,7 +370,7 @@ void unlink_object(Scene *scene, Object *ob)
                
                if(obt->parent==ob) {
                        obt->parent= NULL;
                
                if(obt->parent==ob) {
                        obt->parent= NULL;
-                       obt->recalc |= OB_RECALC_ALL;
+                       obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
                }
                
                modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
                }
                
                modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
@@ -367,15 +380,15 @@ void unlink_object(Scene *scene, Object *ob)
 
                        if(cu->bevobj==ob) {
                                cu->bevobj= NULL;
 
                        if(cu->bevobj==ob) {
                                cu->bevobj= NULL;
-                               obt->recalc |= OB_RECALC_ALL;
+                               obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
                        }
                        if(cu->taperobj==ob) {
                                cu->taperobj= NULL;
                        }
                        if(cu->taperobj==ob) {
                                cu->taperobj= NULL;
-                               obt->recalc |= OB_RECALC_ALL;
+                               obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
                        }
                        if(cu->textoncurve==ob) {
                                cu->textoncurve= NULL;
                        }
                        if(cu->textoncurve==ob) {
                                cu->textoncurve= NULL;
-                               obt->recalc |= OB_RECALC_ALL;
+                               obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
                        }
                }
                else if(obt->type==OB_ARMATURE && obt->pose) {
                        }
                }
                else if(obt->type==OB_ARMATURE && obt->pose) {
@@ -518,7 +531,7 @@ void unlink_object(Scene *scene, Object *ob)
        }
        
        /* materials */
        }
        
        /* materials */
-       mat= G.main->mat.first;
+       mat= bmain->mat.first;
        while(mat) {
        
                for(a=0; a<MAX_MTEX; a++) {
        while(mat) {
        
                for(a=0; a<MAX_MTEX; a++) {
@@ -532,16 +545,14 @@ void unlink_object(Scene *scene, Object *ob)
        }
        
        /* textures */
        }
        
        /* textures */
-       tex= G.main->tex.first;
-       while(tex) {
-               if(tex->env) {
-                       if(tex->env->object == ob) tex->env->object= NULL;
-               }
-               tex= tex->id.next;
+       for(tex= bmain->tex.first; tex; tex= tex->id.next) {
+               if(tex->env && (ob==tex->env->object)) tex->env->object= NULL;
+               if(tex->pd  && (ob==tex->pd->object))  tex->pd->object= NULL;
+               if(tex->vd  && (ob==tex->vd->object))  tex->vd->object= NULL;
        }
 
        /* worlds */
        }
 
        /* worlds */
-       wrld= G.main->world.first;
+       wrld= bmain->world.first;
        while(wrld) {
                if(wrld->id.lib==NULL) {
                        for(a=0; a<MAX_MTEX; a++) {
        while(wrld) {
                if(wrld->id.lib==NULL) {
                        for(a=0; a<MAX_MTEX; a++) {
@@ -554,7 +565,7 @@ void unlink_object(Scene *scene, Object *ob)
        }
                
        /* scenes */
        }
                
        /* scenes */
-       sce= G.main->scene.first;
+       sce= bmain->scene.first;
        while(sce) {
                if(sce->id.lib==NULL) {
                        if(sce->camera==ob) sce->camera= NULL;
        while(sce) {
                if(sce->id.lib==NULL) {
                        if(sce->camera==ob) sce->camera= NULL;
@@ -586,7 +597,7 @@ void unlink_object(Scene *scene, Object *ob)
        
 #if 0 // XXX old animation system
        /* ipos */
        
 #if 0 // XXX old animation system
        /* ipos */
-       ipo= G.main->ipo.first;
+       ipo= bmain->ipo.first;
        while(ipo) {
                if(ipo->id.lib==NULL) {
                        IpoCurve *icu;
        while(ipo) {
                if(ipo->id.lib==NULL) {
                        IpoCurve *icu;
@@ -600,7 +611,7 @@ void unlink_object(Scene *scene, Object *ob)
 #endif // XXX old animation system
        
        /* screens */
 #endif // XXX old animation system
        
        /* screens */
-       sc= G.main->screen.first;
+       sc= bmain->screen.first;
        while(sc) {
                ScrArea *sa= sc->areabase.first;
                while(sa) {
        while(sc) {
                ScrArea *sa= sc->areabase.first;
                while(sa) {
@@ -665,14 +676,14 @@ void unlink_object(Scene *scene, Object *ob)
        }
 
        /* groups */
        }
 
        /* groups */
-       group= G.main->group.first;
+       group= bmain->group.first;
        while(group) {
                rem_from_group(group, ob, NULL, NULL);
                group= group->id.next;
        }
        
        /* cameras */
        while(group) {
                rem_from_group(group, ob, NULL, NULL);
                group= group->id.next;
        }
        
        /* cameras */
-       camera= G.main->camera.first;
+       camera= bmain->camera.first;
        while(camera) {
                if (camera->dof_ob==ob) {
                        camera->dof_ob = NULL;
        while(camera) {
                if (camera->dof_ob==ob) {
                        camera->dof_ob = NULL;
@@ -695,7 +706,7 @@ int exist_object(Object *obtest)
        return 0;
 }
 
        return 0;
 }
 
-void *add_camera(char *name)
+void *add_camera(const char *name)
 {
        Camera *cam;
        
 {
        Camera *cam;
        
@@ -717,7 +728,6 @@ Camera *copy_camera(Camera *cam)
        Camera *camn;
        
        camn= copy_libblock(cam);
        Camera *camn;
        
        camn= copy_libblock(cam);
-       camn->adt= BKE_copy_animdata(cam->adt);
        
        return camn;
 }
        
        return camn;
 }
@@ -726,52 +736,47 @@ Camera *copy_camera(Camera *cam)
 
 void make_local_camera(Camera *cam)
 {
 
 void make_local_camera(Camera *cam)
 {
+       Main *bmain= G.main;
        Object *ob;
        Object *ob;
-       Camera *camn;
        int local=0, lib=0;
 
        /* - only lib users: do nothing
        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) {
        if(cam->id.us==1) {
-               cam->id.lib= 0;
+               cam->id.lib= NULL;
                cam->id.flag= LIB_LOCAL;
                cam->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)cam, 0);
+               new_id(&bmain->camera, (ID *)cam, NULL);
                return;
        }
        
                return;
        }
        
-       ob= G.main->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;
                }
                if(ob->data==cam) {
                        if(ob->id.lib) lib= 1;
                        else local= 1;
                }
-               ob= ob->id.next;
        }
        
        if(local && lib==0) {
        }
        
        if(local && lib==0) {
-               cam->id.lib= 0;
+               cam->id.lib= NULL;
                cam->id.flag= LIB_LOCAL;
                cam->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)cam, 0);
+               new_id(&bmain->camera, (ID *)cam, NULL);
        }
        else if(local && lib) {
        }
        else if(local && lib) {
-               camn= copy_camera(cam);
+               Camera *camn= copy_camera(cam);
                camn->id.us= 0;
                
                camn->id.us= 0;
                
-               ob= G.main->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->data= camn;
                                        camn->id.us++;
                                        cam->id.us--;
                                }
                        }
-                       ob= ob->id.next;
                }
        }
 }
                }
        }
 }
@@ -796,7 +801,7 @@ float dof_camera(Object *ob)
        return cam->YF_dofdist;
 }
 
        return cam->YF_dofdist;
 }
 
-void *add_lamp(char *name)
+void *add_lamp(const char *name)
 {
        Lamp *la;
        
 {
        Lamp *la;
        
@@ -874,6 +879,7 @@ Lamp *copy_lamp(Lamp *la)
 
 void make_local_lamp(Lamp *la)
 {
 
 void make_local_lamp(Lamp *la)
 {
+       Main *bmain= G.main;
        Object *ob;
        Lamp *lan;
        int local=0, lib=0;
        Object *ob;
        Lamp *lan;
        int local=0, lib=0;
@@ -883,15 +889,15 @@ void make_local_lamp(Lamp *la)
                * - mixed: make copy
                */
        
                * - mixed: make copy
                */
        
-       if(la->id.lib==0) return;
+       if(la->id.lib==NULL) return;
        if(la->id.us==1) {
        if(la->id.us==1) {
-               la->id.lib= 0;
+               la->id.lib= NULL;
                la->id.flag= LIB_LOCAL;
                la->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)la, 0);
+               new_id(&bmain->lamp, (ID *)la, NULL);
                return;
        }
        
                return;
        }
        
-       ob= G.main->object.first;
+       ob= bmain->object.first;
        while(ob) {
                if(ob->data==la) {
                        if(ob->id.lib) lib= 1;
        while(ob) {
                if(ob->data==la) {
                        if(ob->id.lib) lib= 1;
@@ -901,19 +907,19 @@ void make_local_lamp(Lamp *la)
        }
        
        if(local && lib==0) {
        }
        
        if(local && lib==0) {
-               la->id.lib= 0;
+               la->id.lib= NULL;
                la->id.flag= LIB_LOCAL;
                la->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)la, 0);
+               new_id(&bmain->lamp, (ID *)la, NULL);
        }
        else if(local && lib) {
                lan= copy_lamp(la);
                lan->id.us= 0;
                
        }
        else if(local && lib) {
                lan= copy_lamp(la);
                lan->id.us= 0;
                
-               ob= G.main->object.first;
+               ob= bmain->object.first;
                while(ob) {
                        if(ob->data==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--;
                                        ob->data= lan;
                                        lan->id.us++;
                                        la->id.us--;
@@ -942,19 +948,13 @@ void free_lamp(Lamp *la)
        
        BKE_free_animdata((ID *)la);
 
        
        BKE_free_animdata((ID *)la);
 
-       curvemapping_free(la->curfalloff);
+       curvemapping_free(la->curfalloff); 
        
        BKE_previewimg_free(&la->preview);
        BKE_icon_delete(&la->id);
        la->id.icon_id = 0;
 }
 
        
        BKE_previewimg_free(&la->preview);
        BKE_icon_delete(&la->id);
        la->id.icon_id = 0;
 }
 
-void *add_wave()
-{
-       return 0;
-}
-
-
 /* *************************************************** */
 
 static void *add_obdata_from_type(int type)
 /* *************************************************** */
 
 static void *add_obdata_from_type(int type)
@@ -968,7 +968,6 @@ static void *add_obdata_from_type(int type)
        case OB_CAMERA: return add_camera("Camera");
        case OB_LAMP: return add_lamp("Lamp");
        case OB_LATTICE: return add_lattice("Lattice");
        case OB_CAMERA: return add_camera("Camera");
        case OB_LAMP: return add_lamp("Lamp");
        case OB_LATTICE: return add_lattice("Lattice");
-       case OB_WAVE: return add_wave();
        case OB_ARMATURE: return add_armature("Armature");
        case OB_EMPTY: return NULL;
        default:
        case OB_ARMATURE: return add_armature("Armature");
        case OB_EMPTY: return NULL;
        default:
@@ -977,7 +976,7 @@ static void *add_obdata_from_type(int type)
        }
 }
 
        }
 }
 
-static char *get_obdata_defname(int type)
+static const char *get_obdata_defname(int type)
 {
        switch (type) {
        case OB_MESH: return "Mesh";
 {
        switch (type) {
        case OB_MESH: return "Mesh";
@@ -988,7 +987,6 @@ static char *get_obdata_defname(int type)
        case OB_CAMERA: return "Camera";
        case OB_LAMP: return "Lamp";
        case OB_LATTICE: return "Lattice";
        case OB_CAMERA: return "Camera";
        case OB_LAMP: return "Lamp";
        case OB_LATTICE: return "Lattice";
-       case OB_WAVE: return "Wave";
        case OB_ARMATURE: return "Armature";
        case OB_EMPTY: return "Empty";
        default:
        case OB_ARMATURE: return "Armature";
        case OB_EMPTY: return "Empty";
        default:
@@ -998,7 +996,7 @@ static char *get_obdata_defname(int type)
 }
 
 /* more general add: creates minimum required data, but without vertices etc. */
 }
 
 /* more general add: creates minimum required data, but without vertices etc. */
-Object *add_only_object(int type, char *name)
+Object *add_only_object(int type, const char *name)
 {
        Object *ob;
 
 {
        Object *ob;
 
@@ -1016,10 +1014,13 @@ Object *add_only_object(int type, char *name)
         * but rotations default to quaternions 
         */
        ob->rotmode= ROT_MODE_EUL;
         * 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;
        
        /* rotation locks should be 4D for 4 component rotations by default... */
        ob->protectflag = OB_LOCK_ROT4D;
        
@@ -1027,7 +1028,7 @@ Object *add_only_object(int type, char *name)
        unit_m4(ob->parentinv);
        unit_m4(ob->obmat);
        ob->dt= OB_TEXTURE;
        unit_m4(ob->parentinv);
        unit_m4(ob->obmat);
        ob->dt= OB_TEXTURE;
-       ob->empty_drawtype= OB_ARROWS;
+       ob->empty_drawtype= OB_PLAINAXES;
        ob->empty_drawsize= 1.0;
 
        if(type==OB_CAMERA || type==OB_LAMP) {
        ob->empty_drawsize= 1.0;
 
        if(type==OB_CAMERA || type==OB_LAMP) {
@@ -1078,7 +1079,7 @@ Object *add_object(struct Scene *scene, int type)
        Base *base;
        char name[32];
 
        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);
        ob = add_only_object(type, name);
 
        ob->data= add_obdata_from_type(type);
@@ -1087,7 +1088,7 @@ Object *add_object(struct Scene *scene, int type)
        
        base= scene_add_base(scene, ob);
        scene_select_base(scene, base);
        
        base= scene_add_base(scene, ob);
        scene_select_base(scene, base);
-       ob->recalc |= OB_RECALC_ALL;
+       ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
 
        return ob;
 }
 
        return ob;
 }
@@ -1127,7 +1128,7 @@ BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
        return bsbn;
 }
 
        return bsbn;
 }
 
-ParticleSystem *copy_particlesystem(ParticleSystem *psys)
+static ParticleSystem *copy_particlesystem(ParticleSystem *psys)
 {
        ParticleSystem *psysn;
        ParticleData *pa;
 {
        ParticleSystem *psysn;
        ParticleData *pa;
@@ -1272,6 +1273,17 @@ static void copy_object_pose(Object *obn, Object *ob)
        }
 }
 
        }
 }
 
+static void copy_object_transform(Object *ob_tar, Object *ob_src)
+{
+       copy_v3_v3(ob_tar->loc, ob_src->loc);
+       copy_v3_v3(ob_tar->rot, ob_src->rot);
+       copy_v3_v3(ob_tar->quat, ob_src->quat);
+       copy_v3_v3(ob_tar->rotAxis, ob_src->rotAxis);
+       ob_tar->rotAngle= ob_src->rotAngle;
+       ob_tar->rotmode= ob_src->rotmode;
+       copy_v3_v3(ob_tar->size, ob_src->size);
+}
+
 Object *copy_object(Object *ob)
 {
        Object *obn;
 Object *copy_object(Object *ob)
 {
        Object *obn;
@@ -1320,8 +1332,8 @@ Object *copy_object(Object *ob)
 
        /* increase user numbers */
        id_us_plus((ID *)obn->data);
 
        /* increase user numbers */
        id_us_plus((ID *)obn->data);
-       id_us_plus((ID *)obn->dup_group);
-       
+       id_us_plus((ID *)obn->gpd);
+       id_lib_extern((ID *)obn->dup_group);
 
        for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
        
 
        for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
        
@@ -1344,15 +1356,16 @@ Object *copy_object(Object *ob)
 
        obn->gpulamp.first = obn->gpulamp.last = NULL;
        obn->pc_ids.first = obn->pc_ids.last = NULL;
 
        obn->gpulamp.first = obn->gpulamp.last = NULL;
        obn->pc_ids.first = obn->pc_ids.last = NULL;
+
+       obn->mpath= NULL;
        
        return obn;
 }
 
        
        return obn;
 }
 
-void expand_local_object(Object *ob)
+static void extern_local_object(Object *ob)
 {
        //bActionStrip *strip;
        ParticleSystem *psys;
 {
        //bActionStrip *strip;
        ParticleSystem *psys;
-       int a;
 
 #if 0 // XXX old animation system
        id_lib_extern((ID *)ob->action);
 
 #if 0 // XXX old animation system
        id_lib_extern((ID *)ob->action);
@@ -1360,10 +1373,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);
 #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);
 #if 0 // XXX old animation system
        for (strip=ob->nlastrips.first; strip; strip=strip->next) {
                id_lib_extern((ID *)strip->act);
@@ -1375,16 +1389,16 @@ void expand_local_object(Object *ob)
 
 void make_local_object(Object *ob)
 {
 
 void make_local_object(Object *ob)
 {
-       Object *obn;
+       Main *bmain= G.main;
        Scene *sce;
        Base *base;
        int local=0, lib=0;
 
        /* - only lib users: do nothing
        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.lib==NULL) return;
        
        ob->proxy= ob->proxy_from= NULL;
@@ -1392,36 +1406,28 @@ void make_local_object(Object *ob)
        if(ob->id.us==1) {
                ob->id.lib= NULL;
                ob->id.flag= LIB_LOCAL;
        if(ob->id.us==1) {
                ob->id.lib= NULL;
                ob->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)ob, 0);
-
+               new_id(&bmain->object, (ID *)ob, NULL);
        }
        else {
        }
        else {
-               sce= G.main->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) {
                if(local && lib==0) {
-                       ob->id.lib= 0;
+                       ob->id.lib= NULL;
                        ob->id.flag= LIB_LOCAL;
                        ob->id.flag= LIB_LOCAL;
-                       new_id(0, (ID *)ob, 0);
+                       new_id(&bmain->object, (ID *)ob, NULL);
                }
                else if(local && lib) {
                }
                else if(local && lib) {
-                       obn= copy_object(ob);
+                       Object *obn= copy_object(ob);
                        obn->id.us= 0;
                        
                        obn->id.us= 0;
                        
-                       sce= G.main->scene.first;
+                       sce= bmain->scene.first;
                        while(sce) {
                        while(sce) {
-                               if(sce->id.lib==0) {
+                               if(sce->id.lib==NULL) {
                                        base= sce->base.first;
                                        while(base) {
                                                if(base->object==ob) {
                                        base= sce->base.first;
                                        while(base) {
                                                if(base->object==ob) {
@@ -1437,7 +1443,7 @@ void make_local_object(Object *ob)
                }
        }
        
                }
        }
        
-       expand_local_object(ob);
+       extern_local_object(ob);
 }
 
 /*
 }
 
 /*
@@ -1470,7 +1476,7 @@ static void armature_set_id_extern(Object *ob)
 {
        bArmature *arm= ob->data;
        bPoseChannel *pchan;
 {
        bArmature *arm= ob->data;
        bPoseChannel *pchan;
-       int lay= arm->layer_protected;
+       unsigned int lay= arm->layer_protected;
        
        for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
                if(!(pchan->bone->layer & lay))
        
        for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
                if(!(pchan->bone->layer & lay))
@@ -1533,25 +1539,31 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
        ob->proxy_group= gob;
        id_lib_extern(&target->id);
        
        ob->proxy_group= gob;
        id_lib_extern(&target->id);
        
-       ob->recalc= target->recalc= OB_RECALC_ALL;
+       ob->recalc= target->recalc= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
        
        
-       /* copy transform */
+       /* copy transform
+        * - gob means this proxy comes from a group, just apply the matrix
+        *   so the object wont move from its dupli-transform.
+        *
+        * - no gob means this is being made from a linked object,
+        *   this is closer to making a copy of the object - in-place. */
        if(gob) {
        if(gob) {
-               VECCOPY(ob->loc, gob->loc);
-               VECCOPY(ob->rot, gob->rot);
-               VECCOPY(ob->size, gob->size);
-               
-               group_tag_recalc(gob->dup_group);
+               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 {
        }
        else {
-               VECCOPY(ob->loc, target->loc);
-               VECCOPY(ob->rot, target->rot);
-               VECCOPY(ob->size, target->size);
+               copy_object_transform(ob, target);
+               ob->parent= target->parent;     /* libdata */
+               copy_m4_m4(ob->parentinv, target->parentinv);
        }
        
        }
        
-       ob->parent= target->parent;     /* libdata */
-       copy_m4_m4(ob->parentinv, target->parentinv);
-       
        /* copy animdata stuff - drivers only for now... */
        object_copy_proxy_drivers(ob, target);
 
        /* copy animdata stuff - drivers only for now... */
        object_copy_proxy_drivers(ob, target);
 
@@ -1592,7 +1604,21 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
                
                armature_set_id_extern(ob);
        }
                
                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) {
+               IDP_FreeProperty(ob->id.properties);
+               MEM_freeN(ob->id.properties);
+               ob->id.properties= NULL;
+       }
+       if(target->id.properties) {
+               ob->id.properties= IDP_CopyProperty(target->id.properties);
+       }
+
        /* copy drawtype info */
        ob->dt= target->dt;
 }
        /* copy drawtype info */
        ob->dt= target->dt;
 }
@@ -1602,7 +1628,7 @@ void object_make_proxy(Object *ob, Object *target, Object *gob)
 
 /* there is also a timing calculation in drawobject() */
 
 
 /* there is also a timing calculation in drawobject() */
 
-int no_speed_curve= 0;
+static int no_speed_curve= 0;
 
 void disable_speed_curve(int val)
 {
 
 void disable_speed_curve(int val)
 {
@@ -1611,7 +1637,7 @@ void disable_speed_curve(int val)
 
 // XXX THIS CRUFT NEEDS SERIOUS RECODING ASAP!
 /* ob can be NULL */
 
 // XXX THIS CRUFT NEEDS SERIOUS RECODING ASAP!
 /* ob can be NULL */
-float bsystem_time(struct Scene *scene, Object *ob, float cfra, float ofs)
+float bsystem_time(struct Scene *scene, Object *UNUSED(ob), float cfra, float ofs)
 {
        /* returns float ( see BKE_curframe in scene.c) */
        cfra += scene->r.subframe;
 {
        /* returns float ( see BKE_curframe in scene.c) */
        cfra += scene->r.subframe;
@@ -1636,14 +1662,11 @@ float bsystem_time(struct Scene *scene, Object *ob, float cfra, float ofs)
 void object_scale_to_mat3(Object *ob, float mat[][3])
 {
        float vec[3];
 void object_scale_to_mat3(Object *ob, float mat[][3])
 {
        float vec[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];
+       add_v3_v3v3(vec, ob->size, ob->dsize);
        size_to_mat3( mat,vec);
 }
 
        size_to_mat3( mat,vec);
 }
 
-// TODO: this should take rotation orders into account later...
+
 void object_rot_to_mat3(Object *ob, float mat[][3])
 {
        float rmat[3][3], dmat[3][3];
 void object_rot_to_mat3(Object *ob, float mat[][3])
 {
        float rmat[3][3], dmat[3][3];
@@ -1666,41 +1689,67 @@ void object_rot_to_mat3(Object *ob, float mat[][3])
        }
        else {
                /* quats are normalised before use to eliminate scaling issues */
        }
        else {
                /* quats are normalised before use to eliminate scaling issues */
-               normalize_qt(ob->quat);
-               quat_to_mat3( rmat,ob->quat);
-               quat_to_mat3( dmat,ob->dquat);
+               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);
        }
        
        /* combine these rotations */
        }
        
        /* combine these rotations */
-       // XXX is this correct? if errors, change the order of multiplication...
        mul_m3_m3m3(mat, dmat, rmat);
 }
 
        mul_m3_m3m3(mat, dmat, rmat);
 }
 
-void object_mat3_to_rot(Object *ob, float mat[][3], int use_compat)
+void object_mat3_to_rot(Object *ob, float mat[][3], short 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);
+       switch(ob->rotmode) {
+       case ROT_MODE_QUAT:
+               {
+                       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);
                }
                }
-               else
-                       mat3_to_eulO(ob->rot, ob->rotmode, mat);
+               break;
+       case ROT_MODE_AXISANGLE:
+               mat3_to_axis_angle(ob->rotAxis, &ob->rotAngle, mat);
+               sub_v3_v3(ob->rotAxis, ob->drotAxis);
+               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);
        }
 }
 
 /* see pchan_apply_mat4() for the equivalent 'pchan' function */
        }
 }
 
 /* see pchan_apply_mat4() for the equivalent 'pchan' function */
-void object_apply_mat4(Object *ob, float mat[][4])
+void object_apply_mat4(Object *ob, float mat[][4], const short use_compat, const short use_parent)
 {
 {
-       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);
+       float rot[3][3];
+
+       if(use_parent && ob->parent) {
+               float rmat[4][4], diff_mat[4][4], imat[4][4];
+               mul_m4_m4m4(diff_mat, ob->parentinv, ob->parent->obmat);
+               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 */
+               mat4_to_loc_rot_size(ob->loc, rot, ob->size, rmat);
+               object_mat3_to_rot(ob, rot, use_compat);
+       }
+       else {
+               mat4_to_loc_rot_size(ob->loc, rot, ob->size, mat);
+               object_mat3_to_rot(ob, rot, use_compat);
+       }
+       
+       sub_v3_v3(ob->loc, ob->dloc);
+       sub_v3_v3(ob->size, ob->dsize);
+       /* object_mat3_to_rot handles delta rotations */
 }
 
 void object_to_mat3(Object *ob, float mat[][3])        /* no parent */
 }
 
 void object_to_mat3(Object *ob, float mat[][3])        /* no parent */
@@ -1728,12 +1777,13 @@ void object_to_mat4(Object *ob, float mat[][4])
        add_v3_v3v3(mat[3], ob->loc, ob->dloc);
 }
 
        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])
 {
        Curve *cu;
 int enable_cu_speed= 1;
 
 static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
 {
        Curve *cu;
-       float q[4], vec[4], dir[3], quat[4], radius, x1, ctime;
+       float vec[4], dir[3], quat[4], radius, ctime;
        float timeoffs = 0.0, sf_orig = 0.0;
        
        unit_m4(mat);
        float timeoffs = 0.0, sf_orig = 0.0;
        
        unit_m4(mat);
@@ -1761,14 +1811,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
                 */
                 * 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);
        }
        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 */
        }
        
        /* time calculus is correct, now apply distance offset */
@@ -1781,9 +1836,11 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
        
        
        /* vec: 4 items! */
        
        
        /* vec: 4 items! */
-        if( where_on_path(par, ctime, vec, dir, NULL, &radius, NULL) ) {
+       if( where_on_path(par, ctime, vec, dir, cu->flag & CU_FOLLOW ? quat:NULL, &radius, NULL) ) {
 
                if(cu->flag & CU_FOLLOW) {
 
                if(cu->flag & CU_FOLLOW) {
+#if 0
+                       float x1, q[4];
                        vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
                        
                        /* the tilt */
                        vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
                        
                        /* the tilt */
@@ -1794,8 +1851,11 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
                        q[2]= -x1*dir[1];
                        q[3]= -x1*dir[2];
                        mul_qt_qtqt(quat, q, quat);
                        q[2]= -x1*dir[1];
                        q[3]= -x1*dir[2];
                        mul_qt_qtqt(quat, q, quat);
-                       
-                       quat_to_mat4( mat,quat);
+#else
+                       quat_apply_track(quat, ob->trackflag, ob->upflag);
+#endif
+                       normalize_qt(quat);
+                       quat_to_mat4(mat, quat);
                }
                
                if(cu->flag & CU_PATH_RADIUS) {
                }
                
                if(cu->flag & CU_PATH_RADIUS) {
@@ -1805,7 +1865,7 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
                        copy_m4_m4(mat, rmat);
                }
 
                        copy_m4_m4(mat, rmat);
                }
 
-               VECCOPY(mat[3], vec);
+               copy_v3_v3(mat[3], vec);
                
        }
 }
                
        }
 }
@@ -1832,59 +1892,63 @@ static void ob_parbone(Object *ob, Object *par, float mat[][4])
        copy_m4_m4(mat, pchan->pose_mat);
 
        /* but for backwards compatibility, the child has to move to the tail */
        copy_m4_m4(mat, pchan->pose_mat);
 
        /* but for backwards compatibility, the child has to move to the tail */
-       VECCOPY(vec, mat[1]);
+       copy_v3_v3(vec, mat[1]);
        mul_v3_fl(vec, pchan->bone->length);
        add_v3_v3(mat[3], vec);
 }
 
 static void give_parvert(Object *par, int nr, float *vec)
 {
        mul_v3_fl(vec, pchan->bone->length);
        add_v3_v3(mat[3], vec);
 }
 
 static void give_parvert(Object *par, int nr, float *vec)
 {
-       EditMesh *em;
+       BMEditMesh *em;
        int a, count;
        
        vec[0]=vec[1]=vec[2]= 0.0f;
        
        if(par->type==OB_MESH) {
                Mesh *me= par->data;
        int a, count;
        
        vec[0]=vec[1]=vec[2]= 0.0f;
        
        if(par->type==OB_MESH) {
                Mesh *me= par->data;
-               em = BKE_mesh_get_editmesh(me);
+               DerivedMesh *dm;
+
+               em = me->edit_btmesh;
 
                if(em) {
 
                if(em) {
-                       EditVert *eve;
-                       
-                       for(eve= em->verts.first; eve; eve= eve->next) {
-                               if(eve->keyindex==nr) {
+                       BMVert *eve;
+                       BMIter iter;
+
+                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                               int *keyindex = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
+                               
+                               if(keyindex && *keyindex==nr) {
                                        memcpy(vec, eve->co, sizeof(float)*3);
                                        break;
                                }
                        }
                                        memcpy(vec, eve->co, sizeof(float)*3);
                                        break;
                                }
                        }
-                       BKE_mesh_end_editmesh(me, em);
                }
                }
-               else {
-                       DerivedMesh *dm = par->derivedFinal;
+
+               dm = (em)? em->derivedFinal: par->derivedFinal;
                        
                        
-                       if(dm) {
-                               MVert *mvert= dm->getVertArray(dm);
-                               int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
-                               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++) {
-                                       vindex= (index)? index[i]: i;
-
-                                       if(vindex == nr) {
-                                               add_v3_v3(vec, mvert[i].co);
-                                               count++;
-                                       }
+               if(dm) {
+                       MVert *mvert= dm->getVertArray(dm);
+                       int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
+                       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;
+
+                               if(vindex == nr) {
+                                       add_v3_v3(vec, mvert[i].co);
+                                       count++;
                                }
                                }
+                       }
 
 
-                               if (count==0) {
-                                       /* keep as 0,0,0 */
-                               } else if(count > 0) {
-                                       mul_v3_fl(vec, 1.0f / count);
-                               } else {
-                                       /* use first index if its out of range */
-                                       dm->getVertCo(dm, 0, vec);
-                               }
+                       if (count==0) {
+                               /* keep as 0,0,0 */
+                       } else if(count > 0) {
+                               mul_v3_fl(vec, 1.0f / count);
+                       } else {
+                               /* use first index if its out of range */
+                               dm->getVertCo(dm, 0, vec);
                        }
                }
        }
                        }
                }
        }
@@ -1894,13 +1958,12 @@ static void give_parvert(Object *par, int nr, float *vec)
                BPoint *bp;
                BezTriple *bezt;
                int found= 0;
                BPoint *bp;
                BezTriple *bezt;
                int found= 0;
-               
+               ListBase *nurbs;
+
                cu= par->data;
                cu= par->data;
-               if(cu->editnurb)
-                       nu= cu->editnurb->first;
-               else
-                       nu= cu->nurb.first;
-               
+               nurbs= BKE_curve_nurbs(cu);
+               nu= nurbs->first;
+
                count= 0;
                while(nu && !found) {
                        if(nu->type == CU_BEZIER) {
                count= 0;
                while(nu && !found) {
                        if(nu->type == CU_BEZIER) {
@@ -1939,7 +2002,7 @@ static void give_parvert(Object *par, int nr, float *vec)
                DispList *dl = find_displist(&par->disp, DL_VERTS);
                float *co = dl?dl->verts:NULL;
                
                DispList *dl = find_displist(&par->disp, DL_VERTS);
                float *co = dl?dl->verts:NULL;
                
-               if(latt->editlatt) latt= latt->editlatt;
+               if(latt->editlatt) latt= latt->editlatt->latt;
                
                a= latt->pntsu*latt->pntsv*latt->pntsw;
                count= 0;
                
                a= latt->pntsu*latt->pntsv*latt->pntsw;
                count= 0;
@@ -2049,7 +2112,7 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
        }
 
        /* solve constraints */
        }
 
        /* solve constraints */
-       if (ob->constraints.first && !(ob->flag & OB_NO_CONSTRAINTS)) {
+       if (ob->constraints.first && !(ob->transflag & OB_NO_CONSTRAINTS)) {
                bConstraintOb *cob;
                
                cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
                bConstraintOb *cob;
                
                cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
@@ -2134,7 +2197,7 @@ static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[
                copy_m3_m4(originmat, tmat);
                
                // origin, voor help line
                copy_m3_m4(originmat, tmat);
                
                // origin, voor help line
-               if( (ob->partype & 15)==PARSKEL ) {
+               if( (ob->partype & PARTYPE)==PARSKEL ) {
                        VECCOPY(ob->orig, par->obmat[3]);
                }
                else {
                        VECCOPY(ob->orig, par->obmat[3]);
                }
                else {
@@ -2215,12 +2278,12 @@ void what_does_parent(Scene *scene, Object *ob, Object *workob)
        workob->constraints.first = ob->constraints.first;
        workob->constraints.last = ob->constraints.last;
 
        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);
 }
 
 
        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};
 {
        BoundBox *bb;
        float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
@@ -2279,9 +2342,9 @@ void object_get_dimensions(Object *ob, float *value)
                
                mat4_to_size( scale,ob->obmat);
                
                
                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;
        }
        } else {
                value[0] = value[1] = value[2] = 0.f;
        }
@@ -2334,11 +2397,9 @@ void minmax_object(Object *ob, float *min, float *max)
                if(ob->pose) {
                        bPoseChannel *pchan;
                        for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
                if(ob->pose) {
                        bPoseChannel *pchan;
                        for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-                               VECCOPY(vec, pchan->pose_head);
-                               mul_m4_v3(ob->obmat, vec);
+                               mul_v3_m4v3(vec, ob->obmat, pchan->pose_head);
                                DO_MINMAX(vec, min, max);
                                DO_MINMAX(vec, min, max);
-                               VECCOPY(vec, pchan->pose_tail);
-                               mul_m4_v3(ob->obmat, vec);
+                               mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
                                DO_MINMAX(vec, min, max);
                        }
                        break;
                                DO_MINMAX(vec, min, max);
                        }
                        break;
@@ -2505,6 +2566,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);
                                        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);
                                }
                                else
                                        copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
@@ -2523,47 +2587,64 @@ void object_handle_update(Scene *scene, Object *ob)
                        if (G.f & G_DEBUG)
                                printf("recalcdata %s\n", ob->id.name+2);
                        
                        if (G.f & G_DEBUG)
                                printf("recalcdata %s\n", ob->id.name+2);
                        
-                       /* includes all keys and modifiers */
-                       if(ob->type==OB_MESH) {
-                               EditMesh *em = BKE_mesh_get_editmesh(ob->data);
-                               
+
+                       if(adt) {
                                /* evaluate drivers */
                                /* evaluate drivers */
-                               // XXX: should we push this to derivedmesh instead?
+                               // 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(data_id, adt, ctime, ADT_RECALC_DRIVERS);
+
                                
                                
-                                       // here was vieweditdatamask? XXX
-                               if(em) {
-                                       makeDerivedMesh(scene, ob, em, CD_MASK_BAREMESH);
-                                       BKE_mesh_end_editmesh(ob->data, em);
-                               } else
-                                       makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH);
-                       }
-                       else if(ob->type==OB_MBALL) {
-                               makeDispListMBall(scene, ob);
-                       } 
-                       else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
-                               makeDispListCurveTypes(scene, ob, 0);
                        }
                        }
-                       else if(ELEM(ob->type, OB_CAMERA, OB_LAMP)) {
-                               /* evaluate drivers */
-                               BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
-                       }
-                       else if(ob->type==OB_LATTICE) {
-                               lattice_calc_modifiers(scene, ob);
-                       }
-                       else if(ob->type==OB_ARMATURE) {
-                               /* evaluate drivers */
-                               BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
-                               
+
+                       /* includes all keys and modifiers */
+                       switch(ob->type) {
+                       case OB_MESH:
+                               {
+#if 0                          // XXX, comment for 2.56a release, background wont set 'scene->customdata_mask'
+                                       BMEditMesh *em = (ob == scene->obedit)? ((Mesh*)ob->data)->edit_btmesh : NULL;
+                                       BLI_assert((scene->customdata_mask & CD_MASK_BAREMESH) == CD_MASK_BAREMESH);
+                                       if(em) {
+                                               makeDerivedMesh(scene, ob, em,  scene->customdata_mask, 0); /* was CD_MASK_BAREMESH */
+                                       } else
+                                               makeDerivedMesh(scene, ob, NULL, scene->customdata_mask, 0);
+
+#else                          /* ensure CD_MASK_BAREMESH for now */
+                                       BMEditMesh *em = (ob == scene->obedit)? ((Mesh*)ob->data)->edit_btmesh : NULL;
+                                       if(em) {
+                                               makeDerivedMesh(scene, ob, em,  scene->customdata_mask | CD_MASK_BAREMESH, 0); /* was CD_MASK_BAREMESH */
+                                       } else
+                                               makeDerivedMesh(scene, ob, NULL, scene->customdata_mask | CD_MASK_BAREMESH, 0);
+#endif
+
+                               }
+                               break;
+
+                       case OB_ARMATURE:
                                if(ob->id.lib && ob->proxy_from) {
                                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);
                                        // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
+                                       copy_pose_result(ob->pose, ob->proxy_from->pose);
                                }
                                else {
                                        where_is_pose(scene, ob);
                                }
                                }
                                else {
                                        where_is_pose(scene, ob);
                                }
+                               break;
+
+                       case OB_MBALL:
+                               makeDispListMBall(scene, ob);
+                               break;
+
+                       case OB_CURVE:
+                       case OB_SURF:
+                       case OB_FONT:
+                               makeDispListCurveTypes(scene, ob, 0);
+                               break;
+                               
+                       case OB_LATTICE:
+                               lattice_calc_modifiers(scene, ob);
+                               break;
                        }
 
                        }
 
+
                        if(ob->particlesystem.first) {
                                ParticleSystem *tpsys, *psys;
                                DerivedMesh *dm;
                        if(ob->particlesystem.first) {
                                ParticleSystem *tpsys, *psys;
                                DerivedMesh *dm;
@@ -2573,7 +2654,7 @@ void object_handle_update(Scene *scene, Object *ob)
                                while(psys) {
                                        if(psys_check_enabled(ob, psys)) {
                                                /* check use of dupli objects here */
                                while(psys) {
                                        if(psys_check_enabled(ob, psys)) {
                                                /* check use of dupli objects here */
-                                               if(psys->part && psys->part->draw_as == PART_DRAW_REND &&
+                                               if(psys->part && (psys->part->draw_as == PART_DRAW_REND || G.rendering) &&
                                                        ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob)
                                                        || (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
                                                        ob->transflag |= OB_DUPLIPARTS;
                                                        ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob)
                                                        || (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
                                                        ob->transflag |= OB_DUPLIPARTS;
@@ -2745,6 +2826,16 @@ int object_insert_ptcache(Object *ob)
        return i;
 }
 
        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,
 /* 'lens' may be set for envmap only */
 void object_camera_matrix(
                RenderData *rd, Object *camera, int winx, int winy, short field_second,
@@ -2754,8 +2845,7 @@ void object_camera_matrix(
        Camera *cam=NULL;
        float pixsize;
        float shiftx=0.0, shifty=0.0, winside, viewfac;
        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;
 
        /* question mark */
        (*ycor)= rd->yasp / rd->xasp;
@@ -2765,8 +2855,9 @@ void object_camera_matrix(
        if(camera->type==OB_CAMERA) {
                cam= camera->data;
 
        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
 
                /* solve this too... all time depending stuff is in convertblender.c?
                 * Need to update the camera early because it's used for projection matrices
@@ -2786,18 +2877,18 @@ void object_camera_matrix(
        }
        else if(camera->type==OB_LAMP) {
                Lamp *la= camera->data;
        }
        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);
 
                float phi= acos(fac);
 
-               (*lens)= 16.0*fac/sin(phi);
+               (*lens)= 16.0f*fac/sinf(phi);
                if((*lens)==0.0f)
                if((*lens)==0.0f)
-                       (*lens)= 35.0;
+                       (*lens)= 35.0f;
                (*clipsta)= la->clipsta;
                (*clipend)= la->clipend;
        }
        else {  /* envmap exception... */;
                if((*lens)==0.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;
 
                if((*clipsta)==0.0f || (*clipend)==0.0f) {
                        (*clipsta)= 0.1f;
@@ -2806,7 +2897,7 @@ void object_camera_matrix(
        }
 
        /* ortho only with camera available */
        }
 
        /* ortho only with camera available */
-       if(cam && rd->mode & R_ORTHO) {
+       if(cam && is_ortho) {
                if(rd->xasp*winx >= rd->yasp*winy) {
                        viewfac= winx;
                }
                if(rd->xasp*winx >= rd->yasp*winy) {
                        viewfac= winx;
                }
@@ -2817,8 +2908,8 @@ void object_camera_matrix(
                pixsize= cam->ortho_scale/viewfac;
        }
        else {
                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;
        }
 
                pixsize= (*clipsta) / viewfac;
        }
 
@@ -2831,12 +2922,12 @@ void object_camera_matrix(
 
        if(field_second) {
                if(rd->mode & R_ODDFIELD) {
 
        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 {
                }
                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 */
                }
        }
        /* the window matrix is used for clipping, and not changed during OSA steps */
@@ -2849,7 +2940,7 @@ void object_camera_matrix(
        (*viewdx)= pixsize;
        (*viewdy)= (*ycor) * pixsize;
 
        (*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);
                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);
@@ -2887,7 +2978,7 @@ void object_delete_ptcache(Object *ob, int index)
 /* shape key utility function */
 
 /************************* Mesh ************************/
 /* shape key utility function */
 
 /************************* Mesh ************************/
-static KeyBlock *insert_meshkey(Scene *scene, Object *ob, char *name, int from_mix)
+static KeyBlock *insert_meshkey(Scene *scene, Object *ob, const char *name, int from_mix)
 {
        Mesh *me= ob->data;
        Key *key= me->key;
 {
        Mesh *me= ob->data;
        Key *key= me->key;
@@ -2918,7 +3009,7 @@ static KeyBlock *insert_meshkey(Scene *scene, Object *ob, char *name, int from_m
        return kb;
 }
 /************************* Lattice ************************/
        return kb;
 }
 /************************* Lattice ************************/
-static KeyBlock *insert_lattkey(Scene *scene, Object *ob, char *name, int from_mix)
+static KeyBlock *insert_lattkey(Scene *scene, Object *ob, const char *name, int from_mix)
 {
        Lattice *lt= ob->data;
        Key *key= lt->key;
 {
        Lattice *lt= ob->data;
        Key *key= lt->key;
@@ -2950,12 +3041,12 @@ static KeyBlock *insert_lattkey(Scene *scene, Object *ob, char *name, int from_m
        return kb;
 }
 /************************* Curve ************************/
        return kb;
 }
 /************************* Curve ************************/
-static KeyBlock *insert_curvekey(Scene *scene, Object *ob, char *name, int from_mix)
+static KeyBlock *insert_curvekey(Scene *scene, Object *ob, const char *name, int from_mix)
 {
        Curve *cu= ob->data;
        Key *key= cu->key;
        KeyBlock *kb;
 {
        Curve *cu= ob->data;
        Key *key= cu->key;
        KeyBlock *kb;
-       ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
+       ListBase *lb= BKE_curve_nurbs(cu);
        int newkey= 0;
 
        if(key==NULL) {
        int newkey= 0;
 
        if(key==NULL) {
@@ -2967,7 +3058,11 @@ static KeyBlock *insert_curvekey(Scene *scene, Object *ob, char *name, int from_
        if(newkey || from_mix==FALSE) {
                /* create from curve */
                kb= add_keyblock(key, name);
        if(newkey || from_mix==FALSE) {
                /* create from curve */
                kb= add_keyblock(key, name);
-               curve_to_key(cu, kb, lb);
+               if (!newkey) {
+                       KeyBlock *basekb= (KeyBlock *)key->block.first;
+                       kb->data= MEM_dupallocN(basekb->data);
+                       kb->totelem= basekb->totelem;
+               } else curve_to_key(cu, kb, lb);
        }
        else {
                /* copy from current values */
        }
        else {
                /* copy from current values */
@@ -2982,7 +3077,7 @@ static KeyBlock *insert_curvekey(Scene *scene, Object *ob, char *name, int from_
        return kb;
 }
 
        return kb;
 }
 
-KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, char *name, int from_mix)
+KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, const 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);
 {
        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);
@@ -2990,3 +3085,53 @@ KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, char *name, int from
        else                                                                     return NULL;
 }
 
        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 | 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);
+}