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