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