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