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