Bugfix: drivers on constraint properties could get set to invalid when
[blender.git] / source / blender / blenkernel / intern / object.c
1 /* object.c
2  *
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <string.h>
33 #include <math.h>
34 #include <stdio.h>                      
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_anim_types.h"
43 #include "DNA_action_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_boid_types.h"
46 #include "DNA_camera_types.h"
47 #include "DNA_constraint_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_group_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_lattice_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meta_types.h"
56 #include "DNA_curve_types.h"
57 #include "DNA_meshdata_types.h"
58 #include "DNA_modifier_types.h"
59 #include "DNA_nla_types.h"
60 #include "DNA_object_types.h"
61 #include "DNA_object_force.h"
62 #include "DNA_object_fluidsim.h"
63 #include "DNA_outliner_types.h"
64 #include "DNA_particle_types.h"
65 #include "DNA_scene_types.h"
66 #include "DNA_screen_types.h"
67 #include "DNA_space_types.h"
68 #include "DNA_texture_types.h"
69 #include "DNA_userdef_types.h"
70 #include "DNA_view3d_types.h"
71 #include "DNA_world_types.h"
72
73 #include "BLI_blenlib.h"
74 #include "BLI_math.h"
75 #include "BLI_editVert.h"
76
77 #include "BKE_utildefines.h"
78
79 #include "BKE_main.h"
80 #include "BKE_global.h"
81
82 #include "BKE_armature.h"
83 #include "BKE_action.h"
84 #include "BKE_bullet.h"
85 #include "BKE_colortools.h"
86 #include "BKE_deform.h"
87 #include "BKE_DerivedMesh.h"
88 #include "BKE_nla.h"
89 #include "BKE_animsys.h"
90 #include "BKE_anim.h"
91 #include "BKE_blender.h"
92 #include "BKE_constraint.h"
93 #include "BKE_curve.h"
94 #include "BKE_displist.h"
95 #include "BKE_effect.h"
96 #include "BKE_fcurve.h"
97 #include "BKE_group.h"
98 #include "BKE_icons.h"
99 #include "BKE_key.h"
100 #include "BKE_lattice.h"
101 #include "BKE_library.h"
102 #include "BKE_mesh.h"
103 #include "BKE_mball.h"
104 #include "BKE_modifier.h"
105 #include "BKE_object.h"
106 #include "BKE_paint.h"
107 #include "BKE_particle.h"
108 #include "BKE_pointcache.h"
109 #include "BKE_property.h"
110 #include "BKE_sca.h"
111 #include "BKE_scene.h"
112 #include "BKE_screen.h"
113 #include "BKE_softbody.h"
114
115 #include "LBM_fluidsim.h"
116
117 #ifndef DISABLE_PYTHON
118 #include "BPY_extern.h"
119 #endif
120
121 #include "GPU_material.h"
122
123 /* Local function protos */
124 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
125
126 float originmat[3][3];  /* after where_is_object(), can be used in other functions (bad!) */
127
128 void clear_workob(Object *workob)
129 {
130         memset(workob, 0, sizeof(Object));
131         
132         workob->size[0]= workob->size[1]= workob->size[2]= 1.0f;
133         workob->rotmode= ROT_MODE_EUL;
134 }
135
136 void copy_baseflags(struct Scene *scene)
137 {
138         Base *base= scene->base.first;
139         
140         while(base) {
141                 base->object->flag= base->flag;
142                 base= base->next;
143         }
144 }
145
146 void copy_objectflags(struct Scene *scene)
147 {
148         Base *base= scene->base.first;
149         
150         while(base) {
151                 base->flag= base->object->flag;
152                 base= base->next;
153         }
154 }
155
156 void update_base_layer(struct Scene *scene, Object *ob)
157 {
158         Base *base= scene->base.first;
159
160         while (base) {
161                 if (base->object == ob) base->lay= ob->lay;
162                 base= base->next;
163         }
164 }
165
166 void object_free_particlesystems(Object *ob)
167 {
168         while(ob->particlesystem.first){
169                 ParticleSystem *psys = ob->particlesystem.first;
170                 
171                 BLI_remlink(&ob->particlesystem,psys);
172                 
173                 psys_free(ob,psys);
174         }
175 }
176
177 void object_free_softbody(Object *ob)
178 {
179         if(ob->soft) {
180                 sbFree(ob->soft);
181                 ob->soft= NULL;
182         }
183 }
184
185 void object_free_bulletsoftbody(Object *ob)
186 {
187         if(ob->bsoft) {
188                 bsbFree(ob->bsoft);
189                 ob->bsoft= NULL;
190         }
191 }
192
193 void object_free_modifiers(Object *ob)
194 {
195         while (ob->modifiers.first) {
196                 ModifierData *md = ob->modifiers.first;
197                 
198                 BLI_remlink(&ob->modifiers, md);
199                 
200                 modifier_free(md);
201         }
202
203         /* particle modifiers were freed, so free the particlesystems as well */
204         object_free_particlesystems(ob);
205
206         /* same for softbody */
207         object_free_softbody(ob);
208 }
209
210 void object_link_modifiers(struct Object *ob, struct Object *from)
211 {
212         ModifierData *md;
213         object_free_modifiers(ob);
214
215         for (md=from->modifiers.first; md; md=md->next) {
216                 ModifierData *nmd = NULL;
217
218                 if(ELEM4(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance, eModifierType_Collision)) continue;
219
220                 nmd = modifier_new(md->type);
221                 modifier_copyData(md, nmd);
222                 BLI_addtail(&ob->modifiers, nmd);
223         }
224
225         copy_object_particlesystems(from, ob);
226         copy_object_softbody(from, ob);
227
228         // TODO: smoke?, cloth?
229 }
230
231 /* here we will collect all local displist stuff */
232 /* also (ab)used in depsgraph */
233 void object_free_display(Object *ob)
234 {
235         if(ob->derivedDeform) {
236                 ob->derivedDeform->needsFree = 1;
237                 ob->derivedDeform->release(ob->derivedDeform);
238                 ob->derivedDeform= NULL;
239         }
240         if(ob->derivedFinal) {
241                 ob->derivedFinal->needsFree = 1;
242                 ob->derivedFinal->release(ob->derivedFinal);
243                 ob->derivedFinal= NULL;
244         }
245         
246         freedisplist(&ob->disp);
247 }
248
249 void free_sculptsession(SculptSession **ssp)
250 {
251         if(ssp && *ssp) {
252                 SculptSession *ss = *ssp;
253
254                 if(ss->texcache)
255                         MEM_freeN(ss->texcache);
256
257                 if(ss->layer_disps)
258                         MEM_freeN(ss->layer_disps);
259
260                 if(ss->layer_co)
261                         MEM_freeN(ss->layer_co);
262
263                 MEM_freeN(ss);
264
265                 *ssp = NULL;
266         }
267 }
268
269 /* do not free object itself */
270 void free_object(Object *ob)
271 {
272         int a;
273         
274         object_free_display(ob);
275         
276         /* disconnect specific data */
277         if(ob->data) {
278                 ID *id= ob->data;
279                 id->us--;
280                 if(id->us==0) {
281                         if(ob->type==OB_MESH) unlink_mesh(ob->data);
282                         else if(ob->type==OB_CURVE) unlink_curve(ob->data);
283                         else if(ob->type==OB_MBALL) unlink_mball(ob->data);
284                 }
285                 ob->data= 0;
286         }
287         
288         for(a=0; a<ob->totcol; a++) {
289                 if(ob->mat[a]) ob->mat[a]->id.us--;
290         }
291         if(ob->mat) MEM_freeN(ob->mat);
292         if(ob->matbits) MEM_freeN(ob->matbits);
293         ob->mat= 0;
294         ob->matbits= 0;
295         if(ob->bb) MEM_freeN(ob->bb); 
296         ob->bb= 0;
297         if(ob->path) free_path(ob->path); 
298         ob->path= 0;
299         if(ob->adt) BKE_free_animdata((ID *)ob);
300         if(ob->poselib) ob->poselib->id.us--;
301         if(ob->dup_group) ob->dup_group->id.us--;
302         if(ob->gpd) ob->gpd->id.us--;
303         if(ob->defbase.first)
304                 BLI_freelistN(&ob->defbase);
305         if(ob->pose)
306                 free_pose(ob->pose);
307         if(ob->mpath)
308                 animviz_free_motionpath(ob->mpath);
309         free_properties(&ob->prop);
310         object_free_modifiers(ob);
311         
312         free_sensors(&ob->sensors);
313         free_controllers(&ob->controllers);
314         free_actuators(&ob->actuators);
315         
316         free_constraints(&ob->constraints);
317         
318         free_partdeflect(ob->pd);
319
320         if(ob->soft) sbFree(ob->soft);
321         if(ob->bsoft) bsbFree(ob->bsoft);
322         if(ob->gpulamp.first) GPU_lamp_free(ob);
323
324         free_sculptsession(&ob->sculpt);
325
326         if(ob->pc_ids.first) BLI_freelistN(&ob->pc_ids);
327 }
328
329 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
330 {
331         Object *unlinkOb = userData;
332
333         if (*obpoin==unlinkOb) {
334                 *obpoin = NULL;
335                 ob->recalc |= OB_RECALC;
336         }
337 }
338 void unlink_object(Scene *scene, Object *ob)
339 {
340         Object *obt;
341         Material *mat;
342         World *wrld;
343         bScreen *sc;
344         Scene *sce;
345         Curve *cu;
346         Tex *tex;
347         Group *group;
348         Camera *camera;
349         bConstraint *con;
350         //bActionStrip *strip; // XXX animsys 
351         ModifierData *md;
352         int a;
353         
354         unlink_controllers(&ob->controllers);
355         unlink_actuators(&ob->actuators);
356         
357         /* check all objects: parents en bevels and fields, also from libraries */
358         // FIXME: need to check all animation blocks (drivers)
359         obt= G.main->object.first;
360         while(obt) {
361                 if(obt->proxy==ob)
362                         obt->proxy= NULL;
363                 if(obt->proxy_from==ob) {
364                         obt->proxy_from= NULL;
365                         obt->recalc |= OB_RECALC_OB;
366                 }
367                 if(obt->proxy_group==ob)
368                         obt->proxy_group= NULL;
369                 
370                 if(obt->parent==ob) {
371                         obt->parent= NULL;
372                         obt->recalc |= OB_RECALC;
373                 }
374                 
375                 if(obt->track==ob) {
376                         obt->track= NULL;
377                         obt->recalc |= OB_RECALC_OB;
378                 }
379                 
380                 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
381                 
382                 if ELEM(obt->type, OB_CURVE, OB_FONT) {
383                         cu= obt->data;
384
385                         if(cu->bevobj==ob) {
386                                 cu->bevobj= NULL;
387                                 obt->recalc |= OB_RECALC;
388                         }
389                         if(cu->taperobj==ob) {
390                                 cu->taperobj= NULL;
391                                 obt->recalc |= OB_RECALC;
392                         }
393                         if(cu->textoncurve==ob) {
394                                 cu->textoncurve= NULL;
395                                 obt->recalc |= OB_RECALC;
396                         }
397                 }
398                 else if(obt->type==OB_ARMATURE && obt->pose) {
399                         bPoseChannel *pchan;
400                         for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
401                                 for (con = pchan->constraints.first; con; con=con->next) {
402                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
403                                         ListBase targets = {NULL, NULL};
404                                         bConstraintTarget *ct;
405                                         
406                                         if (cti && cti->get_constraint_targets) {
407                                                 cti->get_constraint_targets(con, &targets);
408                                                 
409                                                 for (ct= targets.first; ct; ct= ct->next) {
410                                                         if (ct->tar == ob) {
411                                                                 ct->tar = NULL;
412                                                                 strcpy(ct->subtarget, "");
413                                                                 obt->recalc |= OB_RECALC_DATA;
414                                                         }
415                                                 }
416                                                 
417                                                 if (cti->flush_constraint_targets)
418                                                         cti->flush_constraint_targets(con, &targets, 0);
419                                         }
420                                 }
421                                 if(pchan->custom==ob)
422                                         pchan->custom= NULL;
423                         }
424                 }
425                 
426                 sca_remove_ob_poin(obt, ob);
427                 
428                 for (con = obt->constraints.first; con; con=con->next) {
429                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
430                         ListBase targets = {NULL, NULL};
431                         bConstraintTarget *ct;
432                         
433                         if (cti && cti->get_constraint_targets) {
434                                 cti->get_constraint_targets(con, &targets);
435                                 
436                                 for (ct= targets.first; ct; ct= ct->next) {
437                                         if (ct->tar == ob) {
438                                                 ct->tar = NULL;
439                                                 strcpy(ct->subtarget, "");
440                                                 obt->recalc |= OB_RECALC_DATA;
441                                         }
442                                 }
443                                 
444                                 if (cti->flush_constraint_targets)
445                                         cti->flush_constraint_targets(con, &targets, 0);
446                         }
447                 }
448                 
449                 /* object is deflector or field */
450                 if(ob->pd) {
451                         if(obt->soft)
452                                 obt->recalc |= OB_RECALC_DATA;
453
454                         /* cloth */
455                         for(md=obt->modifiers.first; md; md=md->next)
456                                 if(md->type == eModifierType_Cloth)
457                                         obt->recalc |= OB_RECALC_DATA;
458                 }
459                 
460                 /* strips */
461 #if 0 // XXX old animation system
462                 for(strip= obt->nlastrips.first; strip; strip= strip->next) {
463                         if(strip->object==ob)
464                                 strip->object= NULL;
465                         
466                         if(strip->modifiers.first) {
467                                 bActionModifier *amod;
468                                 for(amod= strip->modifiers.first; amod; amod= amod->next)
469                                         if(amod->ob==ob)
470                                                 amod->ob= NULL;
471                         }
472                 }
473 #endif // XXX old animation system
474
475                 /* particle systems */
476                 if(obt->particlesystem.first) {
477                         ParticleSystem *tpsys= obt->particlesystem.first;
478                         for(; tpsys; tpsys=tpsys->next) {
479                                 BoidState *state = NULL;
480                                 BoidRule *rule = NULL;
481
482                                 ParticleTarget *pt = tpsys->targets.first;
483                                 for(; pt; pt=pt->next) {
484                                         if(pt->ob==ob) {
485                                                 pt->ob = NULL;
486                                                 obt->recalc |= OB_RECALC_DATA;
487                                                 break;
488                                         }
489                                 }
490
491                                 if(tpsys->target_ob==ob) {
492                                         tpsys->target_ob= NULL;
493                                         obt->recalc |= OB_RECALC_DATA;
494                                 }
495
496                                 if(tpsys->part->dup_ob==ob)
497                                         tpsys->part->dup_ob= NULL;
498
499                                 if(tpsys->part->phystype==PART_PHYS_BOIDS) {
500                                         ParticleData *pa;
501                                         BoidParticle *bpa;
502                                         int p;
503
504                                         for(p=0,pa=tpsys->particles; p<tpsys->totpart; p++,pa++) {
505                                                 bpa = pa->boid;
506                                                 if(bpa->ground == ob)
507                                                         bpa->ground = NULL;
508                                         }
509                                 }
510                                 if(tpsys->part->boids) {
511                                         for(state = tpsys->part->boids->states.first; state; state=state->next) {
512                                                 for(rule = state->rules.first; rule; rule=rule->next) {
513                                                         if(rule->type==eBoidRuleType_Avoid) {
514                                                                 BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid*)rule;
515                                                                 if(gabr->ob==ob)
516                                                                         gabr->ob= NULL;
517                                                         }
518                                                         else if(rule->type==eBoidRuleType_FollowLeader) {
519                                                                 BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader*)rule;
520                                                                 if(flbr->ob==ob)
521                                                                         flbr->ob= NULL;
522                                                         }
523                                                 }
524                                         }
525                                 }
526                         }
527                         if(ob->pd)
528                                 obt->recalc |= OB_RECALC_DATA;
529                 }
530
531                 obt= obt->id.next;
532         }
533         
534         /* materials */
535         mat= G.main->mat.first;
536         while(mat) {
537         
538                 for(a=0; a<MAX_MTEX; a++) {
539                         if(mat->mtex[a] && ob==mat->mtex[a]->object) {
540                                 /* actually, test for lib here... to do */
541                                 mat->mtex[a]->object= NULL;
542                         }
543                 }
544
545                 mat= mat->id.next;
546         }
547         
548         /* textures */
549         tex= G.main->tex.first;
550         while(tex) {
551                 if(tex->env) {
552                         if(tex->env->object == ob) tex->env->object= NULL;
553                 }
554                 tex= tex->id.next;
555         }
556         
557         /* mballs (scene==NULL when called from library.c) */
558         if(scene && ob->type==OB_MBALL) {
559                 obt= find_basis_mball(scene, ob);
560                 if(obt) freedisplist(&obt->disp);
561         }
562         
563         /* worlds */
564         wrld= G.main->world.first;
565         while(wrld) {
566                 if(wrld->id.lib==NULL) {
567                         for(a=0; a<MAX_MTEX; a++) {
568                                 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
569                                         wrld->mtex[a]->object= NULL;
570                         }
571                 }
572                 
573                 wrld= wrld->id.next;
574         }
575                 
576         /* scenes */
577         sce= G.main->scene.first;
578         while(sce) {
579                 if(sce->id.lib==NULL) {
580                         if(sce->camera==ob) sce->camera= NULL;
581                         if(sce->toolsettings->skgen_template==ob) sce->toolsettings->skgen_template = NULL;
582
583 #ifdef DURIAN_CAMERA_SWITCH
584                         {
585                                 TimeMarker *m;
586
587                                 for (m= sce->markers.first; m; m= m->next) {
588                                         if(m->camera==ob)
589                                                 m->camera= NULL;
590                                 }
591                         }
592 #endif
593                 }
594                 sce= sce->id.next;
595         }
596         
597 #if 0 // XXX old animation system
598         /* ipos */
599         ipo= G.main->ipo.first;
600         while(ipo) {
601                 if(ipo->id.lib==NULL) {
602                         IpoCurve *icu;
603                         for(icu= ipo->curve.first; icu; icu= icu->next) {
604                                 if(icu->driver && icu->driver->ob==ob)
605                                         icu->driver->ob= NULL;
606                         }
607                 }
608                 ipo= ipo->id.next;
609         }
610 #endif // XXX old animation system
611         
612         /* screens */
613         sc= G.main->screen.first;
614         while(sc) {
615                 ScrArea *sa= sc->areabase.first;
616                 while(sa) {
617                         SpaceLink *sl;
618
619                         for (sl= sa->spacedata.first; sl; sl= sl->next) {
620                                 if(sl->spacetype==SPACE_VIEW3D) {
621                                         View3D *v3d= (View3D*) sl;
622
623                                         if(v3d->camera==ob) {
624                                                 v3d->camera= NULL;
625                                                 // XXX if(v3d->persp==V3D_CAMOB) v3d->persp= V3D_PERSP;
626                                         }
627                                         if(v3d->localvd && v3d->localvd->camera==ob ) {
628                                                 v3d->localvd->camera= NULL;
629                                                 // XXX if(v3d->localvd->persp==V3D_CAMOB) v3d->localvd->persp= V3D_PERSP;
630                                         }
631                                 }
632                                 else if(sl->spacetype==SPACE_OUTLINER) {
633                                         SpaceOops *so= (SpaceOops *)sl;
634
635                                         if(so->treestore) {
636                                                 TreeStoreElem *tselem= so->treestore->data;
637                                                 int a;
638                                                 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
639                                                         if(tselem->id==(ID *)ob) tselem->id= NULL;
640                                                 }
641                                         }
642                                 }
643                         }
644
645                         sa= sa->next;
646                 }
647                 sc= sc->id.next;
648         }
649
650         /* groups */
651         group= G.main->group.first;
652         while(group) {
653                 rem_from_group(group, ob, NULL, NULL);
654                 group= group->id.next;
655         }
656         
657         /* cameras */
658         camera= G.main->camera.first;
659         while(camera) {
660                 if (camera->dof_ob==ob) {
661                         camera->dof_ob = NULL;
662                 }
663                 camera= camera->id.next;
664         }
665 }
666
667 int exist_object(Object *obtest)
668 {
669         Object *ob;
670         
671         if(obtest==NULL) return 0;
672         
673         ob= G.main->object.first;
674         while(ob) {
675                 if(ob==obtest) return 1;
676                 ob= ob->id.next;
677         }
678         return 0;
679 }
680
681 void *add_camera(char *name)
682 {
683         Camera *cam;
684         
685         cam=  alloc_libblock(&G.main->camera, ID_CA, name);
686
687         cam->lens= 35.0f;
688         cam->angle= 49.14f;
689         cam->clipsta= 0.1f;
690         cam->clipend= 100.0f;
691         cam->drawsize= 0.5f;
692         cam->ortho_scale= 6.0;
693         cam->flag |= CAM_SHOWTITLESAFE;
694         cam->passepartalpha = 0.2f;
695         
696         return cam;
697 }
698
699 Camera *copy_camera(Camera *cam)
700 {
701         Camera *camn;
702         
703         camn= copy_libblock(cam);
704         camn->adt= BKE_copy_animdata(cam->adt);
705         
706         return camn;
707 }
708
709
710
711 void make_local_camera(Camera *cam)
712 {
713         Object *ob;
714         Camera *camn;
715         int local=0, lib=0;
716
717         /* - only lib users: do nothing
718             * - only local users: set flag
719             * - mixed: make copy
720             */
721         
722         if(cam->id.lib==0) return;
723         if(cam->id.us==1) {
724                 cam->id.lib= 0;
725                 cam->id.flag= LIB_LOCAL;
726                 new_id(0, (ID *)cam, 0);
727                 return;
728         }
729         
730         ob= G.main->object.first;
731         while(ob) {
732                 if(ob->data==cam) {
733                         if(ob->id.lib) lib= 1;
734                         else local= 1;
735                 }
736                 ob= ob->id.next;
737         }
738         
739         if(local && lib==0) {
740                 cam->id.lib= 0;
741                 cam->id.flag= LIB_LOCAL;
742                 new_id(0, (ID *)cam, 0);
743         }
744         else if(local && lib) {
745                 camn= copy_camera(cam);
746                 camn->id.us= 0;
747                 
748                 ob= G.main->object.first;
749                 while(ob) {
750                         if(ob->data==cam) {
751                                 
752                                 if(ob->id.lib==0) {
753                                         ob->data= camn;
754                                         camn->id.us++;
755                                         cam->id.us--;
756                                 }
757                         }
758                         ob= ob->id.next;
759                 }
760         }
761 }
762
763 /* get the camera's dof value, takes the dof object into account */
764 float dof_camera(Object *ob)
765 {
766         Camera *cam = (Camera *)ob->data; 
767         if (ob->type != OB_CAMERA)
768                 return 0.0f;
769         if (cam->dof_ob) {      
770                 /* too simple, better to return the distance on the view axis only
771                  * return len_v3v3(ob->obmat[3], cam->dof_ob->obmat[3]); */
772                 float mat[4][4], imat[4][4], obmat[4][4];
773                 
774                 copy_m4_m4(obmat, ob->obmat);
775                 normalize_m4(obmat);
776                 invert_m4_m4(imat, obmat);
777                 mul_m4_m4m4(mat, cam->dof_ob->obmat, imat);
778                 return (float)fabs(mat[3][2]);
779         }
780         return cam->YF_dofdist;
781 }
782
783 void *add_lamp(char *name)
784 {
785         Lamp *la;
786         
787         la=  alloc_libblock(&G.main->lamp, ID_LA, name);
788         
789         la->r= la->g= la->b= la->k= 1.0f;
790         la->haint= la->energy= 1.0f;
791         la->dist= 25.0f;
792         la->spotsize= 45.0f;
793         la->spotblend= 0.15f;
794         la->att2= 1.0f;
795         la->mode= LA_SHAD_BUF;
796         la->bufsize= 512;
797         la->clipsta= 0.5f;
798         la->clipend= 40.0f;
799         la->shadspotsize= 45.0f;
800         la->samp= 3;
801         la->bias= 1.0f;
802         la->soft= 3.0f;
803         la->compressthresh= 0.05f;
804         la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 
805         la->area_size=la->area_sizey=la->area_sizez= 1.0f; 
806         la->buffers= 1;
807         la->buftype= LA_SHADBUF_HALFWAY;
808         la->ray_samp_method = LA_SAMP_HALTON;
809         la->adapt_thresh = 0.001f;
810         la->preview=NULL;
811         la->falloff_type = LA_FALLOFF_INVSQUARE;
812         la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
813         la->sun_effect_type = 0;
814         la->horizon_brightness = 1.0;
815         la->spread = 1.0;
816         la->sun_brightness = 1.0;
817         la->sun_size = 1.0;
818         la->backscattered_light = 1.0f;
819         la->atm_turbidity = 2.0f;
820         la->atm_inscattering_factor = 1.0f;
821         la->atm_extinction_factor = 1.0f;
822         la->atm_distance_factor = 1.0f;
823         la->sun_intensity = 1.0f;
824         la->skyblendtype= MA_RAMP_ADD;
825         la->skyblendfac= 1.0f;
826         la->sky_colorspace= BLI_XYZ_CIE;
827         la->sky_exposure= 1.0f;
828         
829         curvemapping_initialize(la->curfalloff);
830         return la;
831 }
832
833 Lamp *copy_lamp(Lamp *la)
834 {
835         Lamp *lan;
836         int a;
837         
838         lan= copy_libblock(la);
839
840         for(a=0; a<MAX_MTEX; a++) {
841                 if(lan->mtex[a]) {
842                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
843                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
844                         id_us_plus((ID *)lan->mtex[a]->tex);
845                 }
846         }
847         
848         lan->curfalloff = curvemapping_copy(la->curfalloff);
849         
850 #if 0 // XXX old animation system
851         id_us_plus((ID *)lan->ipo);
852 #endif // XXX old animation system
853
854         if (la->preview) lan->preview = BKE_previewimg_copy(la->preview);
855         
856         return lan;
857 }
858
859 void make_local_lamp(Lamp *la)
860 {
861         Object *ob;
862         Lamp *lan;
863         int local=0, lib=0;
864
865         /* - only lib users: do nothing
866             * - only local users: set flag
867             * - mixed: make copy
868             */
869         
870         if(la->id.lib==0) return;
871         if(la->id.us==1) {
872                 la->id.lib= 0;
873                 la->id.flag= LIB_LOCAL;
874                 new_id(0, (ID *)la, 0);
875                 return;
876         }
877         
878         ob= G.main->object.first;
879         while(ob) {
880                 if(ob->data==la) {
881                         if(ob->id.lib) lib= 1;
882                         else local= 1;
883                 }
884                 ob= ob->id.next;
885         }
886         
887         if(local && lib==0) {
888                 la->id.lib= 0;
889                 la->id.flag= LIB_LOCAL;
890                 new_id(0, (ID *)la, 0);
891         }
892         else if(local && lib) {
893                 lan= copy_lamp(la);
894                 lan->id.us= 0;
895                 
896                 ob= G.main->object.first;
897                 while(ob) {
898                         if(ob->data==la) {
899                                 
900                                 if(ob->id.lib==0) {
901                                         ob->data= lan;
902                                         lan->id.us++;
903                                         la->id.us--;
904                                 }
905                         }
906                         ob= ob->id.next;
907                 }
908         }
909 }
910
911 void free_camera(Camera *ca)
912 {
913         BKE_free_animdata((ID *)ca);
914 }
915
916 void free_lamp(Lamp *la)
917 {
918         MTex *mtex;
919         int a;
920
921         for(a=0; a<MAX_MTEX; a++) {
922                 mtex= la->mtex[a];
923                 if(mtex && mtex->tex) mtex->tex->id.us--;
924                 if(mtex) MEM_freeN(mtex);
925         }
926         
927         BKE_free_animdata((ID *)la);
928
929         curvemapping_free(la->curfalloff);
930         
931         BKE_previewimg_free(&la->preview);
932         BKE_icon_delete(&la->id);
933         la->id.icon_id = 0;
934 }
935
936 void *add_wave()
937 {
938         return 0;
939 }
940
941
942 /* *************************************************** */
943
944 static void *add_obdata_from_type(int type)
945 {
946         switch (type) {
947         case OB_MESH: return add_mesh("Mesh");
948         case OB_CURVE: return add_curve("Curve", OB_CURVE);
949         case OB_SURF: return add_curve("Surf", OB_SURF);
950         case OB_FONT: return add_curve("Text", OB_FONT);
951         case OB_MBALL: return add_mball("Meta");
952         case OB_CAMERA: return add_camera("Camera");
953         case OB_LAMP: return add_lamp("Lamp");
954         case OB_LATTICE: return add_lattice("Lattice");
955         case OB_WAVE: return add_wave();
956         case OB_ARMATURE: return add_armature("Armature");
957         case OB_EMPTY: return NULL;
958         default:
959                 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
960                 return NULL;
961         }
962 }
963
964 static char *get_obdata_defname(int type)
965 {
966         switch (type) {
967         case OB_MESH: return "Mesh";
968         case OB_CURVE: return "Curve";
969         case OB_SURF: return "Surf";
970         case OB_FONT: return "Font";
971         case OB_MBALL: return "Mball";
972         case OB_CAMERA: return "Camera";
973         case OB_LAMP: return "Lamp";
974         case OB_LATTICE: return "Lattice";
975         case OB_WAVE: return "Wave";
976         case OB_ARMATURE: return "Armature";
977         case OB_EMPTY: return "Empty";
978         default:
979                 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
980                 return "Empty";
981         }
982 }
983
984 /* more general add: creates minimum required data, but without vertices etc. */
985 Object *add_only_object(int type, char *name)
986 {
987         Object *ob;
988
989         ob= alloc_libblock(&G.main->object, ID_OB, name);
990
991         /* default object vars */
992         ob->type= type;
993         
994         ob->col[0]= ob->col[1]= ob->col[2]= 1.0;
995         ob->col[3]= 1.0;
996         
997         ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
998         
999         /* objects should default to having Euler XYZ rotations, 
1000          * but rotations default to quaternions 
1001          */
1002         ob->rotmode= ROT_MODE_EUL;
1003         /* axis-angle must not have a 0,0,0 axis, so set y-axis as default... */
1004         ob->rotAxis[1]= ob->drotAxis[1]= 1.0f;
1005         /* quaternions should be 1,0,0,0 by default.... */
1006         ob->quat[0]= ob->dquat[0]= 1.0f;
1007         /* rotation locks should be 4D for 4 component rotations by default... */
1008         ob->protectflag = OB_LOCK_ROT4D;
1009         
1010         unit_m4(ob->constinv);
1011         unit_m4(ob->parentinv);
1012         unit_m4(ob->obmat);
1013         ob->dt= OB_TEXTURE;
1014         ob->empty_drawtype= OB_ARROWS;
1015         ob->empty_drawsize= 1.0;
1016
1017         if(type==OB_CAMERA || type==OB_LAMP) {
1018                 ob->trackflag= OB_NEGZ;
1019                 ob->upflag= OB_POSY;
1020         }
1021         else {
1022                 ob->trackflag= OB_POSY;
1023                 ob->upflag= OB_POSZ;
1024         }
1025         
1026         ob->dupon= 1; ob->dupoff= 0;
1027         ob->dupsta= 1; ob->dupend= 100;
1028         ob->dupfacesca = 1.0;
1029
1030         /* Game engine defaults*/
1031         ob->mass= ob->inertia= 1.0f;
1032         ob->formfactor= 0.4f;
1033         ob->damping= 0.04f;
1034         ob->rdamping= 0.1f;
1035         ob->anisotropicFriction[0] = 1.0f;
1036         ob->anisotropicFriction[1] = 1.0f;
1037         ob->anisotropicFriction[2] = 1.0f;
1038         ob->gameflag= OB_PROP|OB_COLLISION;
1039         ob->margin = 0.0;
1040         /* ob->pad3 == Contact Processing Threshold */
1041         ob->m_contactProcessingThreshold = 1.;
1042         
1043         /* NT fluid sim defaults */
1044         ob->fluidsimFlag = 0;
1045         ob->fluidsimSettings = NULL;
1046
1047         ob->pc_ids.first = ob->pc_ids.last = NULL;
1048         
1049         /* Animation Visualisation defaults */
1050         animviz_settings_init(&ob->avs);
1051
1052         return ob;
1053 }
1054
1055 /* general add: to scene, with layer from area and default name */
1056 /* creates minimum required data, but without vertices etc. */
1057 Object *add_object(struct Scene *scene, int type)
1058 {
1059         Object *ob;
1060         Base *base;
1061         char name[32];
1062
1063         strcpy(name, get_obdata_defname(type));
1064         ob = add_only_object(type, name);
1065
1066         ob->data= add_obdata_from_type(type);
1067
1068         ob->lay= scene->lay;
1069         
1070         base= scene_add_base(scene, ob);
1071         scene_select_base(scene, base);
1072         ob->recalc |= OB_RECALC;
1073
1074         return ob;
1075 }
1076
1077 SoftBody *copy_softbody(SoftBody *sb)
1078 {
1079         SoftBody *sbn;
1080         
1081         if (sb==NULL) return(NULL);
1082         
1083         sbn= MEM_dupallocN(sb);
1084         sbn->totspring= sbn->totpoint= 0;
1085         sbn->bpoint= NULL;
1086         sbn->bspring= NULL;
1087         
1088         sbn->keys= NULL;
1089         sbn->totkey= sbn->totpointkey= 0;
1090         
1091         sbn->scratch= NULL;
1092
1093         sbn->pointcache= BKE_ptcache_copy_list(&sbn->ptcaches, &sb->ptcaches);
1094
1095         if(sb->effector_weights)
1096                 sbn->effector_weights = MEM_dupallocN(sb->effector_weights);
1097
1098         return sbn;
1099 }
1100
1101 BulletSoftBody *copy_bulletsoftbody(BulletSoftBody *bsb)
1102 {
1103         BulletSoftBody *bsbn;
1104
1105         if (bsb == NULL)
1106                 return NULL;
1107         bsbn = MEM_dupallocN(bsb);
1108         /* no pointer in this structure yet */
1109         return bsbn;
1110 }
1111
1112 ParticleSystem *copy_particlesystem(ParticleSystem *psys)
1113 {
1114         ParticleSystem *psysn;
1115         ParticleData *pa;
1116         int p;
1117
1118         psysn= MEM_dupallocN(psys);
1119         psysn->particles= MEM_dupallocN(psys->particles);
1120         psysn->child= MEM_dupallocN(psys->child);
1121
1122         if(psys->part->type == PART_HAIR) {
1123                 for(p=0, pa=psysn->particles; p<psysn->totpart; p++, pa++)
1124                         pa->hair = MEM_dupallocN(pa->hair);
1125         }
1126
1127         if(psysn->particles && (psysn->particles->keys || psysn->particles->boid)) {
1128                 ParticleKey *key = psysn->particles->keys;
1129                 BoidParticle *boid = psysn->particles->boid;
1130
1131                 if(key)
1132                         key = MEM_dupallocN(key);
1133                 
1134                 if(boid)
1135                         boid = MEM_dupallocN(boid);
1136                 
1137                 for(p=0, pa=psysn->particles; p<psysn->totpart; p++, pa++) {
1138                         if(boid)
1139                                 pa->boid = boid++;
1140                         if(key) {
1141                                 pa->keys = key;
1142                                 key += pa->totkey;
1143                         }
1144                 }
1145         }
1146
1147         if(psys->clmd) {
1148                 ClothModifierData *nclmd = (ClothModifierData *)modifier_new(eModifierType_Cloth);
1149                 modifier_copyData((ModifierData*)psys->clmd, (ModifierData*)nclmd);
1150                 psys->hair_in_dm = psys->hair_out_dm = NULL;
1151         }
1152
1153         BLI_duplicatelist(&psysn->targets, &psys->targets);
1154         
1155         psysn->pathcache= NULL;
1156         psysn->childcache= NULL;
1157         psysn->edit= NULL;
1158         psysn->frand= NULL;
1159         psysn->pdd= NULL;
1160         
1161         psysn->pathcachebufs.first = psysn->pathcachebufs.last = NULL;
1162         psysn->childcachebufs.first = psysn->childcachebufs.last = NULL;
1163         psysn->renderdata = NULL;
1164         
1165         psysn->pointcache= BKE_ptcache_copy_list(&psysn->ptcaches, &psys->ptcaches);
1166
1167         id_us_plus((ID *)psysn->part);
1168
1169         return psysn;
1170 }
1171
1172 void copy_object_particlesystems(Object *obn, Object *ob)
1173 {
1174         ParticleSystemModifierData *psmd;
1175         ParticleSystem *psys, *npsys;
1176         ModifierData *md;
1177
1178         obn->particlesystem.first= obn->particlesystem.last= NULL;
1179         for(psys=ob->particlesystem.first; psys; psys=psys->next) {
1180                 npsys= copy_particlesystem(psys);
1181
1182                 BLI_addtail(&obn->particlesystem, npsys);
1183
1184                 /* need to update particle modifiers too */
1185                 for(md=obn->modifiers.first; md; md=md->next) {
1186                         if(md->type==eModifierType_ParticleSystem) {
1187                                 psmd= (ParticleSystemModifierData*)md;
1188                                 if(psmd->psys==psys)
1189                                         psmd->psys= npsys;
1190                         }
1191                 }
1192         }
1193 }
1194
1195 void copy_object_softbody(Object *obn, Object *ob)
1196 {
1197         if(ob->soft)
1198                 obn->soft= copy_softbody(ob->soft);
1199 }
1200
1201 static void copy_object_pose(Object *obn, Object *ob)
1202 {
1203         bPoseChannel *chan;
1204         
1205         /* note: need to clear obn->pose pointer first, so that copy_pose works (otherwise there's a crash) */
1206         obn->pose= NULL;
1207         copy_pose(&obn->pose, ob->pose, 1);     /* 1 = copy constraints */
1208
1209         for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
1210                 bConstraint *con;
1211                 
1212                 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
1213                 
1214                 for (con= chan->constraints.first; con; con= con->next) {
1215                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1216                         ListBase targets = {NULL, NULL};
1217                         bConstraintTarget *ct;
1218                         
1219 #if 0 // XXX old animation system
1220                         /* note that we can't change lib linked ipo blocks. for making
1221                          * proxies this still works correct however because the object
1222                          * is changed to object->proxy_from when evaluating the driver. */
1223                         if(con->ipo && !con->ipo->id.lib) {
1224                                 IpoCurve *icu;
1225                                 
1226                                 con->ipo= copy_ipo(con->ipo);
1227                                 
1228                                 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
1229                                         if(icu->driver && icu->driver->ob==ob)
1230                                                 icu->driver->ob= obn;
1231                                 }
1232                         }
1233 #endif // XXX old animation system
1234                         
1235                         if (cti && cti->get_constraint_targets) {
1236                                 cti->get_constraint_targets(con, &targets);
1237                                 
1238                                 for (ct= targets.first; ct; ct= ct->next) {
1239                                         if (ct->tar == ob)
1240                                                 ct->tar = obn;
1241                                 }
1242                                 
1243                                 if (cti->flush_constraint_targets)
1244                                         cti->flush_constraint_targets(con, &targets, 0);
1245                         }
1246                 }
1247         }
1248 }
1249
1250 Object *copy_object(Object *ob)
1251 {
1252         Object *obn;
1253         ModifierData *md;
1254         int a;
1255
1256         obn= copy_libblock(ob);
1257         
1258         if(ob->totcol) {
1259                 obn->mat= MEM_dupallocN(ob->mat);
1260                 obn->matbits= MEM_dupallocN(ob->matbits);
1261                 obn->totcol= ob->totcol;
1262         }
1263         
1264         if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
1265         obn->path= NULL;
1266         obn->flag &= ~OB_FROMGROUP;
1267         
1268         obn->modifiers.first = obn->modifiers.last= NULL;
1269         
1270         for (md=ob->modifiers.first; md; md=md->next) {
1271                 ModifierData *nmd = modifier_new(md->type);
1272                 modifier_copyData(md, nmd);
1273                 BLI_addtail(&obn->modifiers, nmd);
1274         }
1275
1276         obn->prop.first = obn->prop.last = NULL;
1277         copy_properties(&obn->prop, &ob->prop);
1278         
1279         copy_sensors(&obn->sensors, &ob->sensors);
1280         copy_controllers(&obn->controllers, &ob->controllers);
1281         copy_actuators(&obn->actuators, &ob->actuators);
1282         
1283         if(ob->pose) {
1284                 copy_object_pose(obn, ob);
1285                 /* backwards compat... non-armatures can get poses in older files? */
1286                 if(ob->type==OB_ARMATURE)
1287                         armature_rebuild_pose(obn, obn->data);
1288         }
1289         copy_defgroups(&obn->defbase, &ob->defbase);
1290         copy_constraints(&obn->constraints, &ob->constraints);
1291
1292         obn->mode = 0;
1293         obn->sculpt = NULL;
1294
1295         /* increase user numbers */
1296         id_us_plus((ID *)obn->data);
1297         id_us_plus((ID *)obn->dup_group);
1298         
1299
1300         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
1301         
1302         obn->disp.first= obn->disp.last= NULL;
1303         
1304         if(ob->pd){
1305                 obn->pd= MEM_dupallocN(ob->pd);
1306                 if(obn->pd->tex)
1307                         id_us_plus(&(obn->pd->tex->id));
1308                 if(obn->pd->rng)
1309                         obn->pd->rng = MEM_dupallocN(ob->pd->rng);
1310         }
1311         obn->soft= copy_softbody(ob->soft);
1312         obn->bsoft = copy_bulletsoftbody(ob->bsoft);
1313
1314         copy_object_particlesystems(obn, ob);
1315         
1316         obn->derivedDeform = NULL;
1317         obn->derivedFinal = NULL;
1318
1319         obn->gpulamp.first = obn->gpulamp.last = NULL;
1320         obn->pc_ids.first = obn->pc_ids.last = NULL;
1321         
1322         return obn;
1323 }
1324
1325 void expand_local_object(Object *ob)
1326 {
1327         //bActionStrip *strip;
1328         ParticleSystem *psys;
1329         int a;
1330
1331 #if 0 // XXX old animation system
1332         id_lib_extern((ID *)ob->action);
1333         id_lib_extern((ID *)ob->ipo);
1334 #endif // XXX old animation system
1335         id_lib_extern((ID *)ob->data);
1336         id_lib_extern((ID *)ob->dup_group);
1337         
1338         for(a=0; a<ob->totcol; a++) {
1339                 id_lib_extern((ID *)ob->mat[a]);
1340         }
1341 #if 0 // XXX old animation system
1342         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1343                 id_lib_extern((ID *)strip->act);
1344         }
1345 #endif // XXX old animation system
1346         for(psys=ob->particlesystem.first; psys; psys=psys->next)
1347                 id_lib_extern((ID *)psys->part);
1348 }
1349
1350 void make_local_object(Object *ob)
1351 {
1352         Object *obn;
1353         Scene *sce;
1354         Base *base;
1355         int local=0, lib=0;
1356
1357         /* - only lib users: do nothing
1358             * - only local users: set flag
1359             * - mixed: make copy
1360             */
1361         
1362         if(ob->id.lib==NULL) return;
1363         
1364         ob->proxy= ob->proxy_from= NULL;
1365         
1366         if(ob->id.us==1) {
1367                 ob->id.lib= NULL;
1368                 ob->id.flag= LIB_LOCAL;
1369                 new_id(0, (ID *)ob, 0);
1370
1371         }
1372         else {
1373                 sce= G.main->scene.first;
1374                 while(sce) {
1375                         base= sce->base.first;
1376                         while(base) {
1377                                 if(base->object==ob) {
1378                                         if(sce->id.lib) lib++;
1379                                         else local++;
1380                                         break;
1381                                 }
1382                                 base= base->next;
1383                         }
1384                         sce= sce->id.next;
1385                 }
1386                 
1387                 if(local && lib==0) {
1388                         ob->id.lib= 0;
1389                         ob->id.flag= LIB_LOCAL;
1390                         new_id(0, (ID *)ob, 0);
1391                 }
1392                 else if(local && lib) {
1393                         obn= copy_object(ob);
1394                         obn->id.us= 0;
1395                         
1396                         sce= G.main->scene.first;
1397                         while(sce) {
1398                                 if(sce->id.lib==0) {
1399                                         base= sce->base.first;
1400                                         while(base) {
1401                                                 if(base->object==ob) {
1402                                                         base->object= obn;
1403                                                         obn->id.us++;
1404                                                         ob->id.us--;
1405                                                 }
1406                                                 base= base->next;
1407                                         }
1408                                 }
1409                                 sce= sce->id.next;
1410                         }
1411                 }
1412         }
1413         
1414         expand_local_object(ob);
1415 }
1416
1417 /*
1418  * Returns true if the Object is a from an external blend file (libdata)
1419  */
1420 int object_is_libdata(Object *ob)
1421 {
1422         if (!ob) return 0;
1423         if (ob->proxy) return 0;
1424         if (ob->id.lib) return 1;
1425         return 0;
1426 }
1427
1428 /* Returns true if the Object data is a from an external blend file (libdata) */
1429 int object_data_is_libdata(Object *ob)
1430 {
1431         if(!ob) return 0;
1432         if(ob->proxy) return 0;
1433         if(ob->id.lib) return 1;
1434         if(!ob->data) return 0;
1435         if(((ID *)ob->data)->lib) return 1;
1436
1437         return 0;
1438 }
1439
1440 /* *************** PROXY **************** */
1441
1442 /* when you make proxy, ensure the exposed layers are extern */
1443 static void armature_set_id_extern(Object *ob)
1444 {
1445         bArmature *arm= ob->data;
1446         bPoseChannel *pchan;
1447         int lay= arm->layer_protected;
1448         
1449         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1450                 if(!(pchan->bone->layer & lay))
1451                         id_lib_extern((ID *)pchan->custom);
1452         }
1453                         
1454 }
1455
1456 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1457 /*             local_object->proxy == pointer to library object, saved in files and read */
1458 /*             local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1459
1460 void object_make_proxy(Object *ob, Object *target, Object *gob)
1461 {
1462         /* paranoia checks */
1463         if(ob->id.lib || target->id.lib==NULL) {
1464                 printf("cannot make proxy\n");
1465                 return;
1466         }
1467         
1468         ob->proxy= target;
1469         ob->proxy_group= gob;
1470         id_lib_extern(&target->id);
1471         
1472         ob->recalc= target->recalc= OB_RECALC;
1473         
1474         /* copy transform */
1475         if(gob) {
1476                 VECCOPY(ob->loc, gob->loc);
1477                 VECCOPY(ob->rot, gob->rot);
1478                 VECCOPY(ob->size, gob->size);
1479                 
1480                 group_tag_recalc(gob->dup_group);
1481         }
1482         else {
1483                 VECCOPY(ob->loc, target->loc);
1484                 VECCOPY(ob->rot, target->rot);
1485                 VECCOPY(ob->size, target->size);
1486         }
1487         
1488         ob->parent= target->parent;     /* libdata */
1489         copy_m4_m4(ob->parentinv, target->parentinv);
1490         
1491         /* copy animdata stuff - drivers only for now... */
1492         if ((target->adt) && (target->adt->drivers.first)) {
1493                 FCurve *fcu;
1494                 
1495                 /* add new animdata block */
1496                 ob->adt= BKE_id_add_animdata(&ob->id);
1497                 
1498                 /* make a copy of all the drivers (for now), then correct any links that need fixing */
1499                 copy_fcurves(&ob->adt->drivers, &target->adt->drivers);
1500                 
1501                 for (fcu= ob->adt->drivers.first; fcu; fcu= fcu->next) {
1502                         ChannelDriver *driver= fcu->driver;
1503                         DriverVar *dvar;
1504                         
1505                         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
1506                                 /* all drivers */
1507                                 DRIVER_TARGETS_LOOPER(dvar) 
1508                                 {
1509                                         if ((Object *)dtar->id == target)
1510                                                 dtar->id= (ID *)ob;
1511                                         else
1512                                                 id_lib_extern((ID *)dtar->id);
1513                                 }
1514                                 DRIVER_TARGETS_LOOPER_END
1515                         }
1516                 }
1517         }
1518         
1519         /* skip constraints? */
1520         // FIXME: this is considered by many as a bug
1521         
1522         /* set object type and link to data */
1523         ob->type= target->type;
1524         ob->data= target->data;
1525         id_us_plus((ID *)ob->data);             /* ensures lib data becomes LIB_EXTERN */
1526         
1527         /* copy material and index information */
1528         ob->actcol= ob->totcol= 0;
1529         if(ob->mat) MEM_freeN(ob->mat);
1530         if(ob->matbits) MEM_freeN(ob->matbits);
1531         ob->mat = NULL;
1532         ob->matbits= NULL;
1533         if ((target->totcol) && (target->mat) && ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) { //XXX OB_SUPPORT_MATERIAL
1534                 int i;
1535                 ob->colbits = target->colbits;
1536                 
1537                 ob->actcol= target->actcol;
1538                 ob->totcol= target->totcol;
1539                 
1540                 ob->mat = MEM_dupallocN(target->mat);
1541                 ob->matbits = MEM_dupallocN(target->matbits);
1542                 for(i=0; i<target->totcol; i++) {
1543                         /* dont need to run test_object_materials since we know this object is new and not used elsewhere */
1544                         id_us_plus((ID *)ob->mat[i]); 
1545                 }
1546         }
1547         
1548         /* type conversions */
1549         if(target->type == OB_ARMATURE) {
1550                 copy_object_pose(ob, target);   /* data copy, object pointers in constraints */
1551                 rest_pose(ob->pose);                    /* clear all transforms in channels */
1552                 armature_rebuild_pose(ob, ob->data);    /* set all internal links */
1553                 
1554                 armature_set_id_extern(ob);
1555         }
1556         
1557         /* copy drawtype info */
1558         ob->dt= target->dt;
1559 }
1560
1561
1562 /* *************** CALC ****************** */
1563
1564 /* there is also a timing calculation in drawobject() */
1565
1566 float bluroffs= 0.0f, fieldoffs= 0.0f;
1567 int no_speed_curve= 0;
1568
1569 /* ugly calls from render */
1570 void set_mblur_offs(float blur)
1571 {
1572         bluroffs= blur;
1573 }
1574
1575 void set_field_offs(float field)
1576 {
1577         fieldoffs= field;
1578 }
1579
1580 void disable_speed_curve(int val)
1581 {
1582         no_speed_curve= val;
1583 }
1584
1585 // XXX THIS CRUFT NEEDS SERIOUS RECODING ASAP!
1586 /* ob can be NULL */
1587 float bsystem_time(struct Scene *scene, Object *ob, float cfra, float ofs)
1588 {
1589         /* returns float ( see frame_to_float in ipo.c) */
1590         
1591         /* bluroffs and fieldoffs are ugly globals that are set by render */
1592         cfra+= bluroffs+fieldoffs;
1593
1594         /* global time */
1595         if (scene)
1596                 cfra*= scene->r.framelen;       
1597         
1598 #if 0 // XXX old animation system
1599         if (ob) {
1600                 /* ofset frames */
1601                 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0) 
1602                         cfra-= give_timeoffset(ob);
1603         }
1604 #endif // XXX old animation system
1605         
1606         cfra-= ofs;
1607
1608         return cfra;
1609 }
1610
1611 void object_scale_to_mat3(Object *ob, float mat[][3])
1612 {
1613         float vec[3];
1614         
1615         vec[0]= ob->size[0]+ob->dsize[0];
1616         vec[1]= ob->size[1]+ob->dsize[1];
1617         vec[2]= ob->size[2]+ob->dsize[2];
1618         size_to_mat3( mat,vec);
1619 }
1620
1621 // TODO: this should take rotation orders into account later...
1622 void object_rot_to_mat3(Object *ob, float mat[][3])
1623 {
1624         float rmat[3][3], dmat[3][3];
1625         
1626         /* initialise the delta-rotation matrix, which will get (pre)multiplied 
1627          * with the rotation matrix to yield the appropriate rotation
1628          */
1629         unit_m3(dmat);
1630         
1631         /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1632         if (ob->rotmode > 0) {
1633                 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1634                 eulO_to_mat3( rmat,ob->rot, ob->rotmode);
1635                 eulO_to_mat3( dmat,ob->drot, ob->rotmode);
1636         }
1637         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1638                 /* axis-angle -  not really that great for 3D-changing orientations */
1639                 axis_angle_to_mat3( rmat,ob->rotAxis, ob->rotAngle);
1640                 axis_angle_to_mat3( dmat,ob->drotAxis, ob->drotAngle);
1641         }
1642         else {
1643                 /* quats are normalised before use to eliminate scaling issues */
1644                 normalize_qt(ob->quat);
1645                 quat_to_mat3( rmat,ob->quat);
1646                 quat_to_mat3( dmat,ob->dquat);
1647         }
1648         
1649         /* combine these rotations */
1650         // XXX is this correct? if errors, change the order of multiplication...
1651         mul_m3_m3m3(mat, dmat, rmat);
1652 }
1653
1654 void object_mat3_to_rot(Object *ob, float mat[][3], int use_compat)
1655 {
1656         if (ob->rotmode == ROT_MODE_QUAT)
1657                 mat3_to_quat(ob->quat, mat);
1658         else if (ob->rotmode == ROT_MODE_AXISANGLE)
1659                 mat3_to_axis_angle(ob->rotAxis, &ob->rotAngle, mat);
1660         else {
1661                 if(use_compat) {
1662                         float eul[3];
1663                         VECCOPY(eul, ob->rot);
1664                         mat3_to_compatible_eulO(ob->rot, eul, ob->rotmode, mat);
1665                 }
1666                 else
1667                         mat3_to_eulO(ob->rot, ob->rotmode, mat);
1668         }
1669 }
1670
1671 void object_apply_mat4(Object *ob, float mat[][4])
1672 {
1673         float mat3[3][3];
1674         VECCOPY(ob->loc, mat[3]);
1675         mat4_to_size(ob->size, mat);
1676         copy_m3_m4(mat3, mat);
1677         object_mat3_to_rot(ob, mat3, 0);
1678 }
1679
1680 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1681 {
1682         float smat[3][3];
1683         float rmat[3][3];
1684         /*float q1[4];*/
1685         
1686         /* size */
1687         object_scale_to_mat3(ob, smat);
1688
1689         /* rot */
1690         object_rot_to_mat3(ob, rmat);
1691         mul_m3_m3m3(mat, rmat, smat);
1692 }
1693
1694 void object_to_mat4(Object *ob, float mat[][4])
1695 {
1696         float tmat[3][3];
1697         
1698         object_to_mat3(ob, tmat);
1699         
1700         copy_m4_m3(mat, tmat);
1701         
1702         mat[3][0]= ob->loc[0] + ob->dloc[0];
1703         mat[3][1]= ob->loc[1] + ob->dloc[1];
1704         mat[3][2]= ob->loc[2] + ob->dloc[2];
1705 }
1706
1707 int enable_cu_speed= 1;
1708
1709 static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
1710 {
1711         Curve *cu;
1712         float q[4], vec[4], dir[3], quat[4], radius, x1, ctime;
1713         float timeoffs = 0.0, sf_orig = 0.0;
1714         
1715         unit_m4(mat);
1716         
1717         cu= par->data;
1718         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1719                 makeDispListCurveTypes(scene, par, 0);
1720         if(cu->path==NULL) return;
1721         
1722         /* exception, timeoffset is regarded as distance offset */
1723         if(cu->flag & CU_OFFS_PATHDIST) {
1724                 timeoffs = give_timeoffset(ob);
1725                 SWAP(float, sf_orig, ob->sf);
1726         }
1727         
1728         /* catch exceptions: feature for nla stride editing */
1729         if(ob->ipoflag & OB_DISABLE_PATH) {
1730                 ctime= 0.0f;
1731         }
1732         /* catch exceptions: curve paths used as a duplicator */
1733         else if(enable_cu_speed) {
1734                 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1735                  * but this will only work if it actually is animated... 
1736                  *
1737                  * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1738                  * factor, which then gets clamped to lie within 0.0 - 1.0 range
1739                  */
1740                 ctime= cu->ctime / cu->pathlen;
1741                 CLAMP(ctime, 0.0, 1.0);
1742         }
1743         else {
1744                 ctime= scene->r.cfra - give_timeoffset(ob);
1745                 ctime /= cu->pathlen;
1746                 
1747                 CLAMP(ctime, 0.0, 1.0);
1748         }
1749         
1750         /* time calculus is correct, now apply distance offset */
1751         if(cu->flag & CU_OFFS_PATHDIST) {
1752                 ctime += timeoffs/cu->path->totdist;
1753
1754                 /* restore */
1755                 SWAP(float, sf_orig, ob->sf);
1756         }
1757         
1758         
1759         /* vec: 4 items! */
1760         if( where_on_path(par, ctime, vec, dir, NULL, &radius) ) {
1761
1762                 if(cu->flag & CU_FOLLOW) {
1763                         vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
1764                         
1765                         /* the tilt */
1766                         normalize_v3(dir);
1767                         q[0]= (float)cos(0.5*vec[3]);
1768                         x1= (float)sin(0.5*vec[3]);
1769                         q[1]= -x1*dir[0];
1770                         q[2]= -x1*dir[1];
1771                         q[3]= -x1*dir[2];
1772                         mul_qt_qtqt(quat, q, quat);
1773                         
1774                         quat_to_mat4( mat,quat);
1775                 }
1776                 
1777                 if(cu->flag & CU_PATH_RADIUS) {
1778                         float tmat[4][4], rmat[4][4];
1779                         scale_m4_fl(tmat, radius);
1780                         mul_m4_m4m4(rmat, mat, tmat);
1781                         copy_m4_m4(mat, rmat);
1782                 }
1783
1784                 VECCOPY(mat[3], vec);
1785                 
1786         }
1787 }
1788
1789 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1790 {       
1791         bPoseChannel *pchan;
1792         float vec[3];
1793         
1794         if (par->type!=OB_ARMATURE) {
1795                 unit_m4(mat);
1796                 return;
1797         }
1798         
1799         /* Make sure the bone is still valid */
1800         pchan= get_pose_channel(par->pose, ob->parsubstr);
1801         if (!pchan){
1802                 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1803                 unit_m4(mat);
1804                 return;
1805         }
1806
1807         /* get bone transform */
1808         copy_m4_m4(mat, pchan->pose_mat);
1809
1810         /* but for backwards compatibility, the child has to move to the tail */
1811         VECCOPY(vec, mat[1]);
1812         mul_v3_fl(vec, pchan->bone->length);
1813         add_v3_v3v3(mat[3], mat[3], vec);
1814 }
1815
1816 static void give_parvert(Object *par, int nr, float *vec)
1817 {
1818         EditMesh *em;
1819         int a, count;
1820         
1821         vec[0]=vec[1]=vec[2]= 0.0f;
1822         
1823         if(par->type==OB_MESH) {
1824                 Mesh *me= par->data;
1825                 em = BKE_mesh_get_editmesh(me);
1826
1827                 if(em) {
1828                         EditVert *eve;
1829                         
1830                         for(eve= em->verts.first; eve; eve= eve->next) {
1831                                 if(eve->keyindex==nr) {
1832                                         memcpy(vec, eve->co, sizeof(float)*3);
1833                                         break;
1834                                 }
1835                         }
1836                         BKE_mesh_end_editmesh(me, em);
1837                 }
1838                 else {
1839                         DerivedMesh *dm = par->derivedFinal;
1840                         
1841                         if(dm) {
1842                                 int i, count = 0, vindex, numVerts = dm->getNumVerts(dm);
1843                                 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1844                                 float co[3];
1845
1846                                 /* get the average of all verts with (original index == nr) */
1847                                 for(i = 0; i < numVerts; ++i) {
1848                                         vindex= (index)? *index: i;
1849
1850                                         if(vindex == nr) {
1851                                                 dm->getVertCo(dm, i, co);
1852                                                 add_v3_v3v3(vec, vec, co);
1853                                                 count++;
1854                                         }
1855                                 }
1856
1857                                 if (count==0) {
1858                                         /* keep as 0,0,0 */
1859                                 } else if(count > 0) {
1860                                         mul_v3_fl(vec, 1.0f / count);
1861                                 } else {
1862                                         /* use first index if its out of range */
1863                                         dm->getVertCo(dm, 0, vec);
1864                                 }
1865                         }
1866                 }
1867         }
1868         else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
1869                 Nurb *nu;
1870                 Curve *cu;
1871                 BPoint *bp;
1872                 BezTriple *bezt;
1873                 int found= 0;
1874                 
1875                 cu= par->data;
1876                 if(cu->editnurb)
1877                         nu= cu->editnurb->first;
1878                 else
1879                         nu= cu->nurb.first;
1880                 
1881                 count= 0;
1882                 while(nu && !found) {
1883                         if(nu->type == CU_BEZIER) {
1884                                 bezt= nu->bezt;
1885                                 a= nu->pntsu;
1886                                 while(a--) {
1887                                         if(count==nr) {
1888                                                 found= 1;
1889                                                 VECCOPY(vec, bezt->vec[1]);
1890                                                 break;
1891                                         }
1892                                         count++;
1893                                         bezt++;
1894                                 }
1895                         }
1896                         else {
1897                                 bp= nu->bp;
1898                                 a= nu->pntsu*nu->pntsv;
1899                                 while(a--) {
1900                                         if(count==nr) {
1901                                                 found= 1;
1902                                                 memcpy(vec, bp->vec, sizeof(float)*3);
1903                                                 break;
1904                                         }
1905                                         count++;
1906                                         bp++;
1907                                 }
1908                         }
1909                         nu= nu->next;
1910                 }
1911
1912         }
1913         else if(par->type==OB_LATTICE) {
1914                 Lattice *latt= par->data;
1915                 BPoint *bp;
1916                 DispList *dl = find_displist(&par->disp, DL_VERTS);
1917                 float *co = dl?dl->verts:NULL;
1918                 
1919                 if(latt->editlatt) latt= latt->editlatt;
1920                 
1921                 a= latt->pntsu*latt->pntsv*latt->pntsw;
1922                 count= 0;
1923                 bp= latt->def;
1924                 while(a--) {
1925                         if(count==nr) {
1926                                 if(co)
1927                                         memcpy(vec, co, 3*sizeof(float));
1928                                 else
1929                                         memcpy(vec, bp->vec, 3*sizeof(float));
1930                                 break;
1931                         }
1932                         count++;
1933                         if(co) co+= 3;
1934                         else bp++;
1935                 }
1936         }
1937 }
1938
1939 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1940 {
1941         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1942
1943         /* in local ob space */
1944         unit_m4(mat);
1945         
1946         if (ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE)) {
1947                 
1948                 give_parvert(par, ob->par1, v1);
1949                 give_parvert(par, ob->par2, v2);
1950                 give_parvert(par, ob->par3, v3);
1951                                 
1952                 tri_to_quat( q,v1, v2, v3);
1953                 quat_to_mat3( cmat,q);
1954                 copy_m4_m3(mat, cmat);
1955                 
1956                 if(ob->type==OB_CURVE) {
1957                         VECCOPY(mat[3], v1);
1958                 }
1959                 else {
1960                         add_v3_v3v3(mat[3], v1, v2);
1961                         add_v3_v3v3(mat[3], mat[3], v3);
1962                         mul_v3_fl(mat[3], 0.3333333f);
1963                 }
1964         }
1965 }
1966
1967 // XXX what the hell is this?
1968 static int no_parent_ipo=0;
1969 void set_no_parent_ipo(int val)
1970 {
1971         no_parent_ipo= val;
1972 }
1973
1974 void where_is_object_time(Scene *scene, Object *ob, float ctime)
1975 {
1976         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1977         float stime=ctime, fac1, fac2, vec[3];
1978         int a;
1979         int pop; 
1980         
1981         /* new version: correct parent+vertexparent and track+parent */
1982         /* this one only calculates direct attached parent and track */
1983         /* is faster, but should keep track of timeoffs */
1984         
1985         if(ob==NULL) return;
1986         
1987         /* execute drivers only, as animation has already been done */
1988         BKE_animsys_evaluate_animdata(&ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
1989         
1990         if(ob->parent) {
1991                 Object *par= ob->parent;
1992                 
1993                 // XXX depreceated - animsys
1994                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= give_timeoffset(ob);
1995                 
1996                 /* hurms, code below conflicts with depgraph... (ton) */
1997                 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1998                 pop= 0;
1999                 if(no_parent_ipo==0 && stime != par->ctime) {
2000                         // only for ipo systems? 
2001                         pushdata(par, sizeof(Object));
2002                         pop= 1;
2003                         
2004                         if(par->proxy_from);    // was a copied matrix, no where_is! bad...
2005                         else where_is_object_time(scene, par, ctime);
2006                 }
2007                 
2008                 solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
2009                 
2010                 if(pop) {
2011                         poplast(par);
2012                 }
2013                 
2014                 if(ob->partype & PARSLOW) {
2015                         // include framerate
2016                         fac1= ( 1.0f / (1.0f + (float)fabs(give_timeoffset(ob))) );
2017                         if(fac1 >= 1.0f) return;
2018                         fac2= 1.0f-fac1;
2019                         
2020                         fp1= ob->obmat[0];
2021                         fp2= slowmat[0];
2022                         for(a=0; a<16; a++, fp1++, fp2++) {
2023                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2024                         }
2025                 }
2026         }
2027         else {
2028                 object_to_mat4(ob, ob->obmat);
2029         }
2030
2031         /* Handle tracking */
2032         if(ob->track) {
2033                 if( ctime != ob->track->ctime) where_is_object_time(scene, ob->track, ctime);
2034                 solve_tracking (ob, ob->track->obmat);
2035         }
2036
2037         /* solve constraints */
2038         if (ob->constraints.first && !(ob->flag & OB_NO_CONSTRAINTS)) {
2039                 bConstraintOb *cob;
2040                 
2041                 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2042                 
2043                 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
2044                 solve_constraints (&ob->constraints, cob, ctime);
2045                 
2046                 constraints_clear_evalob(cob);
2047         }
2048         
2049         /* set negative scale flag in object */
2050         cross_v3_v3v3(vec, ob->obmat[0], ob->obmat[1]);
2051         if( dot_v3v3(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
2052         else ob->transflag &= ~OB_NEG_SCALE;
2053 }
2054
2055 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
2056 {
2057         float totmat[4][4];
2058         float tmat[4][4];
2059         float locmat[4][4];
2060         float vec[3];
2061         int ok;
2062         
2063         object_to_mat4(ob, locmat);
2064         
2065         if(ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
2066
2067         switch(ob->partype & PARTYPE) {
2068         case PAROBJECT:
2069                 ok= 0;
2070                 if(par->type==OB_CURVE) {
2071                         if( ((Curve *)par->data)->flag & CU_PATH ) {
2072                                 ob_parcurve(scene, ob, par, tmat);
2073                                 ok= 1;
2074                         }
2075                 }
2076                 
2077                 if(ok) mul_serie_m4(totmat, par->obmat, tmat, 
2078                         NULL, NULL, NULL, NULL, NULL, NULL);
2079                 else copy_m4_m4(totmat, par->obmat);
2080                 
2081                 break;
2082         case PARBONE:
2083                 ob_parbone(ob, par, tmat);
2084                 mul_serie_m4(totmat, par->obmat, tmat,         
2085                         NULL, NULL, NULL, NULL, NULL, NULL);
2086                 break;
2087                 
2088         case PARVERT1:
2089                 unit_m4(totmat);
2090                 if (simul){
2091                         VECCOPY(totmat[3], par->obmat[3]);
2092                 }
2093                 else{
2094                         give_parvert(par, ob->par1, vec);
2095                         mul_v3_m4v3(totmat[3], par->obmat, vec);
2096                 }
2097                 break;
2098         case PARVERT3:
2099                 ob_parvert3(ob, par, tmat);
2100                 
2101                 mul_serie_m4(totmat, par->obmat, tmat,         
2102                         NULL, NULL, NULL, NULL, NULL, NULL);
2103                 break;
2104                 
2105         case PARSKEL:
2106                 copy_m4_m4(totmat, par->obmat);
2107                 break;
2108         }
2109         
2110         // total 
2111         mul_serie_m4(tmat, totmat, ob->parentinv,         
2112                 NULL, NULL, NULL, NULL, NULL, NULL);
2113         mul_serie_m4(obmat, tmat, locmat,         
2114                 NULL, NULL, NULL, NULL, NULL, NULL);
2115         
2116         if (simul) {
2117
2118         }
2119         else{
2120                 // external usable originmat 
2121                 copy_m3_m4(originmat, tmat);
2122                 
2123                 // origin, voor help line
2124                 if( (ob->partype & 15)==PARSKEL ) {
2125                         VECCOPY(ob->orig, par->obmat[3]);
2126                 }
2127                 else {
2128                         VECCOPY(ob->orig, totmat[3]);
2129                 }
2130         }
2131
2132 }
2133 void solve_tracking (Object *ob, float targetmat[][4])
2134 {
2135         float quat[4];
2136         float vec[3];
2137         float totmat[3][3];
2138         float tmat[4][4];
2139         
2140         sub_v3_v3v3(vec, ob->obmat[3], targetmat[3]);
2141         vec_to_quat( quat,vec, ob->trackflag, ob->upflag);
2142         quat_to_mat3( totmat,quat);
2143         
2144         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
2145                 /* 'temporal' : clear parent info */
2146                 object_to_mat4(ob, tmat);
2147                 tmat[0][3]= ob->obmat[0][3];
2148                 tmat[1][3]= ob->obmat[1][3];
2149                 tmat[2][3]= ob->obmat[2][3];
2150                 tmat[3][0]= ob->obmat[3][0];
2151                 tmat[3][1]= ob->obmat[3][1];
2152                 tmat[3][2]= ob->obmat[3][2];
2153                 tmat[3][3]= ob->obmat[3][3];
2154         }
2155         else copy_m4_m4(tmat, ob->obmat);
2156         
2157         mul_m4_m3m4(ob->obmat, totmat, tmat);
2158
2159 }
2160
2161 void where_is_object(struct Scene *scene, Object *ob)
2162 {
2163         where_is_object_time(scene, ob, (float)scene->r.cfra);
2164 }
2165
2166
2167 void where_is_object_simul(Scene *scene, Object *ob)
2168 /* was written for the old game engine (until 2.04) */
2169 /* It seems that this function is only called
2170 for a lamp that is the child of another object */
2171 {
2172         Object *par;
2173         //Ipo *ipo;
2174         float *fp1, *fp2;
2175         float slowmat[4][4];
2176         float fac1, fac2;
2177         int a;
2178         
2179         /* NO TIMEOFFS */
2180         
2181         /* no ipo! (because of dloc and realtime-ipos) */
2182                 // XXX old animation system
2183         //ipo= ob->ipo;
2184         //ob->ipo= NULL;
2185
2186         if(ob->parent) {
2187                 par= ob->parent;
2188                 
2189                 solve_parenting(scene, ob, par, ob->obmat, slowmat, 1);
2190
2191                 if(ob->partype & PARSLOW) {
2192
2193                         fac1= (float)(1.0/(1.0+ fabs(give_timeoffset(ob))));
2194                         fac2= 1.0f-fac1;
2195                         fp1= ob->obmat[0];
2196                         fp2= slowmat[0];
2197                         for(a=0; a<16; a++, fp1++, fp2++) {
2198                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2199                         }
2200                 }
2201                 
2202         }
2203         else {
2204                 object_to_mat4(ob, ob->obmat);
2205         }
2206         
2207         if(ob->track) 
2208                 solve_tracking(ob, ob->track->obmat);
2209
2210         /* solve constraints */
2211         if (ob->constraints.first) {
2212                 bConstraintOb *cob;
2213                 
2214                 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2215                 solve_constraints(&ob->constraints, cob, (float)scene->r.cfra);
2216                 constraints_clear_evalob(cob);
2217         }
2218         
2219         /*  WATCH IT!!! */
2220                 // XXX old animation system
2221         //ob->ipo= ipo;
2222 }
2223
2224 /* for calculation of the inverse parent transform, only used for editor */
2225 void what_does_parent(Scene *scene, Object *ob, Object *workob)
2226 {
2227         clear_workob(workob);
2228         
2229         unit_m4(workob->obmat);
2230         unit_m4(workob->parentinv);
2231         unit_m4(workob->constinv);
2232         workob->parent= ob->parent;
2233         workob->track= ob->track;
2234
2235         workob->trackflag= ob->trackflag;
2236         workob->upflag= ob->upflag;
2237         
2238         workob->partype= ob->partype;
2239         workob->par1= ob->par1;
2240         workob->par2= ob->par2;
2241         workob->par3= ob->par3;
2242
2243         workob->constraints.first = ob->constraints.first;
2244         workob->constraints.last = ob->constraints.last;
2245
2246         strcpy(workob->parsubstr, ob->parsubstr); 
2247
2248         where_is_object(scene, workob);
2249 }
2250
2251 BoundBox *unit_boundbox()
2252 {
2253         BoundBox *bb;
2254         float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
2255
2256         bb= MEM_callocN(sizeof(BoundBox), "bb");
2257         boundbox_set_from_min_max(bb, min, max);
2258         
2259         return bb;
2260 }
2261
2262 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
2263 {
2264         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2265         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2266         
2267         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2268         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2269
2270         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2271         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2272 }
2273
2274 BoundBox *object_get_boundbox(Object *ob)
2275 {
2276         BoundBox *bb= NULL;
2277         
2278         if(ob->type==OB_MESH) {
2279                 bb = mesh_get_bb(ob);
2280         }
2281         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2282                 bb= ( (Curve *)ob->data )->bb;
2283         }
2284         else if(ob->type==OB_MBALL) {
2285                 bb= ob->bb;
2286         }
2287         return bb;
2288 }
2289
2290 /* used to temporally disable/enable boundbox */
2291 void object_boundbox_flag(Object *ob, int flag, int set)
2292 {
2293         BoundBox *bb= object_get_boundbox(ob);
2294         if(bb) {
2295                 if(set) bb->flag |= flag;
2296                 else bb->flag &= ~flag;
2297         }
2298 }
2299
2300 void minmax_object(Object *ob, float *min, float *max)
2301 {
2302         BoundBox bb;
2303         Mesh *me;
2304         Curve *cu;
2305         float vec[3];
2306         int a;
2307         
2308         switch(ob->type) {
2309                 
2310         case OB_CURVE:
2311         case OB_FONT:
2312         case OB_SURF:
2313                 cu= ob->data;
2314                 
2315                 if(cu->bb==NULL) tex_space_curve(cu);
2316                 bb= *(cu->bb);
2317                 
2318                 for(a=0; a<8; a++) {
2319                         mul_m4_v3(ob->obmat, bb.vec[a]);
2320                         DO_MINMAX(bb.vec[a], min, max);
2321                 }
2322                 break;
2323         case OB_ARMATURE:
2324                 if(ob->pose) {
2325                         bPoseChannel *pchan;
2326                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2327                                 VECCOPY(vec, pchan->pose_head);
2328                                 mul_m4_v3(ob->obmat, vec);
2329                                 DO_MINMAX(vec, min, max);
2330                                 VECCOPY(vec, pchan->pose_tail);
2331                                 mul_m4_v3(ob->obmat, vec);
2332                                 DO_MINMAX(vec, min, max);
2333                         }
2334                         break;
2335                 }
2336                 /* no break, get_mesh will give NULL and it passes on to default */
2337         case OB_MESH:
2338                 me= get_mesh(ob);
2339                 
2340                 if(me) {
2341                         bb = *mesh_get_bb(ob);
2342                         
2343                         for(a=0; a<8; a++) {
2344                                 mul_m4_v3(ob->obmat, bb.vec[a]);
2345                                 DO_MINMAX(bb.vec[a], min, max);
2346                         }
2347                 }
2348                 if(min[0] < max[0] ) break;
2349                 
2350                 /* else here no break!!!, mesh can be zero sized */
2351                 
2352         default:
2353                 DO_MINMAX(ob->obmat[3], min, max);
2354
2355                 VECCOPY(vec, ob->obmat[3]);
2356                 add_v3_v3v3(vec, vec, ob->size);
2357                 DO_MINMAX(vec, min, max);
2358
2359                 VECCOPY(vec, ob->obmat[3]);
2360                 sub_v3_v3v3(vec, vec, ob->size);
2361                 DO_MINMAX(vec, min, max);
2362                 break;
2363         }
2364 }
2365
2366 /* TODO - use dupli objects bounding boxes */
2367 void minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
2368 {
2369         if ((ob->transflag & OB_DUPLI)==0) {
2370                 return;
2371         } else {
2372                 ListBase *lb;
2373                 DupliObject *dob;
2374                 
2375                 lb= object_duplilist(scene, ob);
2376                 for(dob= lb->first; dob; dob= dob->next) {
2377                         if(dob->no_draw);
2378                         else {
2379                                 /* should really use bound box of dup object */
2380                                 DO_MINMAX(dob->mat[3], min, max);
2381                         }
2382                 }
2383                 free_object_duplilist(lb);      /* does restore */
2384         }
2385 }
2386
2387
2388 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2389 /*           local_object->proxy      == pointer to library object, saved in files and read */
2390
2391 /* function below is polluted with proxy exceptions, cleanup will follow! */
2392
2393 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2394 /* requires flags to be set! */
2395 void object_handle_update(Scene *scene, Object *ob)
2396 {
2397         if(ob->recalc & OB_RECALC) {
2398                 
2399                 /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers, 
2400                    which is only in where_is_object now */
2401                 if(ob->recalc & OB_RECALC) {
2402                         
2403                         if (G.f & G_DEBUG)
2404                                 printf("recalcob %s\n", ob->id.name+2);
2405                         
2406                         /* handle proxy copy for target */
2407                         if(ob->id.lib && ob->proxy_from) {
2408                                 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2409                                 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2410                                         Object *obg= ob->proxy_from->proxy_group;
2411                                         invert_m4_m4(obg->imat, obg->obmat);
2412                                         mul_m4_m4m4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2413                                 }
2414                                 else
2415                                         copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
2416                         }
2417                         else
2418                                 where_is_object(scene, ob);
2419                 }
2420                 
2421                 if(ob->recalc & OB_RECALC_DATA) {
2422                         ID *data_id= (ID *)ob->data;
2423                         AnimData *adt= BKE_animdata_from_id(data_id);
2424                         float ctime= (float)scene->r.cfra; // XXX this is bad...
2425                         
2426                         if (G.f & G_DEBUG)
2427                                 printf("recalcdata %s\n", ob->id.name+2);
2428                         
2429                         /* includes all keys and modifiers */
2430                         if(ob->type==OB_MESH) {
2431                                 EditMesh *em = BKE_mesh_get_editmesh(ob->data);
2432                                 
2433                                 /* evaluate drivers */
2434                                 // XXX: should we push this to derivedmesh instead?
2435                                 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2436                                 
2437                                         // here was vieweditdatamask? XXX
2438                                 if(ob->mode & OB_MODE_EDIT) {
2439                                         makeDerivedMesh(scene, ob, em, CD_MASK_BAREMESH);
2440                                         BKE_mesh_end_editmesh(ob->data, em);
2441                                 } else
2442                                         makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH);
2443                         }
2444                         else if(ob->type==OB_MBALL) {
2445                                 makeDispListMBall(scene, ob);
2446                         } 
2447                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2448                                 makeDispListCurveTypes(scene, ob, 0);
2449                         }
2450                         else if(ELEM(ob->type, OB_CAMERA, OB_LAMP)) {
2451                                 /* evaluate drivers */
2452                                 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2453                         }
2454                         else if(ob->type==OB_LATTICE) {
2455                                 lattice_calc_modifiers(scene, ob);
2456                         }
2457                         else if(ob->type==OB_ARMATURE) {
2458                                 /* this happens for reading old files and to match library armatures with poses */
2459                                 // XXX this won't screw up the pose set already...
2460                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2461                                         armature_rebuild_pose(ob, ob->data);
2462                                 
2463                                 /* evaluate drivers */
2464                                 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2465                                 
2466                                 if(ob->id.lib && ob->proxy_from) {
2467                                         copy_pose_result(ob->pose, ob->proxy_from->pose);
2468                                         // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2469                                 }
2470                                 else {
2471                                         where_is_pose(scene, ob);
2472                                 }
2473                         }
2474
2475                         if(ob->particlesystem.first) {
2476                                 ParticleSystem *tpsys, *psys;
2477                                 DerivedMesh *dm;
2478                                 ob->transflag &= ~OB_DUPLIPARTS;
2479                                 
2480                                 psys= ob->particlesystem.first;
2481                                 while(psys) {
2482                                         if(psys_check_enabled(ob, psys)) {
2483                                                 /* check use of dupli objects here */
2484                                                 if(psys->part && psys->part->draw_as == PART_DRAW_REND &&
2485                                                         ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob)
2486                                                         || (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
2487                                                         ob->transflag |= OB_DUPLIPARTS;
2488
2489                                                 particle_system_update(scene, ob, psys);
2490                                                 psys= psys->next;
2491                                         }
2492                                         else if(psys->flag & PSYS_DELETE) {
2493                                                 tpsys=psys->next;
2494                                                 BLI_remlink(&ob->particlesystem, psys);
2495                                                 psys_free(ob,psys);
2496                                                 psys= tpsys;
2497                                         }
2498                                         else
2499                                                 psys= psys->next;
2500                                 }
2501
2502                                 if(G.rendering && ob->transflag & OB_DUPLIPARTS) {
2503                                         /* this is to make sure we get render level duplis in groups:
2504                                          * the derivedmesh must be created before init_render_mesh,
2505                                          * since object_duplilist does dupliparticles before that */
2506                                         dm = mesh_create_derived_render(scene, ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
2507                                         dm->release(dm);
2508
2509                                         for(psys=ob->particlesystem.first; psys; psys=psys->next)
2510                                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
2511                                 }
2512                         }
2513                 }
2514
2515                 /* the no-group proxy case, we call update */
2516                 if(ob->proxy && ob->proxy_group==NULL) {
2517                         /* set pointer in library proxy target, for copying, but restore it */
2518                         ob->proxy->proxy_from= ob;
2519                         // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2520                         object_handle_update(scene, ob->proxy);
2521                 }
2522         
2523                 ob->recalc &= ~OB_RECALC;
2524         }
2525
2526         /* the case when this is a group proxy, object_update is called in group.c */
2527         if(ob->proxy) {
2528                 ob->proxy->proxy_from= ob;
2529                 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2530         }
2531 }
2532
2533 float give_timeoffset(Object *ob) {
2534         if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
2535                 return ob->sf + give_timeoffset(ob->parent);
2536         } else {
2537                 return ob->sf;
2538         }
2539 }
2540
2541 int give_obdata_texspace(Object *ob, short **texflag, float **loc, float **size, float **rot) {
2542         
2543         if (ob->data==NULL)
2544                 return 0;
2545         
2546         switch (GS(((ID *)ob->data)->name)) {
2547         case ID_ME:
2548         {
2549                 Mesh *me= ob->data;
2550                 if (texflag)    *texflag = &me->texflag;
2551                 if (loc)                *loc = me->loc;
2552                 if (size)               *size = me->size;
2553                 if (rot)                *rot = me->rot;
2554                 break;
2555         }
2556         case ID_CU:
2557         {
2558                 Curve *cu= ob->data;
2559                 if (texflag)    *texflag = &cu->texflag;
2560                 if (loc)                *loc = cu->loc;
2561                 if (size)               *size = cu->size;
2562                 if (rot)                *rot = cu->rot;
2563                 break;
2564         }
2565         case ID_MB:
2566         {
2567                 MetaBall *mb= ob->data;
2568                 if (texflag)    *texflag = &mb->texflag;
2569                 if (loc)                *loc = mb->loc;
2570                 if (size)               *size = mb->size;
2571                 if (rot)                *rot = mb->rot;
2572                 break;
2573         }
2574         default:
2575                 return 0;
2576         }
2577         return 1;
2578 }
2579
2580 /*
2581  * Test a bounding box for ray intersection
2582  * assumes the ray is already local to the boundbox space
2583  */
2584 int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
2585 {
2586         static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
2587                                                                                   {3, 2, 6}, {3, 6, 7},
2588                                                                                   {1, 2, 6}, {1, 6, 5}, 
2589                                                                                   {5, 6, 7}, {4, 5, 7},
2590                                                                                   {0, 3, 7}, {0, 4, 7},
2591                                                                                   {0, 1, 5}, {0, 4, 5}};
2592         int result = 0;
2593         int i;
2594         
2595         for (i = 0; i < 12 && result == 0; i++)
2596         {
2597                 float lambda;
2598                 int v1, v2, v3;
2599                 v1 = triangle_indexes[i][0];
2600                 v2 = triangle_indexes[i][1];
2601                 v3 = triangle_indexes[i][2];
2602                 result = isect_ray_tri_v3(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
2603         }
2604         
2605         return result;
2606 }
2607
2608 static int pc_cmp(void *a, void *b)
2609 {
2610         LinkData *ad = a, *bd = b;
2611         if(GET_INT_FROM_POINTER(ad->data) > GET_INT_FROM_POINTER(bd->data))
2612                 return 1;
2613         else return 0;
2614 }
2615
2616 int object_insert_ptcache(Object *ob) 
2617 {
2618         LinkData *link = NULL;
2619         int i = 0;
2620
2621         BLI_sortlist(&ob->pc_ids, pc_cmp);
2622
2623         for(link=ob->pc_ids.first, i = 0; link; link=link->next, i++) 
2624         {
2625                 int index = GET_INT_FROM_POINTER(link->data);
2626
2627                 if(i < index)
2628                         break;
2629         }
2630
2631         link = MEM_callocN(sizeof(LinkData), "PCLink");
2632         link->data = SET_INT_IN_POINTER(i);
2633         BLI_addtail(&ob->pc_ids, link);
2634
2635         return i;
2636 }
2637
2638 #if 0
2639 static int pc_findindex(ListBase *listbase, int index)
2640 {
2641         LinkData *link= NULL;
2642         int number= 0;
2643         
2644         if (listbase == NULL) return -1;
2645         
2646         link= listbase->first;
2647         while (link) {
2648                 if ((int)link->data == index)
2649                         return number;
2650                 
2651                 number++;
2652                 link= link->next;
2653         }
2654         
2655         return -1;
2656 }
2657
2658 void object_delete_ptcache(Object *ob, int index) 
2659 {
2660         int list_index = pc_findindex(&ob->pc_ids, index);
2661         LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
2662         BLI_freelinkN(&ob->pc_ids, link);
2663 }
2664 #endif
2665
2666 /* shape key utility function */
2667
2668 /************************* Mesh ************************/
2669 static KeyBlock *insert_meshkey(Scene *scene, Object *ob, char *name, int from_mix)
2670 {
2671         Mesh *me= ob->data;
2672         Key *key= me->key;
2673         KeyBlock *kb;
2674         int newkey= 0;
2675
2676         if(key == NULL) {
2677                 key= me->key= add_key((ID *)me);
2678                 key->type= KEY_RELATIVE;
2679                 newkey= 1;
2680         }
2681
2682         kb= add_keyblock(key, name);
2683
2684         if(newkey || from_mix==FALSE) {
2685                 /* create from mesh */
2686                 mesh_to_key(me, kb);
2687         }
2688         else {
2689                 /* copy from current values */
2690                 kb->data= do_ob_key(scene, ob);
2691                 kb->totelem= me->totvert;
2692         }
2693
2694         return kb;
2695 }
2696 /************************* Lattice ************************/
2697 static KeyBlock *insert_lattkey(Scene *scene, Object *ob, char *name, int from_mix)
2698 {
2699         Lattice *lt= ob->data;
2700         Key *key= lt->key;
2701         KeyBlock *kb;
2702         int newkey= 0;
2703
2704         if(key==NULL) {
2705                 key= lt->key= add_key( (ID *)lt);
2706                 key->type= KEY_RELATIVE;
2707                 newkey= 1;
2708         }
2709
2710         kb= add_keyblock(key, name);
2711
2712         if(newkey || from_mix==FALSE) {
2713                 /* create from lattice */
2714                 latt_to_key(lt, kb);
2715         }
2716         else {
2717                 /* copy from current values */
2718                 kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
2719                 kb->data= do_ob_key(scene, ob);
2720         }
2721
2722         return kb;
2723 }
2724 /************************* Curve ************************/
2725 static KeyBlock *insert_curvekey(Scene *scene, Object *ob, char *name, int from_mix)
2726 {
2727         Curve *cu= ob->data;
2728         Key *key= cu->key;
2729         KeyBlock *kb;
2730         ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
2731         int newkey= 0;
2732
2733         if(key==NULL) {
2734                 key= cu->key= add_key( (ID *)cu);
2735                 key->type = KEY_RELATIVE;
2736                 newkey= 1;
2737         }
2738
2739         kb= add_keyblock(key, name);
2740
2741         if(newkey || from_mix==FALSE) {
2742                 /* create from curve */
2743                 curve_to_key(cu, kb, lb);
2744         }
2745         else {
2746                 /* copy from current values */
2747                 kb->totelem= count_curveverts(lb);
2748                 kb->data= do_ob_key(scene, ob);
2749         }
2750
2751         return kb;
2752 }
2753
2754 KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, char *name, int from_mix)
2755 {
2756         if(ob->type==OB_MESH)                                    return insert_meshkey(scene, ob, name, from_mix);
2757         else if ELEM(ob->type, OB_CURVE, OB_SURF)return insert_curvekey(scene, ob, name, from_mix);
2758         else if(ob->type==OB_LATTICE)                    return insert_lattkey(scene, ob, name, from_mix);
2759         else                                                                     return NULL;
2760 }
2761