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