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