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