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