Code comments add to collision interface
[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_curve.h"
103 #include "BKE_displist.h"
104 #include "BKE_depsgraph.h"
105 #include "BKE_DerivedMesh.h"
106 #include "BKE_effect.h"
107 #include "BKE_font.h"
108 #include "BKE_global.h"
109 #include "BKE_group.h"
110 #include "BKE_ipo.h"
111 #include "BKE_image.h"
112 #include "BKE_key.h"
113 #include "BKE_lattice.h"
114 #include "BKE_library.h"
115 #include "BKE_main.h"
116 #include "BKE_material.h"
117 #include "BKE_mball.h"
118 #include "BKE_mesh.h"
119 #include "BKE_nla.h"
120 #include "BKE_object.h"
121 #include "BKE_property.h"
122 #include "BKE_sca.h"
123 #include "BKE_scene.h"
124 #include "BKE_softbody.h"
125 #include "BKE_subsurf.h"
126 #include "BKE_texture.h"
127 #include "BKE_utildefines.h"
128 #include "BKE_modifier.h"
129
130 #include "BIF_butspace.h"
131 #include "BIF_editconstraint.h"
132 #include "BIF_editdeform.h"
133 #include "BIF_editfont.h"
134 #include "BIF_editlattice.h"
135 #include "BIF_editmesh.h"
136 #include "BIF_editoops.h"
137 #include "BIF_editview.h"
138 #include "BIF_editarmature.h"
139 #include "BIF_gl.h"
140 #include "BIF_graphics.h"
141 #include "BIF_interface.h"
142 #include "BIF_meshtools.h"
143 #include "BIF_mywindow.h"
144 #include "BIF_resources.h"
145 #include "BIF_retopo.h"
146 #include "BIF_screen.h"
147 #include "BIF_space.h"
148 #include "BIF_toolbox.h"
149 #include "BIF_toets.h"
150
151 #ifdef WITH_VERSE
152 #include "BIF_verse.h"
153 #endif
154
155 #include "BSE_edit.h"
156 #include "BSE_editipo.h"
157 #include "BSE_filesel.h"        /* For activate_databrowse() */
158 #include "BSE_view.h"
159 #include "BSE_drawview.h"
160 #include "BSE_trans_types.h"
161 #include "BSE_editipo_types.h"
162
163 #include "BDR_vpaint.h"
164 #include "BDR_sculptmode.h"
165 #include "BDR_editface.h"
166 #include "BDR_editmball.h"
167 #include "BDR_editobject.h"
168 #include "BDR_drawobject.h"
169 #include "BDR_editcurve.h"
170 #include "BDR_unwrapper.h"
171
172 #include <time.h>
173 #include "mydevice.h"
174 #include "nla.h"
175
176 #include "blendef.h"
177 #include "butspace.h"
178 #include "multires.h"
179 #include "BIF_transform.h"
180
181 #include "BIF_poseobject.h"
182
183
184 /* --------------------------------- */
185
186 void add_object_draw(int type)  /* for toolbox or menus, only non-editmode stuff */
187 {
188         Object *ob;
189         
190         G.f &= ~(G_VERTEXPAINT+G_TEXTUREPAINT+G_WEIGHTPAINT+G_SCULPTMODE);
191         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
192
193         if ELEM3(curarea->spacetype, SPACE_VIEW3D, SPACE_BUTS, SPACE_INFO) {
194                 if (G.obedit) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */
195                 ob= add_object(type);
196                 set_active_base(BASACT);
197                 base_init_from_view3d(BASACT, G.vd);
198                 
199                 /* only undo pushes on objects without editmode... */
200                 if(type==OB_EMPTY) BIF_undo_push("Add Empty");
201                 else if(type==OB_LAMP) {
202                         BIF_undo_push("Add Lamp");
203                         reshadeall_displist();  /* only frees */
204                 }
205                 else if(type==OB_LATTICE) BIF_undo_push("Add Lattice");
206                 else if(type==OB_CAMERA) BIF_undo_push("Add Camera");
207                 
208                 allqueue(REDRAWVIEW3D, 0);
209         }
210
211         redraw_test_buttons(OBACT);
212
213         allqueue(REDRAWALL, 0);
214
215         deselect_all_area_oops();
216         set_select_flag_oops();
217         
218         DAG_scene_sort(G.scene);
219         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
220 }
221
222 void add_objectLamp(short type)
223 {
224         Lamp *la;
225
226         /* this function also comes from an info window */
227         if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
228         
229         if(G.obedit==0) {
230                 add_object_draw(OB_LAMP);
231                 base_init_from_view3d(BASACT, G.vd);
232         }
233         
234         la = BASACT->object->data;
235         la->type = type;        
236
237         allqueue(REDRAWALL, 0);
238 }
239
240 /* remove base from a specific scene */
241 /* note: now unlinks constraints as well */
242 void free_and_unlink_base_from_scene(Scene *scene, Base *base)
243 {
244         BLI_remlink(&scene->base, base);
245         free_libblock_us(&G.main->object, base->object);
246         MEM_freeN(base);
247 }
248
249 /* remove base from the current scene */
250 void free_and_unlink_base(Base *base)
251 {
252         if (base==BASACT)
253                 BASACT= NULL;
254         free_and_unlink_base_from_scene(G.scene, base);
255 }
256
257 void delete_obj(int ok)
258 {
259         Base *base;
260         int islamp= 0;
261         
262         if(G.obedit) return;
263         if(G.scene->id.lib) return;
264
265         if(G.f & G_SCULPTMODE) set_sculptmode();
266         
267         base= FIRSTBASE;
268         while(base) {
269                 Base *nbase= base->next;
270
271                 if TESTBASE(base) { 
272                         if(ok==0) {
273                                 /* Shift Del is global delete */
274                                 if (G.qual & LR_SHIFTKEY) {
275                                         if(!okee("Erase selected Object(s) Globally")) return;
276                                         ok= 2;
277                                 } else {
278                                         if(!okee("Erase selected Object(s)")) return;
279                                         ok= 1;
280                                 }
281                         }
282                         
283                         if(base->object->type==OB_LAMP) islamp= 1;
284 #ifdef WITH_VERSE
285                         if(base->object->vnode) b_verse_delete_object(base->object);
286 #endif
287                         if (ok==2) {
288                                 Scene *scene; 
289                                 Base *base_other;
290                                 
291                                 for (scene= G.main->scene.first; scene; scene= scene->id.next) {
292                                         if (scene != G.scene && !(scene->id.lib)) {
293                                                 base_other= object_in_scene( base->object, scene );
294                                                 if (base_other) {
295                                                         if (base_other == scene->basact) scene->basact= NULL;   /* in case the object was active */
296                                                         free_and_unlink_base_from_scene( scene, base_other );
297                                                 }
298                                         }
299                                 }
300                         }
301                         
302                         /* remove from current scene only */
303                         free_and_unlink_base(base);
304                 }
305                 
306                 base= nbase;
307         }
308         countall();
309
310         G.f &= ~(G_VERTEXPAINT+G_TEXTUREPAINT+G_WEIGHTPAINT);
311         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
312         
313         if(islamp) reshadeall_displist();       /* only frees displist */
314
315         redraw_test_buttons(OBACT);
316         allqueue(REDRAWVIEW3D, 0);
317         allqueue (REDRAWACTION, 0);
318         allqueue(REDRAWIPO, 0);
319         allqueue(REDRAWDATASELECT, 0);
320         allspace(OOPS_TEST, 0);
321         allqueue(REDRAWOOPS, 0);
322         allqueue(REDRAWACTION, 0);
323         allqueue(REDRAWNLA, 0);
324         
325         DAG_scene_sort(G.scene);
326
327         BIF_undo_push("Delete object(s)");
328 }
329
330 static int return_editmesh_indexar(int *tot, int **indexar, float *cent)
331 {
332         EditMesh *em = G.editMesh;
333         EditVert *eve;
334         int *index, nr, totvert=0;
335         
336         for(eve= em->verts.first; eve; eve= eve->next) {
337                 if(eve->f & SELECT) totvert++;
338         }
339         if(totvert==0) return 0;
340         
341         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
342         *tot= totvert;
343         nr= 0;
344         cent[0]= cent[1]= cent[2]= 0.0;
345         
346         for(eve= em->verts.first; eve; eve= eve->next) {
347                 if(eve->f & SELECT) {
348                         *index= nr; index++;
349                         VecAddf(cent, cent, eve->co);
350                 }
351                 nr++;
352         }
353         
354         VecMulf(cent, 1.0f/(float)totvert);
355         
356         return totvert;
357 }
358
359 static int return_editmesh_vgroup(char *name, float *cent)
360 {
361         EditMesh *em = G.editMesh;
362         MDeformVert *dvert;
363         EditVert *eve;
364         int i, totvert=0;
365         
366         cent[0]= cent[1]= cent[2]= 0.0;
367         
368         if(G.obedit->actdef) {
369                 
370                 /* find the vertices */
371                 for(eve= em->verts.first; eve; eve= eve->next) {
372                         dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
373
374                         if(dvert) {
375                                 for(i=0; i<dvert->totweight; i++){
376                                         if(dvert->dw[i].def_nr == (G.obedit->actdef-1)) {
377                                                 totvert++;
378                                                 VecAddf(cent, cent, eve->co);
379                                         }
380                                 }
381                         }
382                 }
383                 if(totvert) {
384                         bDeformGroup *defGroup = BLI_findlink(&G.obedit->defbase, G.obedit->actdef-1);
385                         strcpy(name, defGroup->name);
386                         VecMulf(cent, 1.0f/(float)totvert);
387                         return 1;
388                 }
389         }
390         
391         return 0;
392 }       
393
394 static void select_editmesh_hook(HookModifierData *hmd)
395 {
396         EditMesh *em = G.editMesh;
397         EditVert *eve;
398         int index=0, nr=0;
399         
400         for(eve= em->verts.first; eve; eve= eve->next, nr++) {
401                 if(nr==hmd->indexar[index]) {
402                         eve->f |= SELECT;
403                         if(index < hmd->totindex-1) index++;
404                 }
405         }
406         EM_select_flush();
407 }
408
409 static int return_editlattice_indexar(int *tot, int **indexar, float *cent)
410 {
411         BPoint *bp;
412         int *index, nr, totvert=0, a;
413         
414         /* count */
415         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
416         bp= editLatt->def;
417         while(a--) {
418                 if(bp->f1 & SELECT) {
419                         if(bp->hide==0) totvert++;
420                 }
421                 bp++;
422         }
423
424         if(totvert==0) return 0;
425         
426         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
427         *tot= totvert;
428         nr= 0;
429         cent[0]= cent[1]= cent[2]= 0.0;
430         
431         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
432         bp= editLatt->def;
433         while(a--) {
434                 if(bp->f1 & SELECT) {
435                         if(bp->hide==0) {
436                                 *index= nr; index++;
437                                 VecAddf(cent, cent, bp->vec);
438                         }
439                 }
440                 bp++;
441                 nr++;
442         }
443         
444         VecMulf(cent, 1.0f/(float)totvert);
445         
446         return totvert;
447 }
448
449 static void select_editlattice_hook(HookModifierData *hmd)
450 {
451         BPoint *bp;
452         int index=0, nr=0, a;
453         
454         /* count */
455         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
456         bp= editLatt->def;
457         while(a--) {
458                 if(hmd->indexar[index]==nr) {
459                         bp->f1 |= SELECT;
460                         if(index < hmd->totindex-1) index++;
461                 }
462                 nr++;
463                 bp++;
464         }
465 }
466
467 static int return_editcurve_indexar(int *tot, int **indexar, float *cent)
468 {
469         extern ListBase editNurb;
470         Nurb *nu;
471         BPoint *bp;
472         BezTriple *bezt;
473         int *index, a, nr, totvert=0;
474         
475         for(nu= editNurb.first; nu; nu= nu->next) {
476                 if((nu->type & 7)==CU_BEZIER) {
477                         bezt= nu->bezt;
478                         a= nu->pntsu;
479                         while(a--) {
480                                 if(bezt->f1 & SELECT) totvert++;
481                                 if(bezt->f2 & SELECT) totvert++;
482                                 if(bezt->f3 & SELECT) totvert++;
483                                 bezt++;
484                         }
485                 }
486                 else {
487                         bp= nu->bp;
488                         a= nu->pntsu*nu->pntsv;
489                         while(a--) {
490                                 if(bp->f1 & SELECT) totvert++;
491                                 bp++;
492                         }
493                 }
494         }
495         if(totvert==0) return 0;
496         
497         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
498         *tot= totvert;
499         nr= 0;
500         cent[0]= cent[1]= cent[2]= 0.0;
501         
502         for(nu= editNurb.first; nu; nu= nu->next) {
503                 if((nu->type & 7)==CU_BEZIER) {
504                         bezt= nu->bezt;
505                         a= nu->pntsu;
506                         while(a--) {
507                                 if(bezt->f1 & SELECT) {
508                                         *index= nr; index++;
509                                         VecAddf(cent, cent, bezt->vec[0]);
510                                 }
511                                 nr++;
512                                 if(bezt->f2 & SELECT) {
513                                         *index= nr; index++;
514                                         VecAddf(cent, cent, bezt->vec[1]);
515                                 }
516                                 nr++;
517                                 if(bezt->f3 & SELECT) {
518                                         *index= nr; index++;
519                                         VecAddf(cent, cent, bezt->vec[2]);
520                                 }
521                                 nr++;
522                                 bezt++;
523                         }
524                 }
525                 else {
526                         bp= nu->bp;
527                         a= nu->pntsu*nu->pntsv;
528                         while(a--) {
529                                 if(bp->f1 & SELECT) {
530                                         *index= nr; index++;
531                                         VecAddf(cent, cent, bp->vec);
532                                 }
533                                 nr++;
534                                 bp++;
535                         }
536                 }
537         }
538         
539         VecMulf(cent, 1.0f/(float)totvert);
540         
541         return totvert;
542 }
543
544 static void select_editcurve_hook(HookModifierData *hmd)
545 {
546         extern ListBase editNurb;
547         Nurb *nu;
548         BPoint *bp;
549         BezTriple *bezt;
550         int index=0, a, nr=0;
551         
552         for(nu= editNurb.first; nu; nu= nu->next) {
553                 if((nu->type & 7)==CU_BEZIER) {
554                         bezt= nu->bezt;
555                         a= nu->pntsu;
556                         while(a--) {
557                                 if(nr == hmd->indexar[index]) {
558                                         bezt->f1 |= SELECT;
559                                         if(index<hmd->totindex-1) index++;
560                                 }
561                                 nr++;
562                                 if(nr == hmd->indexar[index]) {
563                                         bezt->f2 |= SELECT;
564                                         if(index<hmd->totindex-1) index++;
565                                 }
566                                 nr++;
567                                 if(nr == hmd->indexar[index]) {
568                                         bezt->f3 |= SELECT;
569                                         if(index<hmd->totindex-1) index++;
570                                 }
571                                 nr++;
572
573                                 bezt++;
574                         }
575                 }
576                 else {
577                         bp= nu->bp;
578                         a= nu->pntsu*nu->pntsv;
579                         while(a--) {
580                                 if(nr == hmd->indexar[index]) {
581                                         bp->f1 |= SELECT;
582                                         if(index<hmd->totindex-1) index++;
583                                 }
584                                 nr++;
585                                 bp++;
586                         }
587                 }
588         }
589 }
590
591 void hook_select(HookModifierData *hmd) 
592 {
593         if(G.obedit->type==OB_MESH) select_editmesh_hook(hmd);
594         else if(G.obedit->type==OB_LATTICE) select_editlattice_hook(hmd);
595         else if(G.obedit->type==OB_CURVE) select_editcurve_hook(hmd);
596         else if(G.obedit->type==OB_SURF) select_editcurve_hook(hmd);
597 }
598
599 int hook_getIndexArray(int *tot, int **indexar, char *name, float *cent_r)
600 {
601         *indexar= NULL;
602         *tot= 0;
603         name[0]= 0;
604
605         switch(G.obedit->type) {
606         case OB_MESH:
607                 /* check selected vertices first */
608                 if( return_editmesh_indexar(tot, indexar, cent_r)) return 1;
609                 else return return_editmesh_vgroup(name, cent_r);
610         case OB_CURVE:
611         case OB_SURF:
612                 return return_editcurve_indexar(tot, indexar, cent_r);
613         case OB_LATTICE:
614                 return return_editlattice_indexar(tot, indexar, cent_r);
615         default:
616                 return 0;
617         }
618 }
619
620 void add_hook(void)
621 {
622         ModifierData *md = NULL;
623         HookModifierData *hmd = NULL;
624         Object *ob=NULL;
625         int mode;
626
627         if(G.obedit==NULL) return;
628         
629         if(modifiers_findByType(G.obedit, eModifierType_Hook))
630                 mode= pupmenu("Hooks %t|Add, To New Empty %x1|Add, To Selected Object %x2|Remove... %x3|Reassign... %x4|Select... %x5|Clear Offset...%x6");
631         else
632                 mode= pupmenu("Hooks %t|Add, New Empty %x1|Add, To Selected Object %x2");
633
634         if(mode<1) return;
635         
636         /* preconditions */
637
638         if(mode==2) { /* selected object */
639                 Base *base= FIRSTBASE;
640                 while(base) {
641                         if TESTBASELIB(base) {
642                                 if(base!=BASACT) {
643                                         ob= base->object;
644                                         break;
645                                 }
646                         }
647                         base= base->next;
648                 }
649                 if(ob==NULL) {
650                         error("Requires selected Object");
651                         return;
652                 }
653         }
654         else if(mode!=1) {
655                 int maxlen=0, a, nr;
656                 char *cp;
657                 
658                 /* make pupmenu with hooks */
659                 for(md=G.obedit->modifiers.first; md; md= md->next) {
660                         if (md->type==eModifierType_Hook) 
661                                 maxlen+=32;
662                 }
663                 
664                 if(maxlen==0) {
665                         error("Object has no hooks yet");
666                         return;
667                 }
668                 
669                 cp= MEM_callocN(maxlen+32, "temp string");
670                 if(mode==3) strcpy(cp, "Remove %t|");
671                 else if(mode==4) strcpy(cp, "Reassign %t|");
672                 else if(mode==5) strcpy(cp, "Select %t|");
673                 else if(mode==6) strcpy(cp, "Clear Offset %t|");
674                 
675                 for(md=G.obedit->modifiers.first; md; md= md->next) {
676                         if (md->type==eModifierType_Hook) {
677                                 strcat(cp, md->name);
678                                 strcat(cp, " |");
679                         }
680                 }
681         
682                 nr= pupmenu(cp);
683                 MEM_freeN(cp);
684                 
685                 if(nr<1) return;
686                 
687                 a= 1;
688                 for(md=G.obedit->modifiers.first; md; md=md->next) {
689                         if (md->type==eModifierType_Hook) {
690                                 if(a==nr) break;
691                                 a++;
692                         }
693                 }
694
695                 hmd = (HookModifierData*) md;
696                 ob= hmd->object;
697         }
698
699         /* do it, new hooks or reassign */
700         if(mode==1 || mode==2 || mode==4) {
701                 float cent[3];
702                 int tot, ok, *indexar;
703                 char name[32];
704                 
705                 ok = hook_getIndexArray(&tot, &indexar, name, cent);
706
707                 if(ok==0) {
708                         error("Requires selected vertices or active Vertex Group");
709                 }
710                 else {
711                         
712                         if(mode==1) {
713                                 Base *base= BASACT, *newbase;
714
715                                 ob= add_object(OB_EMPTY);
716                                 /* set layers OK */
717                                 newbase= BASACT;
718                                 newbase->lay= base->lay;
719                                 ob->lay= newbase->lay;
720                                 
721                                 /* transform cent to global coords for loc */
722                                 VecMat4MulVecfl(ob->loc, G.obedit->obmat, cent);
723                                 
724                                 /* restore, add_object sets active */
725                                 BASACT= base;
726                         }
727                         /* if mode is 2 or 4, ob has been set */
728                                                                         
729                         /* new hook */
730                         if(mode==1 || mode==2) {
731                                 ModifierData *md = G.obedit->modifiers.first;
732
733                                 while (md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) {
734                                         md = md->next;
735                                 }
736
737                                 hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
738                                 BLI_insertlinkbefore(&G.obedit->modifiers, md, hmd);
739                                 sprintf(hmd->modifier.name, "Hook-%s", ob->id.name+2);
740                         }
741                         else if (hmd->indexar) MEM_freeN(hmd->indexar); /* reassign, hook was set */
742
743                         hmd->object= ob;
744                         hmd->indexar= indexar;
745                         VECCOPY(hmd->cent, cent);
746                         hmd->totindex= tot;
747                         BLI_strncpy(hmd->name, name, 32);
748                         
749                         if(mode==1 || mode==2) {
750                                 /* matrix calculus */
751                                 /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */
752                                 /*        (parentinv         )                          */
753                                 
754                                 where_is_object(ob);
755                 
756                                 Mat4Invert(ob->imat, ob->obmat);
757                                 /* apparently this call goes from right to left... */
758                                 Mat4MulSerie(hmd->parentinv, ob->imat, G.obedit->obmat, NULL, 
759                                                         NULL, NULL, NULL, NULL, NULL);
760                         }
761                 }
762         }
763         else if(mode==3) { /* remove */
764                 BLI_remlink(&G.obedit->modifiers, md);
765                 modifier_free(md);
766         }
767         else if(mode==5) { /* select */
768                 hook_select(hmd);
769         }
770         else if(mode==6) { /* clear offset */
771                 where_is_object(ob);    /* ob is hook->parent */
772
773                 Mat4Invert(ob->imat, ob->obmat);
774                 /* this call goes from right to left... */
775                 Mat4MulSerie(hmd->parentinv, ob->imat, G.obedit->obmat, NULL, 
776                                         NULL, NULL, NULL, NULL, NULL);
777         }
778
779         allqueue(REDRAWVIEW3D, 0);
780         allqueue(REDRAWBUTSOBJECT, 0);
781         DAG_scene_sort(G.scene);
782         
783         BIF_undo_push("Add hook");
784 }
785
786 void make_track(void)
787 {
788         Base *base;
789         short mode=0;
790         
791         if(G.scene->id.lib) return;
792         if(G.obedit) {
793                 return;
794         }
795         if(BASACT==0) return;
796
797         mode= pupmenu("Make Track %t|TrackTo Constraint %x1|LockTrack Constraint %x2|Old Track %x3");
798         if (mode == 0){
799                 return;
800         }
801         else if (mode == 1){
802                 bConstraint *con;
803                 bTrackToConstraint *data;
804
805                 base= FIRSTBASE;
806                 while(base) {
807                         if TESTBASELIB(base) {
808                                 if(base!=BASACT) {
809                                         con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
810                                         strcpy (con->name, "AutoTrack");
811
812                                         data = con->data;
813                                         data->tar = BASACT->object;
814                                         base->object->recalc |= OB_RECALC;
815                                         
816                                         /* Lamp and Camera track differently by default */
817                                         if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
818                                                 data->reserved1 = TRACK_nZ;
819                                                 data->reserved2 = UP_Y;
820                                         }
821
822                                         add_constraint_to_object(con, base->object);
823                                 }
824                         }
825                         base= base->next;
826                 }
827
828         }
829         else if (mode == 2){
830                 bConstraint *con;
831                 bLockTrackConstraint *data;
832
833                 base= FIRSTBASE;
834                 while(base) {
835                         if TESTBASELIB(base) {
836                                 if(base!=BASACT) {
837                                         con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
838                                         strcpy (con->name, "AutoTrack");
839
840                                         data = con->data;
841                                         data->tar = BASACT->object;
842                                         base->object->recalc |= OB_RECALC;
843                                         
844                                         /* Lamp and Camera track differently by default */
845                                         if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
846                                                 data->trackflag = TRACK_nZ;
847                                                 data->lockflag = LOCK_Y;
848                                         }
849
850                                         add_constraint_to_object(con, base->object);
851                                 }
852                         }
853                         base= base->next;
854                 }
855
856         }
857         else if (mode == 3){
858                 base= FIRSTBASE;
859                 while(base) {
860                         if TESTBASELIB(base) {
861                                 if(base!=BASACT) {
862                                         base->object->track= BASACT->object;
863                                         base->object->recalc |= OB_RECALC;
864                                 }
865                         }
866                         base= base->next;
867                 }
868         }
869
870         allqueue(REDRAWOOPS, 0);
871         allqueue(REDRAWVIEW3D, 0);
872         DAG_scene_sort(G.scene);
873         
874         BIF_undo_push("Make Track");
875 }
876
877 void apply_obmat(Object *ob)
878 {
879         float mat[3][3], imat[3][3], tmat[3][3];
880         
881         /* from obmat to loc rot size */
882         
883         if(ob==0) return;
884         Mat3CpyMat4(mat, ob->obmat);
885         
886         VECCOPY(ob->loc, ob->obmat[3]);
887         
888         if(ob->transflag & OB_QUAT) {
889                 Mat3ToQuat(mat, ob->quat);
890                 QuatToMat3(ob->quat, tmat);
891         }
892         else {
893                 Mat3ToEul(mat, ob->rot);
894                 EulToMat3(ob->rot, tmat);
895         }
896         Mat3Inv(imat, tmat);
897         
898         Mat3MulMat3(tmat, imat, mat);
899         
900         ob->size[0]= tmat[0][0];
901         ob->size[1]= tmat[1][1];
902         ob->size[2]= tmat[2][2];
903
904 }
905
906 void clear_parent(void)
907 {
908         Object *par;
909         Base *base;
910         int mode;
911         
912         if(G.obedit) return;
913         if(G.scene->id.lib) return;
914
915         mode= pupmenu("OK? %t|Clear Parent %x1|Clear and Keep Transformation (Clear Track) %x2|Clear Parent Inverse %x3");
916         
917         if(mode<1) return;
918
919         base= FIRSTBASE;
920         while(base) {
921                 if TESTBASELIB(base) {
922                         par= NULL;
923                         if(mode==1 || mode==2) {
924                                 par= base->object->parent;
925                                 base->object->parent= NULL;
926                                 base->object->recalc |= OB_RECALC;
927                                 
928                                 if(mode==2) {
929                                         base->object->track= NULL;
930                                         apply_obmat(base->object);
931                                 }
932                         }
933                         else if(mode==3) {
934                                 Mat4One(base->object->parentinv);
935                                 base->object->recalc |= OB_RECALC;
936                         }
937                 }
938                 base= base->next;
939         }
940
941         DAG_scene_sort(G.scene);
942         DAG_scene_flush_update(G.scene, screen_view3d_layers());
943         allqueue(REDRAWVIEW3D, 0);
944         allqueue(REDRAWOOPS, 0);
945         
946         BIF_undo_push("Clear Parent");  
947 }
948
949 void clear_track(void)
950 {
951         Base *base;
952         int mode;
953         
954         if(G.obedit) return;
955         if(G.scene->id.lib) return;
956
957         mode= pupmenu("OK? %t|Clear Track %x1| Clear Track and Keep Transform %x2");
958
959         if(mode<1) return;
960
961         base= FIRSTBASE;
962         while(base) {
963                 if TESTBASELIB(base) {
964                         base->object->track= NULL;
965                         base->object->recalc |= OB_RECALC;
966                         
967                         if(mode==2) {
968                                 apply_obmat(base->object);
969                         }                       
970                 }
971                 base= base->next;
972         }
973
974         DAG_scene_sort(G.scene);
975         allqueue(REDRAWVIEW3D, 0);
976         allqueue(REDRAWOOPS, 0);
977         
978         BIF_undo_push("Clear Track");   
979 }
980
981 void clear_object(char mode)
982 {
983         Base *base;
984         Object *ob;
985         float *v1, *v3, mat[3][3];
986         int armature_clear= 0;
987         char *str=NULL;
988         
989         if(G.obedit) return;
990         if(G.scene->id.lib) return;
991         
992         if(mode=='r') str= "Clear rotation";
993         else if(mode=='g') str= "Clear location";
994         else if(mode=='s') str= "Clear scale";
995         else if(mode=='o') str= "Clear origin";
996         else return;
997         
998         base= FIRSTBASE;
999         while(base) {
1000                 if TESTBASELIB(base) {
1001                         ob= base->object;
1002                         
1003                         if ((ob->flag & OB_POSEMODE)) {
1004                                 /* only clear pose transforms if:
1005                                  *      - with a mesh in weightpaint mode, it's related armature needs to be cleared
1006                                  *      - with clearing transform of object being edited at the time
1007                                  */
1008                                 if ((G.f & G_WEIGHTPAINT) || ob==OBACT) {
1009                                         clear_armature(ob, mode);
1010                                         armature_clear= 1;      /* silly system to prevent another dag update, so no action applied */
1011                                 }
1012                         }
1013                         else if((G.f & G_WEIGHTPAINT)==0) {
1014                                 
1015                                 if(mode=='r') {
1016                                         memset(ob->rot, 0, 3*sizeof(float));
1017                                         memset(ob->drot, 0, 3*sizeof(float));
1018                                         QuatOne(ob->quat);
1019                                         QuatOne(ob->dquat);
1020 #ifdef WITH_VERSE
1021                                         if(ob->vnode) {
1022                                                 struct VNode *vnode = (VNode*)ob->vnode;
1023                                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
1024                                                 b_verse_send_transformation(ob);
1025                                         }
1026 #endif
1027
1028                                 }
1029                                 else if(mode=='g') {
1030                                         memset(ob->loc, 0, 3*sizeof(float));
1031                                         memset(ob->dloc, 0, 3*sizeof(float));
1032 #ifdef WITH_VERSE
1033                                         if(ob->vnode) {
1034                                                 struct VNode *vnode = (VNode*)ob->vnode;
1035                                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
1036                                                 b_verse_send_transformation(ob);
1037                                         }
1038 #endif
1039
1040                                 }
1041                                 else if(mode=='s') {
1042                                         memset(ob->dsize, 0, 3*sizeof(float));
1043                                         ob->size[0]= 1.0;
1044                                         ob->size[1]= 1.0;
1045                                         ob->size[2]= 1.0;
1046 #ifdef WITH_VERSE
1047                                         if(ob->vnode) {
1048                                                 struct VNode *vnode = (VNode*)ob->vnode;
1049                                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
1050                                                 b_verse_send_transformation(ob);
1051                                         }
1052 #endif
1053
1054                                 }
1055                                 else if(mode=='o') {
1056                                         if(ob->parent) {
1057                                                 v1= ob->loc;
1058                                                 v3= ob->parentinv[3];
1059                                                 
1060                                                 Mat3CpyMat4(mat, ob->parentinv);
1061                                                 VECCOPY(v3, v1);
1062                                                 v3[0]= -v3[0];
1063                                                 v3[1]= -v3[1];
1064                                                 v3[2]= -v3[2];
1065                                                 Mat3MulVecfl(mat, v3);
1066                                         }
1067                                 }
1068                                 
1069                                 ob->recalc |= OB_RECALC_OB;
1070                         }                       
1071                 }
1072                 base= base->next;
1073         }
1074         
1075         allqueue(REDRAWVIEW3D, 0);
1076         if(armature_clear==0) /* in this case flush was done */
1077                 DAG_scene_flush_update(G.scene, screen_view3d_layers());
1078         BIF_undo_push(str);
1079 }
1080
1081 void reset_slowparents(void)
1082 {
1083         /* back to original locations */
1084         Base *base;
1085
1086         base= FIRSTBASE;
1087         while(base) {
1088                 if(base->object->parent) {
1089                         if(base->object->partype & PARSLOW) {
1090                                 base->object->partype -= PARSLOW;
1091                                 where_is_object(base->object);
1092                                 base->object->partype |= PARSLOW;
1093                         }
1094                 }
1095                 base= base->next;
1096         }
1097 }
1098
1099 void set_slowparent(void)
1100 {
1101         Base *base;
1102
1103         if( okee("Set slow parent")==0 ) return;
1104
1105         base= FIRSTBASE;
1106         while(base) {
1107                 if TESTBASELIB(base) {
1108                         if(base->object->parent) base->object->partype |= PARSLOW;
1109                 }
1110                 base= base->next;
1111         }
1112         BIF_undo_push("Slow parent");
1113 }
1114
1115 void make_vertex_parent(void)
1116 {
1117         EditMesh *em = G.editMesh;
1118         EditVert *eve;
1119         Base *base;
1120         Nurb *nu;
1121         BezTriple *bezt;
1122         BPoint *bp;
1123         Object *par, *ob;
1124         int a, v1=0, v2=0, v3=0, v4=0, nr=1;
1125         
1126         /* we need 1 to 3 selected vertices */
1127         
1128         if(G.obedit->type==OB_MESH) {
1129                 eve= em->verts.first;
1130                 while(eve) {
1131                         if(eve->f & 1) {
1132                                 if(v1==0) v1= nr;
1133                                 else if(v2==0) v2= nr;
1134                                 else if(v3==0) v3= nr;
1135                                 else if(v4==0) v4= nr;
1136                                 else break;
1137                         }
1138                         nr++;
1139                         eve= eve->next;
1140                 }
1141         }
1142         else if ELEM(G.obedit->type, OB_SURF, OB_CURVE) {
1143                 extern ListBase editNurb;
1144                 nu= editNurb.first;
1145                 while(nu) {
1146                         if((nu->type & 7)==CU_BEZIER) {
1147                                 bezt= nu->bezt;
1148                                 a= nu->pntsu;
1149                                 while(a--) {
1150                                         if(BEZSELECTED(bezt)) {
1151                                                 if(v1==0) v1= nr;
1152                                                 else if(v2==0) v2= nr;
1153                                                 else if(v3==0) v3= nr;
1154                                                 else if(v4==0) v4= nr;
1155                                                 else break;
1156                                         }
1157                                         nr++;
1158                                         bezt++;
1159                                 }
1160                         }
1161                         else {
1162                                 bp= nu->bp;
1163                                 a= nu->pntsu*nu->pntsv;
1164                                 while(a--) {
1165                                         if(bp->f1 & SELECT) {
1166                                                 if(v1==0) v1= nr;
1167                                                 else if(v2==0) v2= nr;
1168                                                 else if(v3==0) v3= nr;
1169                                                 else if(v4==0) v4= nr;
1170                                                 else break;
1171                                         }
1172                                         nr++;
1173                                         bp++;
1174                                 }
1175                         }
1176                         nu= nu->next;
1177                 }
1178         }
1179         else if(G.obedit->type==OB_LATTICE) {
1180                 
1181                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1182                 bp= editLatt->def;
1183                 while(a--) {
1184                         if(bp->f1 & SELECT) {
1185                                 if(v1==0) v1= nr;
1186                                 else if(v2==0) v2= nr;
1187                                 else if(v3==0) v3= nr;
1188                                 else if(v4==0) v4= nr;
1189                                 else break;
1190                         }
1191                         nr++;
1192                         bp++;
1193                 }
1194         }
1195         
1196         if(v4 || !((v1 && v2==0 && v3==0) || (v1 && v2 && v3)) ) {
1197                 error("Select either 1 or 3 vertices to parent to");
1198                 return;
1199         }
1200         
1201         if(okee("Make vertex parent")==0) return;
1202         
1203         base= FIRSTBASE;
1204         while(base) {
1205                 if TESTBASELIB(base) {
1206                         if(base!=BASACT) {
1207                                 
1208                                 ob= base->object;
1209                                 ob->recalc |= OB_RECALC;
1210                                 par= BASACT->object->parent;
1211                                 
1212                                 while(par) {
1213                                         if(par==ob) break;
1214                                         par= par->parent;
1215                                 }
1216                                 if(par) {
1217                                         error("Loop in parents");
1218                                 }
1219                                 else {
1220                                         ob->parent= BASACT->object;
1221                                         if(v3) {
1222                                                 ob->partype= PARVERT3;
1223                                                 ob->par1= v1-1;
1224                                                 ob->par2= v2-1;
1225                                                 ob->par3= v3-1;
1226
1227                                                 /* inverse parent matrix */
1228                                                 what_does_parent(ob);
1229                                                 Mat4Invert(ob->parentinv, workob.obmat);
1230                                                 clear_workob();
1231                                         }
1232                                         else {
1233                                                 ob->partype= PARVERT1;
1234                                                 ob->par1= v1-1;
1235
1236                                                 /* inverse parent matrix */
1237                                                 what_does_parent(ob);
1238                                                 Mat4Invert(ob->parentinv, workob.obmat);
1239                                                 clear_workob();
1240                                         }
1241                                 }
1242                         }
1243                 }
1244                 base= base->next;
1245         }
1246         allqueue(REDRAWVIEW3D, 0);
1247         
1248         DAG_scene_sort(G.scene);
1249         /* BIF_undo_push(str); not, conflicts with editmode undo... */
1250 }
1251
1252 static Object *group_objects_menu(Group *group)
1253 {
1254         GroupObject *go;
1255         int len= 0;
1256         short a, nr;
1257         char *str;
1258                 
1259         for(go= group->gobject.first; go; go= go->next) {
1260                 if(go->ob)
1261                         len++;
1262         }
1263         if(len==0) return NULL;
1264         
1265         str= MEM_callocN(40+32*len, "menu");
1266         
1267         strcpy(str, "Make Proxy for: %t");
1268         a= strlen(str);
1269         for(nr=1, go= group->gobject.first; go; go= go->next, nr++) {
1270                 a+= sprintf(str+a, "|%s %%x%d", go->ob->id.name+2, nr);
1271         }
1272         
1273         a= pupmenu_col(str, 20);
1274         MEM_freeN(str);
1275         if(a>0) {
1276                 go= BLI_findlink(&group->gobject, a-1);
1277                 return go->ob;
1278         }
1279         return NULL;
1280 }
1281
1282
1283 /* adds empty object to become local replacement data of a library-linked object */
1284 void make_proxy(void)
1285 {
1286         Object *ob= OBACT;
1287         Object *gob= NULL;
1288         
1289         if(G.scene->id.lib) return;
1290         if(ob==NULL) return;
1291         
1292         
1293         if(ob->dup_group && ob->dup_group->id.lib) {
1294                 gob= ob;
1295                 /* gives menu with list of objects in group */
1296                 ob= group_objects_menu(ob->dup_group);
1297         }
1298         else if(ob->id.lib) {
1299                 if(okee("Make Proxy Object")==0)
1300                 return;
1301         }
1302         else {
1303                 error("Can only make proxy for a referenced object or group");
1304                 return;
1305         }
1306         
1307         if(ob) {
1308                 Object *newob;
1309                 Base *newbase, *oldbase= BASACT;
1310                 char name[32];
1311                 
1312                 newob= add_object(OB_EMPTY);
1313                 if(gob)
1314                         strcpy(name, gob->id.name+2);
1315                 else
1316                         strcpy(name, ob->id.name+2);
1317                 strcat(name, "_proxy");
1318                 rename_id(&newob->id, name);
1319                 
1320                 /* set layers OK */
1321                 newbase= BASACT;        /* add_object sets active... */
1322                 newbase->lay= oldbase->lay;
1323                 newob->lay= newbase->lay;
1324                 
1325                 /* remove base, leave user count of object, it gets linked in object_make_proxy */
1326                 if(gob==NULL) {
1327                         BLI_remlink(&G.scene->base, oldbase);
1328                         MEM_freeN(oldbase);
1329                 }               
1330                 object_make_proxy(newob, ob, gob);
1331                 
1332                 DAG_scene_sort(G.scene);
1333                 DAG_object_flush_update(G.scene, newob, OB_RECALC);
1334                 allqueue(REDRAWALL, 0);
1335                 BIF_undo_push("Make Proxy Object");
1336         }
1337 }
1338
1339 int test_parent_loop(Object *par, Object *ob)
1340 {
1341         /* test if 'ob' is a parent somewhere in par's parents */
1342         
1343         if(par==0) return 0;
1344         if(ob == par) return 1;
1345         
1346         return test_parent_loop(par->parent, ob);
1347
1348 }
1349
1350 void make_parent(void)
1351 {
1352         Base *base;
1353         Object *par;
1354         bPoseChannel *pchan= NULL;
1355         short qual, mode=0;
1356
1357         if(G.scene->id.lib) return;
1358         if(G.obedit) {
1359                 if ELEM4(G.obedit->type, OB_MESH, OB_CURVE, OB_SURF, OB_LATTICE) make_vertex_parent();
1360                 else if (G.obedit->type==OB_ARMATURE) make_bone_parent();
1361                 return;
1362         }
1363         if(BASACT==0) return;
1364         
1365         qual= G.qual;
1366         par= BASACT->object;
1367
1368         if(par->type == OB_LATTICE){
1369                 mode= pupmenu("Make Parent %t|Normal Parent %x1|Lattice Deform %x2");
1370                 if(mode<=0){
1371                         return;
1372                 }
1373                 else if(mode==1) {
1374                         mode= PAROBJECT;
1375                 }
1376                 else if(mode==2) {
1377                         mode= PARSKEL;
1378                 }
1379         }
1380         else if(par->type == OB_CURVE){
1381                 bConstraint *con;
1382                 bFollowPathConstraint *data;
1383
1384                 mode= pupmenu("Make Parent %t|Normal Parent %x1|Follow Path %x2|Curve Deform %x3|Path Constraint %x4");
1385                 if(mode<=0){
1386                         return;
1387                 }
1388                 else if(mode==1) {
1389                         mode= PAROBJECT;
1390                 }
1391                 else if(mode==2) {
1392                         Curve *cu= par->data;
1393                         
1394                         mode= PAROBJECT;
1395                         if((cu->flag & CU_PATH)==0) {
1396                                 cu->flag |= CU_PATH|CU_FOLLOW;
1397                                 makeDispListCurveTypes(par, 0);  /* force creation of path data */
1398                         }
1399                         else cu->flag |= CU_FOLLOW;
1400                 }
1401                 else if(mode==3) {
1402                         mode= PARSKEL;
1403                 }
1404                 else if(mode==4) {
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, TARGET_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, 1, 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 void flip_subdivison(int level)
2881 {
2882         Base *base;
2883         int set= -1;
2884         int mode;
2885         
2886         if(G.qual & LR_ALTKEY)
2887<