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