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