d139b05a6f8a79e88d550cfd4d474c1bc1066ca6
[blender.git] / source / blender / src / editobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 /** 
34  * Theorie: (matrices) A x B x C == A x ( B x C x Binv) x B
35  * ofwel: OB x PAR x EDIT = OB x (PAR x EDIT x PARinv) x PAR
36  */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #ifndef WIN32
47 #include <unistd.h>
48 #else
49 #include <io.h>
50 #endif
51 #include "MEM_guardedalloc.h"
52 #include "PIL_time.h"
53
54 #include "BMF_Api.h"
55
56
57 #include "IMB_imbuf_types.h"
58
59 #include "DNA_action_types.h"
60 #include "DNA_armature_types.h"
61 #include "DNA_camera_types.h"
62 #include "DNA_constraint_types.h"
63 #include "DNA_curve_types.h"
64 #include "DNA_effect_types.h"
65 #include "DNA_ika_types.h"
66 #include "DNA_image_types.h"
67 #include "DNA_ipo_types.h"
68 #include "DNA_key_types.h"
69 #include "DNA_lamp_types.h"
70 #include "DNA_lattice_types.h"
71 #include "DNA_material_types.h"
72 #include "DNA_mesh_types.h"
73 #include "DNA_meshdata_types.h"
74 #include "DNA_meta_types.h"
75 #include "DNA_object_types.h"
76 #include "DNA_scene_types.h"
77 #include "DNA_screen_types.h"
78 #include "DNA_texture_types.h"
79 #include "DNA_view3d_types.h"
80 #include "DNA_world_types.h"
81 #include "DNA_userdef_types.h"
82 #include "DNA_property_types.h"
83 #include "DNA_vfont_types.h"
84
85 #include "BLI_blenlib.h"
86 #include "BLI_arithb.h"
87 #include "BLI_editVert.h"
88 #include "BLI_ghash.h"
89
90 #include "BKE_constraint.h"
91 #include "BKE_action.h"
92 #include "BKE_armature.h"
93 #include "BKE_utildefines.h"
94 #include "BKE_anim.h"
95 #include "BKE_blender.h"
96 #include "BKE_booleanops.h"
97 #include "BKE_curve.h"
98 #include "BKE_displist.h"
99 #include "BKE_effect.h"
100 #include "BKE_font.h"
101 #include "BKE_global.h"
102 #include "BKE_ika.h"
103 #include "BKE_ipo.h"
104 #include "BKE_key.h"
105 #include "BKE_lattice.h"
106 #include "BKE_library.h"
107 #include "BKE_main.h"
108 #include "BKE_material.h"
109 #include "BKE_mball.h"
110 #include "BKE_mesh.h"
111 #include "BKE_nla.h"
112 #include "BKE_object.h"
113 #include "BKE_property.h"
114 #include "BKE_sca.h"
115 #include "BKE_scene.h"
116 #include "BKE_softbody.h"
117 #include "BKE_subsurf.h"
118 #include "BKE_texture.h"
119 #include "BKE_utildefines.h"
120
121 #include "BIF_gl.h"
122 #include "BIF_graphics.h"
123 #include "BIF_interface.h"
124 #include "BIF_mywindow.h"
125 #include "BIF_toolbox.h"
126 #include "BIF_screen.h"
127 #include "BIF_space.h"
128 #include "BIF_toets.h"
129 #include "BIF_butspace.h"
130 #include "BIF_editconstraint.h"
131 #include "BIF_editdeform.h"
132 #include "BIF_editfont.h"
133 #include "BIF_editika.h"
134 #include "BIF_editlattice.h"
135 #include "BIF_editmesh.h"
136 #include "BIF_editoops.h"
137 #include "BIF_editview.h"
138 #include "BIF_editarmature.h"
139 #include "BIF_resources.h"
140
141 #include "BSE_edit.h"
142 #include "BSE_editaction.h"
143 #include "BSE_editipo.h"
144 #include "BSE_filesel.h"        /* For activate_databrowse() */
145 #include "BSE_view.h"
146 #include "BSE_drawview.h"
147 #include "BSE_trans_types.h"
148 #include "BSE_editipo_types.h"
149
150 #include "BDR_vpaint.h"
151 #include "BDR_editmball.h"
152 #include "BDR_editobject.h"
153 #include "BDR_drawobject.h"
154 #include "BDR_editcurve.h"
155 #include "BDR_unwrapper.h"
156
157 #include <time.h>
158 #include "mydevice.h"
159 #include "nla.h"
160
161 #include "blendef.h"
162
163 #include "BIF_transform.h"
164
165 #include "BIF_poseobject.h"
166
167 /*  extern Lattice *copy_lattice(Lattice *lt); */
168 extern ListBase editNurb;
169 extern ListBase editelems;
170
171 TransOb *transmain= 0;
172 TransVert *transvmain= 0;
173 int tottrans=0, transmode=0;    /* 1: texspace */
174
175 float prop_size= 1.0;
176 int prop_mode= 0;
177 float prop_cent[3];
178
179 /* used in editipo, editcurve and here */
180 #define BEZSELECTED(bezt)   (((bezt)->f1 & 1) || ((bezt)->f2 & 1) || ((bezt)->f3 & 1))
181
182 #define TRANS_TEX       1
183
184 #define KEYFLAG_ROT             0x00000001
185 #define KEYFLAG_LOC             0x00000002
186 #define KEYFLAG_SIZE    0x00000004
187
188 float centre[3], centroid[3];
189
190 void mirrormenu(void);
191
192 /* local prototypes -------------*/
193 int pose_flags_reset_done(Object *); /* used in transform_generic.c */
194 void figure_bone_nocalc(Object *); /* used in transform.c */
195 void figure_pose_updating(void); /* used in transform.c */
196 void constline_callback(void);  /* used in drawview.c */
197 /* --------------------------------- */
198
199 void add_object_draw(int type)  /* for toolbox or menus, only non-editmode stuff */
200 {
201         Object *ob;
202         
203         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
204         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
205
206         if ELEM3(curarea->spacetype, SPACE_VIEW3D, SPACE_BUTS, SPACE_INFO) {
207                 if (G.obedit) exit_editmode(2); // freedata, and undo
208                 ob= add_object(type);
209                 base_init_from_view3d(BASACT, G.vd);
210                 
211                 /* only undo pushes on objects without editmode... */
212                 if(type==OB_EMPTY) BIF_undo_push("Add Empty");
213                 else if(type==OB_LAMP) {
214                         BIF_undo_push("Add Lamp");
215                         if(G.vd->drawtype == OB_SHADED) reshadeall_displist();
216                 }
217                 else if(type==OB_LATTICE) BIF_undo_push("Add Lattice");
218                 else if(type==OB_CAMERA) BIF_undo_push("Add Camera");
219                 
220                 allqueue(REDRAWVIEW3D, 0);
221         }
222
223         redraw_test_buttons(OBACT);
224
225         allqueue(REDRAWALL, 0);
226
227         deselect_all_area_oops();
228         set_select_flag_oops();
229         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
230 }
231
232 void add_objectLamp(short type)
233 {
234         Lamp *la;
235
236         /* this function also comes from an info window */
237         if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
238         
239         if(G.obedit==0) {
240                 add_object_draw(OB_LAMP);
241                 base_init_from_view3d(BASACT, G.vd);
242         }
243         
244         la = BASACT->object->data;
245         la->type = type;        
246
247         allqueue(REDRAWALL, 0);
248 }
249
250 // really bad, doesnt do constraints, that has been coded in test_scene_constraints();
251 void free_and_unlink_base(Base *base)
252 {
253         if (base==BASACT)
254                 BASACT= NULL;
255         
256         BLI_remlink(&G.scene->base, base);
257         free_libblock_us(&G.main->object, base->object);
258         MEM_freeN(base);
259 }
260
261 void delete_obj(int ok)
262 {
263         Base *base;
264         int islamp= 0;
265         
266         if(G.obpose) return;
267         if(G.obedit) return;
268         if(G.scene->id.lib) return;
269         
270         base= FIRSTBASE;
271         while(base) {
272                 Base *nbase= base->next;
273
274                 if TESTBASE(base) {
275                         if(ok==0 &&  (ok=okee("Erase selected"))==0) return;
276                         if(base->object->type==OB_LAMP) islamp= 1;
277                         
278                         free_and_unlink_base(base);
279                 }
280                 
281                 base= nbase;
282         }
283         countall();
284
285         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
286         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
287         
288         if(islamp && G.vd->drawtype==OB_SHADED) reshadeall_displist();
289
290         test_scene_constraints(); // do because of delete obj
291         
292         allqueue(REDRAWVIEW3D, 0);
293         redraw_test_buttons(OBACT);
294         allqueue (REDRAWACTION, 0);
295         allqueue(REDRAWIPO, 0);
296         allqueue(REDRAWDATASELECT, 0);
297         allqueue(REDRAWOOPS, 0);
298         allqueue(REDRAWACTION, 0);
299         allqueue(REDRAWNLA, 0);
300         
301         BIF_undo_push("Delete object(s)");
302 }
303
304 static int return_editmesh_indexar(int **indexar, float *cent)
305 {
306         EditMesh *em = G.editMesh;
307         EditVert *eve;
308         int *index, nr, totvert=0;
309         
310         for(eve= em->verts.first; eve; eve= eve->next) {
311                 if(eve->f & SELECT) totvert++;
312         }
313         if(totvert==0) return 0;
314         
315         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
316         nr= 0;
317         cent[0]= cent[1]= cent[2]= 0.0;
318         
319         for(eve= em->verts.first; eve; eve= eve->next) {
320                 if(eve->f & SELECT) {
321                         *index= nr; index++;
322                         VecAddf(cent, cent, eve->co);
323                 }
324                 nr++;
325         }
326         
327         VecMulf(cent, 1.0/(float)totvert);
328         
329         return totvert;
330 }
331
332 static void select_editmesh_hook(ObHook *hook)
333 {
334         EditMesh *em = G.editMesh;
335         EditVert *eve;
336         int index=0, nr=0;
337         
338         for(eve= em->verts.first; eve; eve= eve->next, nr++) {
339                 if(nr==hook->indexar[index]) {
340                         eve->f |= SELECT;
341                         if(index < hook->totindex-1) index++;
342                 }
343         }
344         EM_select_flush();
345 }
346
347 static int return_editlattice_indexar(int **indexar, float *cent)
348 {
349         BPoint *bp;
350         int *index, nr, totvert=0, a;
351         
352         // count
353         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
354         bp= editLatt->def;
355         while(a--) {
356                 if(bp->f1 & SELECT) {
357                         if(bp->hide==0) totvert++;
358                 }
359                 bp++;
360         }
361
362         if(totvert==0) return 0;
363         
364         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
365         nr= 0;
366         cent[0]= cent[1]= cent[2]= 0.0;
367         
368         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
369         bp= editLatt->def;
370         while(a--) {
371                 if(bp->f1 & SELECT) {
372                         if(bp->hide==0) {
373                                 *index= nr; index++;
374                                 VecAddf(cent, cent, bp->vec);
375                         }
376                 }
377                 bp++;
378                 nr++;
379         }
380         
381         VecMulf(cent, 1.0/(float)totvert);
382         
383         return totvert;
384 }
385
386 static void select_editlattice_hook(ObHook *hook)
387 {
388         BPoint *bp;
389         int index=0, nr=0, a;
390         
391         // count
392         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
393         bp= editLatt->def;
394         while(a--) {
395                 if(hook->indexar[index]==nr) {
396                         bp->f1 |= SELECT;
397                         if(index < hook->totindex-1) index++;
398                 }
399                 nr++;
400                 bp++;
401         }
402 }
403
404 static int return_editcurve_indexar(int **indexar, float *cent)
405 {
406         extern ListBase editNurb;
407         Nurb *nu;
408         BPoint *bp;
409         BezTriple *bezt;
410         int *index, a, nr, totvert=0;
411         
412         for(nu= editNurb.first; nu; nu= nu->next) {
413                 if((nu->type & 7)==CU_BEZIER) {
414                         bezt= nu->bezt;
415                         a= nu->pntsu;
416                         while(a--) {
417                                 if(bezt->f1 & SELECT) totvert++;
418                                 if(bezt->f2 & SELECT) totvert++;
419                                 if(bezt->f3 & SELECT) totvert++;
420                                 bezt++;
421                         }
422                 }
423                 else {
424                         bp= nu->bp;
425                         a= nu->pntsu*nu->pntsv;
426                         while(a--) {
427                                 if(bp->f1 & SELECT) totvert++;
428                                 bp++;
429                         }
430                 }
431         }
432         if(totvert==0) return 0;
433         
434         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
435         nr= 0;
436         cent[0]= cent[1]= cent[2]= 0.0;
437         
438         for(nu= editNurb.first; nu; nu= nu->next) {
439                 if((nu->type & 7)==CU_BEZIER) {
440                         bezt= nu->bezt;
441                         a= nu->pntsu;
442                         while(a--) {
443                                 if(bezt->f1 & SELECT) {
444                                         *index= nr; index++;
445                                         VecAddf(cent, cent, bezt->vec[0]);
446                                 }
447                                 nr++;
448                                 if(bezt->f2 & SELECT) {
449                                         *index= nr; index++;
450                                         VecAddf(cent, cent, bezt->vec[1]);
451                                 }
452                                 nr++;
453                                 if(bezt->f3 & SELECT) {
454                                         *index= nr; index++;
455                                         VecAddf(cent, cent, bezt->vec[2]);
456                                 }
457                                 nr++;
458                                 bezt++;
459                         }
460                 }
461                 else {
462                         bp= nu->bp;
463                         a= nu->pntsu*nu->pntsv;
464                         while(a--) {
465                                 if(bp->f1 & SELECT) {
466                                         *index= nr; index++;
467                                         VecAddf(cent, cent, bp->vec);
468                                 }
469                                 nr++;
470                                 bp++;
471                         }
472                 }
473         }
474         
475         VecMulf(cent, 1.0/(float)totvert);
476         
477         return totvert;
478 }
479
480 static void select_editcurve_hook(ObHook *hook)
481 {
482         extern ListBase editNurb;
483         Nurb *nu;
484         BPoint *bp;
485         BezTriple *bezt;
486         int index=0, a, nr=0;
487         
488         for(nu= editNurb.first; nu; nu= nu->next) {
489                 if((nu->type & 7)==CU_BEZIER) {
490                         bezt= nu->bezt;
491                         a= nu->pntsu;
492                         while(a--) {
493                                 if(nr == hook->indexar[index]) {
494                                         bezt->f1 |= SELECT;
495                                         if(index<hook->totindex-1) index++;
496                                 }
497                                 nr++;
498                                 if(nr == hook->indexar[index]) {
499                                         bezt->f2 |= SELECT;
500                                         if(index<hook->totindex-1) index++;
501                                 }
502                                 nr++;
503                                 if(nr == hook->indexar[index]) {
504                                         bezt->f3 |= SELECT;
505                                         if(index<hook->totindex-1) index++;
506                                 }
507                                 nr++;
508
509                                 bezt++;
510                         }
511                 }
512                 else {
513                         bp= nu->bp;
514                         a= nu->pntsu*nu->pntsv;
515                         while(a--) {
516                                 if(nr == hook->indexar[index]) {
517                                         bp->f1 |= SELECT;
518                                         if(index<hook->totindex-1) index++;
519                                 }
520                                 nr++;
521                                 bp++;
522                         }
523                 }
524         }
525 }
526
527 void add_hook(void)
528 {
529         Object *ob=NULL;
530         ObHook *hook=NULL;
531         float cent[3];
532         int tot=0, *indexar, mode;
533
534         if(G.obedit==NULL) return;
535         
536         if(G.obedit->hooks.first)
537                 mode= pupmenu("Hooks %t|Add Hook, To New Empty %x1|Add Hook, To Selected Object %x2|Remove... %x3|Reassign... %x4|Select... %x5|Clear Offset...%x6");
538         else
539                 mode= pupmenu("Hooks %t|Add, New Empty %x1|Add, To Selected Object %x2");
540
541         if(mode<1) return;
542         
543         /* preconditions */
544
545         if(mode==2) { // selected object
546                 Base *base= FIRSTBASE;
547                 while(base) {
548                         if TESTBASELIB(base) {
549                                 if(base!=BASACT) {
550                                         ob= base->object;
551                                         break;
552                                 }
553                         }
554                         base= base->next;
555                 }
556                 if(ob==NULL) {
557                         error("Requires selected Object");
558                         return;
559                 }
560         }
561         else if(mode!=1) {
562                 int maxlen=0, a, nr;
563                 char *cp;
564                 
565                 // make pupmenu with hooks
566                 for(hook= G.obedit->hooks.first; hook; hook= hook->next) maxlen+=32;
567                 
568                 if(maxlen==0) {
569                         error("Object has no hooks yet");
570                         return;
571                 }
572                 
573                 cp= MEM_callocN(maxlen+32, "temp string");
574                 if(mode==3) strcpy(cp, "Remove %t|");
575                 else if(mode==4) strcpy(cp, "Reassign %t|");
576                 else if(mode==5) strcpy(cp, "Select %t|");
577                 else if(mode==6) strcpy(cp, "Clear Offset %t|");
578                 
579                 for(hook= G.obedit->hooks.first; hook; hook= hook->next) {
580                         strcat(cp, hook->name);
581                         strcat(cp, " |");
582                 }
583         
584                 nr= pupmenu(cp);
585                 MEM_freeN(cp);
586                 
587                 if(nr<1) return;
588                 
589                 a= 1;
590                 for(hook= G.obedit->hooks.first; hook; hook= hook->next, a++) {
591                         if(a==nr) break;
592                 }
593                 ob= hook->parent;
594         }
595
596         /* do it, new hooks or reassign */
597         if(mode==1 || mode==2 || mode==4) {
598         
599                 switch(G.obedit->type) {
600                 case OB_MESH:
601                         tot= return_editmesh_indexar(&indexar, cent);
602                         break;
603                 case OB_CURVE:
604                 case OB_SURF:
605                         tot= return_editcurve_indexar(&indexar, cent);
606                         break;
607                 case OB_LATTICE:
608                         tot= return_editlattice_indexar(&indexar, cent);
609                         break;
610                 }
611                 
612                 if(tot==0) {
613                         error("Requires selected vertices");
614                 }
615                 else {
616                         
617                         if(mode==1) {
618                                 Base *base= BASACT;
619
620                                 ob= add_object(OB_EMPTY);
621                                 /* transform cent to global coords for loc */
622                                 VecMat4MulVecfl(ob->loc, G.obedit->obmat, cent);
623                                 
624                                 /* restore, add_object sets active */
625                                 BASACT= base;
626                         }
627                         /* if mode is 2 or 4, ob has been set */
628                                                                         
629                         /* new hook */
630                         if(mode==1 || mode==2) {
631                                 hook= MEM_callocN(sizeof(ObHook), "new hook");
632                                 BLI_addtail(&G.obedit->hooks, hook);
633                                 strncpy(hook->name, ob->id.name+2, 30);
634                                 hook->force= 1.0;
635                         }
636                         else MEM_freeN(hook->indexar); // reassign, hook was set
637
638                         hook->parent= ob;
639                         hook->indexar= indexar;
640                         VECCOPY(hook->cent, cent);
641                         hook->totindex= tot;
642                         
643                         if(mode==1 || mode==2) {
644                                 /* matrix calculus */
645                                 /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */
646                                 /*        (parentinv         )                          */
647                                 
648                                 where_is_object(ob);
649                 
650                                 Mat4Invert(ob->imat, ob->obmat);
651                                 /* apparently this call goes from right to left... */
652                                 Mat4MulSerie(hook->parentinv, ob->imat, G.obedit->obmat, NULL, 
653                                                         NULL, NULL, NULL, NULL, NULL);
654                         }
655                 }
656         }
657         else if(mode==3) { // remove
658                 BLI_remlink(&G.obedit->hooks, hook);
659                 MEM_freeN(hook->indexar);
660                 MEM_freeN(hook);
661         }
662         else if(mode==5) { // select
663                 if(G.obedit->type==OB_MESH) select_editmesh_hook(hook);
664                 else if(G.obedit->type==OB_LATTICE) select_editlattice_hook(hook);
665                 else if(G.obedit->type==OB_CURVE) select_editcurve_hook(hook);
666                 else if(G.obedit->type==OB_SURF) select_editcurve_hook(hook);
667         }
668         else if(mode==6) { // clear offset
669                 where_is_object(ob);    // ob is hook->parent
670
671                 Mat4Invert(ob->imat, ob->obmat);
672                 /* this call goes from right to left... */
673                 Mat4MulSerie(hook->parentinv, ob->imat, G.obedit->obmat, NULL, 
674                                         NULL, NULL, NULL, NULL, NULL);
675         }
676
677         allqueue(REDRAWVIEW3D, 0);
678         allqueue(REDRAWBUTSOBJECT, 0);
679         BIF_undo_push("Add hook");
680 }
681
682 void make_track(void)
683 {
684         Base *base;
685         short mode=0;
686         
687         if(G.scene->id.lib) return;
688         if(G.obedit) {
689                 return;
690         }
691         if(BASACT==0) return;
692
693         mode= pupmenu("Make Track %t|TrackTo Constraint %x1|LockTrack Constraint %x2|Old Track %x3");
694         if (mode == 0){
695                 return;
696         }
697         else if (mode == 1){
698                 bConstraint *con;
699                 bTrackToConstraint *data;
700
701                 base= FIRSTBASE;
702                 while(base) {
703                         if TESTBASELIB(base) {
704                                 if(base!=BASACT) {
705                                         con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
706                                         strcpy (con->name, "AutoTrack");
707
708                                         data = con->data;
709                                         data->tar = BASACT->object;
710
711                                         /* Lamp and Camera track differently by default */
712                                         if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
713                                                 data->reserved1 = TRACK_nZ;
714                                                 data->reserved2 = UP_Y;
715                                         }
716
717                                         add_constraint_to_object(con, base->object);
718                                 }
719                         }
720                         base= base->next;
721                 }
722
723                 test_scene_constraints();
724                 allqueue(REDRAWVIEW3D, 0);
725                 sort_baselist(G.scene);
726         }
727         else if (mode == 2){
728                 bConstraint *con;
729                 bLockTrackConstraint *data;
730
731                 base= FIRSTBASE;
732                 while(base) {
733                         if TESTBASELIB(base) {
734                                 if(base!=BASACT) {
735                                         con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
736                                         strcpy (con->name, "AutoTrack");
737
738                                         data = con->data;
739                                         data->tar = BASACT->object;
740
741                                         /* Lamp and Camera track differently by default */
742                                         if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
743                                                 data->trackflag = TRACK_nZ;
744                                                 data->lockflag = LOCK_Y;
745                                         }
746
747                                         add_constraint_to_object(con, base->object);
748                                 }
749                         }
750                         base= base->next;
751                 }
752
753                 test_scene_constraints();
754                 allqueue(REDRAWVIEW3D, 0);
755                 sort_baselist(G.scene);
756         }
757         else if (mode == 3){
758                 base= FIRSTBASE;
759                 while(base) {
760                         if TESTBASELIB(base) {
761                                 if(base!=BASACT) {
762
763                                         base->object->track= BASACT->object;
764                                 }
765                         }
766                         base= base->next;
767                 }
768
769                 test_scene_constraints();
770                 allqueue(REDRAWVIEW3D, 0);
771                 allqueue(REDRAWOOPS, 0);
772                 sort_baselist(G.scene);
773         }
774         BIF_undo_push("Make Track");
775 }
776
777 void apply_obmat(Object *ob)
778 {
779         float mat[3][3], imat[3][3], tmat[3][3];
780         
781         /* from obmat to loc rot size */
782         
783         if(ob==0) return;
784         Mat3CpyMat4(mat, ob->obmat);
785         
786         VECCOPY(ob->loc, ob->obmat[3]);
787         
788         if(ob->transflag & OB_QUAT) {
789                 Mat3ToQuat(mat, ob->quat);
790                 QuatToMat3(ob->quat, tmat);
791         }
792         else {
793                 Mat3ToEul(mat, ob->rot);
794                 EulToMat3(ob->rot, tmat);
795         }
796         Mat3Inv(imat, tmat);
797         
798         Mat3MulMat3(tmat, imat, mat);
799         
800         ob->size[0]= tmat[0][0];
801         ob->size[1]= tmat[1][1];
802         ob->size[2]= tmat[2][2];
803
804 }
805
806 void clear_parent(void)
807 {
808         Object *par;
809         Base *base;
810         int mode;
811         
812         if(G.obedit) return;
813         if(G.scene->id.lib) return;
814
815         mode= pupmenu("OK? %t|Clear Parent %x1|Clear and Keep Transformation (Clear Track) %x2|Clear Parent Inverse %x3");
816         
817         if(mode<1) return;
818
819         base= FIRSTBASE;
820         while(base) {
821                 if TESTBASELIB(base) {
822                         par= 0;
823                         if(mode==1 || mode==2) {
824                                 if(base->object->type==OB_IKA) {
825                                         Ika *ika= base->object->data;
826                                         ika->parent= 0;
827                                 }
828                                 par= base->object->parent;
829                                 base->object->parent= 0;
830                         
831                                 if(mode==2) {
832                                         base->object->track= 0;
833                                         apply_obmat(base->object);
834                                 }
835                         }
836                         else if(mode==3) {
837                                 Mat4One(base->object->parentinv);
838                         }
839                         
840                         if(par) {
841                                 makeDispList(base->object);     // just always..., checks are not available well (ton)
842                         }
843                 }
844                 base= base->next;
845         }
846
847         test_scene_constraints();
848         allqueue(REDRAWVIEW3D, 0);
849         allqueue(REDRAWOOPS, 0);
850         
851         BIF_undo_push("Clear Parent");  
852 }
853
854 void clear_track(void)
855 {
856         Base *base;
857         int mode;
858         
859         if(G.obedit) return;
860         if(G.scene->id.lib) return;
861
862         mode= pupmenu("OK? %t|Clear Track %x1| Clear Track and Keep Transform %x2");
863
864         if(mode<1) return;
865
866         base= FIRSTBASE;
867         while(base) {
868                 if TESTBASELIB(base) {
869                         base->object->track= 0;
870
871                         if(mode==2) {
872                                 apply_obmat(base->object);
873                         }                       
874                 }
875                 base= base->next;
876         }
877         test_scene_constraints();
878         allqueue(REDRAWVIEW3D, 0);
879         allqueue(REDRAWOOPS, 0);
880         
881         BIF_undo_push("Clear Track");   
882 }
883
884 void clear_object(char mode)
885 {
886         Base *base;
887         Object *ob;
888         float *v1, *v3, mat[3][3];
889         char *str=NULL;
890         
891         if(G.obedit) return;
892         if(G.scene->id.lib) return;
893         
894         if(mode=='r') str= "Clear rotation";
895         else if(mode=='g') str= "Clear location";
896         else if(mode=='s') str= "Clear size";
897         else if(mode=='o') str= "Clear origin";
898         else return;
899         
900         if (G.obpose){
901
902                 switch (G.obpose->type){
903                 case OB_ARMATURE:
904                         clear_armature (G.obpose, mode);
905 #if 1
906                         clear_pose_constraint_status(G.obpose);
907                         make_displists_by_armature (G.obpose);
908 #endif
909                         break;
910                 }
911
912                 allqueue(REDRAWVIEW3D, 0);
913                 BIF_undo_push(str);
914                 return;
915         }
916
917         base= FIRSTBASE;
918         while(base) {
919                 if TESTBASELIB(base) {
920                         ob= base->object;
921                         
922                         if(mode=='r') {
923                                 memset(ob->rot, 0, 3*sizeof(float));
924                                 memset(ob->drot, 0, 3*sizeof(float));
925                                 QuatOne(ob->quat);
926                                 QuatOne(ob->dquat);
927                         }
928                         else if(mode=='g') {
929                                 memset(ob->loc, 0, 3*sizeof(float));
930                                 memset(ob->dloc, 0, 3*sizeof(float));
931                         }
932                         else if(mode=='s') {
933                                 memset(ob->dsize, 0, 3*sizeof(float));
934                                 ob->size[0]= 1.0;
935                                 ob->size[1]= 1.0;
936                                 ob->size[2]= 1.0;
937                         }
938                         else if(mode=='o') {
939                                 if(ob->parent) {
940                                         v1= ob->loc;
941                                         v3= ob->parentinv[3];
942                                         
943                                         Mat3CpyMat4(mat, ob->parentinv);
944                                         VECCOPY(v3, v1);
945                                         v3[0]= -v3[0];
946                                         v3[1]= -v3[1];
947                                         v3[2]= -v3[2];
948                                         Mat3MulVecfl(mat, v3);
949                                 }
950                         }
951                         
952                         if(ob->parent && ob->partype==PARSKEL)
953                                 freedisplist(&ob->disp);
954                         else if(ob->hooks.first)
955                                 freedisplist(&ob->disp);
956                 }
957                 base= base->next;
958         }
959         
960         allqueue(REDRAWVIEW3D, 0);
961         BIF_undo_push(str);
962 }
963
964 void reset_slowparents(void)
965 {
966         /* back to original locations */
967         Base *base;
968         
969         base= FIRSTBASE;
970         while(base) {
971                 if(base->object->parent) {
972                         if(base->object->partype & PARSLOW) {
973                                 base->object->partype -= PARSLOW;
974                                 where_is_object(base->object);
975                                 base->object->partype |= PARSLOW;
976                         }
977                 }
978                 base= base->next;
979         }
980 }
981
982 void set_slowparent(void)
983 {
984         Base *base;
985
986         if( okee("Set slow parent")==0 ) return;
987
988         base= FIRSTBASE;
989         while(base) {
990                 if TESTBASELIB(base) {
991                         if(base->object->parent) base->object->partype |= PARSLOW;
992                 }
993                 base= base->next;
994         }
995         BIF_undo_push("Slow parent");
996 }
997
998 void make_vertex_parent(void)
999 {
1000         EditMesh *em = G.editMesh;
1001         EditVert *eve;
1002         Base *base;
1003         Nurb *nu;
1004         BezTriple *bezt;
1005         BPoint *bp;
1006         Object *par, *ob;
1007         int a, v1=0, v2=0, v3=0, nr=1;
1008         
1009         /* we need 1 to 3 selected vertices */
1010         
1011         if(G.obedit->type==OB_MESH) {
1012                 eve= em->verts.first;
1013                 while(eve) {
1014                         if(eve->f & 1) {
1015                                 if(v1==0) v1= nr;
1016                                 else if(v2==0) v2= nr;
1017                                 else if(v3==0) v3= nr;
1018                                 else break;
1019                         }
1020                         nr++;
1021                         eve= eve->next;
1022                 }
1023         }
1024         else if ELEM(G.obedit->type, OB_SURF, OB_CURVE) {
1025                 nu= editNurb.first;
1026                 while(nu) {
1027                         if((nu->type & 7)==CU_BEZIER) {
1028                                 bezt= nu->bezt;
1029                                 a= nu->pntsu;
1030                                 while(a--) {
1031                                         if(BEZSELECTED(bezt)) {
1032                                                 if(v1==0) v1= nr;
1033                                                 else if(v2==0) v2= nr;
1034                                                 else if(v3==0) v3= nr;
1035                                                 else break;
1036                                         }
1037                                         nr++;
1038                                         bezt++;
1039                                 }
1040                         }
1041                         else {
1042                                 bp= nu->bp;
1043                                 a= nu->pntsu*nu->pntsv;
1044                                 while(a--) {
1045                                         if(bp->f1 & SELECT) {
1046                                                 if(v1==0) v1= nr;
1047                                                 else if(v2==0) v2= nr;
1048                                                 else if(v3==0) v3= nr;
1049                                                 else break;
1050                                         }
1051                                         nr++;
1052                                         bp++;
1053                                 }
1054                         }
1055                         nu= nu->next;
1056                 }
1057                 
1058         }
1059         
1060         if( !(v1 && v2==0 && v3==0) && !(v1 && v2 && v3) ) {
1061                 error("Select either 1 or 3 vertices to parent to");
1062                 return;
1063         }
1064         
1065         if(okee("Make vertex parent")==0) return;
1066         
1067         base= FIRSTBASE;
1068         while(base) {
1069                 if TESTBASELIB(base) {
1070                         if(base!=BASACT) {
1071                                 ob= base->object;
1072                                 par= BASACT->object->parent;
1073                                 
1074                                 while(par) {
1075                                         if(par==ob) break;
1076                                         par= par->parent;
1077                                 }
1078                                 if(par) {
1079                                         error("Loop in parents");
1080                                 }
1081                                 else {
1082                                         ob->parent= BASACT->object;
1083                                         if(v3) {
1084                                                 ob->partype= PARVERT3;
1085                                                 ob->par1= v1-1;
1086                                                 ob->par2= v2-1;
1087                                                 ob->par3= v3-1;
1088
1089                                                 /* inverse parent matrix */
1090                                                 what_does_parent(ob);
1091                                                 Mat4Invert(ob->parentinv, workob.obmat);
1092                                                 clear_workob();
1093                                         }
1094                                         else {
1095                                                 ob->partype= PARVERT1;
1096                                                 ob->par1= v1-1;
1097
1098                                                 /* inverse parent matrix */
1099                                                 what_does_parent(ob);
1100                                                 Mat4Invert(ob->parentinv, workob.obmat);
1101                                                 clear_workob();
1102                                         }
1103                                 }
1104                         }
1105                 }
1106                 base= base->next;
1107         }
1108         allqueue(REDRAWVIEW3D, 0);
1109         
1110         // BIF_undo_push(str); not, conflicts with editmode undo...
1111 }
1112
1113 int test_parent_loop(Object *par, Object *ob)
1114 {
1115         /* test if 'ob' is a parent somewhere in par's parents */
1116         
1117         if(par==0) return 0;
1118         if(ob == par) return 1;
1119         
1120         if(par->type==OB_IKA) {
1121                 Ika *ika= par->data;
1122                 
1123                 if( ob == ika->parent ) return 1;
1124                 if( test_parent_loop(ika->parent, ob) ) return 1;
1125         }
1126
1127         return test_parent_loop(par->parent, ob);
1128
1129 }
1130
1131 void make_parent(void)
1132 {
1133         Base *base;
1134         Object *par;
1135         short qual, mode=0, limbnr=0, effchild=0;
1136         char *bonestr=NULL;
1137         Bone    *bone=NULL;
1138         int     bonenr;
1139
1140         if(G.scene->id.lib) return;
1141         if(G.obedit) {
1142                 if ELEM3(G.obedit->type, OB_MESH, OB_CURVE, OB_SURF) make_vertex_parent();
1143                 else if (G.obedit->type==OB_ARMATURE) make_bone_parent();
1144                 return;
1145         }
1146         if(BASACT==0) return;
1147         
1148         qual= G.qual;
1149         par= BASACT->object;
1150
1151         if(par->type == OB_CURVE){
1152                 bConstraint *con;
1153                 bFollowPathConstraint *data;
1154
1155                 mode= pupmenu("Make Parent %t|Normal Parent %x1|Follow Path %x2|Curve Deform %x3");
1156                 if(mode<=0){
1157                         return;
1158                 }
1159                 else if(mode==1) {
1160                         mode= PAROBJECT;
1161                 }
1162                 else if(mode==3) {
1163                         mode= PARSKEL;
1164                 }
1165                 else if(mode==2) {
1166
1167                         base= FIRSTBASE;
1168                         while(base) {
1169                                 if TESTBASELIB(base) {
1170                                         if(base!=BASACT) {
1171                                                 float cmat[4][4], vec[3], size[3];
1172
1173                                                 con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
1174                                                 strcpy (con->name, "AutoPath");
1175
1176                                                 data = con->data;
1177                                                 data->tar = BASACT->object;
1178
1179                                                 add_constraint_to_object(con, base->object);
1180
1181                                                 get_constraint_target_matrix(con, TARGET_OBJECT, NULL, cmat, size, G.scene->r.cfra - base->object->sf);
1182                                                 VecSubf(vec, base->object->obmat[3], cmat[3]);
1183
1184                                                 base->object->loc[0] = vec[0];
1185                                                 base->object->loc[1] = vec[1];
1186                                                 base->object->loc[2] = vec[2];
1187                                         }
1188                                 }
1189                                 base= base->next;
1190                         }
1191
1192                         test_scene_constraints();
1193                         allqueue(REDRAWVIEW3D, 0);
1194                         sort_baselist(G.scene);
1195                         BIF_undo_push("make Parent");
1196                         return;
1197                 }
1198         }
1199         else if(par->type == OB_ARMATURE){
1200
1201                         base= FIRSTBASE;
1202                         while(base) {
1203                                 if TESTBASELIB(base) {
1204                                         if(base!=BASACT) {
1205                                                 if(base->object->type==OB_MESH) {
1206                                                         mode= pupmenu("Make Parent To%t|Bone %x1|Armature %x2|Object %x3");
1207                                                         break;
1208                                                 }
1209                                                 else {
1210                                                         mode= pupmenu("Make Parent To %t|Bone %x1|Object %x3");
1211                                                         break;
1212                                                 }
1213                                         }
1214                                 }
1215                                 base= base->next;
1216                         }
1217                 
1218                         switch (mode){
1219                         case 1:
1220                                 mode=PARBONE;
1221                                 /* Make bone popup menu */
1222
1223                                 bonestr = make_bone_menu(get_armature(par));
1224                 //              if(mbutton(&bone, bonestr, 1, 24, "Bone: ")==0) {
1225
1226                                 bonenr= pupmenu_col(bonestr, 20);
1227                                 if (bonestr)
1228                                         MEM_freeN (bonestr);
1229                                 
1230                                 if (bonenr==-1){
1231                                         allqueue(REDRAWVIEW3D, 0);
1232                                         return;
1233                                 }
1234
1235                                 apply_pose_armature(get_armature(par), par->pose, 0);
1236                                 bone=get_indexed_bone(get_armature(par), bonenr); 
1237                                 if (!bone){
1238                 //                      error ("Invalid bone!");
1239                                         allqueue(REDRAWVIEW3D, 0);
1240                                         return;
1241                                 }
1242
1243                                 break;
1244                         case 2:
1245                                 mode=PARSKEL;
1246                                 break;
1247                         case 3:
1248                                 mode=PAROBJECT;
1249                                 break;
1250                         default:
1251                                 return;
1252                         }
1253         }
1254         else {
1255                 if(qual & LR_SHIFTKEY) {
1256                         if(okee("Make parent without inverse")==0) return;
1257                 }
1258                 else {
1259                         if(qual & LR_ALTKEY) {
1260                                 if(okee("Make vertex parent")==0) return;
1261                         }
1262                         else if(okee("Make parent")==0) return;
1263
1264                         /* test effchild */
1265                         base= FIRSTBASE;
1266                         while(base) {
1267                                 if TESTBASELIB(base) {
1268                                         if(base->object->type==OB_IKA && base->object != par) {
1269                                                 if(effchild==0) {
1270                                                         if(okee("Effector as Child")) effchild= 1;
1271                                                         else effchild= 2;
1272                                                 }
1273                                         }
1274                                 }
1275                                 if(effchild) break;
1276                                 base= base->next;
1277                         }
1278                         
1279                         /* now we'll clearparentandkeeptransform all objects */
1280                         base= FIRSTBASE;
1281                         while(base) {
1282                                 if TESTBASELIB(base) {
1283                                         if(base!=BASACT && base->object->parent) {
1284                                                 if(base->object->type==OB_IKA && effchild==1);
1285                                                 else {
1286                                                         base->object->parent= 0;
1287                                                         apply_obmat(base->object);
1288                                                 }
1289                                         }
1290                                 }
1291                                 base= base->next;
1292                         }
1293                 }
1294         }
1295         
1296         
1297         base= FIRSTBASE;
1298         while(base) {
1299                 if TESTBASELIB(base) {
1300                         if(base!=BASACT) {
1301                                 
1302                                 if( test_parent_loop(par, base->object) ) {
1303                                         error("Loop in parents");
1304                                 }
1305                                 else {
1306                                         
1307                                         /* the ifs below are horrible code (ton) */
1308                                         
1309                                         if(par->type==OB_IKA){
1310                                                 base->object->partype= mode;
1311                                                 base->object->par1= limbnr;
1312                                         }
1313                                         else if (par->type==OB_ARMATURE){
1314                                                 base->object->partype= mode;
1315                                                 if (bone)
1316                                                         strcpy (base->object->parsubstr, bone->name);
1317                                                 else
1318                                                         base->object->parsubstr[0]=0;
1319                                         }
1320                                         else {
1321                                                 if(qual & LR_ALTKEY) {
1322                                                         base->object->partype= PARVERT1;
1323                                                 }
1324                                                 else if(par->type==OB_CURVE) {
1325                                                         base->object->partype= mode;
1326                                                 }
1327                                                 else {
1328                                                         base->object->partype= PAROBJECT;
1329                                                 }
1330                                         }
1331                                         base->object->parent= par;
1332                                         
1333                                         /* calculate inverse parent matrix? */
1334                                         if( (qual & LR_SHIFTKEY) ) {
1335                                                 /* not... */
1336                                                 Mat4One(base->object->parentinv);
1337                                                 memset(base->object->loc, 0, 3*sizeof(float));
1338                                         }
1339                                         else {
1340                                                 if(mode==PARSKEL && par->type == OB_ARMATURE) {
1341                                                         /* Prompt the user as to whether he wants to
1342                                                                 * add some vertex groups based on the bones
1343                                                                 * in the parent armature.
1344                                                                 */
1345                                                         create_vgroups_from_armature(base->object, 
1346                                                                                                                         par);
1347
1348                                                         base->object->partype= PAROBJECT;
1349                                                         what_does_parent(base->object);
1350                                                         Mat4One (base->object->parentinv);
1351                                                         base->object->partype= mode;
1352                                                 }
1353                                                 else
1354                                                         what_does_parent(base->object);
1355                                                 Mat4Invert(base->object->parentinv, workob.obmat);
1356                                         }
1357                                         
1358                                         if(par->type==OB_LATTICE) makeDispList(base->object);
1359                                         if(par->type==OB_CURVE && mode==PARSKEL) makeDispList(base->object);
1360                                         if(par->type==OB_ARMATURE && mode == PARSKEL){
1361                                                 verify_defgroups(base->object);
1362                                                 makeDispList(base->object);
1363                                         }
1364                                 }
1365                         }
1366                 }
1367                 base= base->next;
1368         }
1369         allqueue(REDRAWVIEW3D, 0);
1370         allqueue(REDRAWOOPS, 0);
1371         
1372         test_scene_constraints();
1373         sort_baselist(G.scene);
1374
1375         BIF_undo_push("make Parent");
1376 }
1377
1378
1379 void enter_editmode(void)
1380 {
1381         Base *base;
1382         Object *ob;
1383         Ika *ika;
1384         ID *id;
1385         Mesh *me;
1386         int ok= 0;
1387         bArmature *arm;
1388         
1389         if(G.scene->id.lib) return;
1390         base= BASACT;
1391         if(base==0) return;
1392         if((G.vd==NULL || (base->lay & G.vd->lay))==0) return;
1393         
1394         ob= base->object;
1395         if(ob->data==0) return;
1396         
1397         id= ob->data;
1398         if(id->lib) {
1399                 error("Can't edit library data");
1400                 return;
1401         }
1402         
1403         if(ob->type==OB_MESH) {
1404                 me= get_mesh(ob);
1405                 if( me==0 ) return;
1406                 if(me->id.lib) {
1407                         error("Can't edit library data");
1408                         return;
1409                 }
1410                 ok= 1;
1411                 G.obedit= ob;
1412                 make_editMesh();
1413                 allqueue(REDRAWBUTSLOGIC, 0);
1414                 if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);
1415         }
1416         if (ob->type==OB_ARMATURE){
1417                 arm=base->object->data;
1418                 if (!arm) return;
1419                 if (arm->id.lib){
1420                         error("Can't edit library data");
1421                         return;
1422                 }
1423                 ok=1;
1424                 G.obedit=ob;
1425                 make_editArmature();
1426                 allqueue (REDRAWVIEW3D,0);
1427         }
1428         else if(ob->type==OB_IKA) {     /* grab type */
1429                 base= FIRSTBASE;
1430                 while(base) {
1431                         if TESTBASE(base) {
1432                                 if(base->object->type==OB_IKA) {
1433                                         ika= base->object->data;
1434                                         if(ika->flag & IK_GRABEFF) ika->flag &= ~IK_GRABEFF;
1435                                         else ika->flag |= IK_GRABEFF;
1436                                 }
1437                         }
1438                         base= base->next;
1439                 }
1440                 allqueue(REDRAWVIEW3D, 0);
1441         }
1442         else if(ob->type==OB_FONT) {
1443                 G.obedit= ob;
1444                 ok= 1;
1445                 make_editText();
1446         }
1447         else if(ob->type==OB_MBALL) {
1448                 G.obedit= ob;
1449                 ok= 1;
1450                 make_editMball();
1451         }
1452         else if(ob->type==OB_LATTICE) {
1453                 G.obedit= ob;
1454                 ok= 1;
1455                 make_editLatt();
1456         }
1457         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1458                 ok= 1;
1459                 G.obedit= ob;
1460                 make_editNurb();
1461         }
1462         allqueue(REDRAWBUTSEDIT, 0);
1463         allqueue(REDRAWOOPS, 0);
1464         countall();
1465         
1466         if(ok) {
1467                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1468         
1469                 allqueue(REDRAWVIEW3D, 1);
1470         }
1471         else G.obedit= 0;
1472
1473         if (G.obpose)
1474                 exit_posemode (1);
1475         scrarea_queue_headredraw(curarea);
1476 }
1477
1478 void make_displists_by_parent(Object *ob) {
1479         Base *base;
1480         
1481         for (base= FIRSTBASE; base; base= base->next)
1482                 if (ob==base->object->parent)
1483                         makeDispList(base->object);
1484 }
1485
1486 void exit_editmode(int freedata)        /* freedata==0 at render, 1= freedata, 2= do undo buffer too */
1487 {
1488         Base *base;
1489         Object *ob;
1490         Curve *cu;
1491
1492         if(G.obedit==NULL) return;
1493
1494         if(G.obedit->type==OB_MESH) {
1495
1496                 /* temporal */
1497                 countall();
1498
1499                 if(G.totvert>MESH_MAX_VERTS) {
1500                         error("Too many vertices");
1501                         return;
1502                 }
1503                 load_editMesh();        /* makes new displist */
1504
1505                 if(freedata) free_editMesh(G.editMesh);
1506
1507                 if(G.f & G_FACESELECT) {
1508                         set_seamtface();
1509                         allqueue(REDRAWIMAGE, 0);
1510                 }
1511
1512                 build_particle_system(G.obedit);
1513         }
1514         else if (G.obedit->type==OB_ARMATURE){  
1515                 load_editArmature();
1516                 if (freedata) free_editArmature();
1517         }
1518         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1519                 load_editNurb();
1520                 if(freedata) freeNurblist(&editNurb);
1521         }
1522         else if(G.obedit->type==OB_FONT && freedata) {
1523                 load_editText();
1524         }
1525         else if(G.obedit->type==OB_LATTICE) {
1526                 load_editLatt();
1527                 if(freedata) free_editLatt();
1528         }
1529         else if(G.obedit->type==OB_MBALL) {
1530                 load_editMball();
1531                 if(freedata) BLI_freelistN(&editelems);
1532         }
1533
1534         ob= G.obedit;
1535         
1536         /* displist make is different in editmode */
1537         if(freedata) G.obedit= NULL;
1538
1539 // need to be here since 
1540 // makeDispList(ob); calls 
1541 // int mesh_modifier(Object *ob, char mode)..
1542 // calling object_softbody_step(ob, ctime);
1543 // needs a valid *up to date* softbody object or NULL pointer at "soft" member  
1544 // anyhow *new* dependacy graph should take care for that :)
1545         if(ob->softflag & 0x01) object_to_softbody(ob,1.0f);
1546         
1547         makeDispList(ob);
1548
1549         /* has this influence at other objects? */
1550         if(ob->type==OB_CURVE) {
1551
1552                 /* test if ob is use as bevelcurve r textoncurve */
1553                 base= FIRSTBASE;
1554                 while(base) {
1555                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
1556                                 cu= base->object->data;
1557                                 
1558                                 if(cu->textoncurve==ob) {
1559                                         text_to_curve(base->object, 0);
1560                                         makeDispList(base->object);
1561                                 }
1562                                 if(cu->bevobj==ob || cu->taperobj==ob) {
1563                                         makeDispList(base->object);
1564                                 }
1565                         }
1566                         base= base->next;
1567                 }
1568                 
1569         }
1570         else if(ob->type==OB_LATTICE) {
1571                 make_displists_by_parent(ob);
1572         }
1573
1574         if(freedata) {
1575                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1576         
1577                 countall();
1578                 allqueue(REDRAWVIEW3D, 1);
1579                 allqueue(REDRAWBUTSEDIT, 0);
1580                 allqueue(REDRAWBUTSLOGIC, 0);
1581                 allqueue(REDRAWOOPS, 0);
1582         }
1583         scrarea_queue_headredraw(curarea);
1584 // faaaaar to late
1585 //      if(ob->softflag & 0x01) object_to_softbody(ob);
1586         
1587         if(G.obedit==NULL && freedata==2) 
1588                 BIF_undo_push("Editmode");
1589 }
1590
1591 void check_editmode(int type)
1592 {
1593         
1594         if (G.obedit==0 || G.obedit->type==type) return;
1595
1596         exit_editmode(2); // freedata, and undo
1597 }
1598
1599 /* 0 == do centre, 1 == centre new, 2 == centre cursor */
1600
1601 void docentre(int centremode)
1602 {
1603         EditMesh *em = G.editMesh;
1604         Base *base;
1605         Object *ob;
1606         Mesh *me, *tme;
1607         Curve *cu;
1608 //      BezTriple *bezt;
1609 //      BPoint *bp;
1610         Nurb *nu, *nu1;
1611         EditVert *eve;
1612         float cent[3], centn[3], min[3], max[3], omat[3][3];
1613         int a;
1614         MVert *mvert;
1615
1616         if(G.scene->id.lib) return;
1617
1618         if(G.obedit) {
1619
1620                 INIT_MINMAX(min, max);
1621         
1622                 if(G.obedit->type==OB_MESH) {
1623                         eve= em->verts.first;
1624                         while(eve) {
1625                                 DO_MINMAX(eve->co, min, max);
1626                                 eve= eve->next;
1627                         }
1628                         cent[0]= (min[0]+max[0])/2.0f;
1629                         cent[1]= (min[1]+max[1])/2.0f;
1630                         cent[2]= (min[2]+max[2])/2.0f;
1631                         
1632                         eve= em->verts.first;
1633                         while(eve) {
1634                                 VecSubf(eve->co, eve->co, cent);                        
1635                                 eve= eve->next;
1636                         }
1637                 }
1638         }
1639         
1640         /* reset flags */
1641         base= FIRSTBASE;
1642         while(base) {
1643                 if TESTBASELIB(base) {
1644                         base->object->flag &= ~OB_DONE;
1645                 }
1646                 base= base->next;
1647         }
1648         me= G.main->mesh.first;
1649         while(me) {
1650                 me->flag &= ~ME_ISDONE;
1651                 me= me->id.next;
1652         }
1653         
1654         base= FIRSTBASE;
1655         while(base) {
1656                 
1657                 if TESTBASELIB(base) {
1658                         if((base->object->flag & OB_DONE)==0) {
1659                                 
1660                                 base->object->flag |= OB_DONE;
1661                                 
1662                                 if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1663                                         
1664                                         if(me->key) {
1665                                                 error("Can't change the center of a mesh with vertex keys");
1666                                                 return;
1667                                         }
1668                                         
1669                                         if(centremode==2) {
1670                                                 VECCOPY(cent, give_cursor());
1671                                                 Mat4Invert(base->object->imat, base->object->obmat);
1672                                                 Mat4MulVecfl(base->object->imat, cent);
1673                                         } else {
1674                                                 INIT_MINMAX(min, max);
1675                 
1676                                                 mvert= me->mvert;
1677                                                 for(a=0; a<me->totvert; a++, mvert++) {
1678                                                         DO_MINMAX(mvert->co, min, max);
1679                                                 }
1680                                 
1681                                                 cent[0]= (min[0]+max[0])/2.0f;
1682                                                 cent[1]= (min[1]+max[1])/2.0f;
1683                                                 cent[2]= (min[2]+max[2])/2.0f;
1684                                         }
1685                                                 
1686                                         mvert= me->mvert;
1687                                         for(a=0; a<me->totvert; a++, mvert++) {
1688                                                 VecSubf(mvert->co, mvert->co, cent);
1689                                         }
1690                                         me->flag |= ME_ISDONE;
1691                                         
1692                                         if(centremode) {
1693                                                 Mat3CpyMat4(omat, base->object->obmat);
1694                                                 
1695                                                 VECCOPY(centn, cent);
1696                                                 Mat3MulVecfl(omat, centn);
1697                                                 base->object->loc[0]+= centn[0];
1698                                                 base->object->loc[1]+= centn[1];
1699                                                 base->object->loc[2]+= centn[2];
1700                                                 
1701                                                 /* other users? */
1702                                                 ob= G.main->object.first;
1703                                                 while(ob) {
1704                                                         if((ob->flag & OB_DONE)==0) {
1705                                                                 tme= get_mesh(ob);
1706                                                                 
1707                                                                 if(tme==me) {
1708                                                                         
1709                                                                         ob->flag |= OB_DONE;
1710
1711                                                                         Mat3CpyMat4(omat, ob->obmat);
1712                                                                         VECCOPY(centn, cent);
1713                                                                         Mat3MulVecfl(omat, centn);
1714                                                                         ob->loc[0]+= centn[0];
1715                                                                         ob->loc[1]+= centn[1];
1716                                                                         ob->loc[2]+= centn[2];
1717                                                                         
1718                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
1719                                                                                 mvert= tme->mvert;
1720                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
1721                                                                                         VecSubf(mvert->co, mvert->co, cent);
1722                                                                                 }
1723                                                                                 tme->flag |= ME_ISDONE;
1724                                                                         }
1725                                                                 }
1726                                                         }
1727                                                         
1728                                                         ob= ob->id.next;
1729                                                 }
1730                                         }
1731                                 
1732                                         /* displist of all users, also this one */
1733                                         makeDispList(base->object);
1734                                         
1735                                         /* DO: check all users... */
1736                                         tex_space_mesh(me);
1737                 
1738                                 }
1739                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1740                                                                         
1741                                         if(G.obedit) {
1742                                                 nu1= editNurb.first;
1743                                         }
1744                                         else {
1745                                                 cu= base->object->data;
1746                                                 nu1= cu->nurb.first;
1747                                         }
1748                                         
1749                                         if(centremode==2) {
1750                                                 VECCOPY(cent, give_cursor());
1751                                                 Mat4Invert(base->object->imat, base->object->obmat);
1752                                                 Mat4MulVecfl(base->object->imat, cent);
1753                                                         
1754                                                         /* Curves need to be 2d, never offset in
1755                                                          * Z. Is a somewhat arbitrary restriction, 
1756                                                          * would probably be nice to remove.
1757                                                          */
1758                                                 cent[2]= 0.0;
1759                                         } else {
1760                                                 INIT_MINMAX(min, max);
1761         
1762                                                 nu= nu1;
1763                                                 while(nu) {
1764                                                         minmaxNurb(nu, min, max);
1765                                                         nu= nu->next;
1766                                                 }
1767                                                 
1768                                                 cent[0]= (min[0]+max[0])/2.0f;
1769                                                 cent[1]= (min[1]+max[1])/2.0f;
1770                                                 cent[2]= (min[2]+max[2])/2.0f;
1771                                         }
1772                                         
1773                                         nu= nu1;
1774                                         while(nu) {
1775                                                 if( (nu->type & 7)==1) {
1776                                                         a= nu->pntsu;
1777                                                         while (a--) {
1778                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1779                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1780                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1781                                                         }
1782                                                 }
1783                                                 else {
1784                                                         a= nu->pntsu*nu->pntsv;
1785                                                         while (a--)
1786                                                                 VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1787                                                 }
1788                                                 nu= nu->next;
1789                                         }
1790                         
1791                                         if(centremode && G.obedit==0) {
1792                                                 Mat3CpyMat4(omat, base->object->obmat);
1793                                                 
1794                                                 Mat3MulVecfl(omat, cent);
1795                                                 base->object->loc[0]+= cent[0];
1796                                                 base->object->loc[1]+= cent[1];
1797                                                 base->object->loc[2]+= cent[2];
1798                                         }
1799                         
1800                                         if(G.obedit) {
1801                                                 makeDispList(G.obedit);
1802                                                 break;
1803                                         }
1804                                         else makeDispList(base->object);
1805         
1806                                 }
1807                                 else if(base->object->type==OB_FONT) {
1808                                         /* get from bb */
1809                                         
1810                                         cu= base->object->data;
1811                                         if(cu->bb==0) return;
1812                                         
1813                                         cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
1814                                         cu->yof= -0.5f -0.5f*( cu->bb->vec[0][1] - cu->bb->vec[2][1]);  /* extra 0.5 is the height of above line */
1815                                         
1816                                         /* not really ok, do this better once! */
1817                                         cu->xof /= cu->fsize;
1818                                         cu->yof /= cu->fsize;
1819                                         
1820                                         text_to_curve(base->object, 0);
1821                                         makeDispList(base->object);
1822                                         
1823                                         allqueue(REDRAWBUTSEDIT, 0);
1824                                 }
1825                         }
1826                 }
1827                 base= base->next;
1828         }
1829
1830         allqueue(REDRAWVIEW3D, 0);
1831         BIF_undo_push("Do Centre");     
1832 }
1833
1834 void docentre_new(void)
1835 {
1836         if(G.scene->id.lib) return;
1837
1838         if(G.obedit) {
1839                 error("Unable to center new in Edit Mode");
1840         }
1841         else {
1842                 docentre(1);
1843         }
1844 }
1845
1846 void docentre_cursor(void)
1847 {
1848         if(G.scene->id.lib) return;
1849
1850         if(G.obedit) {
1851                 error("Unable to center cursor in Edit Mode");
1852         }
1853         else {
1854                 docentre(2);
1855         }
1856 }
1857
1858 void movetolayer(void)
1859 {
1860         Base *base;
1861         unsigned int lay= 0, local;
1862         int islamp= 0;
1863         
1864         if(G.scene->id.lib) return;
1865
1866         base= FIRSTBASE;
1867         while(base) {
1868                 if TESTBASE(base) lay |= base->lay;
1869                 base= base->next;
1870         }
1871         if(lay==0) return;
1872         lay &= 0xFFFFFF;
1873         
1874         if( movetolayer_buts(&lay)==0 ) return;
1875         if(lay==0) return;
1876
1877         base= FIRSTBASE;
1878         while(base) {
1879                 if TESTBASE(base) {
1880                         /* upper byte is used for local view */
1881                         local= base->lay & 0xFF000000;  
1882                         base->lay= lay + local;
1883                         base->object->lay= lay;
1884                         if(base->object->type==OB_LAMP) islamp= 1;
1885                 }
1886                 base= base->next;
1887         }
1888         
1889         if(islamp && G.vd->drawtype == OB_SHADED) reshadeall_displist();
1890
1891         countall();
1892         allqueue(REDRAWBUTSEDIT, 0);
1893         allqueue(REDRAWVIEW3D, 0);
1894         allqueue(REDRAWOOPS, 0);
1895         allqueue(REDRAWINFO, 0);
1896         
1897         BIF_undo_push("Move to layer");
1898 }
1899
1900
1901 void special_editmenu(void)
1902 {
1903         extern short editbutflag;
1904         extern float doublimit;
1905         float fac;
1906         int nr,ret;
1907         short randfac;
1908         
1909         if(G.obedit==0) {
1910                 if(G.f & G_FACESELECT) {
1911                         Mesh *me= get_mesh(OBACT);
1912                         TFace *tface;
1913                         int a;
1914                         
1915                         if(me==0 || me->tface==0) return;
1916                         
1917                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|Clr     Tex%x6|         Shared%x7|         Light%x8|         Invisible%x9|         Collision%x10");
1918         
1919                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1920                                 if(tface->flag & SELECT) {
1921                                         switch(nr) {
1922                                         case 1:
1923                                                 tface->mode |= TF_TEX; break;
1924                                         case 2:
1925                                                 tface->mode |= TF_SHAREDCOL; break;
1926                                         case 3:
1927                                                 tface->mode |= TF_LIGHT; break; 
1928                                         case 4:
1929                                                 tface->mode |= TF_INVISIBLE; break;
1930                                         case 5:
1931                                                 tface->mode |= TF_DYNAMIC; break;
1932                                         case 6:
1933                                                 tface->mode &= ~TF_TEX;
1934                                                 tface->tpage= 0;
1935                                                 break;
1936                                         case 7:
1937                                                 tface->mode &= ~TF_SHAREDCOL; break;
1938                                         case 8:
1939                                                 tface->mode &= ~TF_LIGHT; break;
1940                                         case 9:
1941                                                 tface->mode &= ~TF_INVISIBLE; break;
1942                                         case 10:
1943                                                 tface->mode &= ~TF_DYNAMIC; break;
1944                                         }
1945                                 }
1946                         }
1947                         allqueue(REDRAWVIEW3D, 0);
1948                         allqueue(REDRAWBUTSEDIT, 0);
1949                         BIF_undo_push("Change texture face");
1950                 }
1951                 else if(G.f & G_VERTEXPAINT) {
1952                         Mesh *me= get_mesh(OBACT);
1953                         
1954                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1955                         
1956                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1957                         if(nr==1) {
1958                                 
1959                                 if(me->tface) tface_to_mcol(me);
1960                                 
1961                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1962                                 do_shared_vertexcol(me);
1963                                 
1964                                 if(me->tface) mcol_to_tface(me, 1);
1965                                 BIF_undo_push("Shared VertexCol");
1966                         }
1967                 }
1968                 else {
1969                         Base *base, *base_select= NULL;
1970
1971                         // Get the active object mesh.
1972                         Mesh *me= get_mesh(OBACT);
1973
1974                         // If the active object is a mesh...
1975                         if (me) {
1976                                 // Bring up a little menu with the boolean operation choices on.
1977                                 nr= pupmenu("Boolean %t|Intersect%x1|Union%x2|Difference%x3");
1978
1979                                 if (nr > 0) {
1980                                         // user has made a choice of a menu element.
1981                                         // All of the boolean functions require 2 mesh objects 
1982                                         // we search through the object list to find the other 
1983                                         // selected item and make sure it is distinct and a mesh.
1984
1985                                         base= FIRSTBASE;
1986                                         while(base) {
1987                                                 if(base->flag & SELECT) {
1988                                                         if(base->object != OBACT) base_select= base;
1989                                                 }
1990
1991                                                 base= base->next;
1992                                         }
1993
1994                                         if (base_select) {
1995                                                 if (get_mesh(base_select->object)) {
1996                                                         waitcursor(1);
1997                                                         ret = NewBooleanMesh(BASACT,base_select,nr);
1998                                                         if (ret==0) {
1999                                                                 error("An internal error occurred -- sorry!");
2000                                                         } else if(ret==-1) {
2001                                                                 error("Selected meshes must have faces to perform boolean operations");
2002                                                         }
2003                                                         else BIF_undo_push("Boolean");
2004
2005                                                         waitcursor(0);
2006                                                 } else {
2007                                                         error("Please select 2 meshes");
2008                                                 }
2009                                         } else {
2010                                                 error("Please select 2 meshes");
2011                                         }
2012                                 }
2013
2014                                 allqueue(REDRAWVIEW3D, 0);
2015                         }
2016                 }
2017         }
2018         else if(G.obedit->type==OB_MESH) {
2019
2020                 nr= pupmenu("Specials%t|Subdivide%x1|Subdivide Fractal%x2|Subdivide Smooth%x3|Merge%x4|Remove Doubles%x5|Hide%x6|Reveal%x7|Select Swap%x8|Flip Normals %x9|Smooth %x10|Bevel %x11");
2021                 if(nr>0) waitcursor(1);
2022                 
2023                 switch(nr) {
2024                 case 1:
2025                         subdivideflag(1, 0.0, editbutflag);
2026                         BIF_undo_push("Subdivide");
2027                         break;
2028                 case 2:
2029                         randfac= 10;
2030                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2031                         fac= -( (float)randfac )/100;
2032                         subdivideflag(1, fac, editbutflag);
2033                         BIF_undo_push("Subdivide Fractal");
2034                         break;
2035                 case 3:
2036                         subdivideflag(1, 0.0, editbutflag | B_SMOOTH);
2037                         BIF_undo_push("Subdivide Smooth");
2038                         break;
2039                 case 4:
2040                         mergemenu();
2041                         break;
2042                 case 5:
2043                         notice("Removed %d Vertices", removedoublesflag(1, doublimit));
2044                         BIF_undo_push("Remove Doubles");
2045                         break;
2046                 case 6:
2047                         hide_mesh(0);
2048                         break;
2049                 case 7:
2050                         reveal_mesh();
2051                         break;
2052                 case 8:
2053                         selectswap_mesh();
2054                         break;
2055                 case 9:
2056                         flip_editnormals();
2057                         BIF_undo_push("Flip Normals");
2058                         break;
2059                 case 10:
2060                         vertexsmooth();
2061                         break;
2062                 case 11:
2063                         bevel_menu();
2064                         break;
2065                 }               
2066                 
2067                 makeDispList(G.obedit);
2068                 
2069                 if(nr>0) waitcursor(0);
2070                 
2071         }
2072         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
2073
2074                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2");
2075                 
2076                 switch(nr) {
2077                 case 1:
2078                         subdivideNurb();
2079                         break;
2080                 case 2:
2081                         switchdirectionNurb2();
2082                         break;
2083                 }
2084         }
2085
2086         countall();
2087         allqueue(REDRAWVIEW3D, 0);
2088         
2089 }
2090
2091 void convertmenu(void)
2092 {
2093         Base *base, *basen, *basact, *basedel=NULL;
2094         Object *ob, *ob1;
2095         Curve *cu;
2096         MetaBall *mb;
2097         Mesh *me;
2098         DispList *dl;
2099         int ok=0, nr = 0, a;
2100         
2101         if(G.scene->id.lib) return;
2102
2103         ob= OBACT;
2104         if(ob==0) return;
2105         if(G.obedit) return;
2106         
2107         basact= BASACT; /* will be restored */
2108                 
2109         if(ob->type==OB_FONT) {
2110                 nr= pupmenu("Convert Font to%t|Curve");
2111                 if(nr>0) ok= 1;
2112         }
2113         else if(ob->type==OB_MBALL) {
2114                 nr= pupmenu("Convert Metaball to%t|Mesh (keep original)%x1|Mesh (Delete Original)%x2");
2115                 if(nr>0) ok= 1;
2116         }
2117         else if(ob->type==OB_CURVE) {
2118                 nr= pupmenu("Convert Curve to%t|Mesh");
2119                 if(nr>0) ok= 1;
2120         }
2121         else if(ob->type==OB_SURF) {
2122                 nr= pupmenu("Convert Nurbs Surface to%t|Mesh");
2123                 if(nr>0) ok= 1;
2124         }
2125         else if(ob->type==OB_MESH && mesh_uses_displist((Mesh*) ob->data)) {
2126                 nr= pupmenu("Convert SubSurf to%t|Mesh (Keep Original)%x1|Mesh (Delete Original)%x2");
2127                 if(nr>0) ok= 1;
2128         }
2129         if(ok==0) return;
2130
2131         /* don't forget multiple users! */
2132
2133         /* reset flags */
2134         base= FIRSTBASE;
2135         while(base) {
2136                 if TESTBASELIB(base) {
2137                         base->object->flag &= ~OB_DONE;
2138                 }
2139                 base= base->next;
2140         }
2141
2142         base= FIRSTBASE;
2143         while(base) {
2144                 if TESTBASELIB(base) {
2145                         
2146                         ob= base->object;
2147                         
2148                         if(ob->flag & OB_DONE);
2149                         else if(ob->type==OB_MESH) {
2150                                 Mesh *oldme= ob->data;
2151                                 
2152                                 if (mesh_uses_displist(oldme)) {
2153                                         DispListMesh *dlm;
2154
2155                                         basedel = base;
2156
2157                                         ob->flag |= OB_DONE;
2158
2159                                         ob1= copy_object(ob);
2160
2161                                         basen= MEM_mallocN(sizeof(Base), "duplibase");
2162                                         *basen= *base;
2163                                         BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
2164                                         basen->object= ob1;
2165                                         basen->flag &= ~SELECT;
2166                                                 
2167                                         me= ob1->data;
2168                                         me->id.us--;
2169                                                 
2170                                         ob1->data= add_mesh();
2171                                         G.totmesh++;
2172                                         ob1->type= OB_MESH;
2173
2174                                         me= ob1->data;
2175                                         me->totcol= oldme->totcol;
2176                                         if(ob1->totcol) {
2177                                                 me->mat= MEM_dupallocN(oldme->mat);
2178                                                 for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2179                                         }
2180                                                 
2181                                         dlm= subsurf_make_dispListMesh_from_mesh(oldme, oldme->subdiv, oldme->flag);
2182                                         displistmesh_to_mesh(dlm, ob1->data);
2183                                         displistmesh_free(dlm);
2184
2185                                         /* texspace and normals */
2186                                         BASACT= basen;
2187                                         enter_editmode();
2188                                         exit_editmode(1); // freedata, but no undo
2189                                         BASACT= basact;
2190                                         
2191                                 }
2192                         }
2193                         else if(ob->type==OB_FONT) {
2194                                 if(nr==1) {
2195                                 
2196                                         ob->flag |= OB_DONE;
2197                                 
2198                                         ob->type= OB_CURVE;
2199                                         cu= ob->data;
2200                                         
2201                                         if(cu->vfont) {
2202                                                 cu->vfont->id.us--;
2203                                                 cu->vfont= 0;
2204                                         }
2205                                         /* other users */
2206                                         if(cu->id.us>1) {
2207                                                 ob1= G.main->object.first;
2208                                                 while(ob1) {
2209                                                         if(ob1->data==cu) ob1->type= OB_CURVE;
2210                                                         ob1= ob1->id.next;
2211                                                 }
2212                                         }
2213                                 }
2214                         }
2215                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2216                                 if(nr==1) {
2217
2218                                         ob->flag |= OB_DONE;
2219                                         cu= ob->data;
2220                                         
2221                                         dl= cu->disp.first;
2222                                         if(dl==0) makeDispList(ob);
2223
2224                                         nurbs_to_mesh(ob); /* also does users */
2225
2226                                         /* texspace and normals */
2227                                         BASACT= base;
2228                                         enter_editmode();
2229                                         exit_editmode(1); // freedata, but no undo
2230                                         BASACT= basact;
2231                                 }
2232                         }
2233                         else if(ob->type==OB_MBALL) {
2234                         
2235                                 if(nr==1 || nr == 2) {
2236                                         ob= find_basis_mball(ob);
2237                                         
2238                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
2239                                                 basedel = base;
2240                                         
2241                                                 ob->flag |= OB_DONE;
2242
2243                                                 ob1= copy_object(ob);
2244
2245                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2246                                                 *basen= *base;
2247                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2248                                                 basen->object= ob1;
2249                                                 basen->flag &= ~SELECT;
2250                                                 
2251                                                 mb= ob1->data;
2252                                                 mb->id.us--;
2253                                                 
2254                                                 ob1->data= add_mesh();
2255                                                 G.totmesh++;
2256                                                 ob1->type= OB_MESH;
2257                                                 
2258                                                 me= ob1->data;
2259                                                 me->totcol= mb->totcol;
2260                                                 if(ob1->totcol) {
2261                                                         me->mat= MEM_dupallocN(mb->mat);
2262                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2263                                                 }
2264                                                 
2265                                                 mball_to_mesh(&ob->disp, ob1->data);
2266                                                 tex_space_mesh(me);
2267                                         }
2268                                 }
2269                         }
2270                 }
2271                 base= base->next;
2272                 if(basedel != NULL && nr == 2)
2273                         free_and_unlink_base(basedel);  
2274                 basedel = NULL;                         
2275         }
2276         
2277         test_scene_constraints();       // always call after delete object
2278         
2279         countall();
2280         allqueue(REDRAWVIEW3D, 0);
2281         allqueue(REDRAWOOPS, 0);
2282         allqueue(REDRAWBUTSEDIT, 0);
2283         BIF_undo_push("Convert Object");
2284 }
2285
2286         /* Change subdivision properties of mesh object ob, if
2287          * level==-1 then toggle subsurf, else set to level.
2288          */
2289 void flip_subdivison(Object *ob, int level)
2290 {
2291         Mesh *me = ob->data;
2292
2293         if (level == -1) {
2294                 me->flag ^= ME_SUBSURF;
2295         } else {
2296                 me->subdiv = level;
2297         }
2298
2299         allqueue(REDRAWVIEW3D, 0);
2300         allqueue(REDRAWOOPS, 0);
2301         allqueue(REDRAWBUTSEDIT, 0);
2302         makeDispList(ob);
2303         
2304         BIF_undo_push("Switch subsurf on/off");
2305 }
2306  
2307 void copymenu_properties(Object *ob)
2308 {       
2309         bProperty *prop, *propn, *propc;
2310         Base *base;
2311         int nr, tot=0;
2312         char *str;
2313         
2314         prop= ob->prop.first;
2315         while(prop) {
2316                 tot++;
2317                 prop= prop->next;
2318         }
2319         
2320         if(tot==0) {
2321                 error("No properties in the active object to copy");
2322                 return;
2323         }
2324         
2325         str= MEM_callocN(24+32*tot, "copymenu prop");
2326         
2327         strcpy(str, "Copy Property %t");
2328         
2329         tot= 0; 
2330         prop= ob->prop.first;
2331         while(prop) {
2332                 tot++;
2333                 strcat(str, " |");
2334                 strcat(str, prop->name);
2335                 prop= prop->next;
2336         }
2337
2338         nr= pupmenu(str);
2339         if(nr>0) {
2340                 tot= 0;
2341                 prop= ob->prop.first;
2342                 while(prop) {
2343                         tot++;
2344                         if(tot==nr) break;
2345                         prop= prop->next;
2346                 }
2347                 if(prop) {
2348                         propc= prop;
2349                         
2350                         base= FIRSTBASE;
2351                         while(base) {
2352                                 if(base != BASACT) {
2353                                         if(TESTBASELIB(base)) {
2354                                                 prop= get_property(base->object, propc->name);
2355                                                 if(prop) {
2356                                                         free_property(prop);
2357                                                         BLI_remlink(&base->object->prop, prop);
2358                                                 }
2359                                                 propn= copy_property(propc);
2360                                                 BLI_addtail(&base->object->prop, propn);
2361                                         }
2362                                 }
2363                                 base= base->next;
2364                         }
2365                 }
2366         }
2367         MEM_freeN(str);
2368         allqueue(REDRAWVIEW3D, 0);
2369         
2370         BIF_undo_push("Copy properties");
2371 }
2372
2373 void copymenu_logicbricks(Object *ob)
2374 {
2375         Base *base;
2376         
2377         base= FIRSTBASE;
2378         while(base) {
2379                 if(base->object != ob) {
2380                         if(TESTBASELIB(base)) {
2381                                 
2382                                 /* first: free all logic */
2383                                 free_sensors(&base->object->sensors);                           
2384                                 unlink_controllers(&base->object->controllers);
2385                                 free_controllers(&base->object->controllers);
2386                                 unlink_actuators(&base->object->actuators);
2387                                 free_actuators(&base->object->actuators);
2388                                 
2389                                 /* now copy it, this also works without logicbricks! */
2390                                 clear_sca_new_poins_ob(ob);
2391                                 copy_sensors(&base->object->sensors, &ob->sensors);
2392                                 copy_controllers(&base->object->controllers, &ob->controllers);
2393                                 copy_actuators(&base->object->actuators, &ob->actuators);
2394                                 set_sca_new_poins_ob(base->object);
2395                                 
2396                                 /* some menu settings */
2397                                 base->object->scavisflag= ob->scavisflag;
2398                                 base->object->scaflag= ob->scaflag;
2399                                 
2400                         }
2401                 }
2402                 base= base->next;
2403         }
2404         BIF_undo_push("Copy logic");
2405 }
2406
2407 void copy_attr_menu()
2408 {
2409         Object *ob;
2410         short event;
2411         char str[256];
2412
2413         /* If you change this menu, don't forget to update the menu in header_view3d.c
2414          * view3d_edit_object_copyattrmenu() and in toolbox.c
2415          */
2416         strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Drawtype%x4|Time Offset%x5|Dupli%x6|%l|Mass%x7|Damping%x8|Properties%x9|Logic Bricks%x10|%l");
2417
2418         if(!(ob=OBACT)) return;
2419         
2420         strcat (str, "|Object Constraints%x22");
2421         
2422         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2423                 strcat(str, "|Texture Space%x17");
2424         }       
2425         
2426         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
2427         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
2428
2429         if(ob->type==OB_MESH){
2430                 strcat(str, "|Subdiv%x21");
2431         }
2432
2433         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
2434
2435         event= pupmenu(str);
2436         if(event<= 0) return;
2437         
2438         copy_attr(event);
2439 }
2440
2441 void copy_attr(short event)
2442 {
2443         Object *ob, *obt;
2444         Base *base;
2445         Curve *cu, *cu1;
2446         void *poin1, *poin2=0;
2447         
2448         if(G.scene->id.lib) return;
2449
2450         if(!(ob=OBACT)) return;
2451         
2452         if(G.obedit) {
2453                 /* obedit_copymenu(); */
2454                 return;
2455         }
2456         
2457         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2458                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
2459                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
2460                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
2461         }       
2462
2463         if(event==9) {
2464                 copymenu_properties(ob);
2465                 return;
2466         }
2467         else if(event==10) {
2468                 copymenu_logicbricks(ob);
2469                 return;
2470         }
2471
2472         base= FIRSTBASE;
2473         while(base) {
2474                 if(base != BASACT) {
2475                         if(TESTBASELIB(base)) {
2476                                 
2477                                 if(event==1) {  /* loc */
2478                                         VECCOPY(base->object->loc, ob->loc);
2479                                         VECCOPY(base->object->dloc, ob->dloc);
2480                                 }
2481                                 else if(event==2) {  /* rot */
2482                                         VECCOPY(base->object->rot, ob->rot);
2483                                         VECCOPY(base->object->drot, ob->drot);
2484                                         VECCOPY(base->object->quat, ob->quat);
2485                                         VECCOPY(base->object->dquat, ob->dquat);
2486                                 }
2487                                 else if(event==3) {  /* size */
2488                                         VECCOPY(base->object->size, ob->size);
2489                                         VECCOPY(base->object->dsize, ob->dsize);
2490                                 }
2491                                 else if(event==4) {  /* drawtype */
2492                                         base->object->dt= ob->dt;
2493                                         base->object->dtx= ob->dtx;
2494                                         }
2495                                 else if(event==5) {  /* time offs */
2496                                         base->object->sf= ob->sf;
2497                                 }
2498                                 else if(event==6) {  /* dupli */
2499                                         base->object->dupon= ob->dupon;
2500                                         base->object->dupoff= ob->dupoff;
2501                                         base->object->dupsta= ob->dupsta;
2502                                         base->object->dupend= ob->dupend;
2503                                         
2504                                         base->object->transflag &= ~OB_DUPLI;
2505                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2506                                 }
2507                                 else if(event==7) {     /* mass */
2508                                         base->object->mass= ob->mass;
2509                                 }
2510                                 else if(event==8) {     /* damping */
2511                                         base->object->damping= ob->damping;
2512                                         base->object->rdamping= ob->rdamping;
2513                                 }
2514                                 else if(event==17) {    /* tex space */
2515                                         obt= base->object;
2516                                         poin1= 0;
2517                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2518                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2519                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2520                                         
2521                                         if(poin1) {
2522                                                 memcpy(poin1, poin2, 4+12+12+12);
2523                                         
2524                                                 if(obt->type==OB_MESH) tex_space_mesh(obt->data);
2525                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2526                                                 else tex_space_curve(obt->data);
2527                                         }
2528                                 }
2529                                 else if(event==18) {    /* font settings */
2530                                         
2531                                         if(base->object->type==ob->type) {
2532                                                 cu= ob->data;
2533                                                 cu1= base->object->data;
2534                                                 
2535                                                 cu1->spacemode= cu->spacemode;
2536                                                 cu1->spacing= cu->spacing;
2537                                                 cu1->linedist= cu->linedist;
2538                                                 cu1->shear= cu->shear;
2539                                                 cu1->fsize= cu->fsize;
2540                                                 cu1->xof= cu->xof;
2541                                                 cu1->yof= cu->yof;
2542                                                 cu1->textoncurve= cu->textoncurve;
2543                                                 if(cu1->vfont) cu1->vfont->id.us--;
2544                                                 cu1->vfont= cu->vfont;
2545                                                 id_us_plus((ID *)cu1->vfont);
2546                                                 text_to_curve(base->object, 0);
2547                                                 
2548                                                 strcpy(cu1->family, cu->family);
2549                                                 
2550                                                 makeDispList(base->object);
2551                                         }
2552                                 }
2553                                 else if(event==19) {    /* bevel settings */
2554                                         
2555                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2556                                                 cu= ob->data;
2557                                                 cu1= base->object->data;
2558                                                 
2559                                                 cu1->bevobj= cu->bevobj;
2560                                                 cu1->taperobj= cu->taperobj;
2561                                                 cu1->width= cu->width;
2562                                                 cu1->bevresol= cu->bevresol;
2563                                                 cu1->ext1= cu->ext1;
2564                                                 cu1->ext2= cu->ext2;
2565                                                 
2566                                                 makeDispList(base->object);
2567                                         }
2568                                 }
2569                                 else if(event==20) {    /* particle settings */
2570                                         PartEff *pa1, *pa2;
2571                                         char *p1, *p2;
2572                                         
2573                                         pa1= give_parteff(ob);
2574                                         pa2= give_parteff(base->object);
2575
2576                                         if(pa1==0 && pa2) {
2577                                                 BLI_remlink( &(base->object->effect), pa2);
2578                                                 free_effect( (Effect *) pa2);
2579                                         }
2580                                         else if(pa1 && pa2==0) {
2581                                                 free_effects(&(base->object->effect));
2582                                                 copy_effects(&(base->object->effect), &ob->effect);
2583                                                 build_particle_system(base->object);
2584                                         }
2585                                         else if(pa1 && pa2) {
2586                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2587                                                 
2588                                                 p1= (char *)pa1; p2= (char *)pa2;
2589                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2590                                                 pa2->keys= 0;
2591                                                 
2592                                                 build_particle_system(base->object);
2593                                         }
2594                                 }
2595                                 else if(event==21){
2596                                         if (base->object->type==OB_MESH) {
2597                                                 Mesh *targetme= base->object->data;
2598                                                 Mesh *sourceme= ob->data;
2599
2600                                                 targetme->flag= (targetme->flag&~ME_SUBSURF) | (sourceme->flag&ME_SUBSURF);
2601                                                 targetme->subsurftype = sourceme->subsurftype;
2602                                                 targetme->subdiv= sourceme->subdiv;
2603                                                 targetme->subdivr= sourceme->subdivr;
2604                                                 makeDispList(base->object);
2605                                         }
2606                                 }
2607                                 else if(event==22){
2608                                         /* Clear the constraints on the target */
2609                                         free_constraints(&base->object->constraints);
2610                                         free_constraint_channels(&base->object->constraintChannels);
2611
2612                                         /* Copy the constraint channels over */
2613                                         copy_constraints(&base->object->constraints, &ob->constraints);
2614                                         if (U.dupflag& USER_DUP_IPO)
2615                                                 copy_constraint_channels(&base->object->constraintChannels, &ob->constraintChannels);
2616                                         else
2617                                                 clone_constraint_channels (&base->object->constraintChannels, &ob->constraintChannels, NULL);
2618
2619                                         base->object->activecon = NULL;
2620                                 }
2621                         }
2622                 }
2623                 base= base->next;
2624         }
2625         
2626         allqueue(REDRAWVIEW3D, 0);
2627         if(event==20) {
2628                 allqueue(REDRAWBUTSOBJECT, 0);
2629         }
2630         
2631         BIF_undo_push("Copy attributes");
2632 }
2633
2634 void link_to_scene(unsigned short nr)
2635 {       
2636         Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1);
2637         Base *base, *nbase;
2638         
2639         if(sce==0) return;
2640         if(sce->id.lib) return;
2641         
2642         base= FIRSTBASE;
2643         while(base) {
2644                 if(TESTBASE(base)) {
2645                         
2646                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2647                         *nbase= *base;
2648                         BLI_addhead( &(sce->base), nbase);
2649                         id_us_plus((ID *)base->object);
2650                 }
2651                 base= base->next;
2652         }
2653 }
2654
2655 void make_links_menu()
2656 {
2657         Object *ob;
2658         short event=0;
2659         char str[140];
2660         
2661         if(!(ob=OBACT)) return;
2662         
2663         strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4");
2664         
2665         if(ob->type==OB_MESH)
2666                 strcat(str, "|Mesh Data%x2|Materials%x3");
2667         else if(ob->type==OB_CURVE)
2668                 strcat(str, "|Curve Data%x2|Materials%x3");
2669         else if(ob->type==OB_FONT)
2670                 strcat(str, "|Text Data%x2|Materials%x3");
2671         else if(ob->type==OB_SURF)
2672                 strcat(str, "|Surface Data%x2|Materials%x3");
2673         else if(ob->type==OB_MBALL)
2674                 strcat(str, "|Materials%x3");
2675         else if(ob->type==OB_CAMERA)
2676                 strcat(str, "|Camera Data%x2");
2677         else if(ob->type==OB_LAMP)
2678                 strcat(str, "|Lamp Data%x2");
2679         else if(ob->type==OB_LATTICE)
2680                 strcat(str, "|Lattice Data%x2");
2681         else if(ob->type==OB_ARMATURE)
2682                 strcat(str, "|Armature Data%x2");
2683
2684         event= pupmenu(str);
2685
2686         if(event<= 0) return;
2687         
2688         make_links(event);
2689 }
2690
2691 void make_links(short event)
2692 {
2693         Object *ob, *obt;
2694         Base *base, *nbase, *sbase;
2695         Scene *sce = NULL;
2696         ID *id;
2697         Material ***matarar, ***obmatarar, **matar1, **matar2;
2698         int a;
2699         short *totcolp, nr;
2700         char *strp;
2701
2702         if(!(ob=OBACT)) return;
2703
2704         if(event==1) {
2705                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr);
2706                 
2707                 if(strncmp(strp, "DataBrow", 8)==0) {
2708                         MEM_freeN(strp);
2709
2710                         activate_databrowse((ID *)G.scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene );
2711                         
2712                         return;                 
2713                 }
2714                 else {
2715                         event= pupmenu(strp);
2716                         MEM_freeN(strp);
2717                 
2718                         if(event<= 0) return;
2719                 
2720                         nr= 1;
2721                         sce= G.main->scene.first;
2722                         while(sce) {
2723                                 if(nr==event) break;
2724                                 nr++;
2725                                 sce= sce->id.next;
2726                         }
2727                         if(sce==G.scene) {
2728                                 error("This is the current scene");
2729                                 return;
2730                         }
2731                         if(sce==0 || sce->id.lib) return;
2732                         
2733                         /* remember: is needed below */
2734                         event= 1;
2735                 }
2736         }
2737
2738         base= FIRSTBASE;
2739         while(base) {
2740                 if(event==1 || base != BASACT) {
2741                         
2742                         obt= base->object;
2743
2744                         if(TESTBASE(base)) {
2745                                 
2746                                 if(event==1) {          /* to scene */
2747                                         
2748                                         /* test if already linked */
2749                                         sbase= sce->base.first;
2750                                         while(sbase) {
2751                                                 if(sbase->object==base->object) break;
2752                                                 sbase= sbase->next;
2753                                         }
2754                                         if(sbase) {     /* remove */
2755                                                 base= base->next;
2756                                                 continue;
2757                                         }
2758                                         
2759                                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2760                                         *nbase= *base;
2761                                         BLI_addhead( &(sce->base), nbase);
2762                                         id_us_plus((ID *)base->object);
2763                                 }
2764                         }
2765                         if(TESTBASELIB(base)) {
2766                                 if(event==2 || event==5) {  /* obdata */
2767                                         if(ob->type==obt->type) {
2768                                                 
2769                                                         id= obt->data;
2770                                                         id->us--;
2771                                                         
2772                                                         id= ob->data;
2773                                                         id_us_plus(id);
2774                                                         obt->data= id;
2775                                                         
2776                                                         /* if amount of material indices changed: */
2777                                                         test_object_materials(obt->data);
2778                                                 }
2779                                         }
2780                                 else if(event==4) {  /* ob ipo */
2781                                         if(obt->ipo) obt->ipo->id.us--;
2782                                         obt->ipo= ob->ipo;
2783                                         if(obt->ipo) {
2784                                                 id_us_plus((ID *)obt->ipo);
2785                                                 do_ob_ipo(obt);
2786                                         }
2787                                 }
2788                                 else if(event==3) {  /* materials */
2789                                         
2790                                         /* only if obt has no material: make arrays */
2791                                         /* from ob to obt! */
2792                                         
2793                                         obmatarar= give_matarar(ob);
2794                                         matarar= give_matarar(obt);
2795                                         totcolp= give_totcolp(obt);
2796
2797                                         /* if one of the two is zero: no render-able object */                                          
2798                                         if( matarar && obmatarar) {
2799                                                 
2800                                                 /* take care of users! so first a copy of original: */
2801
2802                                                 if(ob->totcol) {
2803                                                         matar1= MEM_dupallocN(ob->mat);
2804                                                         matar2= MEM_dupallocN(*obmatarar);
2805                                                 }
2806                                                 else {
2807                                                         matar1= matar2= 0;
2808                                                 }
2809                                                 
2810                                                 /* remove links from obt */
2811                                                 for(a=0; a<obt->totcol; a++) {
2812                                                         if(obt->mat[a]) obt->mat[a]->id.us--;
2813                                                         if( (*matarar)[a]) (*matarar)[a]->id.us--;
2814                                                 }
2815                                                 
2816                                                 /* free */
2817                                                 if(obt->mat) MEM_freeN(obt->mat);
2818                                                 if(*matarar) MEM_freeN(*matarar);
2819                                                 
2820                                                 /* connect a copy */
2821                                                 obt->mat= matar1;
2822                                                 *matarar= matar2;
2823                                                 obt->totcol= ob->totcol;
2824                                                 *totcolp= ob->totcol;
2825                                         
2826                                                 /* increase users */
2827                                                 for(a=0; a<obt->totcol; a++) {
2828                                                         if(obt->mat[a]) id_us_plus((ID *)obt->mat[a]);
2829                                                         if( (*matarar)[a]) id_us_plus((ID *)(*matarar)[a]);
2830                                                 }
2831
2832                                                 obt->colbits= ob->colbits;
2833                                                 
2834                                                 /* if amount of material indices changed: */
2835                                                 test_object_materials(obt->data);
2836                                         }
2837                                 }
2838                         }
2839                 }
2840                 base= base->next;
2841         }
2842         
2843         allqueue(REDRAWVIEW3D, 0);
2844         allqueue(REDRAWOOPS, 0);
2845         allqueue(REDRAWBUTSHEAD, 0);
2846         
2847         BIF_undo_push("Create links");
2848 }
2849
2850 void make_duplilist_real()
2851 {
2852         Base *base, *basen;
2853         Object *ob;
2854         extern ListBase duplilist;
2855         
2856         if(okee("Make dupli objects real")==0) return;
2857         
2858         base= FIRSTBASE;
2859         while(base) {
2860                 if TESTBASELIB(base) {
2861
2862                         if(base->object->transflag & OB_DUPLI) {
2863                                 
2864                                 make_duplilist(G.scene, base->object);
2865                                 ob= duplilist.first;
2866                                 while(ob) {
2867                                         
2868                                         /* font duplis can have a totcol without material, we get them from parent
2869                                          * should be implemented better...
2870                                          */
2871                                         if(ob->mat==0) ob->totcol= 0;
2872                                         
2873                                         basen= MEM_dupallocN(base);
2874                                         basen->flag &= ~OB_FROMDUPLI;
2875                                         BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2876                                         ob->ipo= 0;             /* make sure apply works */
2877                                         ob->parent= ob->track= 0;
2878                                         ob->disp.first= ob->disp.last= 0;
2879                                         ob->transflag &= ~OB_DUPLI;
2880                                         basen->object= copy_object(ob);
2881                                         
2882                                         apply_obmat(basen->object);
2883                                         ob= ob->id.next;
2884                                 }
2885                                 
2886                                 free_duplilist();
2887                                 
2888