Filling in branch from trunk
[blender-staging.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 /* proxy rule: lib_object->proxy_from == the one we borrow from, set temporally while object_update */
1145 /*             local_object->proxy == pointer to library object, saved in files and read */
1146 /*             local_object->proxy_group == pointer to group dupli-object, saved in files and read */
1147
1148 void object_make_proxy(Object *ob, Object *target, Object *gob)
1149 {
1150         /* paranoia checks */
1151         if(ob->id.lib || target->id.lib==NULL) {
1152                 printf("cannot make proxy\n");
1153                 return;
1154         }
1155         
1156         ob->proxy= target;
1157         ob->proxy_group= gob;
1158         id_lib_extern(&target->id);
1159         
1160         ob->recalc= target->recalc= OB_RECALC;
1161         
1162         /* copy transform */
1163         if(gob) {
1164                 VECCOPY(ob->loc, gob->loc);
1165                 VECCOPY(ob->rot, gob->rot);
1166                 VECCOPY(ob->size, gob->size);
1167         }
1168         else {
1169                 VECCOPY(ob->loc, target->loc);
1170                 VECCOPY(ob->rot, target->rot);
1171                 VECCOPY(ob->size, target->size);
1172         }
1173         
1174         ob->parent= target->parent;     /* libdata */
1175         Mat4CpyMat4(ob->parentinv, target->parentinv);
1176         ob->ipo= target->ipo;           /* libdata */
1177         
1178         /* skip constraints, constraintchannels, nla? */
1179         
1180         ob->type= target->type;
1181         ob->data= target->data;
1182         id_us_plus((ID *)ob->data);             /* ensures lib data becomes LIB_EXTERN */
1183         
1184         /* type conversions */
1185         if(target->type == OB_ARMATURE) {
1186                 copy_object_pose(ob, target);   /* data copy, object pointers in constraints */
1187                 rest_pose(ob->pose);                    /* clear all transforms in channels */
1188                 armature_rebuild_pose(ob, ob->data);    /* set all internal links */
1189         }
1190 }
1191
1192
1193 /* *************** CALC ****************** */
1194
1195 /* there is also a timing calculation in drawobject() */
1196
1197 float bluroffs= 0.0f, fieldoffs= 0.0f;
1198 int no_speed_curve= 0;
1199
1200 /* ugly calls from render */
1201 void set_mblur_offs(float blur)
1202 {
1203         bluroffs= blur;
1204 }
1205
1206 void set_field_offs(float field)
1207 {
1208         fieldoffs= field;
1209 }
1210
1211 void disable_speed_curve(int val)
1212 {
1213         no_speed_curve= val;
1214 }
1215
1216 /* ob can be NULL */
1217 float bsystem_time(Object *ob, float cfra, float ofs)
1218 {
1219         /* returns float ( see frame_to_float in ipo.c) */
1220         
1221         /* bluroffs and fieldoffs are ugly globals that are set by render */
1222         cfra+= bluroffs+fieldoffs;
1223
1224         /* global time */
1225         cfra*= G.scene->r.framelen;     
1226         
1227         if (ob) {
1228                 if (no_speed_curve==0 && ob->ipo) 
1229                         cfra= calc_ipo_time(ob->ipo, cfra);
1230                 
1231                 /* ofset frames */
1232                 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0) 
1233                         cfra-= ob->sf;
1234         }
1235         
1236         cfra-= ofs;
1237
1238         return cfra;
1239 }
1240
1241 void object_to_mat3(Object *ob, float mat[][3]) /* no parent */
1242 {
1243         float smat[3][3], vec[3];
1244         float rmat[3][3];
1245         float q1[4];
1246         
1247         /* size */
1248         if(ob->ipo) {
1249                 vec[0]= ob->size[0]+ob->dsize[0];
1250                 vec[1]= ob->size[1]+ob->dsize[1];
1251                 vec[2]= ob->size[2]+ob->dsize[2];
1252                 SizeToMat3(vec, smat);
1253         }
1254         else {
1255                 SizeToMat3(ob->size, smat);
1256         }
1257
1258         /* rot */
1259         if(ob->transflag & OB_QUAT) {
1260                 if(ob->ipo) {
1261                         QuatMul(q1, ob->quat, ob->dquat);
1262                         QuatToMat3(q1, rmat);
1263                 }
1264                 else {
1265                         QuatToMat3(ob->quat, rmat);
1266                 }
1267         }
1268         else {
1269                 if(ob->ipo) {
1270                         vec[0]= ob->rot[0]+ob->drot[0];
1271                         vec[1]= ob->rot[1]+ob->drot[1];
1272                         vec[2]= ob->rot[2]+ob->drot[2];
1273                         EulToMat3(vec, rmat);
1274                 }
1275                 else {
1276                         EulToMat3(ob->rot, rmat);
1277                 }
1278         }
1279         Mat3MulMat3(mat, rmat, smat);
1280 }
1281
1282 void object_to_mat4(Object *ob, float mat[][4])
1283 {
1284         float tmat[3][3];
1285         
1286         object_to_mat3(ob, tmat);
1287         
1288         Mat4CpyMat3(mat, tmat);
1289         
1290         VECCOPY(mat[3], ob->loc);
1291         if(ob->ipo) {
1292                 mat[3][0]+= ob->dloc[0];
1293                 mat[3][1]+= ob->dloc[1];
1294                 mat[3][2]+= ob->dloc[2];
1295         }
1296 }
1297
1298 int enable_cu_speed= 1;
1299
1300 static void ob_parcurve(Object *ob, Object *par, float mat[][4])
1301 {
1302         Curve *cu;
1303         float q[4], vec[4], dir[3], *quat, x1, ctime;
1304         float timeoffs= 0.0;
1305         
1306         Mat4One(mat);
1307         
1308         cu= par->data;
1309         if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1310                 makeDispListCurveTypes(par, 0);
1311         if(cu->path==NULL) return;
1312         
1313         /* exception, timeoffset is regarded as distance offset */
1314         if(cu->flag & CU_OFFS_PATHDIST) {
1315                 SWAP(float, timeoffs, ob->sf);
1316         }
1317         
1318         /* catch exceptions: feature for nla stride editing */
1319         if(ob->ipoflag & OB_DISABLE_PATH) {
1320                 ctime= 0.0f;
1321         }
1322         /* catch exceptions: curve paths used as a duplicator */
1323         else if(enable_cu_speed) {
1324                 ctime= bsystem_time(ob, (float)G.scene->r.cfra, 0.0);
1325                 
1326                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1327                         ctime /= cu->pathlen;
1328                         CLAMP(ctime, 0.0, 1.0);
1329                 }
1330         }
1331         else {
1332                 ctime= G.scene->r.cfra - ob->sf;
1333                 ctime /= cu->pathlen;
1334                 
1335                 CLAMP(ctime, 0.0, 1.0);
1336         }
1337         
1338         /* time calculus is correct, now apply distance offset */
1339         if(cu->flag & CU_OFFS_PATHDIST) {
1340                 ctime += timeoffs/cu->path->totdist;
1341
1342                 /* restore */
1343                 SWAP(float, timeoffs, ob->sf);
1344         }
1345         
1346         
1347         /* vec: 4 items! */
1348         if( where_on_path(par, ctime, vec, dir) ) {
1349
1350                 if(cu->flag & CU_FOLLOW) {
1351                         quat= vectoquat(dir, ob->trackflag, ob->upflag);
1352                         
1353                         /* the tilt */
1354                         Normalize(dir);
1355                         q[0]= (float)cos(0.5*vec[3]);
1356                         x1= (float)sin(0.5*vec[3]);
1357                         q[1]= -x1*dir[0];
1358                         q[2]= -x1*dir[1];
1359                         q[3]= -x1*dir[2];
1360                         QuatMul(quat, q, quat);
1361                         
1362                         QuatToMat4(quat, mat);
1363                 }
1364                 
1365                 VECCOPY(mat[3], vec);
1366                 
1367         }
1368 }
1369
1370 static void ob_parbone(Object *ob, Object *par, float mat[][4])
1371 {       
1372         bPoseChannel *pchan;
1373         bArmature *arm;
1374         float vec[3];
1375         
1376         arm=get_armature(par);
1377         if (!arm) {
1378                 Mat4One(mat);
1379                 return;
1380         }
1381         
1382         /* Make sure the bone is still valid */
1383         pchan= get_pose_channel(par->pose, ob->parsubstr);
1384         if (!pchan){
1385                 printf ("Object %s with Bone parent: bone %s doesn't exist\n", ob->id.name+2, ob->parsubstr);
1386                 Mat4One(mat);
1387                 return;
1388         }
1389
1390         /* get bone transform */
1391         Mat4CpyMat4(mat, pchan->pose_mat);
1392
1393         /* but for backwards compatibility, the child has to move to the tail */
1394         VECCOPY(vec, mat[1]);
1395         VecMulf(vec, pchan->bone->length);
1396         VecAddf(mat[3], mat[3], vec);
1397 }
1398
1399 static void give_parvert(Object *par, int nr, float *vec)
1400 {
1401         int a, count;
1402         
1403         vec[0]=vec[1]=vec[2]= 0.0f;
1404         
1405         if(par->type==OB_MESH) {
1406                 if(G.obedit && (par->data==G.obedit->data)) {
1407                         EditMesh *em = G.editMesh;
1408                         EditVert *eve;
1409                         
1410                         for(eve= em->verts.first; eve; eve= eve->next) {
1411                                 if(eve->keyindex==nr) {
1412                                         memcpy(vec, eve->co, 12);
1413                                         break;
1414                                 }
1415                         }
1416                 }
1417                 else {
1418                         DerivedMesh *dm = par->derivedFinal;
1419                         
1420                         if(dm) {
1421                                 int i, count = 0, numVerts = dm->getNumVerts(dm);
1422                                 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
1423                                 float co[3];
1424
1425                                 /* get the average of all verts with (original index == nr) */
1426                                 for(i = 0; i < numVerts; ++i, ++index) {
1427                                         if(*index == nr) {
1428                                                 dm->getVertCo(dm, i, co);
1429                                                 VecAddf(vec, vec, co);
1430                                                 count++;
1431                                         }
1432                                 }
1433
1434                                 if(count > 0) {
1435                                         VecMulf(vec, 1.0f / count);
1436                                 } else {
1437                                         dm->getVertCo(dm, 0, vec);
1438                                 }
1439                         }
1440                 }
1441         }
1442         else if ELEM(par->type, OB_CURVE, OB_SURF) {
1443                 Nurb *nu;
1444                 Curve *cu;
1445                 BPoint *bp;
1446                 BezTriple *bezt;
1447                 
1448                 cu= par->data;
1449                 nu= cu->nurb.first;
1450                 if(par==G.obedit) nu= editNurb.first;
1451                 
1452                 count= 0;
1453                 while(nu) {
1454                         if((nu->type & 7)==CU_BEZIER) {
1455                                 bezt= nu->bezt;
1456                                 a= nu->pntsu;
1457                                 while(a--) {
1458                                         if(count==nr) {
1459                                                 VECCOPY(vec, bezt->vec[1]);
1460                                                 break;
1461                                         }
1462                                         count++;
1463                                         bezt++;
1464                                 }
1465                         }
1466                         else {
1467                                 bp= nu->bp;
1468                                 a= nu->pntsu*nu->pntsv;
1469                                 while(a--) {
1470                                         if(count==nr) {
1471                                                 memcpy(vec, bp->vec, 12);
1472                                                 break;
1473                                         }
1474                                         count++;
1475                                         bp++;
1476                                 }
1477                         }
1478                         nu= nu->next;
1479                 }
1480
1481         }
1482         else if(par->type==OB_LATTICE) {
1483                 Lattice *latt= par->data;
1484                 BPoint *bp;
1485                 DispList *dl = find_displist(&par->disp, DL_VERTS);
1486                 float *co = dl?dl->verts:NULL;
1487                 
1488                 if(par==G.obedit) latt= editLatt;
1489                 
1490                 a= latt->pntsu*latt->pntsv*latt->pntsw;
1491                 count= 0;
1492                 bp= latt->def;
1493                 while(a--) {
1494                         if(count==nr) {
1495                                 if(co)
1496                                         memcpy(vec, co, 3*sizeof(float));
1497                                 else
1498                                         memcpy(vec, bp->vec, 3*sizeof(float));
1499                                 break;
1500                         }
1501                         count++;
1502                         if(co) co+= 3;
1503                         else bp++;
1504                 }
1505         }
1506 }
1507
1508 static void ob_parvert3(Object *ob, Object *par, float mat[][4])
1509 {
1510         float cmat[3][3], v1[3], v2[3], v3[3], q[4];
1511
1512         /* in local ob space */
1513         Mat4One(mat);
1514         
1515         if ELEM4(par->type, OB_MESH, OB_SURF, OB_CURVE, OB_LATTICE) {
1516                 
1517                 give_parvert(par, ob->par1, v1);
1518                 give_parvert(par, ob->par2, v2);
1519                 give_parvert(par, ob->par3, v3);
1520                                 
1521                 triatoquat(v1, v2, v3, q);
1522                 QuatToMat3(q, cmat);
1523                 Mat4CpyMat3(mat, cmat);
1524                 
1525                 if(ob->type==OB_CURVE) {
1526                         VECCOPY(mat[3], v1);
1527                 }
1528                 else {
1529                         VecAddf(mat[3], v1, v2);
1530                         VecAddf(mat[3], mat[3], v3);
1531                         VecMulf(mat[3], 0.3333333f);
1532                 }
1533         }
1534 }
1535
1536 static int no_parent_ipo=0;
1537 void set_no_parent_ipo(int val)
1538 {
1539         no_parent_ipo= val;
1540 }
1541
1542 static int during_script_flag=0;
1543 void disable_where_script(short on)
1544 {
1545         during_script_flag= on;
1546 }
1547
1548 int during_script(void) {
1549         return during_script_flag;
1550 }
1551
1552 static int during_scriptlink_flag=0;
1553 void disable_where_scriptlink(short on)
1554 {
1555         during_scriptlink_flag= on;
1556 }
1557
1558 int during_scriptlink(void) {
1559         return during_scriptlink_flag;
1560 }
1561
1562 void where_is_object_time(Object *ob, float ctime)
1563 {
1564         float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
1565         float stime, fac1, fac2, vec[3];
1566         int a;
1567         int pop; 
1568         
1569         /* new version: correct parent+vertexparent and track+parent */
1570         /* this one only calculates direct attached parent and track */
1571         /* is faster, but should keep track of timeoffs */
1572         
1573         if(ob==NULL) return;
1574         
1575         /* this is needed to be able to grab objects with ipos, otherwise it always freezes them */
1576         stime= bsystem_time(ob, ctime, 0.0);
1577         if(stime != ob->ctime) {
1578                 
1579                 ob->ctime= stime;
1580                 
1581                 if(ob->ipo) {
1582                         calc_ipo(ob->ipo, stime);
1583                         execute_ipo((ID *)ob, ob->ipo);
1584                 }
1585                 else 
1586                         do_all_object_actions(ob);
1587                 
1588                 /* do constraint ipos ..., note it needs stime (0 = all ipos) */
1589                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 0);
1590         }
1591         else {
1592                 /* but, the drivers have to be done */
1593                 if(ob->ipo) do_ob_ipodrivers(ob, ob->ipo, stime);
1594                 /* do constraint ipos ..., note it needs stime (1 = only drivers ipos) */
1595                 do_constraint_channels(&ob->constraints, &ob->constraintChannels, stime, 1);
1596         }
1597         
1598         if(ob->parent) {
1599                 Object *par= ob->parent;
1600                 
1601                 if(ob->ipoflag & OB_OFFS_PARENT) ctime-= ob->sf;
1602                 
1603                 /* hurms, code below conflicts with depgraph... (ton) */
1604                 /* and even worse, it gives bad effects for NLA stride too (try ctime != par->ctime, with MBlur) */
1605                 pop= 0;
1606                 if(no_parent_ipo==0 && stime != par->ctime) {
1607                 
1608                         // only for ipo systems? 
1609                         pushdata(par, sizeof(Object));
1610                         pop= 1;
1611                         
1612                         if(par->proxy_from);    // was a copied matrix, no where_is! bad...
1613                         else where_is_object_time(par, ctime);
1614                 }
1615                 
1616                 solve_parenting(ob, par, ob->obmat, slowmat, 0);
1617
1618                 if(pop) {
1619                         poplast(par);
1620                 }
1621                 
1622                 if(ob->partype & PARSLOW) {
1623                         // include framerate
1624
1625                         fac1= (1.0f/(1.0f+ fabs(ob->sf)));
1626                         if(fac1>=1.0) return;
1627                         fac2= 1.0f-fac1;
1628                         
1629                         fp1= ob->obmat[0];
1630                         fp2= slowmat[0];
1631                         for(a=0; a<16; a++, fp1++, fp2++) {
1632                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1633                         }
1634                 }
1635         
1636         }
1637         else {
1638                 object_to_mat4(ob, ob->obmat);
1639         }
1640
1641         /* Handle tracking */
1642         if(ob->track) {
1643                 if( ctime != ob->track->ctime) where_is_object_time(ob->track, ctime);
1644                 solve_tracking (ob, ob->track->obmat);
1645                 
1646         }
1647
1648         /* solve constraints */
1649         if (ob->constraints.first) {
1650                 bConstraintOb *cob;
1651                 
1652                 cob= constraints_make_evalob(ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
1653                 
1654                 /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */
1655                 solve_constraints (&ob->constraints, cob, ctime);
1656                 
1657                 constraints_clear_evalob(cob);
1658         }
1659         
1660         if(ob->scriptlink.totscript && !during_script()) {
1661                 if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW);
1662         }
1663         
1664         /* set negative scale flag in object */
1665         Crossf(vec, ob->obmat[0], ob->obmat[1]);
1666         if( Inpf(vec, ob->obmat[2]) < 0.0 ) ob->transflag |= OB_NEG_SCALE;
1667         else ob->transflag &= ~OB_NEG_SCALE;
1668 }
1669
1670 static void solve_parenting (Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
1671 {
1672         float totmat[4][4];
1673         float tmat[4][4];
1674         float locmat[4][4];
1675         float vec[3];
1676         int ok;
1677
1678         object_to_mat4(ob, locmat);
1679         
1680         if(ob->partype & PARSLOW) Mat4CpyMat4(slowmat, obmat);
1681         
1682
1683         switch(ob->partype & PARTYPE) {
1684         case PAROBJECT:
1685                 ok= 0;
1686                 if(par->type==OB_CURVE) {
1687                         if( ((Curve *)par->data)->flag & CU_PATH ) {
1688                                 ob_parcurve(ob, par, tmat);
1689                                 ok= 1;
1690                         }
1691                 }
1692                 
1693                 if(ok) Mat4MulSerie(totmat, par->obmat, tmat, 
1694                         NULL, NULL, NULL, NULL, NULL, NULL);
1695                 else Mat4CpyMat4(totmat, par->obmat);
1696                 
1697                 break;
1698         case PARBONE:
1699                 ob_parbone(ob, par, tmat);
1700                 Mat4MulSerie(totmat, par->obmat, tmat,         
1701                         NULL, NULL, NULL, NULL, NULL, NULL);
1702                 break;
1703                 
1704         case PARVERT1:
1705                 Mat4One(totmat);
1706                 if (simul){
1707                         VECCOPY(totmat[3], par->obmat[3]);
1708                 }
1709                 else{
1710                         give_parvert(par, ob->par1, vec);
1711                         VecMat4MulVecfl(totmat[3], par->obmat, vec);
1712                 }
1713                 break;
1714         case PARVERT3:
1715                 ob_parvert3(ob, par, tmat);
1716                 
1717                 Mat4MulSerie(totmat, par->obmat, tmat,         
1718                         NULL, NULL, NULL, NULL, NULL, NULL);
1719                 break;
1720                 
1721         case PARSKEL:
1722                 Mat4CpyMat4(totmat, par->obmat);
1723                 break;
1724         }
1725         
1726         // total 
1727         Mat4MulSerie(tmat, totmat, ob->parentinv,         
1728                 NULL, NULL, NULL, NULL, NULL, NULL);
1729         Mat4MulSerie(obmat, tmat, locmat,         
1730                 NULL, NULL, NULL, NULL, NULL, NULL);
1731         
1732         if (simul) {
1733
1734         }
1735         else{
1736                 // external usable originmat 
1737                 Mat3CpyMat4(originmat, tmat);
1738                 
1739                 // origin, voor help line
1740                 if( (ob->partype & 15)==PARSKEL ) {
1741                         VECCOPY(ob->orig, par->obmat[3]);
1742                 }
1743                 else {
1744                         VECCOPY(ob->orig, totmat[3]);
1745                 }
1746         }
1747
1748 }
1749 void solve_tracking (Object *ob, float targetmat[][4])
1750 {
1751         float *quat;
1752         float vec[3];
1753         float totmat[3][3];
1754         float tmat[4][4];
1755         
1756         VecSubf(vec, ob->obmat[3], targetmat[3]);
1757         quat= vectoquat(vec, ob->trackflag, ob->upflag);
1758         QuatToMat3(quat, totmat);
1759         
1760         if(ob->parent && (ob->transflag & OB_POWERTRACK)) {
1761                 /* 'temporal' : clear parent info */
1762                 object_to_mat4(ob, tmat);
1763                 tmat[0][3]= ob->obmat[0][3];
1764                 tmat[1][3]= ob->obmat[1][3];
1765                 tmat[2][3]= ob->obmat[2][3];
1766                 tmat[3][0]= ob->obmat[3][0];
1767                 tmat[3][1]= ob->obmat[3][1];
1768                 tmat[3][2]= ob->obmat[3][2];
1769                 tmat[3][3]= ob->obmat[3][3];
1770         }
1771         else Mat4CpyMat4(tmat, ob->obmat);
1772         
1773         Mat4MulMat34(ob->obmat, totmat, tmat);
1774
1775 }
1776
1777 void where_is_object(Object *ob)
1778 {
1779         where_is_object_time(ob, (float)G.scene->r.cfra);
1780 }
1781
1782
1783 void where_is_object_simul(Object *ob)
1784 /* was written for the old game engine (until 2.04) */
1785 /* It seems that this function is only called
1786 for a lamp that is the child of another object */
1787 {
1788         Object *par;
1789         Ipo *ipo;
1790         float *fp1, *fp2;
1791         float slowmat[4][4];
1792         float fac1, fac2;
1793         int a;
1794         
1795         /* NO TIMEOFFS */
1796         
1797         /* no ipo! (because of dloc and realtime-ipos) */
1798         ipo= ob->ipo;
1799         ob->ipo= NULL;
1800
1801         if(ob->parent) {
1802                 par= ob->parent;
1803                 
1804                 solve_parenting(ob, par, ob->obmat, slowmat, 1);
1805
1806                 if(ob->partype & PARSLOW) {
1807
1808                         fac1= (float)(1.0/(1.0+ fabs(ob->sf)));
1809                         fac2= 1.0f-fac1;
1810                         fp1= ob->obmat[0];
1811                         fp2= slowmat[0];
1812                         for(a=0; a<16; a++, fp1++, fp2++) {
1813                                 fp1[0]= fac1*fp1[0] + fac2*fp2[0];
1814                         }
1815                 }
1816                 
1817         }
1818         else {
1819                 object_to_mat4(ob, ob->obmat);
1820         }
1821         
1822         if(ob->track) 
1823                 solve_tracking(ob, ob->track->obmat);
1824
1825         /* solve constraints */
1826         if (ob->constraints.first) {
1827                 bConstraintOb *cob;
1828                 
1829                 cob= constraints_make_evalob(ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
1830                 solve_constraints (&ob->constraints, cob, G.scene->r.cfra);
1831                 constraints_clear_evalob(cob);
1832         }
1833         
1834         /*  WATCH IT!!! */
1835         ob->ipo= ipo;
1836 }
1837
1838 /* for calculation of the inverse parent transform, only used for editor */
1839 void what_does_parent(Object *ob)
1840 {
1841         clear_workob();
1842         Mat4One(workob.obmat);
1843         Mat4One(workob.parentinv);
1844         Mat4One(workob.constinv);
1845         workob.parent= ob->parent;
1846         workob.track= ob->track;
1847
1848         workob.trackflag= ob->trackflag;
1849         workob.upflag= ob->upflag;
1850         
1851         workob.partype= ob->partype;
1852         workob.par1= ob->par1;
1853         workob.par2= ob->par2;
1854         workob.par3= ob->par3;
1855
1856         workob.constraints.first = ob->constraints.first;
1857         workob.constraints.last = ob->constraints.last;
1858
1859         strcpy(workob.parsubstr, ob->parsubstr); 
1860
1861         where_is_object(&workob);
1862 }
1863
1864 BoundBox *unit_boundbox()
1865 {
1866         BoundBox *bb;
1867         float min[3] = {-1,-1,-1}, max[3] = {-1,-1,-1};
1868
1869         bb= MEM_mallocN(sizeof(BoundBox), "bb");
1870         boundbox_set_from_min_max(bb, min, max);
1871         
1872         return bb;
1873 }
1874
1875 void boundbox_set_from_min_max(BoundBox *bb, float min[3], float max[3])
1876 {
1877         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
1878         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
1879         
1880         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
1881         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
1882
1883         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
1884         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
1885 }
1886
1887 BoundBox *object_get_boundbox(Object *ob)
1888 {
1889         BoundBox *bb= NULL;
1890         
1891         if(ob->type==OB_MESH) {
1892                 bb = mesh_get_bb(ob->data);
1893         }
1894         else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
1895                 bb= ( (Curve *)ob->data )->bb;
1896         }
1897         else if(ob->type==OB_MBALL) {
1898                 bb= ob->bb;
1899         }
1900         return bb;
1901 }
1902
1903 /* used to temporally disable/enable boundbox */
1904 void object_boundbox_flag(Object *ob, int flag, int set)
1905 {
1906         BoundBox *bb= object_get_boundbox(ob);
1907         if(bb) {
1908                 if(set) bb->flag |= flag;
1909                 else bb->flag &= ~flag;
1910         }
1911 }
1912
1913 void minmax_object(Object *ob, float *min, float *max)
1914 {
1915         BoundBox bb;
1916         Mesh *me;
1917         Curve *cu;
1918         float vec[3];
1919         int a;
1920         
1921         switch(ob->type) {
1922                 
1923         case OB_CURVE:
1924         case OB_FONT:
1925         case OB_SURF:
1926                 cu= ob->data;
1927                 
1928                 if(cu->bb==NULL) tex_space_curve(cu);
1929                 bb= *(cu->bb);
1930                 
1931                 for(a=0; a<8; a++) {
1932                         Mat4MulVecfl(ob->obmat, bb.vec[a]);
1933                         DO_MINMAX(bb.vec[a], min, max);
1934                 }
1935                 break;
1936         case OB_ARMATURE:
1937                 if(ob->pose) {
1938                         bPoseChannel *pchan;
1939                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1940                                 VECCOPY(vec, pchan->pose_head);
1941                                 Mat4MulVecfl(ob->obmat, vec);
1942                                 DO_MINMAX(vec, min, max);
1943                                 VECCOPY(vec, pchan->pose_tail);
1944                                 Mat4MulVecfl(ob->obmat, vec);
1945                                 DO_MINMAX(vec, min, max);
1946                         }
1947                         break;
1948                 }
1949                 /* no break, get_mesh will give NULL and it passes on to default */
1950         case OB_MESH:
1951                 me= get_mesh(ob);
1952                 
1953                 if(me) {
1954                         bb = *mesh_get_bb(me);
1955                         
1956                         for(a=0; a<8; a++) {
1957                                 Mat4MulVecfl(ob->obmat, bb.vec[a]);
1958                                 DO_MINMAX(bb.vec[a], min, max);
1959                         }
1960                 }
1961                 if(min[0] < max[0] ) break;
1962                 
1963                 /* else here no break!!!, mesh can be zero sized */
1964                 
1965         default:
1966                 DO_MINMAX(ob->obmat[3], min, max);
1967
1968                 VECCOPY(vec, ob->obmat[3]);
1969                 VecAddf(vec, vec, ob->size);
1970                 DO_MINMAX(vec, min, max);
1971
1972                 VECCOPY(vec, ob->obmat[3]);
1973                 VecSubf(vec, vec, ob->size);
1974                 DO_MINMAX(vec, min, max);
1975                 break;
1976         }
1977 }
1978
1979 /* TODO - use dupli objects bounding boxes */
1980 void minmax_object_duplis(Object *ob, float *min, float *max)
1981 {
1982         if ((ob->transflag & OB_DUPLI)==0) {
1983                 return;
1984         } else {
1985                 ListBase *lb;
1986                 DupliObject *dob;
1987                 
1988                 lb= object_duplilist(G.scene, ob);
1989                 for(dob= lb->first; dob; dob= dob->next) {
1990                         if(dob->no_draw);
1991                         else {
1992                                 /* should really use bound box of dup object */
1993                                 DO_MINMAX(dob->mat[3], min, max);
1994                         }
1995                 }
1996                 free_object_duplilist(lb);      /* does restore */
1997         }
1998 }
1999
2000
2001 /* proxy rule: lib_object->proxy_from == the one we borrow from, only set temporal and cleared here */
2002 /*           local_object->proxy      == pointer to library object, saved in files and read */
2003
2004 /* function below is polluted with proxy exceptions, cleanup will follow! */
2005
2006 /* the main object update call, for object matrix, constraints, keys and displist (modifiers) */
2007 /* requires flags to be set! */
2008 void object_handle_update(Object *ob)
2009 {
2010         if(ob->recalc & OB_RECALC) {
2011                 
2012                 if(ob->recalc & OB_RECALC_OB) {
2013                         
2014                         // printf("recalcob %s\n", ob->id.name+2);
2015                         
2016                         /* handle proxy copy for target */
2017                         if(ob->id.lib && ob->proxy_from) {
2018                                 // printf("ob proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2019                                 if(ob->proxy_from->proxy_group) {/* transform proxy into group space */
2020                                         Object *obg= ob->proxy_from->proxy_group;
2021                                         Mat4Invert(obg->imat, obg->obmat);
2022                                         Mat4MulMat4(ob->obmat, ob->proxy_from->obmat, obg->imat);
2023                                 }
2024                                 else
2025                                         Mat4CpyMat4(ob->obmat, ob->proxy_from->obmat);
2026                         }
2027                         else
2028                                 where_is_object(ob);
2029                 }
2030                 
2031                 if(ob->recalc & OB_RECALC_DATA) {
2032                         
2033                         // printf("recalcdata %s\n", ob->id.name+2);
2034                         
2035                         /* includes all keys and modifiers */
2036                         if(ob->type==OB_MESH) {
2037                                 makeDerivedMesh(ob, get_viewedit_datamask());
2038                         }
2039                         else if(ob->type==OB_MBALL) {
2040                                 makeDispListMBall(ob);
2041                         } 
2042                         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2043                                 makeDispListCurveTypes(ob, 0);
2044                         }
2045                         else if(ob->type==OB_LATTICE) {
2046                                 lattice_calc_modifiers(ob);
2047                         }
2048                         else if(ob->type==OB_ARMATURE) {
2049                                 /* this happens for reading old files and to match library armatures with poses */
2050                                 if(ob->pose==NULL || (ob->pose->flag & POSE_RECALC))
2051                                         armature_rebuild_pose(ob, ob->data);
2052                                 
2053                                 if(ob->id.lib && ob->proxy_from) {
2054                                         copy_pose_result(ob->pose, ob->proxy_from->pose);
2055                                         // printf("pose proxy copy, lib ob %s proxy %s\n", ob->id.name, ob->proxy_from->id.name);
2056                                 }
2057                                 else {
2058                                         do_all_pose_actions(ob);
2059                                         where_is_pose(ob);
2060                                 }
2061                         }
2062                 }
2063         
2064                 /* the no-group proxy case, we call update */
2065                 if(ob->proxy && ob->proxy_group==NULL) {
2066                         /* set pointer in library proxy target, for copying, but restore it */
2067                         ob->proxy->proxy_from= ob;
2068                         // printf("call update, lib ob %s proxy %s\n", ob->proxy->id.name, ob->id.name);
2069                         object_handle_update(ob->proxy);
2070                 }
2071         
2072                 ob->recalc &= ~OB_RECALC;
2073         }
2074
2075         /* the case when this is a group proxy, object_update is called in group.c */
2076         if(ob->proxy) {
2077                 ob->proxy->proxy_from= ob;
2078                 // printf("set proxy pointer for later group stuff %s\n", ob->id.name);
2079         }
2080 }