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