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