copy modifiers, as "Link Modifiers" - in Ctrl+L menu. difference between copy and...
[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                         DriverTarget *dtar;
1504                         
1505                         for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
1506                                 if ((Object *)dtar->id == target)
1507                                         dtar->id= (ID *)ob;
1508                                 else
1509                                         id_lib_extern((ID *)dtar->id);
1510                         }
1511                 }
1512         }
1513         
1514         /* skip constraints? */
1515         // FIXME: this is considered by many as a bug
1516         
1517         /* set object type and link to data */
1518         ob->type= target->type;
1519         ob->data= target->data;
1520         id_us_plus((ID *)ob->data);             /* ensures lib data becomes LIB_EXTERN */
1521         
1522         /* copy material and index information */
1523         ob->actcol= ob->totcol= 0;
1524         if(ob->mat) MEM_freeN(ob->mat);
1525         if(ob->matbits) MEM_freeN(ob->matbits);
1526         ob->mat = NULL;
1527         ob->matbits= NULL;
1528         if ((target->totcol) && (target->mat) && ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) { //XXX OB_SUPPORT_MATERIAL
1529                 int i;
1530                 ob->colbits = target->colbits;
1531                 
1532                 ob->actcol= target->actcol;
1533                 ob->totcol= target->totcol;
1534                 
1535                 ob->mat = MEM_dupallocN(target->mat);
1536                 ob->matbits = MEM_dupallocN(target->matbits);
1537                 for(i=0; i<target->totcol; i++) {
1538                         /* dont need to run test_object_materials since we know this object is new and not used elsewhere */
1539                         id_us_plus((ID *)ob->mat[i]); 
1540                 }
1541         }
1542         
1543         /* type conversions */
1544         if(target->type == OB_ARMATURE) {
1545                 copy_object_pose(ob, target);   /* data copy, object pointers in constraints */
1546                 rest_pose(ob->pose);                    /* clear all transforms in channels */
1547                 armature_rebuild_pose(ob, ob->data);    /* set all internal links */
1548                 
1549                 armature_set_id_extern(ob);
1550         }
1551         
1552         /* copy drawtype info */
1553         ob->dt= target->dt;
1554 }
1555
1556
1557 /* *************** CALC ****************** */
1558
1559 /* there is also a timing calculation in drawobject() */
1560
1561 float bluroffs= 0.0f, fieldoffs= 0.0f;
1562 int no_speed_curve= 0;
1563
1564 /* ugly calls from render */
1565 void set_mblur_offs(float blur)
1566 {
1567         bluroffs= blur;
1568 }
1569
1570 void set_field_offs(float field)
1571 {
1572         fieldoffs= field;
1573 }
1574
1575 void disable_speed_curve(int val)
1576 {
1577         no_speed_curve= val;
1578 }
1579
1580 // XXX THIS CRUFT NEEDS SERIOUS RECODING ASAP!
1581 /* ob can be NULL */
1582 float bsystem_time(struct Scene *scene, Object *ob, float cfra, float ofs)
1583 {
1584         /* returns float ( see frame_to_float in ipo.c) */
1585         
1586         /* bluroffs and fieldoffs are ugly globals that are set by render */
1587         cfra+= bluroffs+fieldoffs;
1588
1589         /* global time */
1590         if (scene)
1591                 cfra*= scene->r.framelen;       
1592         
1593 #if 0 // XXX old animation system
1594         if (ob) {
1595                 /* ofset frames */
1596                 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0) 
1597                         cfra-= give_timeoffset(ob);
1598         }
1599 #endif // XXX old animation system
1600         
1601         cfra-= ofs;
1602
1603         return cfra;
1604 }
1605
1606 void object_scale_to_mat3(Object *ob, float mat[][3])
1607 {
1608         float vec[3];
1609         
1610         vec[0]= ob->size[0]+ob->dsize[0];
1611         vec[1]= ob->size[1]+ob->dsize[1];
1612         vec[2]= ob->size[2]+ob->dsize[2];
1613         size_to_mat3( mat,vec);
1614 }
1615
1616 // TODO: this should take rotation orders into account later...
1617 void object_rot_to_mat3(Object *ob, float mat[][3])
1618 {
1619         float rmat[3][3], dmat[3][3];
1620         
1621         /* initialise the delta-rotation matrix, which will get (pre)multiplied 
1622          * with the rotation matrix to yield the appropriate rotation
1623          */
1624         unit_m3(dmat);
1625         
1626         /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1627         if (ob->rotmode > 0) {
1628                 /* euler rotations (will cause gimble lock, but this can be alleviated a bit with rotation orders) */
1629                 eulO_to_mat3( rmat,ob->rot, ob->rotmode);
1630                 eulO_to_mat3( dmat,ob->drot, ob->rotmode);
1631         }
1632         else if (ob->rotmode == ROT_MODE_AXISANGLE) {
1633                 /* axis-angle -  not really that great for 3D-changing orientations */
1634                 axis_angle_to_mat3( rmat,ob->rotAxis, ob->rotAngle);
1635                 axis_angle_to_mat3( dmat,ob->drotAxis, ob->drotAngle);
1636         }
1637         else {
1638                 /* quats are normalised before use to eliminate scaling issues */
1639                 normalize_qt(ob->quat);
1640                 quat_to_mat3( rmat,ob->quat);
1641                 quat_to_mat3( dmat,ob->dquat);
1642         }
1643         
1644         /* combine these rotations */
1645         // XXX is this correct? if errors, change the order of multiplication...
1646         mul_m3_m3m3(mat, dmat, rmat);
1647 }
1648
1649 void object_mat3_to_rot(Object *ob, float mat[][3], int use_compat)
1650 {
1651         if (ob->rotmode == ROT_MODE_QUAT)
1652                 mat3_to_quat(ob->quat, mat);
1653         else if (ob->rotmode == ROT_MODE_AXISANGLE)
1654                 mat3_to_axis_angle(ob->rotAxis, &ob->rotAngle, mat);
1655         else {
1656                 if(use_compat) {
1657                         float eul[3];
1658                         VECCOPY(eul, ob->rot);
1659                         mat3_to_compatible_eulO(ob->rot, eul, ob->rotmode, mat);
1660                 }
1661                 else
1662                         mat3_to_eulO(ob->rot, ob->rotmode, mat);
1663         }
1664 }
1665
1666 void object_apply_mat4(Object *ob, float mat[][4])
1667 {
1668         float mat3[3][3];
1669         VECCOPY(ob->loc, mat[3]);
1670         mat4_to_size(ob->size, mat);
1671         copy_m3_m4(mat3, mat);
1672         object_mat3_to_rot(ob, mat3, 0);
1673 }
1674
1675 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1676 {
1677         float smat[3][3];
1678         float rmat[3][3];
1679         /*float q1[4];*/
1680         
1681         /* size */
1682         object_scale_to_mat3(ob, smat);
1683
1684         /* rot */
1685         object_rot_to_mat3(ob, rmat);
1686         mul_m3_m3m3(mat, rmat, smat);
1687 }
1688
1689 void object_to_mat4(Object *ob, float mat[][4])
1690 {
1691         float tmat[3][3];
1692         
1693         object_to_mat3(ob, tmat);
1694         
1695         copy_m4_m3(mat, tmat);
1696         
1697         mat[3][0]= ob->loc[0] + ob->dloc[0];
1698         mat[3][1]= ob->loc[1] + ob->dloc[1];
1699         mat[3][2]= ob->loc[2] + ob->dloc[2];
1700 }
1701
1702 int enable_cu_speed= 1;
1703
1704 static void ob_parcurve(Scene *scene, Object *ob, Object *par, float mat[][4])
1705 {
1706         Curve *cu;
1707         float q[4], vec[4], dir[3], quat[4], radius, x1, ctime;
1708         float timeoffs = 0.0, sf_orig = 0.0;
1709         
1710         unit_m4(mat);
1711         
1712         cu= par->data;
1713         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1714                 makeDispListCurveTypes(scene, par, 0);
1715         if(cu->path==NULL) return;
1716         
1717         /* exception, timeoffset is regarded as distance offset */
1718         if(cu->flag & CU_OFFS_PATHDIST) {
1719                 timeoffs = give_timeoffset(ob);
1720                 SWAP(float, sf_orig, ob->sf);
1721         }
1722         
1723         /* catch exceptions: feature for nla stride editing */
1724         if(ob->ipoflag & OB_DISABLE_PATH) {
1725                 ctime= 0.0f;
1726         }
1727         /* catch exceptions: curve paths used as a duplicator */
1728         else if(enable_cu_speed) {
1729                 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1730                  * but this will only work if it actually is animated... 
1731                  *
1732                  * we divide the curvetime calculated in the previous step by the length of the path, to get a time
1733                  * factor, which then gets clamped to lie within 0.0 - 1.0 range
1734                  */
1735                 ctime= cu->ctime / cu->pathlen;
1736                 CLAMP(ctime, 0.0, 1.0);
1737         }
1738         else {
1739                 ctime= scene->r.cfra - give_timeoffset(ob);
1740                 ctime /= cu->pathlen;
1741                 
1742                 CLAMP(ctime, 0.0, 1.0);
1743         }
1744         
1745         /* time calculus is correct, now apply distance offset */
1746         if(cu->flag & CU_OFFS_PATHDIST) {
1747                 ctime += timeoffs/cu->path->totdist;
1748
1749                 /* restore */
1750                 SWAP(float, sf_orig, ob->sf);
1751         }
1752         
1753         
1754         /* vec: 4 items! */
1755         if( where_on_path(par, ctime, vec, dir, NULL, &radius) ) {
1756
1757                 if(cu->flag & CU_FOLLOW) {
1758                         vec_to_quat( quat,dir, ob->trackflag, ob->upflag);
1759                         
1760                         /* the tilt */
1761                         normalize_v3(dir);
1762                         q[0]= (float)cos(0.5*vec[3]);
1763                         x1= (float)sin(0.5*vec[3]);
1764                         q[1]= -x1*dir[0];
1765                         q[2]= -x1*dir[1];
1766                         q[3]= -x1*dir[2];
1767                         mul_qt_qtqt(quat, q, quat);
1768                         
1769                         quat_to_mat4( mat,quat);
1770                 }
1771                 
1772                 if(cu->flag & CU_PATH_RADIUS) {
1773                         float tmat[4][4], rmat[4][4];
1774                         scale_m4_fl(tmat, radius);
1775                         mul_m4_m4m4(rmat, mat, tmat);
1776                         copy_m4_m4(mat, rmat);
1777                 }
1778
1779                 VECCOPY(mat[3], vec);
1780                 
1781         }
1782 }
1783
1784 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1785 {       
1786         bPoseChannel *pchan;
1787         float vec[3];
1788         
1789         if (par->type!=OB_ARMATURE) {
1790                 unit_m4(mat);
1791                 return;
1792         }
1793         
1794         /* Make sure the bone is still valid */
1795         pchan= get_pose_channel(par->pose, ob->parsubstr);
1796         if (!pchan){
1797                 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1798                 unit_m4(mat);
1799                 return;
1800         }
1801
1802         /* get bone transform */
1803         copy_m4_m4(mat, pchan->pose_mat);
1804
1805         /* but for backwards compatibility, the child has to move to the tail */
1806         VECCOPY(vec, mat[1]);
1807         mul_v3_fl(vec, pchan->bone->length);
1808         add_v3_v3v3(mat[3], mat[3], vec);
1809 }
1810
1811 static void give_parvert(Object *par, int nr, float *vec)
1812 {
1813         EditMesh *em;
1814         int a, count;
1815         
1816         vec[0]=vec[1]=vec[2]= 0.0f;
1817         
1818         if(par->type==OB_MESH) {
1819                 Mesh *me= par->data;
1820                 em = BKE_mesh_get_editmesh(me);
1821
1822                 if(em) {
1823                         EditVert *eve;
1824                         
1825                         for(eve= em->verts.first; eve; eve= eve->next) {
1826                                 if(eve->keyindex==nr) {
1827                                         memcpy(vec, eve->co, sizeof(float)*3);
1828                                         break;
1829                                 }
1830                         }
1831                         BKE_mesh_end_editmesh(me, em);
1832                 }
1833                 else {
1834                         DerivedMesh *dm = par->derivedFinal;
1835                         
1836                         if(dm) {
1837                                 int i, count = 0, vindex, numVerts = dm->getNumVerts(dm);
1838                                 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1839                                 float co[3];
1840
1841                                 /* get the average of all verts with (original index == nr) */
1842                                 for(i = 0; i < numVerts; ++i) {
1843                                         vindex= (index)? *index: i;
1844
1845                                         if(vindex == nr) {
1846                                                 dm->getVertCo(dm, i, co);
1847                                                 add_v3_v3v3(vec, vec, co);
1848                                                 count++;
1849                                         }
1850                                 }
1851
1852                                 if (count==0) {
1853                                         /* keep as 0,0,0 */
1854                                 } else if(count > 0) {
1855                                         mul_v3_fl(vec, 1.0f / count);
1856                                 } else {
1857                                         /* use first index if its out of range */
1858                                         dm->getVertCo(dm, 0, vec);
1859                                 }
1860                         }
1861                 }
1862         }
1863         else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
1864                 Nurb *nu;
1865                 Curve *cu;
1866                 BPoint *bp;
1867                 BezTriple *bezt;
1868                 int found= 0;
1869                 
1870                 cu= par->data;
1871                 if(cu->editnurb)
1872                         nu= cu->editnurb->first;
1873                 else
1874                         nu= cu->nurb.first;
1875                 
1876                 count= 0;
1877                 while(nu && !found) {
1878                         if(nu->type == CU_BEZIER) {
1879                                 bezt= nu->bezt;
1880                                 a= nu->pntsu;
1881                                 while(a--) {
1882                                         if(count==nr) {
1883                                                 found= 1;
1884                                                 VECCOPY(vec, bezt->vec[1]);
1885                                                 break;
1886                                         }
1887                                         count++;
1888                                         bezt++;
1889                                 }
1890                         }
1891                         else {
1892                                 bp= nu->bp;
1893                                 a= nu->pntsu*nu->pntsv;
1894                                 while(a--) {
1895                                         if(count==nr) {
1896                                                 found= 1;
1897                                                 memcpy(vec, bp->vec, sizeof(float)*3);
1898                                                 break;
1899                                         }
1900                                         count++;
1901                                         bp++;
1902                                 }
1903                         }
1904                         nu= nu->next;
1905                 }
1906
1907         }
1908         else if(par->type==OB_LATTICE) {
1909                 Lattice *latt= par->data;
1910                 BPoint *bp;
1911                 DispList *dl = find_displist(&par->disp, DL_VERTS);
1912                 float *co = dl?dl->verts:NULL;
1913                 
1914                 if(latt->editlatt) latt= latt->editlatt;
1915                 
1916                 a= latt->pntsu*latt->pntsv*latt->pntsw;
1917                 count= 0;
1918                 bp= latt->def;
1919                 while(a--) {
1920                         if(count==nr) {
1921                                 if(co)
1922                                         memcpy(vec, co, 3*sizeof(float));
1923                                 else
1924                                         memcpy(vec, bp->vec, 3*sizeof(float));
1925                                 break;
1926                         }
1927                         count++;
1928                         if(co) co+= 3;
1929                         else bp++;
1930                 }
1931         }
1932 }
1933
1934 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1935 {
1936         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1937
1938         /* in local ob space */
1939         unit_m4(mat);
1940         
1941         if (ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE)) {
1942                 
1943                 give_parvert(par, ob->par1, v1);
1944                 give_parvert(par, ob->par2, v2);
1945                 give_parvert(par, ob->par3, v3);
1946                                 
1947                 tri_to_quat( q,v1, v2, v3);
1948                 quat_to_mat3( cmat,q);
1949                 copy_m4_m3(mat, cmat);
1950                 
1951                 if(ob->type==OB_CURVE) {
1952                         VECCOPY(mat[3], v1);
1953                 }
1954                 else {
1955                         add_v3_v3v3(mat[3], v1, v2);
1956                         add_v3_v3v3(mat[3], mat[3], v3);
1957                         mul_v3_fl(mat[3], 0.3333333f);
1958                 }
1959         }
1960 }
1961
1962 // XXX what the hell is this?
1963 static int no_parent_ipo=0;
1964 void set_no_parent_ipo(int val)
1965 {
1966         no_parent_ipo= val;
1967 }
1968
1969 void where_is_object_time(Scene *scene, Object *ob, float ctime)
1970 {
1971         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1972         float stime=ctime, fac1, fac2, vec[3];
1973         int a;
1974         int pop; 
1975         
1976         /* new version: correct parent+vertexparent and track+parent */
1977         /* this one only calculates direct attached parent and track */
1978         /* is faster, but should keep track of timeoffs */
1979         
1980         if(ob==NULL) return;
1981         
1982         /* execute drivers only, as animation has already been done */
1983         BKE_animsys_evaluate_animdata(&ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
1984         
1985         if(ob->parent) {
1986                 Object *par= ob->parent;
1987                 
1988                 // XXX depreceated - animsys
1989                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= give_timeoffset(ob);
1990                 
1991                 /* hurms, code below conflicts with depgraph... (ton) */
1992                 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1993                 pop= 0;
1994                 if(no_parent_ipo==0 && stime != par->ctime) {
1995                         // only for ipo systems? 
1996                         pushdata(par, sizeof(Object));
1997                         pop= 1;
1998                         
1999                         if(par->proxy_from);    // was a copied matrix, no where_is! bad...
2000                         else where_is_object_time(scene, par, ctime);
2001                 }
2002                 
2003                 solve_parenting(scene, ob, par, ob->obmat, slowmat, 0);
2004                 
2005                 if(pop) {
2006                         poplast(par);
2007                 }
2008                 
2009                 if(ob->partype & PARSLOW) {
2010                         // include framerate
2011                         fac1= ( 1.0f / (1.0f + (float)fabs(give_timeoffset(ob))) );
2012                         if(fac1 >= 1.0f) return;
2013                         fac2= 1.0f-fac1;
2014                         
2015                         fp1= ob->obmat[0];
2016                         fp2= slowmat[0];
2017                         for(a=0; a<16; a++, fp1++, fp2++) {
2018                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2019                         }
2020                 }
2021         }
2022         else {
2023                 object_to_mat4(ob, ob->obmat);
2024         }
2025
2026         /* Handle tracking */
2027         if(ob->track) {
2028                 if( ctime != ob->track->ctime) where_is_object_time(scene, ob->track, ctime);
2029                 solve_tracking (ob, ob->track->obmat);
2030         }
2031
2032         /* solve constraints */
2033         if (ob->constraints.first) {
2034                 bConstraintOb *cob;
2035                 
2036                 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2037                 
2038                 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
2039                 solve_constraints (&ob->constraints, cob, ctime);
2040                 
2041                 constraints_clear_evalob(cob);
2042         }
2043         
2044         /* set negative scale flag in object */
2045         cross_v3_v3v3(vec, ob->obmat[0], ob->obmat[1]);
2046         if( dot_v3v3(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
2047         else ob->transflag &= ~OB_NEG_SCALE;
2048 }
2049
2050 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
2051 {
2052         float totmat[4][4];
2053         float tmat[4][4];
2054         float locmat[4][4];
2055         float vec[3];
2056         int ok;
2057         
2058         object_to_mat4(ob, locmat);
2059         
2060         if(ob->partype & PARSLOW) copy_m4_m4(slowmat, obmat);
2061
2062         switch(ob->partype & PARTYPE) {
2063         case PAROBJECT:
2064                 ok= 0;
2065                 if(par->type==OB_CURVE) {
2066                         if( ((Curve *)par->data)->flag & CU_PATH ) {
2067                                 ob_parcurve(scene, ob, par, tmat);
2068                                 ok= 1;
2069                         }
2070                 }
2071                 
2072                 if(ok) mul_serie_m4(totmat, par->obmat, tmat, 
2073                         NULL, NULL, NULL, NULL, NULL, NULL);
2074                 else copy_m4_m4(totmat, par->obmat);
2075                 
2076                 break;
2077         case PARBONE:
2078                 ob_parbone(ob, par, tmat);
2079                 mul_serie_m4(totmat, par->obmat, tmat,         
2080                         NULL, NULL, NULL, NULL, NULL, NULL);
2081                 break;
2082                 
2083         case PARVERT1:
2084                 unit_m4(totmat);
2085                 if (simul){
2086                         VECCOPY(totmat[3], par->obmat[3]);
2087                 }
2088                 else{
2089                         give_parvert(par, ob->par1, vec);
2090                         mul_v3_m4v3(totmat[3], par->obmat, vec);
2091                 }
2092                 break;
2093         case PARVERT3:
2094                 ob_parvert3(ob, par, tmat);
2095                 
2096                 mul_serie_m4(totmat, par->obmat, tmat,         
2097                         NULL, NULL, NULL, NULL, NULL, NULL);
2098                 break;
2099                 
2100         case PARSKEL:
2101                 copy_m4_m4(totmat, par->obmat);
2102                 break;
2103         }
2104         
2105         // total 
2106         mul_serie_m4(tmat, totmat, ob->parentinv,         
2107                 NULL, NULL, NULL, NULL, NULL, NULL);
2108         mul_serie_m4(obmat, tmat, locmat,         
2109                 NULL, NULL, NULL, NULL, NULL, NULL);
2110         
2111         if (simul) {
2112
2113         }
2114         else{
2115                 // external usable originmat 
2116                 copy_m3_m4(originmat, tmat);
2117                 
2118                 // origin, voor help line
2119                 if( (ob->partype & 15)==PARSKEL ) {
2120                         VECCOPY(ob->orig, par->obmat[3]);
2121                 }
2122                 else {
2123                         VECCOPY(ob->orig, totmat[3]);
2124                 }
2125         }
2126
2127 }
2128 void solve_tracking (Object *ob, float targetmat[][4])
2129 {
2130         float quat[4];
2131         float vec[3];
2132         float totmat[3][3];
2133         float tmat[4][4];
2134         
2135         sub_v3_v3v3(vec, ob->obmat[3], targetmat[3]);
2136         vec_to_quat( quat,vec, ob->trackflag, ob->upflag);
2137         quat_to_mat3( totmat,quat);
2138         
2139         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
2140                 /* 'temporal' : clear parent info */
2141                 object_to_mat4(ob, tmat);
2142                 tmat[0][3]= ob->obmat[0][3];
2143                 tmat[1][3]= ob->obmat[1][3];
2144                 tmat[2][3]= ob->obmat[2][3];
2145                 tmat[3][0]= ob->obmat[3][0];
2146                 tmat[3][1]= ob->obmat[3][1];
2147                 tmat[3][2]= ob->obmat[3][2];
2148                 tmat[3][3]= ob->obmat[3][3];
2149         }
2150         else copy_m4_m4(tmat, ob->obmat);
2151         
2152         mul_m4_m3m4(ob->obmat, totmat, tmat);
2153
2154 }
2155
2156 void where_is_object(struct Scene *scene, Object *ob)
2157 {
2158         where_is_object_time(scene, ob, (float)scene->r.cfra);
2159 }
2160
2161
2162 void where_is_object_simul(Scene *scene, Object *ob)
2163 /* was written for the old game engine (until 2.04) */
2164 /* It seems that this function is only called
2165 for a lamp that is the child of another object */
2166 {
2167         Object *par;
2168         //Ipo *ipo;
2169         float *fp1, *fp2;
2170         float slowmat[4][4];
2171         float fac1, fac2;
2172         int a;
2173         
2174         /* NO TIMEOFFS */
2175         
2176         /* no ipo! (because of dloc and realtime-ipos) */
2177                 // XXX old animation system
2178         //ipo= ob->ipo;
2179         //ob->ipo= NULL;
2180
2181         if(ob->parent) {
2182                 par= ob->parent;
2183                 
2184                 solve_parenting(scene, ob, par, ob->obmat, slowmat, 1);
2185
2186                 if(ob->partype & PARSLOW) {
2187
2188                         fac1= (float)(1.0/(1.0+ fabs(give_timeoffset(ob))));
2189                         fac2= 1.0f-fac1;
2190                         fp1= ob->obmat[0];
2191                         fp2= slowmat[0];
2192                         for(a=0; a<16; a++, fp1++, fp2++) {
2193                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
2194                         }
2195                 }
2196                 
2197         }
2198         else {
2199                 object_to_mat4(ob, ob->obmat);
2200         }
2201         
2202         if(ob->track) 
2203                 solve_tracking(ob, ob->track->obmat);
2204
2205         /* solve constraints */
2206         if (ob->constraints.first) {
2207                 bConstraintOb *cob;
2208                 
2209                 cob= constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
2210                 solve_constraints(&ob->constraints, cob, (float)scene->r.cfra);
2211                 constraints_clear_evalob(cob);
2212         }
2213         
2214         /*  WATCH IT!!! */
2215                 // XXX old animation system
2216         //ob->ipo= ipo;
2217 }
2218
2219 /* for calculation of the inverse parent transform, only used for editor */
2220 void what_does_parent(Scene *scene, Object *ob, Object *workob)
2221 {
2222         clear_workob(workob);
2223         
2224         unit_m4(workob->obmat);
2225         unit_m4(workob->parentinv);
2226         unit_m4(workob->constinv);
2227         workob->parent= ob->parent;
2228         workob->track= ob->track;
2229
2230         workob->trackflag= ob->trackflag;
2231         workob->upflag= ob->upflag;
2232         
2233         workob->partype= ob->partype;
2234         workob->par1= ob->par1;
2235         workob->par2= ob->par2;
2236         workob->par3= ob->par3;
2237
2238         workob->constraints.first = ob->constraints.first;
2239         workob->constraints.last = ob->constraints.last;
2240
2241         strcpy(workob->parsubstr, ob->parsubstr); 
2242
2243         where_is_object(scene, workob);
2244 }
2245
2246 BoundBox *unit_boundbox()
2247 {
2248         BoundBox *bb;
2249         float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
2250
2251         bb= MEM_callocN(sizeof(BoundBox), "bb");
2252         boundbox_set_from_min_max(bb, min, max);
2253         
2254         return bb;
2255 }
2256
2257 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
2258 {
2259         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2260         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2261         
2262         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2263         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2264
2265         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2266         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2267 }
2268
2269 BoundBox *object_get_boundbox(Object *ob)
2270 {
2271         BoundBox *bb= NULL;
2272         
2273         if(ob->type==OB_MESH) {
2274                 bb = mesh_get_bb(ob);
2275         }
2276         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2277                 bb= ( (Curve *)ob->data )->bb;
2278         }
2279         else if(ob->type==OB_MBALL) {
2280                 bb= ob->bb;
2281         }
2282         return bb;
2283 }
2284
2285 /* used to temporally disable/enable boundbox */
2286 void object_boundbox_flag(Object *ob, int flag, int set)
2287 {
2288         BoundBox *bb= object_get_boundbox(ob);
2289         if(bb) {
2290                 if(set) bb->flag |= flag;
2291                 else bb->flag &= ~flag;
2292         }
2293 }
2294
2295 void minmax_object(Object *ob, float *min, float *max)
2296 {
2297         BoundBox bb;
2298         Mesh *me;
2299         Curve *cu;
2300         float vec[3];
2301         int a;
2302         
2303         switch(ob->type) {
2304                 
2305         case OB_CURVE:
2306         case OB_FONT:
2307         case OB_SURF:
2308                 cu= ob->data;
2309                 
2310                 if(cu->bb==NULL) tex_space_curve(cu);
2311                 bb= *(cu->bb);
2312                 
2313                 for(a=0; a<8; a++) {
2314                         mul_m4_v3(ob->obmat, bb.vec[a]);
2315                         DO_MINMAX(bb.vec[a], min, max);
2316                 }
2317                 break;
2318         case OB_ARMATURE:
2319                 if(ob->pose) {
2320                         bPoseChannel *pchan;
2321                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2322                                 VECCOPY(vec, pchan->pose_head);
2323                                 mul_m4_v3(ob->obmat, vec);
2324                                 DO_MINMAX(vec, min, max);
2325                                 VECCOPY(vec, pchan->pose_tail);
2326                                 mul_m4_v3(ob->obmat, vec);
2327                                 DO_MINMAX(vec, min, max);
2328                         }
2329                         break;
2330                 }
2331                 /* no break, get_mesh will give NULL and it passes on to default */
2332         case OB_MESH:
2333                 me= get_mesh(ob);
2334                 
2335                 if(me) {
2336                         bb = *mesh_get_bb(ob);
2337                         
2338                         for(a=0; a<8; a++) {
2339                                 mul_m4_v3(ob->obmat, bb.vec[a]);
2340                                 DO_MINMAX(bb.vec[a], min, max);
2341                         }
2342                 }
2343                 if(min[0] < max[0] ) break;
2344                 
2345                 /* else here no break!!!, mesh can be zero sized */
2346                 
2347         default:
2348                 DO_MINMAX(ob->obmat[3], min, max);
2349
2350                 VECCOPY(vec, ob->obmat[3]);
2351                 add_v3_v3v3(vec, vec, ob->size);
2352                 DO_MINMAX(vec, min, max);
2353
2354                 VECCOPY(vec, ob->obmat[3]);
2355                 sub_v3_v3v3(vec, vec, ob->size);
2356                 DO_MINMAX(vec, min, max);
2357                 break;
2358         }
2359 }
2360
2361 /* TODO - use dupli objects bounding boxes */
2362 void minmax_object_duplis(Scene *scene, Object *ob, float *min, float *max)
2363 {
2364         if ((ob->transflag & OB_DUPLI)==0) {
2365                 return;
2366         } else {
2367                 ListBase *lb;
2368                 DupliObject *dob;
2369                 
2370                 lb= object_duplilist(scene, ob);
2371                 for(dob= lb->first; dob; dob= dob->next) {
2372                         if(dob->no_draw);
2373                         else {
2374                                 /* should really use bound box of dup object */
2375                                 DO_MINMAX(dob->mat[3], min, max);
2376                         }
2377                 }
2378                 free_object_duplilist(lb);      /* does restore */
2379         }
2380 }
2381
2382
2383 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2384 /*           local_object->proxy      == pointer to library object, saved in files and read */
2385
2386 /* function below is polluted with proxy exceptions, cleanup will follow! */
2387
2388 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2389 /* requires flags to be set! */
2390 void object_handle_update(Scene *scene, Object *ob)
2391 {
2392         if(ob->recalc & OB_RECALC) {
2393                 
2394                 /* XXX new animsys warning: depsgraph tag OB_RECALC_DATA should not skip drivers, 
2395                    which is only in where_is_object now */
2396                 if(ob->recalc & OB_RECALC) {
2397                         
2398                         if (G.f & G_DEBUG)
2399                                 printf("recalcob %s\n", ob->id.name+2);
2400                         
2401                         /* handle proxy copy for target */
2402                         if(ob->id.lib && ob->proxy_from) {
2403                                 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2404                                 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2405                                         Object *obg= ob->proxy_from->proxy_group;
2406                                         invert_m4_m4(obg->imat, obg->obmat);
2407                                         mul_m4_m4m4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2408                                 }
2409                                 else
2410                                         copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
2411                         }
2412                         else
2413                                 where_is_object(scene, ob);
2414                 }
2415                 
2416                 if(ob->recalc & OB_RECALC_DATA) {
2417                         ID *data_id= (ID *)ob->data;
2418                         AnimData *adt= BKE_animdata_from_id(data_id);
2419                         float ctime= (float)scene->r.cfra; // XXX this is bad...
2420                         
2421                         if (G.f & G_DEBUG)
2422                                 printf("recalcdata %s\n", ob->id.name+2);
2423                         
2424                         /* includes all keys and modifiers */
2425                         if(ob->type==OB_MESH) {
2426                                 EditMesh *em = BKE_mesh_get_editmesh(ob->data);
2427                                 
2428                                 /* evaluate drivers */
2429                                 // XXX: should we push this to derivedmesh instead?
2430                                 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2431                                 
2432                                         // here was vieweditdatamask? XXX
2433                                 if(ob->mode & OB_MODE_EDIT) {
2434                                         makeDerivedMesh(scene, ob, em, CD_MASK_BAREMESH);
2435                                         BKE_mesh_end_editmesh(ob->data, em);
2436                                 } else
2437                                         makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH);
2438                         }
2439                         else if(ob->type==OB_MBALL) {
2440                                 makeDispListMBall(scene, ob);
2441                         } 
2442                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2443                                 makeDispListCurveTypes(scene, ob, 0);
2444                         }
2445                         else if(ELEM(ob->type, OB_CAMERA, OB_LAMP)) {
2446                                 /* evaluate drivers */
2447                                 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2448                         }
2449                         else if(ob->type==OB_LATTICE) {
2450                                 lattice_calc_modifiers(scene, ob);
2451                         }
2452                         else if(ob->type==OB_ARMATURE) {
2453                                 /* this happens for reading old files and to match library armatures with poses */
2454                                 // XXX this won't screw up the pose set already...
2455                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2456                                         armature_rebuild_pose(ob, ob->data);
2457                                 
2458                                 /* evaluate drivers */
2459                                 BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
2460                                 
2461                                 if(ob->id.lib && ob->proxy_from) {
2462                                         copy_pose_result(ob->pose, ob->proxy_from->pose);
2463                                         // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2464                                 }
2465                                 else {
2466                                         where_is_pose(scene, ob);
2467                                 }
2468                         }
2469
2470                         if(ob->particlesystem.first) {
2471                                 ParticleSystem *tpsys, *psys;
2472                                 DerivedMesh *dm;
2473                                 ob->transflag &= ~OB_DUPLIPARTS;
2474                                 
2475                                 psys= ob->particlesystem.first;
2476                                 while(psys) {
2477                                         if(psys_check_enabled(ob, psys)) {
2478                                                 /* check use of dupli objects here */
2479                                                 if(psys->part && psys->part->draw_as == PART_DRAW_REND &&
2480                                                         ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob)
2481                                                         || (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
2482                                                         ob->transflag |= OB_DUPLIPARTS;
2483
2484                                                 particle_system_update(scene, ob, psys);
2485                                                 psys= psys->next;
2486                                         }
2487                                         else if(psys->flag & PSYS_DELETE) {
2488                                                 tpsys=psys->next;
2489                                                 BLI_remlink(&ob->particlesystem, psys);
2490                                                 psys_free(ob,psys);
2491                                                 psys= tpsys;
2492                                         }
2493                                         else
2494                                                 psys= psys->next;
2495                                 }
2496
2497                                 if(G.rendering && ob->transflag & OB_DUPLIPARTS) {
2498                                         /* this is to make sure we get render level duplis in groups:
2499                                          * the derivedmesh must be created before init_render_mesh,
2500                                          * since object_duplilist does dupliparticles before that */
2501                                         dm = mesh_create_derived_render(scene, ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
2502                                         dm->release(dm);
2503
2504                                         for(psys=ob->particlesystem.first; psys; psys=psys->next)
2505                                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
2506                                 }
2507                         }
2508                 }
2509
2510                 /* the no-group proxy case, we call update */
2511                 if(ob->proxy && ob->proxy_group==NULL) {
2512                         /* set pointer in library proxy target, for copying, but restore it */
2513                         ob->proxy->proxy_from= ob;
2514                         // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2515                         object_handle_update(scene, ob->proxy);
2516                 }
2517         
2518                 ob->recalc &= ~OB_RECALC;
2519         }
2520
2521         /* the case when this is a group proxy, object_update is called in group.c */
2522         if(ob->proxy) {
2523                 ob->proxy->proxy_from= ob;
2524                 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2525         }
2526 }
2527
2528 float give_timeoffset(Object *ob) {
2529         if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
2530                 return ob->sf + give_timeoffset(ob->parent);
2531         } else {
2532                 return ob->sf;
2533         }
2534 }
2535
2536 int give_obdata_texspace(Object *ob, short **texflag, float **loc, float **size, float **rot) {
2537         
2538         if (ob->data==NULL)
2539                 return 0;
2540         
2541         switch (GS(((ID *)ob->data)->name)) {
2542         case ID_ME:
2543         {
2544                 Mesh *me= ob->data;
2545                 if (texflag)    *texflag = &me->texflag;
2546                 if (loc)                *loc = me->loc;
2547                 if (size)               *size = me->size;
2548                 if (rot)                *rot = me->rot;
2549                 break;
2550         }
2551         case ID_CU:
2552         {
2553                 Curve *cu= ob->data;
2554                 if (texflag)    *texflag = &cu->texflag;
2555                 if (loc)                *loc = cu->loc;
2556                 if (size)               *size = cu->size;
2557                 if (rot)                *rot = cu->rot;
2558                 break;
2559         }
2560         case ID_MB:
2561         {
2562                 MetaBall *mb= ob->data;
2563                 if (texflag)    *texflag = &mb->texflag;
2564                 if (loc)                *loc = mb->loc;
2565                 if (size)               *size = mb->size;
2566                 if (rot)                *rot = mb->rot;
2567                 break;
2568         }
2569         default:
2570                 return 0;
2571         }
2572         return 1;
2573 }
2574
2575 /*
2576  * Test a bounding box for ray intersection
2577  * assumes the ray is already local to the boundbox space
2578  */
2579 int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
2580 {
2581         static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
2582                                                                                   {3, 2, 6}, {3, 6, 7},
2583                                                                                   {1, 2, 6}, {1, 6, 5}, 
2584                                                                                   {5, 6, 7}, {4, 5, 7},
2585                                                                                   {0, 3, 7}, {0, 4, 7},
2586                                                                                   {0, 1, 5}, {0, 4, 5}};
2587         int result = 0;
2588         int i;
2589         
2590         for (i = 0; i < 12 && result == 0; i++)
2591         {
2592                 float lambda;
2593                 int v1, v2, v3;
2594                 v1 = triangle_indexes[i][0];
2595                 v2 = triangle_indexes[i][1];
2596                 v3 = triangle_indexes[i][2];
2597                 result = isect_ray_tri_v3(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
2598         }
2599         
2600         return result;
2601 }
2602
2603 static int pc_cmp(void *a, void *b)
2604 {
2605         LinkData *ad = a, *bd = b;
2606         if(GET_INT_FROM_POINTER(ad->data) > GET_INT_FROM_POINTER(bd->data))
2607                 return 1;
2608         else return 0;
2609 }
2610
2611 int object_insert_ptcache(Object *ob) 
2612 {
2613         LinkData *link = NULL;
2614         int i = 0;
2615
2616         BLI_sortlist(&ob->pc_ids, pc_cmp);
2617
2618         for(link=ob->pc_ids.first, i = 0; link; link=link->next, i++) 
2619         {
2620                 int index = GET_INT_FROM_POINTER(link->data);
2621
2622                 if(i < index)
2623                         break;
2624         }
2625
2626         link = MEM_callocN(sizeof(LinkData), "PCLink");
2627         link->data = SET_INT_IN_POINTER(i);
2628         BLI_addtail(&ob->pc_ids, link);
2629
2630         return i;
2631 }
2632
2633 #if 0
2634 static int pc_findindex(ListBase *listbase, int index)
2635 {
2636         LinkData *link= NULL;
2637         int number= 0;
2638         
2639         if (listbase == NULL) return -1;
2640         
2641         link= listbase->first;
2642         while (link) {
2643                 if ((int)link->data == index)
2644                         return number;
2645                 
2646                 number++;
2647                 link= link->next;
2648         }
2649         
2650         return -1;
2651 }
2652
2653 void object_delete_ptcache(Object *ob, int index) 
2654 {
2655         int list_index = pc_findindex(&ob->pc_ids, index);
2656         LinkData *link = BLI_findlink(&ob->pc_ids, list_index);
2657         BLI_freelinkN(&ob->pc_ids, link);
2658 }
2659 #endif
2660
2661 /* shape key utility function */
2662
2663 /************************* Mesh ************************/
2664 static KeyBlock *insert_meshkey(Scene *scene, Object *ob, char *name, int from_mix)
2665 {
2666         Mesh *me= ob->data;
2667         Key *key= me->key;
2668         KeyBlock *kb;
2669         int newkey= 0;
2670
2671         if(key == NULL) {
2672                 key= me->key= add_key((ID *)me);
2673                 key->type= KEY_RELATIVE;
2674                 newkey= 1;
2675         }
2676
2677         kb= add_keyblock(key, name);
2678
2679         if(newkey || from_mix==FALSE) {
2680                 /* create from mesh */
2681                 mesh_to_key(me, kb);
2682         }
2683         else {
2684                 /* copy from current values */
2685                 kb->data= do_ob_key(scene, ob);
2686                 kb->totelem= me->totvert;
2687         }
2688
2689         return kb;
2690 }
2691 /************************* Lattice ************************/
2692 static KeyBlock *insert_lattkey(Scene *scene, Object *ob, char *name, int from_mix)
2693 {
2694         Lattice *lt= ob->data;
2695         Key *key= lt->key;
2696         KeyBlock *kb;
2697         int newkey= 0;
2698
2699         if(key==NULL) {
2700                 key= lt->key= add_key( (ID *)lt);
2701                 key->type= KEY_RELATIVE;
2702                 newkey= 1;
2703         }
2704
2705         kb= add_keyblock(key, name);
2706
2707         if(newkey || from_mix==FALSE) {
2708                 /* create from lattice */
2709                 latt_to_key(lt, kb);
2710         }
2711         else {
2712                 /* copy from current values */
2713                 kb->totelem= lt->pntsu*lt->pntsv*lt->pntsw;
2714                 kb->data= do_ob_key(scene, ob);
2715         }
2716
2717         return kb;
2718 }
2719 /************************* Curve ************************/
2720 static KeyBlock *insert_curvekey(Scene *scene, Object *ob, char *name, int from_mix)
2721 {
2722         Curve *cu= ob->data;
2723         Key *key= cu->key;
2724         KeyBlock *kb;
2725         ListBase *lb= (cu->editnurb)? cu->editnurb: &cu->nurb;
2726         int newkey= 0;
2727
2728         if(key==NULL) {
2729                 key= cu->key= add_key( (ID *)cu);
2730                 key->type = KEY_RELATIVE;
2731                 newkey= 1;
2732         }
2733
2734         kb= add_keyblock(key, name);
2735
2736         if(newkey || from_mix==FALSE) {
2737                 /* create from curve */
2738                 curve_to_key(cu, kb, lb);
2739         }
2740         else {
2741                 /* copy from current values */
2742                 kb->totelem= count_curveverts(lb);
2743                 kb->data= do_ob_key(scene, ob);
2744         }
2745
2746         return kb;
2747 }
2748
2749 KeyBlock *object_insert_shape_key(Scene *scene, Object *ob, char *name, int from_mix)
2750 {
2751         if(ob->type==OB_MESH)                                    return insert_meshkey(scene, ob, name, from_mix);
2752         else if ELEM(ob->type, OB_CURVE, OB_SURF)return insert_curvekey(scene, ob, name, from_mix);
2753         else if(ob->type==OB_LATTICE)                    return insert_lattkey(scene, ob, name, from_mix);
2754         else                                                                     return NULL;
2755 }
2756