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