== Constraints System ==
[blender.git] / source / blender / blenkernel / intern / object.c
1 /* object.c
2  *
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <string.h>
36 #include <math.h>
37 #include <stdio.h>                      
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_action_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_camera_types.h"
48 #include "DNA_constraint_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_ipo_types.h"
52 #include "DNA_lamp_types.h"
53 #include "DNA_lattice_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_mesh_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_oops_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_screen_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_texture_types.h"
67 #include "DNA_userdef_types.h"
68 #include "DNA_view3d_types.h"
69 #include "DNA_world_types.h"
70
71 #include "BKE_armature.h"
72 #include "BKE_action.h"
73 #include "BKE_deform.h"
74 #include "BKE_DerivedMesh.h"
75 #include "BKE_nla.h"
76
77 #include "BLI_blenlib.h"
78 #include "BLI_arithb.h"
79 #include "BLI_editVert.h"
80
81 #include "BKE_utildefines.h"
82 #include "BKE_bad_level_calls.h"
83
84 #include "BKE_main.h"
85 #include "BKE_global.h"
86
87 #include "BKE_anim.h"
88 #include "BKE_blender.h"
89 #include "BKE_constraint.h"
90 #include "BKE_curve.h"
91 #include "BKE_displist.h"
92 #include "BKE_effect.h"
93 #include "BKE_group.h"
94 #include "BKE_icons.h"
95 #include "BKE_ipo.h"
96 #include "BKE_key.h"
97 #include "BKE_lattice.h"
98 #include "BKE_library.h"
99 #include "BKE_mesh.h"
100 #include "BKE_mball.h"
101 #include "BKE_modifier.h"
102 #include "BKE_object.h"
103 #include "BKE_property.h"
104 #include "BKE_sca.h"
105 #include "BKE_scene.h"
106 #include "BKE_screen.h"
107 #include "BKE_softbody.h"
108
109 #include "LBM_fluidsim.h"
110
111 #include "BPY_extern.h"
112
113 /* Local function protos */
114 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
115
116 float originmat[3][3];  /* after where_is_object(), can be used in other functions (bad!) */
117 Object workob;
118
119 void clear_workob(void)
120 {
121         memset(&workob, 0, sizeof(Object));
122         
123         workob.size[0]= workob.size[1]= workob.size[2]= 1.0;
124         
125 }
126
127 void copy_baseflags()
128 {
129         Base *base= G.scene->base.first;
130         
131         while(base) {
132                 base->object->flag= base->flag;
133                 base= base->next;
134         }
135 }
136
137 void copy_objectflags()
138 {
139         Base *base= G.scene->base.first;
140         
141         while(base) {
142                 base->flag= base->object->flag;
143                 base= base->next;
144         }
145 }
146
147 void update_base_layer(Object *ob)
148 {
149         Base *base= G.scene->base.first;
150
151         while (base) {
152                 if (base->object == ob) base->lay= ob->lay;
153                 base= base->next;
154         }
155 }
156
157 void object_free_modifiers(Object *ob)
158 {
159         while (ob->modifiers.first) {
160                 ModifierData *md = ob->modifiers.first;
161
162                 BLI_remlink(&ob->modifiers, md);
163
164                 modifier_free(md);
165         }
166 }
167
168 /* here we will collect all local displist stuff */
169 /* also (ab)used in depsgraph */
170 void object_free_display(Object *ob)
171 {
172         if(ob->derivedDeform) {
173                 ob->derivedDeform->needsFree = 1;
174                 ob->derivedDeform->release(ob->derivedDeform);
175                 ob->derivedDeform= NULL;
176         }
177         if(ob->derivedFinal) {
178                 ob->derivedFinal->needsFree = 1;
179                 ob->derivedFinal->release(ob->derivedFinal);
180                 ob->derivedFinal= NULL;
181         }
182         
183         freedisplist(&ob->disp);
184 }
185
186 /* do not free object itself */
187 void free_object(Object *ob)
188 {
189         int a;
190         
191         object_free_display(ob);
192         
193         /* disconnect specific data */
194         if(ob->data) {
195                 ID *id= ob->data;
196                 id->us--;
197                 if(id->us==0) {
198                         if(ob->type==OB_MESH) unlink_mesh(ob->data);
199                         else if(ob->type==OB_CURVE) unlink_curve(ob->data);
200                         else if(ob->type==OB_MBALL) unlink_mball(ob->data);
201                 }
202                 ob->data= 0;
203         }
204         
205         for(a=0; a<ob->totcol; a++) {
206                 if(ob->mat[a]) ob->mat[a]->id.us--;
207         }
208         if(ob->mat) MEM_freeN(ob->mat);
209         ob->mat= 0;
210         if(ob->bb) MEM_freeN(ob->bb); 
211         ob->bb= 0;
212         if(ob->path) free_path(ob->path); 
213         ob->path= 0;
214         if(ob->ipo) ob->ipo->id.us--;
215         if(ob->action) ob->action->id.us--;
216         if(ob->dup_group) ob->dup_group->id.us--;
217         if(ob->defbase.first)
218                 BLI_freelistN(&ob->defbase);
219         if(ob->pose) {
220                 free_pose_channels(ob->pose);
221                 MEM_freeN(ob->pose);
222         }
223         free_effects(&ob->effect);
224         BLI_freelistN(&ob->network);
225         free_properties(&ob->prop);
226         object_free_modifiers(ob);
227         
228         free_sensors(&ob->sensors);
229         free_controllers(&ob->controllers);
230         free_actuators(&ob->actuators);
231         
232         free_constraints(&ob->constraints);
233         free_constraint_channels(&ob->constraintChannels);
234         free_nlastrips(&ob->nlastrips);
235         
236         BPY_free_scriptlink(&ob->scriptlink);
237         
238         if(ob->pd) MEM_freeN(ob->pd);
239         if(ob->soft) sbFree(ob->soft);
240         if(ob->fluidsimSettings) fluidsimSettingsFree(ob->fluidsimSettings);
241 }
242
243 static void unlink_object__unlinkModifierLinks(void *userData, Object *ob, Object **obpoin)
244 {
245         Object *unlinkOb = userData;
246
247         if (*obpoin==unlinkOb) {
248                 *obpoin = NULL;
249                 ob->recalc |= OB_RECALC;
250         }
251 }
252 void unlink_object(Object *ob)
253 {
254         Object *obt;
255         Material *mat;
256         World *wrld;
257         bScreen *sc;
258         Scene *sce;
259         Curve *cu;
260         Tex *tex;
261         Ipo *ipo;
262         Group *group;
263         bConstraint *con;
264         bActionStrip *strip;
265         int a;
266         char *str;
267         
268         unlink_controllers(&ob->controllers);
269         unlink_actuators(&ob->actuators);
270         
271         /* check all objects: parents en bevels and fields, also from libraries */
272         obt= G.main->object.first;
273         while(obt) {
274                 if(obt->proxy==ob)
275                         obt->proxy= NULL;
276                 if(obt->proxy_from==ob) {
277                         obt->proxy_from= NULL;
278                         obt->recalc |= OB_RECALC_OB;
279                 }
280                 if(obt->proxy_group==ob)
281                         obt->proxy_group= NULL;
282                 
283                 if(obt->parent==ob) {
284                         obt->parent= NULL;
285                         obt->recalc |= OB_RECALC;
286                 }
287                 
288                 if(obt->track==ob) {
289                         obt->track= NULL;
290                         obt->recalc |= OB_RECALC_OB;
291                 }
292                 
293                 modifiers_foreachObjectLink(obt, unlink_object__unlinkModifierLinks, ob);
294                 
295                 if ELEM(obt->type, OB_CURVE, OB_FONT) {
296                         cu= obt->data;
297
298                         if(cu->bevobj==ob) {
299                                 cu->bevobj= NULL;
300                                 obt->recalc |= OB_RECALC;
301                         }
302                         if(cu->taperobj==ob) {
303                                 cu->taperobj= NULL;
304                                 obt->recalc |= OB_RECALC;
305                         }
306                         if(cu->textoncurve==ob) {
307                                 cu->textoncurve= NULL;
308                                 obt->recalc |= OB_RECALC;
309                         }
310                 }
311                 else if(obt->type==OB_ARMATURE && obt->pose) {
312                         bPoseChannel *pchan;
313                         for(pchan= obt->pose->chanbase.first; pchan; pchan= pchan->next) {
314                                 for (con = pchan->constraints.first; con; con=con->next) {
315                                         if(ob==get_constraint_target(con, &str)) {
316                                                 set_constraint_target(con, NULL, NULL);
317                                                 obt->recalc |= OB_RECALC_DATA;
318                                         }
319                                 }
320                                 if(pchan->custom==ob)
321                                         pchan->custom= NULL;
322                         }
323                 }
324                 
325                 sca_remove_ob_poin(obt, ob);
326                 
327                 for (con = obt->constraints.first; con; con=con->next) {
328                         if(ob==get_constraint_target(con, &str)) {
329                                 set_constraint_target(con, NULL, NULL);
330                                 obt->recalc |= OB_RECALC_OB;
331                         }
332                 }
333                 
334                 /* object is deflector or field */
335                 if(ob->pd) {
336                         if(give_parteff(obt))
337                                 obt->recalc |= OB_RECALC_DATA;
338                         else if(obt->soft)
339                                 obt->recalc |= OB_RECALC_DATA;
340                 }
341                 
342                 /* strips */
343                 for(strip= obt->nlastrips.first; strip; strip= strip->next) {
344                         if(strip->object==ob)
345                                 strip->object= NULL;
346                         
347                         if(strip->modifiers.first) {
348                                 bActionModifier *amod;
349                                 for(amod= strip->modifiers.first; amod; amod= amod->next)
350                                         if(amod->ob==ob)
351                                                 amod->ob= NULL;
352                         }
353                 }
354
355                 obt= obt->id.next;
356         }
357         
358         /* materials */
359         mat= G.main->mat.first;
360         while(mat) {
361         
362                 for(a=0; a<MAX_MTEX; a++) {
363                         if(mat->mtex[a] && ob==mat->mtex[a]->object) {
364                                 /* actually, test for lib here... to do */
365                                 mat->mtex[a]->object= NULL;
366                         }
367                 }
368
369                 mat= mat->id.next;
370         }
371         
372         /* textures */
373         tex= G.main->tex.first;
374         while(tex) {
375                 if(tex->env) {
376                         if(tex->env->object == ob) tex->env->object= NULL;
377                 }
378                 tex= tex->id.next;
379         }
380         
381         /* mballs */
382         if(ob->type==OB_MBALL) {
383                 obt= find_basis_mball(ob);
384                 if(obt) freedisplist(&obt->disp);
385         }
386         
387         /* worlds */
388         wrld= G.main->world.first;
389         while(wrld) {
390                 if(wrld->id.lib==NULL) {
391                         for(a=0; a<MAX_MTEX; a++) {
392                                 if(wrld->mtex[a] && ob==wrld->mtex[a]->object)
393                                         wrld->mtex[a]->object= NULL;
394                         }
395                 }
396                 
397                 wrld= wrld->id.next;
398         }
399                 
400         /* scenes */
401         sce= G.main->scene.first;
402         while(sce) {
403                 if(sce->id.lib==NULL) {
404                         if(sce->camera==ob) sce->camera= NULL;
405                 }
406                 sce= sce->id.next;
407         }
408         /* ipos */
409         ipo= G.main->ipo.first;
410         while(ipo) {
411                 if(ipo->id.lib==NULL) {
412                         IpoCurve *icu;
413                         for(icu= ipo->curve.first; icu; icu= icu->next) {
414                                 if(icu->driver && icu->driver->ob==ob)
415                                         icu->driver->ob= NULL;
416                         }
417                 }
418                 ipo= ipo->id.next;
419         }
420         
421         /* screens */
422         sc= G.main->screen.first;
423         while(sc) {
424                 ScrArea *sa= sc->areabase.first;
425                 while(sa) {
426                         SpaceLink *sl;
427
428                         for (sl= sa->spacedata.first; sl; sl= sl->next) {
429                                 if(sl->spacetype==SPACE_VIEW3D) {
430                                         View3D *v3d= (View3D*) sl;
431
432                                         if(v3d->camera==ob) {
433                                                 v3d->camera= NULL;
434                                                 if(v3d->persp>1) v3d->persp= 1;
435                                         }
436                                         if(v3d->localvd && v3d->localvd->camera==ob ) {
437                                                 v3d->localvd->camera= NULL;
438                                                 if(v3d->localvd->persp>1) v3d->localvd->persp= 1;
439                                         }
440                                 }
441                                 else if(sl->spacetype==SPACE_IPO) {
442                                         SpaceIpo *sipo= (SpaceIpo *)sl;
443                                         if(sipo->from == (ID *)ob) sipo->from= NULL;
444                                 }
445                                 else if(sl->spacetype==SPACE_OOPS) {
446                                         SpaceOops *so= (SpaceOops *)sl;
447                                         Oops *oops;
448
449                                         oops= so->oops.first;
450                                         while(oops) {
451                                                 if(oops->id==(ID *)ob) oops->id= NULL;
452                                                 oops= oops->next;
453                                         }
454                                         if(so->treestore) {
455                                                 TreeStoreElem *tselem= so->treestore->data;
456                                                 int a;
457                                                 for(a=0; a<so->treestore->usedelem; a++, tselem++) {
458                                                         if(tselem->id==(ID *)ob) tselem->id= NULL;
459                                                 }
460                                         }
461                                         so->lockpoin= NULL;
462                                 }
463                         }
464
465                         sa= sa->next;
466                 }
467                 sc= sc->id.next;
468         }
469
470         /* groups */
471         group= G.main->group.first;
472         while(group) {
473                 rem_from_group(group, ob);
474                 group= group->id.next;
475         }
476 }
477
478 int exist_object(Object *obtest)
479 {
480         Object *ob;
481         
482         if(obtest==NULL) return 0;
483         
484         ob= G.main->object.first;
485         while(ob) {
486                 if(ob==obtest) return 1;
487                 ob= ob->id.next;
488         }
489         return 0;
490 }
491
492 void *add_camera(char *name)
493 {
494         Camera *cam;
495         
496         cam=  alloc_libblock(&G.main->camera, ID_CA, name);
497
498         cam->lens= 35.0f;
499         cam->angle= 49.14f;
500         cam->clipsta= 0.1f;
501         cam->clipend= 100.0f;
502         cam->drawsize= 0.5f;
503         cam->ortho_scale= 6.0;
504         cam->flag |= CAM_SHOWTITLESAFE;
505         cam->passepartalpha = 0.2f;
506         
507         return cam;
508 }
509
510 Camera *copy_camera(Camera *cam)
511 {
512         Camera *camn;
513         
514         camn= copy_libblock(cam);
515         id_us_plus((ID *)camn->ipo);
516
517         BPY_copy_scriptlink(&camn->scriptlink);
518         
519         return camn;
520 }
521
522
523
524 void make_local_camera(Camera *cam)
525 {
526         Object *ob;
527         Camera *camn;
528         int local=0, lib=0;
529
530         /* - only lib users: do nothing
531             * - only local users: set flag
532             * - mixed: make copy
533             */
534         
535         if(cam->id.lib==0) return;
536         if(cam->id.us==1) {
537                 cam->id.lib= 0;
538                 cam->id.flag= LIB_LOCAL;
539                 new_id(0, (ID *)cam, 0);
540                 return;
541         }
542         
543         ob= G.main->object.first;
544         while(ob) {
545                 if(ob->data==cam) {
546                         if(ob->id.lib) lib= 1;
547                         else local= 1;
548                 }
549                 ob= ob->id.next;
550         }
551         
552         if(local && lib==0) {
553                 cam->id.lib= 0;
554                 cam->id.flag= LIB_LOCAL;
555                 new_id(0, (ID *)cam, 0);
556         }
557         else if(local && lib) {
558                 camn= copy_camera(cam);
559                 camn->id.us= 0;
560                 
561                 ob= G.main->object.first;
562                 while(ob) {
563                         if(ob->data==cam) {
564                                 
565                                 if(ob->id.lib==0) {
566                                         ob->data= camn;
567                                         camn->id.us++;
568                                         cam->id.us--;
569                                 }
570                         }
571                         ob= ob->id.next;
572                 }
573         }
574 }
575
576
577
578 void *add_lamp(char *name)
579 {
580         Lamp *la;
581         
582         la=  alloc_libblock(&G.main->lamp, ID_LA, name);
583         
584         la->r= la->g= la->b= la->k= 1.0;
585         la->haint= la->energy= 1.0;
586         la->dist= 20.0;
587         la->spotsize= 45.0;
588         la->spotblend= 0.15;
589         la->att2= 1.0;
590         la->mode= LA_SHAD_BUF;
591         la->bufsize= 512;
592         la->clipsta= 0.5;
593         la->clipend= 40.0;
594         la->shadspotsize= 45.0;
595         la->samp= 3;
596         la->bias= 1.0;
597         la->soft= 3.0;
598         la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 
599         la->area_size=la->area_sizey=la->area_sizez= 1.0; 
600         la->buffers= 1;
601         la->buftype= LA_SHADBUF_HALFWAY;
602         
603         return la;
604 }
605
606 Lamp *copy_lamp(Lamp *la)
607 {
608         Lamp *lan;
609         int a;
610         
611         lan= copy_libblock(la);
612
613         for(a=0; a<MAX_MTEX; a++) {
614                 if(lan->mtex[a]) {
615                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
616                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
617                         id_us_plus((ID *)lan->mtex[a]->tex);
618                 }
619         }
620         
621         id_us_plus((ID *)lan->ipo);
622
623         BPY_copy_scriptlink(&la->scriptlink);
624         
625         return lan;
626 }
627
628 void make_local_lamp(Lamp *la)
629 {
630         Object *ob;
631         Lamp *lan;
632         int local=0, lib=0;
633
634         /* - only lib users: do nothing
635             * - only local users: set flag
636             * - mixed: make copy
637             */
638         
639         if(la->id.lib==0) return;
640         if(la->id.us==1) {
641                 la->id.lib= 0;
642                 la->id.flag= LIB_LOCAL;
643                 new_id(0, (ID *)la, 0);
644                 return;
645         }
646         
647         ob= G.main->object.first;
648         while(ob) {
649                 if(ob->data==la) {
650                         if(ob->id.lib) lib= 1;
651                         else local= 1;
652                 }
653                 ob= ob->id.next;
654         }
655         
656         if(local && lib==0) {
657                 la->id.lib= 0;
658                 la->id.flag= LIB_LOCAL;
659                 new_id(0, (ID *)la, 0);
660         }
661         else if(local && lib) {
662                 lan= copy_lamp(la);
663                 lan->id.us= 0;
664                 
665                 ob= G.main->object.first;
666                 while(ob) {
667                         if(ob->data==la) {
668                                 
669                                 if(ob->id.lib==0) {
670                                         ob->data= lan;
671                                         lan->id.us++;
672                                         la->id.us--;
673                                 }
674                         }
675                         ob= ob->id.next;
676                 }
677         }
678 }
679
680 void free_camera(Camera *ca)
681 {
682         BPY_free_scriptlink(&ca->scriptlink);
683 }
684
685 void free_lamp(Lamp *la)
686 {
687         MTex *mtex;
688         int a;
689
690         /* scriptlinks */
691                 
692         BPY_free_scriptlink(&la->scriptlink);
693         
694         for(a=0; a<MAX_MTEX; a++) {
695                 mtex= la->mtex[a];
696                 if(mtex && mtex->tex) mtex->tex->id.us--;
697                 if(mtex) MEM_freeN(mtex);
698         }
699         la->ipo= 0;
700
701         BKE_icon_delete(&la->id);
702         la->id.icon_id = 0;
703 }
704
705 void *add_wave()
706 {
707         return 0;
708 }
709
710
711 /* *************************************************** */
712
713 static void *add_obdata_from_type(int type)
714 {
715         switch (type) {
716         case OB_MESH: G.totmesh++; return add_mesh("Mesh");
717         case OB_CURVE: G.totcurve++; return add_curve("Curve", OB_CURVE);
718         case OB_SURF: G.totcurve++; return add_curve("Surf", OB_SURF);
719         case OB_FONT: return add_curve("Text", OB_FONT);
720         case OB_MBALL: return add_mball("Meta");
721         case OB_CAMERA: return add_camera("Camera");
722         case OB_LAMP: G.totlamp++; return add_lamp("Lamp");
723         case OB_LATTICE: return add_lattice("Lattice");
724         case OB_WAVE: return add_wave();
725         case OB_ARMATURE: return add_armature("Armature");
726         case OB_EMPTY: return NULL;
727         default:
728                 printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
729                 return NULL;
730         }
731 }
732
733 static char *get_obdata_defname(int type)
734 {
735         switch (type) {
736         case OB_MESH: return "Mesh";
737         case OB_CURVE: return "Curve";
738         case OB_SURF: return "Surf";
739         case OB_FONT: return "Font";
740         case OB_MBALL: return "Mball";
741         case OB_CAMERA: return "Camera";
742         case OB_LAMP: return "Lamp";
743         case OB_LATTICE: return "Lattice";
744         case OB_WAVE: return "Wave";
745         case OB_ARMATURE: return "Armature";
746         case OB_EMPTY: return "Empty";
747         default:
748                 printf("get_obdata_defname: Internal error, bad type: %d\n", type);
749                 return "Empty";
750         }
751 }
752
753 /* more general add: creates minimum required data, but without vertices etc. */
754 Object *add_only_object(int type, char *name)
755 {
756         Object *ob;
757
758         ob= alloc_libblock(&G.main->object, ID_OB, name);
759         G.totobj++;
760
761         /* default object vars */
762         ob->type= type;
763         /* ob->transflag= OB_QUAT; */
764
765         QuatOne(ob->quat);
766         QuatOne(ob->dquat);
767
768         ob->col[0]= ob->col[1]= ob->col[2]= 0.0;
769         ob->col[3]= 1.0;
770
771         ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0;
772         ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
773         ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
774
775         Mat4One(ob->constinv);
776         Mat4One(ob->parentinv);
777         Mat4One(ob->obmat);
778         ob->dt= OB_SHADED;
779         if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
780         ob->empty_drawtype= OB_ARROWS;
781         ob->empty_drawsize= 1.0;
782
783         if(type==OB_CAMERA || type==OB_LAMP) {
784                 ob->trackflag= OB_NEGZ;
785                 ob->upflag= OB_POSY;
786         }
787         else {
788                 ob->trackflag= OB_POSY;
789                 ob->upflag= OB_POSZ;
790         }
791         ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
792         ob->ipowin= ID_OB;      /* the ipowin shown */
793         ob->dupon= 1; ob->dupoff= 0;
794         ob->dupsta= 1; ob->dupend= 100;
795
796         /* Game engine defaults*/
797         ob->mass= ob->inertia= 1.0f;
798         ob->formfactor= 0.4f;
799         ob->damping= 0.04f;
800         ob->rdamping= 0.1f;
801         ob->anisotropicFriction[0] = 1.0f;
802         ob->anisotropicFriction[1] = 1.0f;
803         ob->anisotropicFriction[2] = 1.0f;
804         ob->gameflag= OB_PROP;
805
806         /* NT fluid sim defaults */
807         ob->fluidsimFlag = 0;
808         ob->fluidsimSettings = NULL;
809
810         return ob;
811 }
812
813 /* general add: to G.scene, with layer from area and default name */
814 /* creates minimum required data, but without vertices etc. */
815 Object *add_object(int type)
816 {
817         Object *ob;
818         Base *base;
819         char name[32];
820
821         strcpy(name, get_obdata_defname(type));
822         ob = add_only_object(type, name);
823
824         ob->data= add_obdata_from_type(type);
825
826         ob->lay= G.scene->lay;
827
828         base= scene_add_base(G.scene, ob);
829         scene_select_base(G.scene, base);
830         ob->recalc |= OB_RECALC;
831
832         return ob;
833 }
834
835 void base_init_from_view3d(Base *base, View3D *v3d)
836 {
837         Object *ob= base->object;
838         
839         if (!v3d) {
840                 /* no 3d view, this wont happen often */
841                 base->lay = 1;
842                 VECCOPY(ob->loc, G.scene->cursor);
843                 
844                 /* return now because v3d->viewquat isnt available */
845                 return;
846         } else if (v3d->localview) {
847                 base->lay= ob->lay= v3d->layact + v3d->lay;
848                 VECCOPY(ob->loc, v3d->cursor);
849         } else {
850                 base->lay= ob->lay= v3d->layact;
851                 VECCOPY(ob->loc, G.scene->cursor);
852         }
853
854         v3d->viewquat[0]= -v3d->viewquat[0];
855         if (ob->transflag & OB_QUAT) {
856                 QUATCOPY(ob->quat, v3d->viewquat);
857         } else {
858                 QuatToEul(v3d->viewquat, ob->rot);
859         }
860         v3d->viewquat[0]= -v3d->viewquat[0];
861 }
862
863 SoftBody *copy_softbody(SoftBody *sb)
864 {
865         SoftBody *sbn;
866         
867         if (sb==NULL) return(NULL);
868         
869         sbn= MEM_dupallocN(sb);
870         sbn->totspring= sbn->totpoint= 0;
871         sbn->bpoint= NULL;
872         sbn->bspring= NULL;
873         sbn->ctime= 0.0f;
874         
875         sbn->keys= NULL;
876         sbn->totkey= sbn->totpointkey= 0;
877         
878         sbn->scratch= NULL;
879         return sbn;
880 }
881
882 static void copy_object_pose(Object *obn, Object *ob)
883 {
884         bPoseChannel *chan;
885         
886         copy_pose(&obn->pose, ob->pose, 1);
887
888         for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
889                 bConstraint *con;
890                 char *str;
891                 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
892                 for(con= chan->constraints.first; con; con= con->next) {
893                         if(ob==get_constraint_target(con, &str))
894                                 set_constraint_target(con, obn, NULL);
895                 }
896         }
897 }
898
899 Object *copy_object(Object *ob)
900 {
901         Object *obn;
902         ModifierData *md;
903         int a;
904
905         obn= copy_libblock(ob);
906         
907         if(ob->totcol) {
908                 obn->mat= MEM_dupallocN(ob->mat);
909         }
910         
911         if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
912         obn->path= NULL;
913         obn->flag &= ~OB_FROMGROUP;
914         
915         copy_effects(&obn->effect, &ob->effect);
916         obn->modifiers.first = obn->modifiers.last= NULL;
917         
918         for (md=ob->modifiers.first; md; md=md->next) {
919                 ModifierData *nmd = modifier_new(md->type);
920                 modifier_copyData(md, nmd);
921                 BLI_addtail(&obn->modifiers, nmd);
922         }
923         
924         obn->network.first= obn->network.last= 0;
925         
926         BPY_copy_scriptlink(&ob->scriptlink);
927         
928         copy_properties(&obn->prop, &ob->prop);
929         copy_sensors(&obn->sensors, &ob->sensors);
930         copy_controllers(&obn->controllers, &ob->controllers);
931         copy_actuators(&obn->actuators, &ob->actuators);
932         
933         if(ob->pose) {
934                 copy_object_pose(obn, ob);
935                 /* backwards compat... non-armatures can get poses in older files? */
936                 if(ob->type==OB_ARMATURE)
937                         armature_rebuild_pose(obn, obn->data);
938         }
939         copy_defgroups(&obn->defbase, &ob->defbase);
940         copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
941         copy_constraints (&obn->constraints, &ob->constraints);
942
943         clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
944
945         /* increase user numbers */
946         id_us_plus((ID *)obn->data);
947         id_us_plus((ID *)obn->ipo);
948         id_us_plus((ID *)obn->action);
949         id_us_plus((ID *)obn->dup_group);
950
951         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
952         
953         obn->disp.first= obn->disp.last= NULL;
954         
955         if(ob->pd) obn->pd= MEM_dupallocN(ob->pd);
956         obn->soft= copy_softbody(ob->soft);
957
958         /* NT copy fluid sim setting memory */
959         if(obn->fluidsimSettings) {
960                 obn->fluidsimSettings = fluidsimSettingsCopy(ob->fluidsimSettings);
961                 /* copying might fail... */
962                 if(obn->fluidsimSettings) {
963                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
964                 }
965         }
966         
967         obn->derivedDeform = NULL;
968         obn->derivedFinal = NULL;
969
970 #ifdef WITH_VERSE
971         obn->vnode = NULL;
972 #endif
973
974
975         return obn;
976 }
977
978 void expand_local_object(Object *ob)
979 {
980         bActionStrip *strip;
981         int a;
982         
983         id_lib_extern((ID *)ob->action);
984         id_lib_extern((ID *)ob->ipo);
985         id_lib_extern((ID *)ob->data);
986         
987         for(a=0; a<ob->totcol; a++) {
988                 id_lib_extern((ID *)ob->mat[a]);
989         }
990         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
991                 id_lib_extern((ID *)strip->act);
992         }
993
994 }
995
996 void make_local_object(Object *ob)
997 {
998         Object *obn;
999         Scene *sce;
1000         Base *base;
1001         int local=0, lib=0;
1002
1003         /* - only lib users: do nothing
1004             * - only local users: set flag
1005             * - mixed: make copy
1006             */
1007         
1008         if(ob->id.lib==NULL) return;
1009         
1010         ob->proxy= ob->proxy_from= NULL;
1011         
1012         if(ob->id.us==1) {
1013                 ob->id.lib= NULL;
1014                 ob->id.flag= LIB_LOCAL;
1015                 new_id(0, (ID *)ob, 0);
1016
1017         }
1018         else {
1019                 sce= G.main->scene.first;
1020                 while(sce) {
1021                         base= sce->base.first;
1022                         while(base) {
1023                                 if(base->object==ob) {
1024                                         if(sce->id.lib) lib++;
1025                                         else local++;
1026                                         break;
1027                                 }
1028                                 base= base->next;
1029                         }
1030                         sce= sce->id.next;
1031                 }
1032                 
1033                 if(local && lib==0) {
1034                         ob->id.lib= 0;
1035                         ob->id.flag= LIB_LOCAL;
1036                         new_id(0, (ID *)ob, 0);
1037                 }
1038                 else if(local && lib) {
1039                         obn= copy_object(ob);
1040                         obn->id.us= 0;
1041                         
1042                         sce= G.main->scene.first;
1043                         while(sce) {
1044                                 if(sce->id.lib==0) {
1045                                         base= sce->base.first;
1046                                         while(base) {
1047                                                 if(base->object==ob) {
1048                                                         base->object= obn;
1049                                                         obn->id.us++;
1050                                                         ob->id.us--;
1051                                                 }
1052                                                 base= base->next;
1053                                         }
1054                                 }
1055                                 sce= sce->id.next;
1056                         }
1057                 }
1058         }
1059         
1060         expand_local_object(ob);
1061 }
1062
1063 /* *************** PROXY **************** */
1064
1065 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1066 /*             local_object->proxy == pointer to library object, saved in files and read */
1067 /*             local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1068
1069 void object_make_proxy(Object *ob, Object *target, Object *gob)
1070 {
1071         /* paranoia checks */
1072         if(ob->id.lib || target->id.lib==NULL) {
1073                 printf("cannot make proxy\n");
1074                 return;
1075         }
1076         
1077         ob->proxy= target;
1078         ob->proxy_group= gob;
1079         id_lib_extern(&target->id);
1080         
1081         ob->recalc= target->recalc= OB_RECALC;
1082         
1083         /* copy transform */
1084         if(gob) {
1085                 VECCOPY(ob->loc, gob->loc);
1086                 VECCOPY(ob->rot, gob->rot);
1087                 VECCOPY(ob->size, gob->size);
1088         }
1089         else {
1090                 VECCOPY(ob->loc, target->loc);
1091                 VECCOPY(ob->rot, target->rot);
1092                 VECCOPY(ob->size, target->size);
1093         }
1094         
1095         ob->parent= target->parent;     /* libdata */
1096         Mat4CpyMat4(ob->parentinv, target->parentinv);
1097         ob->ipo= target->ipo;           /* libdata */
1098         
1099         /* skip constraints, constraintchannels, nla? */
1100         
1101         ob->type= target->type;
1102         ob->data= target->data;
1103         id_us_plus((ID *)ob->data);             /* ensures lib data becomes LIB_EXTERN */
1104         
1105         /* type conversions */
1106         if(target->type == OB_ARMATURE) {
1107                 copy_object_pose(ob, target);   /* data copy, object pointers in constraints */
1108                 rest_pose(ob->pose);                    /* clear all transforms in channels */
1109                 armature_rebuild_pose(ob, ob->data);    /* set all internal links */
1110         }
1111 }
1112
1113
1114 /* *************** CALC ****************** */
1115
1116 /* there is also a timing calculation in drawobject() */
1117
1118 float bluroffs= 0.0f, fieldoffs= 0.0f;
1119 int no_speed_curve= 0;
1120
1121 /* ugly calls from render */
1122 void set_mblur_offs(float blur)
1123 {
1124         bluroffs= blur;
1125 }
1126
1127 void set_field_offs(float field)
1128 {
1129         fieldoffs= field;
1130 }
1131
1132 void disable_speed_curve(int val)
1133 {
1134         no_speed_curve= val;
1135 }
1136
1137 /* ob can be NULL */
1138 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
1139 {
1140         /* returns float ( see frame_to_float in ipo.c) */
1141
1142         cfra+= bluroffs+fieldoffs;
1143
1144         /* global time */
1145         cfra*= G.scene->r.framelen;     
1146         
1147         if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
1148         
1149         /* ofset frames */
1150         if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
1151                 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
1152         }
1153         
1154         cfra-= ofs;
1155
1156         return cfra;
1157 }
1158
1159 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1160 {
1161         float smat[3][3], vec[3];
1162         float rmat[3][3];
1163         float q1[4];
1164         
1165         /* size */
1166         if(ob->ipo) {
1167                 vec[0]= ob->size[0]+ob->dsize[0];
1168                 vec[1]= ob->size[1]+ob->dsize[1];
1169                 vec[2]= ob->size[2]+ob->dsize[2];
1170                 SizeToMat3(vec, smat);
1171         }
1172         else {
1173                 SizeToMat3(ob->size, smat);
1174         }
1175
1176         /* rot */
1177         if(ob->transflag & OB_QUAT) {
1178                 if(ob->ipo) {
1179                         QuatMul(q1, ob->quat, ob->dquat);
1180                         QuatToMat3(q1, rmat);
1181                 }
1182                 else {
1183                         QuatToMat3(ob->quat, rmat);
1184                 }
1185         }
1186         else {
1187                 if(ob->ipo) {
1188                         vec[0]= ob->rot[0]+ob->drot[0];
1189                         vec[1]= ob->rot[1]+ob->drot[1];
1190                         vec[2]= ob->rot[2]+ob->drot[2];
1191                         EulToMat3(vec, rmat);
1192                 }
1193                 else {
1194                         EulToMat3(ob->rot, rmat);
1195                 }
1196         }
1197         Mat3MulMat3(mat, rmat, smat);
1198 }
1199
1200 void object_to_mat4(Object *ob, float mat[][4])
1201 {
1202         float tmat[3][3];
1203         
1204         object_to_mat3(ob, tmat);
1205         
1206         Mat4CpyMat3(mat, tmat);
1207         
1208         VECCOPY(mat[3], ob->loc);
1209         if(ob->ipo) {
1210                 mat[3][0]+= ob->dloc[0];
1211                 mat[3][1]+= ob->dloc[1];
1212                 mat[3][2]+= ob->dloc[2];
1213         }
1214 }
1215
1216 int enable_cu_speed= 1;
1217
1218 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1219 {
1220         Curve *cu;
1221         float q[4], vec[4], dir[3], *quat, x1, ctime;
1222         float timeoffs= 0.0;
1223         
1224         Mat4One(mat);
1225         
1226         cu= par->data;
1227         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1228                 makeDispListCurveTypes(par, 0);
1229         if(cu->path==NULL) return;
1230         
1231         /* exception, timeoffset is regarded as distance offset */
1232         if(cu->flag & CU_OFFS_PATHDIST) {
1233                 SWAP(float, timeoffs, ob->sf);
1234         }
1235         
1236         /* catch exceptions: feature for nla stride editing */
1237         if(ob->ipoflag & OB_DISABLE_PATH) {
1238                 ctime= 0.0f;
1239         }
1240         /* catch exceptions: curve paths used as a duplicator */
1241         else if(enable_cu_speed) {
1242                 ctime= bsystem_time(ob, par, (float)G.scene->r.cfra, 0.0);
1243                 
1244                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1245                         ctime /= cu->pathlen;
1246                         CLAMP(ctime, 0.0, 1.0);
1247                 }
1248         }
1249         else {
1250                 ctime= G.scene->r.cfra - ob->sf;
1251                 ctime /= cu->pathlen;
1252                 
1253                 CLAMP(ctime, 0.0, 1.0);
1254         }
1255         
1256         /* time calculus is correct, now apply distance offset */
1257         if(cu->flag & CU_OFFS_PATHDIST) {
1258                 ctime += timeoffs/cu->path->totdist;
1259
1260                 /* restore */
1261                 SWAP(float, timeoffs, ob->sf);
1262         }
1263         
1264         
1265         /* vec: 4 items! */
1266         if( where_on_path(par, ctime, vec, dir) ) {
1267
1268                 if(cu->flag & CU_FOLLOW) {
1269                         quat= vectoquat(dir, ob->trackflag, ob->upflag);
1270                         
1271                         /* the tilt */
1272                         Normalize(dir);
1273                         q[0]= (float)cos(0.5*vec[3]);
1274                         x1= (float)sin(0.5*vec[3]);
1275                         q[1]= -x1*dir[0];
1276                         q[2]= -x1*dir[1];
1277                         q[3]= -x1*dir[2];
1278                         QuatMul(quat, q, quat);
1279                         
1280                         QuatToMat4(quat, mat);
1281                 }
1282                 
1283                 VECCOPY(mat[3], vec);
1284                 
1285         }
1286 }
1287
1288 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1289 {       
1290         bPoseChannel *pchan;
1291         bArmature *arm;
1292         float vec[3];
1293         
1294         arm=get_armature(par);
1295         if (!arm) {
1296                 Mat4One(mat);
1297                 return;
1298         }
1299         
1300         /* Make sure the bone is still valid */
1301         pchan= get_pose_channel(par->pose, ob->parsubstr);
1302         if (!pchan){
1303                 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1304                 Mat4One(mat);
1305                 return;
1306         }
1307
1308         /* get bone transform */
1309         Mat4CpyMat4(mat, pchan->pose_mat);
1310
1311         /* but for backwards compatibility, the child has to move to the tail */
1312         VECCOPY(vec, mat[1]);
1313         VecMulf(vec, pchan->bone->length);
1314         VecAddf(mat[3], mat[3], vec);
1315 }
1316
1317 static void give_parvert(Object *par, int nr, float *vec)
1318 {
1319         int a, count;
1320         
1321         vec[0]=vec[1]=vec[2]= 0.0f;
1322         
1323         if(par->type==OB_MESH) {
1324                 if(G.obedit && (par->data==G.obedit->data)) {
1325                         EditMesh *em = G.editMesh;
1326                         EditVert *eve;
1327                         
1328                         for(eve= em->verts.first; eve; eve= eve->next) {
1329                                 if(eve->keyindex==nr) {
1330                                         memcpy(vec, eve->co, 12);
1331                                         break;
1332                                 }
1333                         }
1334                 }
1335                 else {
1336                         DerivedMesh *dm = par->derivedFinal;
1337                         
1338                         if(dm) {
1339                                 int i, count = 0, numVerts = dm->getNumVerts(dm);
1340                                 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1341                                 float co[3];
1342
1343                                 /* get the average of all verts with (original index == nr) */
1344                                 for(i = 0; i < numVerts; ++i, ++index) {
1345                                         if(*index == nr) {
1346                                                 dm->getVertCo(dm, i, co);
1347                                                 VecAddf(vec, vec, co);
1348                                                 count++;
1349                                         }
1350                                 }
1351
1352                                 if(count > 0) {
1353                                         VecMulf(vec, 1.0f / count);
1354                                 } else {
1355                                         dm->getVertCo(dm, 0, vec);
1356                                 }
1357                         }
1358                 }
1359         }
1360         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1361                 Nurb *nu;
1362                 Curve *cu;
1363                 BPoint *bp;
1364                 BezTriple *bezt;
1365                 
1366                 cu= par->data;
1367                 nu= cu->nurb.first;
1368                 if(par==G.obedit) nu= editNurb.first;
1369                 
1370                 count= 0;
1371                 while(nu) {
1372                         if((nu->type & 7)==CU_BEZIER) {
1373                                 bezt= nu->bezt;
1374                                 a= nu->pntsu;
1375                                 while(a--) {
1376                                         if(count==nr) {
1377                                                 VECCOPY(vec, bezt->vec[1]);
1378                                                 break;
1379                                         }
1380                                         count++;
1381                                         bezt++;
1382                                 }
1383                         }
1384                         else {
1385                                 bp= nu->bp;
1386                                 a= nu->pntsu*nu->pntsv;
1387                                 while(a--) {
1388                                         if(count==nr) {
1389                                                 memcpy(vec, bp->vec, 12);
1390                                                 break;
1391                                         }
1392                                         count++;
1393                                         bp++;
1394                                 }
1395                         }
1396                         nu= nu->next;
1397                 }
1398
1399         }
1400         else if(par->type==OB_LATTICE) {
1401                 Lattice *latt= par->data;
1402                 BPoint *bp;
1403                 DispList *dl = find_displist(&par->disp, DL_VERTS);
1404                 float *co = dl?dl->verts:NULL;
1405                 
1406                 if(par==G.obedit) latt= editLatt;
1407                 
1408                 a= latt->pntsu*latt->pntsv*latt->pntsw;
1409                 count= 0;
1410                 bp= latt->def;
1411                 while(a--) {
1412                         if(count==nr) {
1413                                 if(co)
1414                                         memcpy(vec, co, 3*sizeof(float));
1415                                 else
1416                                         memcpy(vec, bp->vec, 3*sizeof(float));
1417                                 break;
1418                         }
1419                         count++;
1420                         if(co) co+= 3;
1421                         else bp++;
1422                 }
1423         }
1424 }
1425
1426 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1427 {
1428         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1429
1430         /* in local ob space */
1431         Mat4One(mat);
1432         
1433         if ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE) {
1434                 
1435                 give_parvert(par, ob->par1, v1);
1436                 give_parvert(par, ob->par2, v2);
1437                 give_parvert(par, ob->par3, v3);
1438                                 
1439                 triatoquat(v1, v2, v3, q);
1440                 QuatToMat3(q, cmat);
1441                 Mat4CpyMat3(mat, cmat);
1442                 
1443                 if(ob->type==OB_CURVE) {
1444                         VECCOPY(mat[3], v1);
1445                 }
1446                 else {
1447                         VecAddf(mat[3], v1, v2);
1448                         VecAddf(mat[3], mat[3], v3);
1449                         VecMulf(mat[3], 0.3333333f);
1450                 }
1451         }
1452 }
1453
1454 static int no_parent_ipo=0;
1455 void set_no_parent_ipo(int val)
1456 {
1457         no_parent_ipo= val;
1458 }
1459
1460 static int during_script_flag=0;
1461 void disable_where_script(short on)
1462 {
1463         during_script_flag= on;
1464 }
1465
1466 int during_script(void) {
1467         return during_script_flag;
1468 }
1469
1470 static int during_scriptlink_flag=0;
1471 void disable_where_scriptlink(short on)
1472 {
1473         during_scriptlink_flag= on;
1474 }
1475
1476 int during_scriptlink(void) {
1477         return during_scriptlink_flag;
1478 }
1479
1480 void where_is_object_time(Object *ob, float ctime)
1481 {
1482         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1483         float stime, fac1, fac2, vec[3];
1484         int a;
1485         int pop; 
1486         
1487         /* new version: correct parent+vertexparent and track+parent */
1488         /* this one only calculates direct attached parent and track */
1489         /* is faster, but should keep track of timeoffs */
1490         
1491         if(ob==NULL) return;
1492         
1493         /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1494         stime= bsystem_time(ob, 0, ctime, 0.0);
1495         if(stime != ob->ctime) {
1496                 
1497                 ob->ctime= stime;
1498                 
1499                 if(ob->ipo) {
1500                         calc_ipo(ob->ipo, stime);
1501                         execute_ipo((ID *)ob, ob->ipo);
1502                 }
1503                 else 
1504                         do_all_object_actions(ob);
1505                 
1506                 /* do constraint ipos ..., note it needs stime */
1507                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime);
1508         }
1509         else {
1510                 /* but, the drivers have to be done */
1511                 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo, stime);
1512         }
1513         
1514         if(ob->parent) {
1515                 Object *par= ob->parent;
1516                 
1517                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1518                 
1519                 /* hurms, code below conflicts with depgraph... (ton) */
1520                 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1521                 pop= 0;
1522                 if(no_parent_ipo==0 && stime != par->ctime) {
1523                 
1524                         // only for ipo systems? 
1525                         pushdata(par, sizeof(Object));
1526                         pop= 1;
1527                         
1528                         if(par->proxy_from);    // was a copied matrix, no where_is! bad...
1529                         else where_is_object_time(par, ctime);
1530                 }
1531                 
1532                 solve_parenting(ob, par, ob->obmat, slowmat, 0);
1533
1534                 if(pop) {
1535                         poplast(par);
1536                 }
1537                 
1538                 if(ob->partype & PARSLOW) {
1539                         // include framerate
1540
1541                         fac1= (1.0f/(1.0f+ fabs(ob->sf)));
1542                         if(fac1>=1.0) return;
1543                         fac2= 1.0f-fac1;
1544                         
1545                         fp1= ob->obmat[0];
1546                         fp2= slowmat[0];
1547                         for(a=0; a<16; a++, fp1++, fp2++) {
1548                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1549                         }
1550                 }
1551         
1552         }
1553         else {
1554                 object_to_mat4(ob, ob->obmat);
1555         }
1556
1557         /* Handle tracking */
1558         if(ob->track) {
1559                 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1560                 solve_tracking (ob, ob->track->obmat);
1561                 
1562         }
1563
1564         /* solve constraints */
1565         if (ob->constraints.first) {
1566                 bConstraintOb *cob;
1567                 
1568                 cob= constraints_make_evalob(ob, NULL, TARGET_OBJECT);
1569                 
1570                 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
1571                 solve_constraints (&ob->constraints, cob, ctime);
1572                 
1573                 constraints_clear_evalob(cob);
1574         }
1575         
1576         if(ob->scriptlink.totscript && !during_script()) {
1577                 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1578         }
1579         
1580         /* set negative scale flag in object */
1581         Crossf(vec, ob->obmat[0], ob->obmat[1]);
1582         if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1583         else ob->transflag &= ~OB_NEG_SCALE;
1584 }
1585
1586 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
1587 {
1588         float totmat[4][4];
1589         float tmat[4][4];
1590         float locmat[4][4];
1591         float vec[3];
1592         int ok;
1593
1594         object_to_mat4(ob, locmat);
1595         
1596         if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, obmat);
1597         
1598
1599         switch(ob->partype & PARTYPE) {
1600         case PAROBJECT:
1601                 ok= 0;
1602                 if(par->type==OB_CURVE) {
1603                         if( ((Curve *)par->data)->flag & CU_PATH ) {
1604                                 ob_parcurve(ob, par, tmat);
1605                                 ok= 1;
1606                         }
1607                 }
1608                 
1609                 if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
1610                         NULL, NULL, NULL, NULL, NULL, NULL);
1611                 else Mat4CpyMat4(totmat, par->obmat);
1612                 
1613                 break;
1614         case PARBONE:
1615                 ob_parbone(ob, par, tmat);
1616                 Mat4MulSerie(totmat, par->obmat, tmat,         
1617                         NULL, NULL, NULL, NULL, NULL, NULL);
1618                 break;
1619                 
1620         case PARVERT1:
1621                 Mat4One(totmat);
1622                 if (simul){
1623                         VECCOPY(totmat[3], par->obmat[3]);
1624                 }
1625                 else{
1626                         give_parvert(par, ob->par1, vec);
1627                         VecMat4MulVecfl(totmat[3], par->obmat, vec);
1628                 }
1629                 break;
1630         case PARVERT3:
1631                 ob_parvert3(ob, par, tmat);
1632                 
1633                 Mat4MulSerie(totmat, par->obmat, tmat,         
1634                         NULL, NULL, NULL, NULL, NULL, NULL);
1635                 break;
1636                 
1637         case PARSKEL:
1638                 Mat4CpyMat4(totmat, par->obmat);
1639                 break;
1640         }
1641         
1642         // total 
1643         Mat4MulSerie(tmat, totmat, ob->parentinv,         
1644                 NULL, NULL, NULL, NULL, NULL, NULL);
1645         Mat4MulSerie(obmat, tmat, locmat,         
1646                 NULL, NULL, NULL, NULL, NULL, NULL);
1647         
1648         if (simul) {
1649
1650         }
1651         else{
1652                 // external usable originmat 
1653                 Mat3CpyMat4(originmat, tmat);
1654                 
1655                 // origin, voor help line
1656                 if( (ob->partype & 15)==PARSKEL ) {
1657                         VECCOPY(ob->orig, par->obmat[3]);
1658                 }
1659                 else {
1660                         VECCOPY(ob->orig, totmat[3]);
1661                 }
1662         }
1663
1664 }
1665 void solve_tracking (Object *ob, float targetmat[][4])
1666 {
1667         float *quat;
1668         float vec[3];
1669         float totmat[3][3];
1670         float tmat[4][4];
1671         
1672         VecSubf(vec, ob->obmat[3], targetmat[3]);
1673         quat= vectoquat(vec, ob->trackflag, ob->upflag);
1674         QuatToMat3(quat, totmat);
1675         
1676         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
1677                 /* 'temporal' : clear parent info */
1678                 object_to_mat4(ob, tmat);
1679                 tmat[0][3]= ob->obmat[0][3];
1680                 tmat[1][3]= ob->obmat[1][3];
1681                 tmat[2][3]= ob->obmat[2][3];
1682                 tmat[3][0]= ob->obmat[3][0];
1683                 tmat[3][1]= ob->obmat[3][1];
1684                 tmat[3][2]= ob->obmat[3][2];
1685                 tmat[3][3]= ob->obmat[3][3];
1686         }
1687         else Mat4CpyMat4(tmat, ob->obmat);
1688         
1689         Mat4MulMat34(ob->obmat, totmat, tmat);
1690
1691 }
1692
1693 void where_is_object(Object *ob)
1694 {
1695         where_is_object_time(ob, (float)G.scene->r.cfra);
1696 }
1697
1698
1699 void where_is_object_simul(Object *ob)
1700 /* was written for the old game engine (until 2.04) */
1701 /* It seems that this function is only called
1702 for a lamp that is the child of another object */
1703 {
1704         Object *par;
1705         Ipo *ipo;
1706         float *fp1, *fp2;
1707         float slowmat[4][4];
1708         float fac1, fac2;
1709         int a;
1710         
1711         /* NO TIMEOFFS */
1712         
1713         /* no ipo! (because of dloc and realtime-ipos) */
1714         ipo= ob->ipo;
1715         ob->ipo= NULL;
1716
1717         if(ob->parent) {
1718                 par= ob->parent;
1719                 
1720                 solve_parenting(ob, par, ob->obmat, slowmat, 1);
1721
1722                 if(ob->partype & PARSLOW) {
1723
1724                         fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1725                         fac2= 1.0f-fac1;
1726                         fp1= ob->obmat[0];
1727                         fp2= slowmat[0];
1728                         for(a=0; a<16; a++, fp1++, fp2++) {
1729                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1730                         }
1731                 }
1732                 
1733         }
1734         else {
1735                 object_to_mat4(ob, ob->obmat);
1736         }
1737         
1738         if(ob->track) 
1739                 solve_tracking(ob, ob->track->obmat);
1740
1741         /* solve constraints */
1742         if (ob->constraints.first) {
1743                 bConstraintOb *cob;
1744                 
1745                 cob= constraints_make_evalob(ob, NULL, TARGET_OBJECT);
1746                 solve_constraints (&ob->constraints, cob, G.scene->r.cfra);
1747                 constraints_clear_evalob(cob);
1748         }
1749         
1750         /*  WATCH IT!!! */
1751         ob->ipo= ipo;
1752 }
1753
1754 /* for calculation of the inverse parent transform, only used for editor */
1755 void what_does_parent(Object *ob)
1756 {
1757
1758         clear_workob();
1759         Mat4One(workob.obmat);
1760         Mat4One(workob.parentinv);
1761         Mat4One(workob.constinv);
1762         workob.parent= ob->parent;
1763         workob.track= ob->track;
1764
1765         workob.trackflag= ob->trackflag;
1766         workob.upflag= ob->upflag;
1767         
1768         workob.partype= ob->partype;
1769         workob.par1= ob->par1;
1770         workob.par2= ob->par2;
1771         workob.par3= ob->par3;
1772
1773         workob.constraints.first = ob->constraints.first;
1774         workob.constraints.last = ob->constraints.last;
1775
1776         strcpy (workob.parsubstr, ob->parsubstr); 
1777
1778         where_is_object(&workob);
1779 }
1780
1781 BoundBox *unit_boundbox()
1782 {
1783         BoundBox *bb;
1784         float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
1785
1786         bb= MEM_mallocN(sizeof(BoundBox), "bb");
1787         boundbox_set_from_min_max(bb, min, max);
1788         
1789         return bb;
1790 }
1791
1792 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
1793 {
1794         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
1795         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
1796         
1797         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
1798         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
1799
1800         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
1801         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
1802 }
1803
1804 BoundBox *object_get_boundbox(Object *ob)
1805 {
1806         BoundBox *bb= NULL;
1807         
1808         if(ob->type==OB_MESH) {
1809                 bb = mesh_get_bb(ob->data);
1810         }
1811         else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
1812                 bb= ( (Curve *)ob->data )->bb;
1813         }
1814         else if(ob->type==OB_MBALL) {
1815                 bb= ob->bb;
1816         }
1817         return bb;
1818 }
1819
1820 /* used to temporally disable/enable boundbox */
1821 void object_boundbox_flag(Object *ob, int flag, int set)
1822 {
1823         BoundBox *bb= object_get_boundbox(ob);
1824         if(bb) {
1825                 if(set) bb->flag |= flag;
1826                 else bb->flag &= ~flag;
1827         }
1828 }
1829
1830 void minmax_object(Object *ob, float *min, float *max)
1831 {
1832         BoundBox bb;
1833         Mesh *me;
1834         Curve *cu;
1835         float vec[3];
1836         int a;
1837         
1838         switch(ob->type) {
1839                 
1840         case OB_CURVE:
1841         case OB_FONT:
1842         case OB_SURF:
1843                 cu= ob->data;
1844                 
1845                 if(cu->bb==NULL) tex_space_curve(cu);
1846                 bb= *(cu->bb);
1847                 
1848                 for(a=0; a<8; a++) {
1849                         Mat4MulVecfl(ob->obmat, bb.vec[a]);
1850                         DO_MINMAX(bb.vec[a], min, max);
1851                 }
1852                 break;
1853         case OB_ARMATURE:
1854                 if(ob->pose) {
1855                         bPoseChannel *pchan;
1856                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1857                                 VECCOPY(vec, pchan->pose_head);
1858                                 Mat4MulVecfl(ob->obmat, vec);
1859                                 DO_MINMAX(vec, min, max);
1860                                 VECCOPY(vec, pchan->pose_tail);
1861                                 Mat4MulVecfl(ob->obmat, vec);
1862                                 DO_MINMAX(vec, min, max);
1863                         }
1864                         break;
1865                 }
1866                 /* no break, get_mesh will give NULL and it passes on to default */
1867         case OB_MESH:
1868                 me= get_mesh(ob);
1869                 
1870                 if(me) {
1871                         bb = *mesh_get_bb(me);
1872                         
1873                         for(a=0; a<8; a++) {
1874                                 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1875                                 DO_MINMAX(bb.vec[a], min, max);
1876                         }
1877                 }
1878                 if(min[0] < max[0] ) break;
1879                 
1880                 /* else here no break!!!, mesh can be zero sized */
1881                 
1882         default:
1883                 DO_MINMAX(ob->obmat[3], min, max);
1884
1885                 VECCOPY(vec, ob->obmat[3]);
1886                 VecAddf(vec, vec, ob->size);
1887                 DO_MINMAX(vec, min, max);
1888
1889                 VECCOPY(vec, ob->obmat[3]);
1890                 VecSubf(vec, vec, ob->size);
1891                 DO_MINMAX(vec, min, max);
1892                 break;
1893         }
1894 }
1895
1896 /* TODO - use dupli objects bounding boxes */
1897 void minmax_object_duplis(Object *ob, float *min, float *max)
1898 {
1899         if ((ob->transflag & OB_DUPLI)==0) {
1900                 return;
1901         } else {
1902                 ListBase *lb;
1903                 DupliObject *dob;
1904                 
1905                 lb= object_duplilist(G.scene, ob);
1906                 for(dob= lb->first; dob; dob= dob->next) {
1907                         if(dob->no_draw);
1908                         else {
1909                                 /* should really use bound box of dup object */
1910                                 DO_MINMAX(dob->mat[3], min, max);
1911                         }
1912                 }
1913                 free_object_duplilist(lb);      /* does restore */
1914         }
1915 }
1916
1917
1918 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
1919 /*           local_object->proxy      == pointer to library object, saved in files and read */
1920
1921 /* function below is polluted with proxy exceptions, cleanup will follow! */
1922
1923 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
1924 /* requires flags to be set! */
1925 void object_handle_update(Object *ob)
1926 {
1927         if(ob->recalc & OB_RECALC) {
1928                 
1929                 if(ob->recalc & OB_RECALC_OB) {
1930                         
1931                         // printf("recalcob %s\n", ob->id.name+2);
1932                         
1933                         /* handle proxy copy for target */
1934                         if(ob->id.lib && ob->proxy_from) {
1935                                 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
1936                                 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
1937                                         Object *obg= ob->proxy_from->proxy_group;
1938                                         Mat4Invert(obg->imat, obg->obmat);
1939                                         Mat4MulMat4(ob->obmat, ob->proxy_from->obmat, obg->imat);
1940                                 }
1941                                 else
1942                                         Mat4CpyMat4(ob->obmat, ob->proxy_from->obmat);
1943                         }
1944                         else
1945                                 where_is_object(ob);
1946                 }
1947                 
1948                 if(ob->recalc & OB_RECALC_DATA) {
1949                         
1950                         // printf("recalcdata %s\n", ob->id.name+2);
1951                         
1952                         /* includes all keys and modifiers */
1953                         if(ob->type==OB_MESH) {
1954                                 makeDerivedMesh(ob, get_viewedit_datamask());
1955                         }
1956                         else if(ob->type==OB_MBALL) {
1957                                 makeDispListMBall(ob);
1958                         } 
1959                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1960                                 makeDispListCurveTypes(ob, 0);
1961                         }
1962                         else if(ob->type==OB_LATTICE) {
1963                                 lattice_calc_modifiers(ob);
1964                         }
1965                         else if(ob->type==OB_ARMATURE) {
1966                                 /* this happens for reading old files and to match library armatures with poses */
1967                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
1968                                         armature_rebuild_pose(ob, ob->data);
1969                                 
1970                                 if(ob->id.lib && ob->proxy_from) {
1971                                         copy_pose_result(ob->pose, ob->proxy_from->pose);
1972                                         // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
1973                                 }
1974                                 else {
1975                                         do_all_pose_actions(ob);
1976                                         where_is_pose(ob);
1977                                 }
1978                         }
1979                 }
1980         
1981                 /* the no-group proxy case, we call update */
1982                 if(ob->proxy && ob->proxy_group==NULL) {
1983                         /* set pointer in library proxy target, for copying, but restore it */
1984                         ob->proxy->proxy_from= ob;
1985                         // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
1986                         object_handle_update(ob->proxy);
1987                 }
1988         
1989                 ob->recalc &= ~OB_RECALC;
1990         }
1991
1992         /* the case when this is a group proxy, object_update is called in group.c */
1993         if(ob->proxy) {
1994                 ob->proxy->proxy_from= ob;
1995                 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
1996         }
1997 }