missing decref.. win Window.RedrawAll() pointed out by ken
[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->parentinv);
776         Mat4One(ob->obmat);
777         ob->dt= OB_SHADED;
778         if(U.flag & USER_MAT_ON_OB) ob->colbits= -1;
779         ob->empty_drawtype= OB_ARROWS;
780         ob->empty_drawsize= 1.0;
781
782         if(type==OB_CAMERA || type==OB_LAMP) {
783                 ob->trackflag= OB_NEGZ;
784                 ob->upflag= OB_POSY;
785         }
786         else {
787                 ob->trackflag= OB_POSY;
788                 ob->upflag= OB_POSZ;
789         }
790         ob->ipoflag = OB_OFFS_OB+OB_OFFS_PARENT;
791         ob->ipowin= ID_OB;      /* the ipowin shown */
792         ob->dupon= 1; ob->dupoff= 0;
793         ob->dupsta= 1; ob->dupend= 100;
794
795         /* Game engine defaults*/
796         ob->mass= ob->inertia= 1.0f;
797         ob->formfactor= 0.4f;
798         ob->damping= 0.04f;
799         ob->rdamping= 0.1f;
800         ob->anisotropicFriction[0] = 1.0f;
801         ob->anisotropicFriction[1] = 1.0f;
802         ob->anisotropicFriction[2] = 1.0f;
803         ob->gameflag= OB_PROP;
804
805         /* NT fluid sim defaults */
806         ob->fluidsimFlag = 0;
807         ob->fluidsimSettings = NULL;
808
809         return ob;
810 }
811
812 /* general add: to G.scene, with layer from area and default name */
813 /* creates minimum required data, but without vertices etc. */
814 Object *add_object(int type)
815 {
816         Object *ob;
817         Base *base;
818         char name[32];
819
820         strcpy(name, get_obdata_defname(type));
821         ob = add_only_object(type, name);
822
823         ob->data= add_obdata_from_type(type);
824
825         ob->lay= G.scene->lay;
826
827         base= scene_add_base(G.scene, ob);
828         scene_select_base(G.scene, base);
829         ob->recalc |= OB_RECALC;
830
831         return ob;
832 }
833
834 void base_init_from_view3d(Base *base, View3D *v3d)
835 {
836         Object *ob= base->object;
837         
838         if (!v3d) {
839                 /* no 3d view, this wont happen often */
840                 base->lay = 1;
841                 VECCOPY(ob->loc, G.scene->cursor);
842                 
843                 /* return now because v3d->viewquat isnt available */
844                 return;
845         } else if (v3d->localview) {
846                 base->lay= ob->lay= v3d->layact + v3d->lay;
847                 VECCOPY(ob->loc, v3d->cursor);
848         } else {
849                 base->lay= ob->lay= v3d->layact;
850                 VECCOPY(ob->loc, G.scene->cursor);
851         }
852
853         v3d->viewquat[0]= -v3d->viewquat[0];
854         if (ob->transflag & OB_QUAT) {
855                 QUATCOPY(ob->quat, v3d->viewquat);
856         } else {
857                 QuatToEul(v3d->viewquat, ob->rot);
858         }
859         v3d->viewquat[0]= -v3d->viewquat[0];
860 }
861
862 SoftBody *copy_softbody(SoftBody *sb)
863 {
864         SoftBody *sbn;
865         
866         if (sb==NULL) return(NULL);
867         
868         sbn= MEM_dupallocN(sb);
869         sbn->totspring= sbn->totpoint= 0;
870         sbn->bpoint= NULL;
871         sbn->bspring= NULL;
872         sbn->ctime= 0.0f;
873         
874         sbn->keys= NULL;
875         sbn->totkey= sbn->totpointkey= 0;
876         
877         sbn->scratch= NULL;
878         return sbn;
879 }
880
881 static void copy_object_pose(Object *obn, Object *ob)
882 {
883         bPoseChannel *chan;
884         
885         copy_pose(&obn->pose, ob->pose, 1);
886
887         for (chan = obn->pose->chanbase.first; chan; chan=chan->next){
888                 bConstraint *con;
889                 char *str;
890                 chan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
891                 for(con= chan->constraints.first; con; con= con->next) {
892                         if(ob==get_constraint_target(con, &str))
893                                 set_constraint_target(con, obn, NULL);
894                 }
895         }
896 }
897
898 Object *copy_object(Object *ob)
899 {
900         Object *obn;
901         ModifierData *md;
902         int a;
903
904         obn= copy_libblock(ob);
905         
906         if(ob->totcol) {
907                 obn->mat= MEM_dupallocN(ob->mat);
908         }
909         
910         if(ob->bb) obn->bb= MEM_dupallocN(ob->bb);
911         obn->path= NULL;
912         obn->flag &= ~OB_FROMGROUP;
913         
914         copy_effects(&obn->effect, &ob->effect);
915         obn->modifiers.first = obn->modifiers.last= NULL;
916         
917         for (md=ob->modifiers.first; md; md=md->next) {
918                 ModifierData *nmd = modifier_new(md->type);
919                 modifier_copyData(md, nmd);
920                 BLI_addtail(&obn->modifiers, nmd);
921         }
922         
923         obn->network.first= obn->network.last= 0;
924         
925         BPY_copy_scriptlink(&ob->scriptlink);
926         
927         copy_properties(&obn->prop, &ob->prop);
928         copy_sensors(&obn->sensors, &ob->sensors);
929         copy_controllers(&obn->controllers, &ob->controllers);
930         copy_actuators(&obn->actuators, &ob->actuators);
931         
932         if(ob->pose) {
933                 copy_object_pose(obn, ob);
934                 /* backwards compat... non-armatures can get poses in older files? */
935                 if(ob->type==OB_ARMATURE)
936                         armature_rebuild_pose(obn, obn->data);
937         }
938         copy_defgroups(&obn->defbase, &ob->defbase);
939         copy_nlastrips(&obn->nlastrips, &ob->nlastrips);
940         copy_constraints (&obn->constraints, &ob->constraints);
941
942         clone_constraint_channels (&obn->constraintChannels, &ob->constraintChannels);
943
944         /* increase user numbers */
945         id_us_plus((ID *)obn->data);
946         id_us_plus((ID *)obn->ipo);
947         id_us_plus((ID *)obn->action);
948         id_us_plus((ID *)obn->dup_group);
949
950         for(a=0; a<obn->totcol; a++) id_us_plus((ID *)obn->mat[a]);
951         
952         obn->disp.first= obn->disp.last= NULL;
953         
954         if(ob->pd) obn->pd= MEM_dupallocN(ob->pd);
955         obn->soft= copy_softbody(ob->soft);
956
957         /* NT copy fluid sim setting memory */
958         if(obn->fluidsimSettings) {
959                 obn->fluidsimSettings = fluidsimSettingsCopy(ob->fluidsimSettings);
960                 /* copying might fail... */
961                 if(obn->fluidsimSettings) {
962                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
963                 }
964         }
965         
966         obn->derivedDeform = NULL;
967         obn->derivedFinal = NULL;
968
969 #ifdef WITH_VERSE
970         obn->vnode = NULL;
971 #endif
972
973
974         return obn;
975 }
976
977 void expand_local_object(Object *ob)
978 {
979         bActionStrip *strip;
980         int a;
981         
982         id_lib_extern((ID *)ob->action);
983         id_lib_extern((ID *)ob->ipo);
984         id_lib_extern((ID *)ob->data);
985         
986         for(a=0; a<ob->totcol; a++) {
987                 id_lib_extern((ID *)ob->mat[a]);
988         }
989         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
990                 id_lib_extern((ID *)strip->act);
991         }
992
993 }
994
995 void make_local_object(Object *ob)
996 {
997         Object *obn;
998         Scene *sce;
999         Base *base;
1000         int local=0, lib=0;
1001
1002         /* - only lib users: do nothing
1003             * - only local users: set flag
1004             * - mixed: make copy
1005             */
1006         
1007         if(ob->id.lib==NULL) return;
1008         
1009         ob->proxy= ob->proxy_from= NULL;
1010         
1011         if(ob->id.us==1) {
1012                 ob->id.lib= NULL;
1013                 ob->id.flag= LIB_LOCAL;
1014                 new_id(0, (ID *)ob, 0);
1015
1016         }
1017         else {
1018                 sce= G.main->scene.first;
1019                 while(sce) {
1020                         base= sce->base.first;
1021                         while(base) {
1022                                 if(base->object==ob) {
1023                                         if(sce->id.lib) lib++;
1024                                         else local++;
1025                                         break;
1026                                 }
1027                                 base= base->next;
1028                         }
1029                         sce= sce->id.next;
1030                 }
1031                 
1032                 if(local && lib==0) {
1033                         ob->id.lib= 0;
1034                         ob->id.flag= LIB_LOCAL;
1035                         new_id(0, (ID *)ob, 0);
1036                 }
1037                 else if(local && lib) {
1038                         obn= copy_object(ob);
1039                         obn->id.us= 0;
1040                         
1041                         sce= G.main->scene.first;
1042                         while(sce) {
1043                                 if(sce->id.lib==0) {
1044                                         base= sce->base.first;
1045                                         while(base) {
1046                                                 if(base->object==ob) {
1047                                                         base->object= obn;
1048                                                         obn->id.us++;
1049                                                         ob->id.us--;
1050                                                 }
1051                                                 base= base->next;
1052                                         }
1053                                 }
1054                                 sce= sce->id.next;
1055                         }
1056                 }
1057         }
1058         
1059         expand_local_object(ob);
1060 }
1061
1062 /* *************** PROXY **************** */
1063
1064 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1065 /*             local_object->proxy == pointer to library object, saved in files and read */
1066 /*             local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1067
1068 void object_make_proxy(Object *ob, Object *target, Object *gob)
1069 {
1070         /* paranoia checks */
1071         if(ob->id.lib || target->id.lib==NULL) {
1072                 printf("cannot make proxy\n");
1073                 return;
1074         }
1075         
1076         ob->proxy= target;
1077         ob->proxy_group= gob;
1078         id_lib_extern(&target->id);
1079         
1080         ob->recalc= target->recalc= OB_RECALC;
1081         
1082         /* copy transform */
1083         if(gob) {
1084                 VECCOPY(ob->loc, gob->loc);
1085                 VECCOPY(ob->rot, gob->rot);
1086                 VECCOPY(ob->size, gob->size);
1087         }
1088         else {
1089                 VECCOPY(ob->loc, target->loc);
1090                 VECCOPY(ob->rot, target->rot);
1091                 VECCOPY(ob->size, target->size);
1092         }
1093         
1094         ob->parent= target->parent;     /* libdata */
1095         Mat4CpyMat4(ob->parentinv, target->parentinv);
1096         ob->ipo= target->ipo;           /* libdata */
1097         
1098         /* skip constraints, constraintchannels, nla? */
1099         
1100         ob->type= target->type;
1101         ob->data= target->data;
1102         id_us_plus((ID *)ob->data);             /* ensures lib data becomes LIB_EXTERN */
1103         
1104         /* type conversions */
1105         if(target->type == OB_ARMATURE) {
1106                 copy_object_pose(ob, target);   /* data copy, object pointers in constraints */
1107                 rest_pose(ob->pose);                    /* clear all transforms in channels */
1108                 armature_rebuild_pose(ob, ob->data);    /* set all internal links */
1109         }
1110 }
1111
1112
1113 /* *************** CALC ****************** */
1114
1115 /* there is also a timing calculation in drawobject() */
1116
1117 float bluroffs= 0.0f, fieldoffs= 0.0f;
1118 int no_speed_curve= 0;
1119
1120 /* ugly calls from render */
1121 void set_mblur_offs(float blur)
1122 {
1123         bluroffs= blur;
1124 }
1125
1126 void set_field_offs(float field)
1127 {
1128         fieldoffs= field;
1129 }
1130
1131 void disable_speed_curve(int val)
1132 {
1133         no_speed_curve= val;
1134 }
1135
1136 /* ob can be NULL */
1137 float bsystem_time(Object *ob, Object *par, float cfra, float ofs)
1138 {
1139         /* returns float ( see frame_to_float in ipo.c) */
1140
1141         cfra+= bluroffs+fieldoffs;
1142
1143         /* global time */
1144         cfra*= G.scene->r.framelen;     
1145         
1146         if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
1147         
1148         /* ofset frames */
1149         if(ob && (ob->ipoflag & OB_OFFS_PARENT)) {
1150                 if((ob->partype & PARSLOW)==0) cfra-= ob->sf;
1151         }
1152         
1153         cfra-= ofs;
1154
1155         return cfra;
1156 }
1157
1158 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1159 {
1160         float smat[3][3], vec[3];
1161         float rmat[3][3];
1162         float q1[4];
1163         
1164         /* size */
1165         if(ob->ipo) {
1166                 vec[0]= ob->size[0]+ob->dsize[0];
1167                 vec[1]= ob->size[1]+ob->dsize[1];
1168                 vec[2]= ob->size[2]+ob->dsize[2];
1169                 SizeToMat3(vec, smat);
1170         }
1171         else {
1172                 SizeToMat3(ob->size, smat);
1173         }
1174
1175         /* rot */
1176         if(ob->transflag & OB_QUAT) {
1177                 if(ob->ipo) {
1178                         QuatMul(q1, ob->quat, ob->dquat);
1179                         QuatToMat3(q1, rmat);
1180                 }
1181                 else {
1182                         QuatToMat3(ob->quat, rmat);
1183                 }
1184         }
1185         else {
1186                 if(ob->ipo) {
1187                         vec[0]= ob->rot[0]+ob->drot[0];
1188                         vec[1]= ob->rot[1]+ob->drot[1];
1189                         vec[2]= ob->rot[2]+ob->drot[2];
1190                         EulToMat3(vec, rmat);
1191                 }
1192                 else {
1193                         EulToMat3(ob->rot, rmat);
1194                 }
1195         }
1196         Mat3MulMat3(mat, rmat, smat);
1197 }
1198
1199 void object_to_mat4(Object *ob, float mat[][4])
1200 {
1201         float tmat[3][3];
1202         
1203         object_to_mat3(ob, tmat);
1204         
1205         Mat4CpyMat3(mat, tmat);
1206         
1207         VECCOPY(mat[3], ob->loc);
1208         if(ob->ipo) {
1209                 mat[3][0]+= ob->dloc[0];
1210                 mat[3][1]+= ob->dloc[1];
1211                 mat[3][2]+= ob->dloc[2];
1212         }
1213 }
1214
1215 int enable_cu_speed= 1;
1216
1217 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1218 {
1219         Curve *cu;
1220         float q[4], vec[4], dir[3], *quat, x1, ctime;
1221         float timeoffs= 0.0;
1222         
1223         Mat4One(mat);
1224         
1225         cu= par->data;
1226         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1227                 makeDispListCurveTypes(par, 0);
1228         if(cu->path==NULL) return;
1229         
1230         /* exception, timeoffset is regarded as distance offset */
1231         if(cu->flag & CU_OFFS_PATHDIST) {
1232                 SWAP(float, timeoffs, ob->sf);
1233         }
1234         
1235         /* catch exceptions: feature for nla stride editing */
1236         if(ob->ipoflag & OB_DISABLE_PATH) {
1237                 ctime= 0.0f;
1238         }
1239         /* catch exceptions: curve paths used as a duplicator */
1240         else if(enable_cu_speed) {
1241                 ctime= bsystem_time(ob, par, (float)G.scene->r.cfra, 0.0);
1242                 
1243                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1244                         ctime /= cu->pathlen;
1245                         CLAMP(ctime, 0.0, 1.0);
1246                 }
1247         }
1248         else {
1249                 ctime= G.scene->r.cfra - ob->sf;
1250                 ctime /= cu->pathlen;
1251                 
1252                 CLAMP(ctime, 0.0, 1.0);
1253         }
1254         
1255         /* time calculus is correct, now apply distance offset */
1256         if(cu->flag & CU_OFFS_PATHDIST) {
1257                 ctime += timeoffs/cu->path->totdist;
1258
1259                 /* restore */
1260                 SWAP(float, timeoffs, ob->sf);
1261         }
1262         
1263         
1264         /* vec: 4 items! */
1265         if( where_on_path(par, ctime, vec, dir) ) {
1266
1267                 if(cu->flag & CU_FOLLOW) {
1268                         quat= vectoquat(dir, ob->trackflag, ob->upflag);
1269                         
1270                         /* the tilt */
1271                         Normalize(dir);
1272                         q[0]= (float)cos(0.5*vec[3]);
1273                         x1= (float)sin(0.5*vec[3]);
1274                         q[1]= -x1*dir[0];
1275                         q[2]= -x1*dir[1];
1276                         q[3]= -x1*dir[2];
1277                         QuatMul(quat, q, quat);
1278                         
1279                         QuatToMat4(quat, mat);
1280                 }
1281                 
1282                 VECCOPY(mat[3], vec);
1283                 
1284         }
1285 }
1286
1287 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1288 {       
1289         bPoseChannel *pchan;
1290         bArmature *arm;
1291         float vec[3];
1292         
1293         arm=get_armature(par);
1294         if (!arm) {
1295                 Mat4One(mat);
1296                 return;
1297         }
1298         
1299         /* Make sure the bone is still valid */
1300         pchan= get_pose_channel(par->pose, ob->parsubstr);
1301         if (!pchan){
1302                 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1303                 Mat4One(mat);
1304                 return;
1305         }
1306
1307         /* get bone transform */
1308         Mat4CpyMat4(mat, pchan->pose_mat);
1309
1310         /* but for backwards compatibility, the child has to move to the tail */
1311         VECCOPY(vec, mat[1]);
1312         VecMulf(vec, pchan->bone->length);
1313         VecAddf(mat[3], mat[3], vec);
1314 }
1315
1316 static void give_parvert(Object *par, int nr, float *vec)
1317 {
1318         int a, count;
1319         
1320         vec[0]=vec[1]=vec[2]= 0.0f;
1321         
1322         if(par->type==OB_MESH) {
1323                 if(G.obedit && (par->data==G.obedit->data)) {
1324                         EditMesh *em = G.editMesh;
1325                         EditVert *eve;
1326                         
1327                         for(eve= em->verts.first; eve; eve= eve->next) {
1328                                 if(eve->keyindex==nr) {
1329                                         memcpy(vec, eve->co, 12);
1330                                         break;
1331                                 }
1332                         }
1333                 }
1334                 else {
1335                         DerivedMesh *dm = par->derivedFinal;
1336                         
1337                         if(dm) {
1338                                 int i, count = 0, numVerts = dm->getNumVerts(dm);
1339                                 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1340                                 float co[3];
1341
1342                                 /* get the average of all verts with (original index == nr) */
1343                                 for(i = 0; i < numVerts; ++i, ++index) {
1344                                         if(*index == nr) {
1345                                                 dm->getVertCo(dm, i, co);
1346                                                 VecAddf(vec, vec, co);
1347                                                 count++;
1348                                         }
1349                                 }
1350
1351                                 if(count > 0) {
1352                                         VecMulf(vec, 1.0f / count);
1353                                 } else {
1354                                         dm->getVertCo(dm, 0, vec);
1355                                 }
1356                         }
1357                 }
1358         }
1359         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1360                 Nurb *nu;
1361                 Curve *cu;
1362                 BPoint *bp;
1363                 BezTriple *bezt;
1364                 
1365                 cu= par->data;
1366                 nu= cu->nurb.first;
1367                 if(par==G.obedit) nu= editNurb.first;
1368                 
1369                 count= 0;
1370                 while(nu) {
1371                         if((nu->type & 7)==CU_BEZIER) {
1372                                 bezt= nu->bezt;
1373                                 a= nu->pntsu;
1374                                 while(a--) {
1375                                         if(count==nr) {
1376                                                 VECCOPY(vec, bezt->vec[1]);
1377                                                 break;
1378                                         }
1379                                         count++;
1380                                         bezt++;
1381                                 }
1382                         }
1383                         else {
1384                                 bp= nu->bp;
1385                                 a= nu->pntsu*nu->pntsv;
1386                                 while(a--) {
1387                                         if(count==nr) {
1388                                                 memcpy(vec, bp->vec, 12);
1389                                                 break;
1390                                         }
1391                                         count++;
1392                                         bp++;
1393                                 }
1394                         }
1395                         nu= nu->next;
1396                 }
1397
1398         }
1399         else if(par->type==OB_LATTICE) {
1400                 Lattice *latt= par->data;
1401                 BPoint *bp;
1402                 DispList *dl = find_displist(&par->disp, DL_VERTS);
1403                 float *co = dl?dl->verts:NULL;
1404                 
1405                 if(par==G.obedit) latt= editLatt;
1406                 
1407                 a= latt->pntsu*latt->pntsv*latt->pntsw;
1408                 count= 0;
1409                 bp= latt->def;
1410                 while(a--) {
1411                         if(count==nr) {
1412                                 if(co)
1413                                         memcpy(vec, co, 3*sizeof(float));
1414                                 else
1415                                         memcpy(vec, bp->vec, 3*sizeof(float));
1416                                 break;
1417                         }
1418                         count++;
1419                         if(co) co+= 3;
1420                         else bp++;
1421                 }
1422         }
1423 }
1424
1425 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1426 {
1427         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1428
1429         /* in local ob space */
1430         Mat4One(mat);
1431         
1432         if ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE) {
1433                 
1434                 give_parvert(par, ob->par1, v1);
1435                 give_parvert(par, ob->par2, v2);
1436                 give_parvert(par, ob->par3, v3);
1437                                 
1438                 triatoquat(v1, v2, v3, q);
1439                 QuatToMat3(q, cmat);
1440                 Mat4CpyMat3(mat, cmat);
1441                 
1442                 if(ob->type==OB_CURVE) {
1443                         VECCOPY(mat[3], v1);
1444                 }
1445                 else {
1446                         VecAddf(mat[3], v1, v2);
1447                         VecAddf(mat[3], mat[3], v3);
1448                         VecMulf(mat[3], 0.3333333f);
1449                 }
1450         }
1451 }
1452
1453 static int no_parent_ipo=0;
1454 void set_no_parent_ipo(int val)
1455 {
1456         no_parent_ipo= val;
1457 }
1458
1459 static int during_script_flag=0;
1460 void disable_where_script(short on)
1461 {
1462         during_script_flag= on;
1463 }
1464
1465 int during_script(void) {
1466         return during_script_flag;
1467 }
1468
1469 static int during_scriptlink_flag=0;
1470 void disable_where_scriptlink(short on)
1471 {
1472         during_scriptlink_flag= on;
1473 }
1474
1475 int during_scriptlink(void) {
1476         return during_scriptlink_flag;
1477 }
1478
1479 void where_is_object_time(Object *ob, float ctime)
1480 {
1481         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1482         float stime, fac1, fac2, vec[3];
1483         int a;
1484         int pop; 
1485         
1486         /* new version: correct parent+vertexparent and track+parent */
1487         /* this one only calculates direct attached parent and track */
1488         /* is faster, but should keep track of timeoffs */
1489         
1490         if(ob==NULL) return;
1491         
1492         /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1493         stime= bsystem_time(ob, 0, ctime, 0.0);
1494         if(stime != ob->ctime) {
1495                 
1496                 ob->ctime= stime;
1497                 
1498                 if(ob->ipo) {
1499                         calc_ipo(ob->ipo, stime);
1500                         execute_ipo((ID *)ob, ob->ipo);
1501                 }
1502                 else 
1503                         do_all_object_actions(ob);
1504                 
1505                 /* do constraint ipos ..., note it needs stime */
1506                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime);
1507         }
1508         else {
1509                 /* but, the drivers have to be done */
1510                 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo, stime);
1511         }
1512         
1513         if(ob->parent) {
1514                 Object *par= ob->parent;
1515                 
1516                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1517                 
1518                 /* hurms, code below conflicts with depgraph... (ton) */
1519                 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1520                 pop= 0;
1521                 if(no_parent_ipo==0 && stime != par->ctime) {
1522                 
1523                         // only for ipo systems? 
1524                         pushdata(par, sizeof(Object));
1525                         pop= 1;
1526                         
1527                         if(par->proxy_from);    // was a copied matrix, no where_is! bad...
1528                         else where_is_object_time(par, ctime);
1529                 }
1530                 
1531                 solve_parenting(ob, par, ob->obmat, slowmat, 0);
1532
1533                 if(pop) {
1534                         poplast(par);
1535                 }
1536                 
1537                 if(ob->partype & PARSLOW) {
1538                         // include framerate
1539
1540                         fac1= (1.0f/(1.0f+ fabs(ob->sf)));
1541                         if(fac1>=1.0) return;
1542                         fac2= 1.0f-fac1;
1543                         
1544                         fp1= ob->obmat[0];
1545                         fp2= slowmat[0];
1546                         for(a=0; a<16; a++, fp1++, fp2++) {
1547                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1548                         }
1549                 }
1550         
1551         }
1552         else {
1553                 object_to_mat4(ob, ob->obmat);
1554         }
1555
1556         /* Handle tracking */
1557         if(ob->track) {
1558                 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1559                 solve_tracking (ob, ob->track->obmat);
1560                 
1561         }
1562
1563         /* constraints need ctime, not stime. it calls where_is_object_time and bsystem_time */
1564         solve_constraints (ob, TARGET_OBJECT, NULL, ctime);
1565
1566         if(ob->scriptlink.totscript && !during_script()) {
1567                 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1568         }
1569         
1570         /* set negative scale flag in object */
1571         Crossf(vec, ob->obmat[0], ob->obmat[1]);
1572         if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1573         else ob->transflag &= ~OB_NEG_SCALE;
1574 }
1575
1576 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
1577 {
1578         float totmat[4][4];
1579         float tmat[4][4];
1580         float locmat[4][4];
1581         float vec[3];
1582         int ok;
1583
1584         object_to_mat4(ob, locmat);
1585         
1586         if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, obmat);
1587         
1588
1589         switch(ob->partype & PARTYPE) {
1590         case PAROBJECT:
1591                 ok= 0;
1592                 if(par->type==OB_CURVE) {
1593                         if( ((Curve *)par->data)->flag & CU_PATH ) {
1594                                 ob_parcurve(ob, par, tmat);
1595                                 ok= 1;
1596                         }
1597                 }
1598                 
1599                 if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
1600                         NULL, NULL, NULL, NULL, NULL, NULL);
1601                 else Mat4CpyMat4(totmat, par->obmat);
1602                 
1603                 break;
1604         case PARBONE:
1605                 ob_parbone(ob, par, tmat);
1606                 Mat4MulSerie(totmat, par->obmat, tmat,         
1607                         NULL, NULL, NULL, NULL, NULL, NULL);
1608                 break;
1609                 
1610         case PARVERT1:
1611                 Mat4One(totmat);
1612                 if (simul){
1613                         VECCOPY(totmat[3], par->obmat[3]);
1614                 }
1615                 else{
1616                         give_parvert(par, ob->par1, vec);
1617                         VecMat4MulVecfl(totmat[3], par->obmat, vec);
1618                 }
1619                 break;
1620         case PARVERT3:
1621                 ob_parvert3(ob, par, tmat);
1622                 
1623                 Mat4MulSerie(totmat, par->obmat, tmat,         
1624                         NULL, NULL, NULL, NULL, NULL, NULL);
1625                 break;
1626                 
1627         case PARSKEL:
1628                 Mat4CpyMat4(totmat, par->obmat);
1629                 break;
1630         }
1631         
1632         // total 
1633         Mat4MulSerie(tmat, totmat, ob->parentinv,         
1634                 NULL, NULL, NULL, NULL, NULL, NULL);
1635         Mat4MulSerie(obmat, tmat, locmat,         
1636                 NULL, NULL, NULL, NULL, NULL, NULL);
1637         
1638         if (simul) {
1639
1640         }
1641         else{
1642                 // external usable originmat 
1643                 Mat3CpyMat4(originmat, tmat);
1644                 
1645                 // origin, voor help line
1646                 if( (ob->partype & 15)==PARSKEL ) {
1647                         VECCOPY(ob->orig, par->obmat[3]);
1648                 }
1649                 else {
1650                         VECCOPY(ob->orig, totmat[3]);
1651                 }
1652         }
1653
1654 }
1655 void solve_tracking (Object *ob, float targetmat[][4])
1656 {
1657         float *quat;
1658         float vec[3];
1659         float totmat[3][3];
1660         float tmat[4][4];
1661         
1662         VecSubf(vec, ob->obmat[3], targetmat[3]);
1663         quat= vectoquat(vec, ob->trackflag, ob->upflag);
1664         QuatToMat3(quat, totmat);
1665         
1666         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
1667                 /* 'temporal' : clear parent info */
1668                 object_to_mat4(ob, tmat);
1669                 tmat[0][3]= ob->obmat[0][3];
1670                 tmat[1][3]= ob->obmat[1][3];
1671                 tmat[2][3]= ob->obmat[2][3];
1672                 tmat[3][0]= ob->obmat[3][0];
1673                 tmat[3][1]= ob->obmat[3][1];
1674                 tmat[3][2]= ob->obmat[3][2];
1675                 tmat[3][3]= ob->obmat[3][3];
1676         }
1677         else Mat4CpyMat4(tmat, ob->obmat);
1678         
1679         Mat4MulMat34(ob->obmat, totmat, tmat);
1680
1681 }
1682
1683 void where_is_object(Object *ob)
1684 {
1685         where_is_object_time(ob, (float)G.scene->r.cfra);
1686 }
1687
1688
1689 void where_is_object_simul(Object *ob)
1690 /* was written for the old game engine (until 2.04) */
1691 /* It seems that this function is only called
1692 for a lamp that is the child of another object */
1693 {
1694         Object *par;
1695         Ipo *ipo;
1696         float *fp1, *fp2;
1697         float slowmat[4][4];
1698         float fac1, fac2;
1699         int a;
1700         
1701         /* NO TIMEOFFS */
1702         
1703         /* no ipo! (because of dloc and realtime-ipos) */
1704         ipo= ob->ipo;
1705         ob->ipo= NULL;
1706
1707         if(ob->parent) {
1708                 par= ob->parent;
1709                 
1710                 solve_parenting(ob, par, ob->obmat, slowmat, 1);
1711
1712                 if(ob->partype & PARSLOW) {
1713
1714                         fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1715                         fac2= 1.0f-fac1;
1716                         fp1= ob->obmat[0];
1717                         fp2= slowmat[0];
1718                         for(a=0; a<16; a++, fp1++, fp2++) {
1719                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1720                         }
1721                 }
1722                 
1723         }
1724         else {
1725                 object_to_mat4(ob, ob->obmat);
1726         }
1727         
1728         if(ob->track) 
1729                 solve_tracking(ob, ob->track->obmat);
1730
1731         solve_constraints(ob, TARGET_OBJECT, NULL, G.scene->r.cfra);
1732         
1733         /*  WATCH IT!!! */
1734         ob->ipo= ipo;
1735         
1736 }
1737 extern void Mat4BlendMat4(float out[][4], float dst[][4], float src[][4], float srcweight);
1738
1739 void solve_constraints (Object *ob, short obtype, void *obdata, float ctime)
1740 {
1741         bConstraint *con;
1742         float tmat[4][4], focusmat[4][4], lastmat[4][4];
1743         int i, clear=1, tot=0;
1744         float   a=0;
1745         float   aquat[4], quat[4];
1746         float   aloc[3], loc[3];
1747         float   asize[3], size[3];
1748         float   oldmat[4][4];
1749         float   smat[3][3], rmat[3][3], mat[3][3];
1750         float enf;
1751
1752         for (con = ob->constraints.first; con; con=con->next) {
1753                 // inverse kinematics is solved seperate 
1754                 if (con->type==CONSTRAINT_TYPE_KINEMATIC) continue;
1755                 // and this we can skip completely
1756                 if (con->flag & CONSTRAINT_DISABLE) continue;
1757                 // local constraints are handled in armature.c only 
1758                 if (con->flag & CONSTRAINT_LOCAL) continue;
1759
1760                 /* Clear accumulators if necessary*/
1761                 if (clear) {
1762                         clear= 0;
1763                         a= 0;
1764                         tot= 0;
1765                         memset(aquat, 0, sizeof(float)*4);
1766                         memset(aloc, 0, sizeof(float)*3);
1767                         memset(asize, 0, sizeof(float)*3);
1768                 }
1769                 
1770                 enf = con->enforce;     // value from ipos (from action channels)
1771
1772                 /* Get the targetmat */
1773                 get_constraint_target_matrix(con, obtype, obdata, tmat, size, ctime);
1774                 
1775                 Mat4CpyMat4(focusmat, tmat);
1776                 
1777                 /* Extract the components & accumulate */
1778                 Mat4ToQuat(focusmat, quat);
1779                 VECCOPY(loc, focusmat[3]);
1780                 Mat3CpyMat4(mat, focusmat);
1781                 Mat3ToSize(mat, size);
1782                 
1783                 a+= enf;
1784                 tot++;
1785                 
1786                 for(i=0; i<3; i++) {
1787                         aquat[i+1]+=(quat[i+1]) * enf;
1788                         aloc[i]+=(loc[i]) * enf;
1789                         asize[i]+=(size[i]-1.0f) * enf;
1790                 }
1791                 aquat[0]+=(quat[0])*enf;
1792                 Mat4CpyMat4(lastmat, focusmat);
1793                 
1794                 /* removed for now, probably becomes option? (ton) */
1795                 
1796                 /* If the next constraint is not the same type (or there isn't one),
1797                  *      then evaluate the accumulator & request a clear */
1798                 if (TRUE) { //(!con->next)||(con->next && con->next->type!=con->type)) {
1799                         clear= 1;
1800                         Mat4CpyMat4(oldmat, ob->obmat);
1801
1802                         /*      If we have several inputs, do a blend of them */
1803                         if (tot) {
1804                                 if (tot>1) {
1805                                         if (a) {
1806                                                 for (i=0; i<3; i++) {
1807                                                         asize[i]=1.0f + (asize[i]/(a));
1808                                                         aloc[i]=(aloc[i]/a);
1809                                                 }
1810                                                 
1811                                                 NormalQuat(aquat);
1812                                                 
1813                                                 QuatToMat3(aquat, rmat);
1814                                                 SizeToMat3(asize, smat);
1815                                                 Mat3MulMat3(mat, rmat, smat);
1816                                                 Mat4CpyMat3(focusmat, mat);
1817                                                 VECCOPY(focusmat[3], aloc);
1818
1819                                                 evaluate_constraint(con, ob, obtype, obdata, focusmat);
1820                                         }
1821                                         
1822                                 }       
1823                                 /* If we only have one, blend with the current obmat */
1824                                 else {
1825                                         float solution[4][4];
1826                                         float delta[4][4];
1827                                         float imat[4][4];
1828                                         float identity[4][4];
1829                                         
1830                                         /* solve the constraint then blend it to the previous one */
1831                                         evaluate_constraint(con, ob, obtype, obdata, lastmat);
1832                                         
1833                                         Mat4CpyMat4 (solution, ob->obmat);
1834                                         
1835                                         /* Interpolate the enforcement */                                       
1836                                         Mat4Invert (imat, oldmat);
1837                                         Mat4MulMat4 (delta, solution, imat);
1838                                         
1839                                         if (a<1.0) {
1840                                                 Mat4One(identity);
1841                                                 Mat4BlendMat4(delta, identity, delta, a);
1842                                         }
1843                                         Mat4MulMat4 (ob->obmat, delta, oldmat);
1844                                 }
1845                         }
1846                 }
1847         }       
1848 }
1849
1850 /* for calculation of the inverse parent transform, only used for editor */
1851 void what_does_parent(Object *ob)
1852 {
1853
1854         clear_workob();
1855         Mat4One(workob.obmat);
1856         Mat4One(workob.parentinv);
1857         workob.parent= ob->parent;
1858         workob.track= ob->track;
1859
1860         workob.trackflag= ob->trackflag;
1861         workob.upflag= ob->upflag;
1862         
1863         workob.partype= ob->partype;
1864         workob.par1= ob->par1;
1865         workob.par2= ob->par2;
1866         workob.par3= ob->par3;
1867
1868         workob.constraints.first = ob->constraints.first;
1869         workob.constraints.last = ob->constraints.last;
1870
1871         strcpy (workob.parsubstr, ob->parsubstr); 
1872
1873         where_is_object(&workob);
1874 }
1875
1876 BoundBox *unit_boundbox()
1877 {
1878         BoundBox *bb;
1879         float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
1880
1881         bb= MEM_mallocN(sizeof(BoundBox), "bb");
1882         boundbox_set_from_min_max(bb, min, max);
1883         
1884         return bb;
1885 }
1886
1887 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
1888 {
1889         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
1890         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
1891         
1892         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
1893         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
1894
1895         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
1896         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
1897 }
1898
1899 BoundBox *object_get_boundbox(Object *ob)
1900 {
1901         BoundBox *bb= NULL;
1902         
1903         if(ob->type==OB_MESH) {
1904                 bb = mesh_get_bb(ob->data);
1905         }
1906         else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
1907                 bb= ( (Curve *)ob->data )->bb;
1908         }
1909         else if(ob->type==OB_MBALL) {
1910                 bb= ob->bb;
1911         }
1912         return bb;
1913 }
1914
1915 /* used to temporally disable/enable boundbox */
1916 void object_boundbox_flag(Object *ob, int flag, int set)
1917 {
1918         BoundBox *bb= object_get_boundbox(ob);
1919         if(bb) {
1920                 if(set) bb->flag |= flag;
1921                 else bb->flag &= ~flag;
1922         }
1923 }
1924
1925 void minmax_object(Object *ob, float *min, float *max)
1926 {
1927         BoundBox bb;
1928         Mesh *me;
1929         Curve *cu;
1930         float vec[3];
1931         int a;
1932         
1933         switch(ob->type) {
1934                 
1935         case OB_CURVE:
1936         case OB_FONT:
1937         case OB_SURF:
1938                 cu= ob->data;
1939                 
1940                 if(cu->bb==NULL) tex_space_curve(cu);
1941                 bb= *(cu->bb);
1942                 
1943                 for(a=0; a<8; a++) {
1944                         Mat4MulVecfl(ob->obmat, bb.vec[a]);
1945                         DO_MINMAX(bb.vec[a], min, max);
1946                 }
1947                 break;
1948         case OB_ARMATURE:
1949                 if(ob->pose) {
1950                         bPoseChannel *pchan;
1951                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1952                                 VECCOPY(vec, pchan->pose_head);
1953                                 Mat4MulVecfl(ob->obmat, vec);
1954                                 DO_MINMAX(vec, min, max);
1955                                 VECCOPY(vec, pchan->pose_tail);
1956                                 Mat4MulVecfl(ob->obmat, vec);
1957                                 DO_MINMAX(vec, min, max);
1958                         }
1959                         break;
1960                 }
1961                 /* no break, get_mesh will give NULL and it passes on to default */
1962         case OB_MESH:
1963                 me= get_mesh(ob);
1964                 
1965                 if(me) {
1966                         bb = *mesh_get_bb(me);
1967                         
1968                         for(a=0; a<8; a++) {
1969                                 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1970                                 DO_MINMAX(bb.vec[a], min, max);
1971                         }
1972                 }
1973                 if(min[0] < max[0] ) break;
1974                 
1975                 /* else here no break!!!, mesh can be zero sized */
1976                 
1977         default:
1978                 DO_MINMAX(ob->obmat[3], min, max);
1979
1980                 VECCOPY(vec, ob->obmat[3]);
1981                 VecAddf(vec, vec, ob->size);
1982                 DO_MINMAX(vec, min, max);
1983
1984                 VECCOPY(vec, ob->obmat[3]);
1985                 VecSubf(vec, vec, ob->size);
1986                 DO_MINMAX(vec, min, max);
1987                 break;
1988         }
1989 }
1990
1991 /* TODO - use dupli objects bounding boxes */
1992 void minmax_object_duplis(Object *ob, float *min, float *max)
1993 {
1994         if ((ob->transflag & OB_DUPLI)==0) {
1995                 return;
1996         } else {
1997                 ListBase *lb;
1998                 DupliObject *dob;
1999                 
2000                 lb= object_duplilist(G.scene, ob);
2001                 for(dob= lb->first; dob; dob= dob->next) {
2002                         if(dob->no_draw);
2003                         else {
2004                                 /* should really use bound box of dup object */
2005                                 DO_MINMAX(dob->mat[3], min, max);
2006                         }
2007                 }
2008                 free_object_duplilist(lb);      /* does restore */
2009         }
2010 }
2011
2012
2013 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2014 /*           local_object->proxy      == pointer to library object, saved in files and read */
2015
2016 /* function below is polluted with proxy exceptions, cleanup will follow! */
2017
2018 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2019 /* requires flags to be set! */
2020 void object_handle_update(Object *ob)
2021 {
2022         if(ob->recalc & OB_RECALC) {
2023                 
2024                 if(ob->recalc & OB_RECALC_OB) {
2025                         
2026                         // printf("recalcob %s\n", ob->id.name+2);
2027                         
2028                         /* handle proxy copy for target */
2029                         if(ob->id.lib && ob->proxy_from) {
2030                                 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2031                                 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2032                                         Object *obg= ob->proxy_from->proxy_group;
2033                                         Mat4Invert(obg->imat, obg->obmat);
2034                                         Mat4MulMat4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2035                                 }
2036                                 else
2037                                         Mat4CpyMat4(ob->obmat, ob->proxy_from->obmat);
2038                         }
2039                         else
2040                                 where_is_object(ob);
2041                 }
2042                 
2043                 if(ob->recalc & OB_RECALC_DATA) {
2044                         
2045                         // printf("recalcdata %s\n", ob->id.name+2);
2046                         
2047                         /* includes all keys and modifiers */
2048                         if(ob->type==OB_MESH) {
2049                                 makeDerivedMesh(ob, get_viewedit_datamask());
2050                         }
2051                         else if(ob->type==OB_MBALL) {
2052                                 makeDispListMBall(ob);
2053                         } 
2054                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2055                                 makeDispListCurveTypes(ob, 0);
2056                         }
2057                         else if(ob->type==OB_LATTICE) {
2058                                 lattice_calc_modifiers(ob);
2059                         }
2060                         else if(ob->type==OB_ARMATURE) {
2061                                 /* this happens for reading old files and to match library armatures with poses */
2062                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2063                                         armature_rebuild_pose(ob, ob->data);
2064                                 
2065                                 if(ob->id.lib && ob->proxy_from) {
2066                                         copy_pose_result(ob->pose, ob->proxy_from->pose);
2067                                         // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2068                                 }
2069                                 else {
2070                                         do_all_pose_actions(ob);
2071                                         where_is_pose(ob);
2072                                 }
2073                         }
2074                 }
2075         
2076                 /* the no-group proxy case, we call update */
2077                 if(ob->proxy && ob->proxy_group==NULL) {
2078                         /* set pointer in library proxy target, for copying, but restore it */
2079                         ob->proxy->proxy_from= ob;
2080                         // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2081                         object_handle_update(ob->proxy);
2082                 }
2083         
2084                 ob->recalc &= ~OB_RECALC;
2085         }
2086
2087         /* the case when this is a group proxy, object_update is called in group.c */
2088         if(ob->proxy) {
2089                 ob->proxy->proxy_from= ob;
2090                 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2091         }
2092 }