merge with trunk at r31523
[blender.git] / source / blender / blenkernel / intern / object.c
index a6565ade0286a7000b112f04a0eff7d5e80a9567..2649d95dd8890300b8cb5692cb2107359a06cb6a 100644 (file)
@@ -284,7 +284,7 @@ void free_object(Object *ob)
        ob->path= 0;
        if(ob->adt) BKE_free_animdata((ID *)ob);
        if(ob->poselib) ob->poselib->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)
@@ -323,6 +323,7 @@ static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Objec
 
 void unlink_object(Scene *scene, Object *ob)
 {
+       Main *bmain= G.main;
        Object *obt;
        Material *mat;
        World *wrld;
@@ -344,7 +345,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)
-       obt= G.main->object.first;
+       obt= bmain->object.first;
        while(obt) {
                if(obt->proxy==ob)
                        obt->proxy= NULL;
@@ -518,7 +519,7 @@ void unlink_object(Scene *scene, Object *ob)
        }
        
        /* materials */
-       mat= G.main->mat.first;
+       mat= bmain->mat.first;
        while(mat) {
        
                for(a=0; a<MAX_MTEX; a++) {
@@ -532,7 +533,7 @@ void unlink_object(Scene *scene, Object *ob)
        }
        
        /* textures */
-       tex= G.main->tex.first;
+       tex= bmain->tex.first;
        while(tex) {
                if(tex->env) {
                        if(tex->env->object == ob) tex->env->object= NULL;
@@ -541,7 +542,7 @@ void unlink_object(Scene *scene, Object *ob)
        }
 
        /* worlds */
-       wrld= G.main->world.first;
+       wrld= bmain->world.first;
        while(wrld) {
                if(wrld->id.lib==NULL) {
                        for(a=0; a<MAX_MTEX; a++) {
@@ -554,7 +555,7 @@ void unlink_object(Scene *scene, Object *ob)
        }
                
        /* scenes */
-       sce= G.main->scene.first;
+       sce= bmain->scene.first;
        while(sce) {
                if(sce->id.lib==NULL) {
                        if(sce->camera==ob) sce->camera= NULL;
@@ -586,7 +587,7 @@ void unlink_object(Scene *scene, Object *ob)
        
 #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;
@@ -600,7 +601,7 @@ void unlink_object(Scene *scene, Object *ob)
 #endif // XXX old animation system
        
        /* screens */
-       sc= G.main->screen.first;
+       sc= bmain->screen.first;
        while(sc) {
                ScrArea *sa= sc->areabase.first;
                while(sa) {
@@ -665,14 +666,14 @@ void unlink_object(Scene *scene, Object *ob)
        }
 
        /* groups */
-       group= G.main->group.first;
+       group= bmain->group.first;
        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;
@@ -726,6 +727,7 @@ Camera *copy_camera(Camera *cam)
 
 void make_local_camera(Camera *cam)
 {
+       Main *bmain= G.main;
        Object *ob;
        Camera *camn;
        int local=0, lib=0;
@@ -743,7 +745,7 @@ void make_local_camera(Camera *cam)
                return;
        }
        
-       ob= G.main->object.first;
+       ob= bmain->object.first;
        while(ob) {
                if(ob->data==cam) {
                        if(ob->id.lib) lib= 1;
@@ -761,7 +763,7 @@ void make_local_camera(Camera *cam)
                camn= copy_camera(cam);
                camn->id.us= 0;
                
-               ob= G.main->object.first;
+               ob= bmain->object.first;
                while(ob) {
                        if(ob->data==cam) {
                                
@@ -874,6 +876,7 @@ Lamp *copy_lamp(Lamp *la)
 
 void make_local_lamp(Lamp *la)
 {
+       Main *bmain= G.main;
        Object *ob;
        Lamp *lan;
        int local=0, lib=0;
@@ -891,7 +894,7 @@ void make_local_lamp(Lamp *la)
                return;
        }
        
-       ob= G.main->object.first;
+       ob= bmain->object.first;
        while(ob) {
                if(ob->data==la) {
                        if(ob->id.lib) lib= 1;
@@ -909,7 +912,7 @@ void make_local_lamp(Lamp *la)
                lan= copy_lamp(la);
                lan->id.us= 0;
                
-               ob= G.main->object.first;
+               ob= bmain->object.first;
                while(ob) {
                        if(ob->data==la) {
                                
@@ -949,12 +952,6 @@ void free_lamp(Lamp *la)
        la->id.icon_id = 0;
 }
 
-void *add_wave()
-{
-       return 0;
-}
-
-
 /* *************************************************** */
 
 static void *add_obdata_from_type(int type)
@@ -968,7 +965,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_WAVE: return add_wave();
        case OB_ARMATURE: return add_armature("Armature");
        case OB_EMPTY: return NULL;
        default:
@@ -988,7 +984,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_WAVE: return "Wave";
        case OB_ARMATURE: return "Armature";
        case OB_EMPTY: return "Empty";
        default:
@@ -1375,6 +1370,7 @@ void expand_local_object(Object *ob)
 
 void make_local_object(Object *ob)
 {
+       Main *bmain= G.main;
        Object *obn;
        Scene *sce;
        Base *base;
@@ -1396,7 +1392,7 @@ void make_local_object(Object *ob)
 
        }
        else {
-               sce= G.main->scene.first;
+               sce= bmain->scene.first;
                while(sce) {
                        base= sce->base.first;
                        while(base) {
@@ -1419,7 +1415,7 @@ void make_local_object(Object *ob)
                        obn= copy_object(ob);
                        obn->id.us= 0;
                        
-                       sce= G.main->scene.first;
+                       sce= bmain->scene.first;
                        while(sce) {
                                if(sce->id.lib==0) {
                                        base= sce->base.first;
@@ -1636,10 +1632,7 @@ 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];
-       
-       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);
 }
 
@@ -1697,7 +1690,7 @@ void object_mat3_to_rot(Object *ob, float mat[][3], int use_compat)
 void object_apply_mat4(Object *ob, float mat[][4])
 {
        float mat3[3][3];
-       VECCOPY(ob->loc, mat[3]);
+       copy_v3_v3(ob->loc, mat[3]);
        mat4_to_size(ob->size, mat);
        copy_m3_m4(mat3, mat);
        object_mat3_to_rot(ob, mat3, 0);
@@ -1805,7 +1798,7 @@ static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
                        copy_m4_m4(mat, rmat);
                }
 
-               VECCOPY(mat[3], vec);
+               copy_v3_v3(mat[3], vec);
                
        }
 }
@@ -1832,7 +1825,7 @@ 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 */
-       VECCOPY(vec, mat[1]);
+       copy_v3_v3(vec, mat[1]);
        mul_v3_fl(vec, pchan->bone->length);
        add_v3_v3(mat[3], vec);
 }
@@ -1847,6 +1840,7 @@ static void give_parvert(Object *par, int nr, float *vec)
        if(par->type==OB_MESH) {
                Mesh *me= par->data;
                em = me->edit_btmesh;
+               DerivedMesh *dm;
 
                if(em) {
                        BMVert *eve;
@@ -1861,33 +1855,32 @@ static void give_parvert(Object *par, int nr, float *vec)
                                }
                        }
                }
-               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, 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 (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(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);
+                       }
                }
        }
        else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
@@ -1896,13 +1889,12 @@ static void give_parvert(Object *par, int nr, float *vec)
                BPoint *bp;
                BezTriple *bezt;
                int found= 0;
-               
+               ListBase *nurbs;
+
                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) {
@@ -1941,7 +1933,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;
                
-               if(latt->editlatt) latt= latt->editlatt;
+               if(latt->editlatt) latt= latt->editlatt->latt;
                
                a= latt->pntsu*latt->pntsv*latt->pntsw;
                count= 0;
@@ -2336,11 +2328,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) {
-                               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);
-                               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;
@@ -2527,7 +2517,7 @@ void object_handle_update(Scene *scene, Object *ob)
                        
                        /* includes all keys and modifiers */
                        if(ob->type==OB_MESH) {
-                               BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
+                               BMEditMesh *em = (ob == scene->obedit)? ((Mesh*)ob->data)->edit_btmesh : NULL;
                                
                                /* evaluate drivers */
                                // XXX: should we push this to derivedmesh instead?
@@ -2956,7 +2946,7 @@ static KeyBlock *insert_curvekey(Scene *scene, Object *ob, char *name, int from_
        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) {
@@ -2968,7 +2958,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);
-               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 */