7edf910816b07ddcad0b12d5797b5dfbd896d57c
[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 #include "BLI_winstuff.h"
51 #endif
52 #include "MEM_guardedalloc.h"
53 #include "PIL_time.h"
54
55 #include "BMF_Api.h"
56
57
58 #include "IMB_imbuf_types.h"
59
60 #include "DNA_camera_types.h"
61 #include "DNA_curve_types.h"
62 #include "DNA_effect_types.h"
63 #include "DNA_ika_types.h"
64 #include "DNA_image_types.h"
65 #include "DNA_ipo_types.h"
66 #include "DNA_key_types.h"
67 #include "DNA_lamp_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_mesh_types.h"
70 #include "DNA_meshdata_types.h"
71 #include "DNA_meta_types.h"
72 #include "DNA_object_types.h"
73 #include "DNA_scene_types.h"
74 #include "DNA_screen_types.h"
75 #include "DNA_texture_types.h"
76 #include "DNA_view3d_types.h"
77 #include "DNA_world_types.h"
78 #include "DNA_userdef_types.h"
79 #include "DNA_property_types.h"
80 #include "DNA_vfont_types.h"
81 #include "DNA_constraint_types.h"
82
83 #include "BLI_blenlib.h"
84 #include "BLI_arithb.h"
85 #include "BLI_editVert.h"
86 #include "BLI_ghash.h"
87
88 #include "BKE_nla.h"
89 #include "BKE_utildefines.h"
90 #include "BKE_anim.h"
91 #include "BKE_blender.h"
92 #include "BKE_curve.h"
93 #include "BKE_displist.h"
94 #include "BKE_effect.h"
95 #include "BKE_font.h"
96 #include "BKE_global.h"
97 #include "BKE_ika.h"
98 #include "BKE_ipo.h"
99 #include "BKE_key.h"
100 #include "BKE_lattice.h"
101 #include "BKE_library.h"
102 #include "BKE_main.h"
103 #include "BKE_material.h"
104 #include "BKE_mball.h"
105 #include "BKE_mesh.h"
106 #include "BKE_object.h"
107 #include "BKE_property.h"
108 #include "BKE_sca.h"
109 #include "BKE_scene.h"
110 #include "BKE_subsurf.h"
111 #include "BKE_texture.h"
112 #include "BKE_booleanops.h"
113
114 #include "BIF_gl.h"
115 #include "BIF_graphics.h"
116 #include "BIF_interface.h"
117 #include "BIF_mywindow.h"
118 #include "BIF_toolbox.h"
119 #include "BIF_screen.h"
120 #include "BIF_space.h"
121 #include "BIF_toets.h"
122 #include "BIF_butspace.h"
123 #include "BIF_editdeform.h"
124 #include "BIF_editfont.h"
125 #include "BIF_editika.h"
126 #include "BIF_editlattice.h"
127 #include "BIF_editmesh.h"
128 #include "BIF_editoops.h"
129 #include "BIF_editview.h"
130 #include "BIF_editarmature.h"
131 #include "BIF_resources.h"
132
133 #include "BSE_edit.h"
134 #include "BSE_editaction.h"
135 #include "BSE_editipo.h"
136 #include "BSE_filesel.h"        /* For activate_databrowse() */
137 #include "BSE_view.h"
138 #include "BSE_trans_types.h"
139 #include "BSE_editipo_types.h"
140
141 #include "BDR_vpaint.h"
142 #include "BDR_editmball.h"
143 #include "BDR_editobject.h"
144 #include "BDR_drawobject.h"
145 #include "BDR_editcurve.h"
146
147 #include "render.h"
148 #include <time.h>
149 #include "mydevice.h"
150 #include "nla.h"
151
152 #include "blendef.h"
153
154 #include "BKE_constraint.h"
155 #include "BIF_editconstraint.h"
156
157 #include "BKE_action.h"
158 #include "DNA_action_types.h"
159 #include "BKE_armature.h"
160 #include "DNA_armature_types.h"
161 #include "BIF_poseobject.h"
162
163 /*  extern Lattice *copy_lattice(Lattice *lt); */
164 extern ListBase editNurb;
165 extern ListBase editelems;
166
167 TransOb *transmain= 0;
168 TransVert *transvmain= 0;
169 int tottrans=0, transmode=0;    /* 1: texspace */
170
171 float prop_size= 1.0;
172 int prop_mode= 0;
173 float prop_cent[3];
174
175 /* used in editipo, editcurve and here */
176 #define BEZSELECTED(bezt)   (((bezt)->f1 & 1) || ((bezt)->f2 & 1) || ((bezt)->f3 & 1))
177
178 #define TRANS_TEX       1
179
180 #define KEYFLAG_ROT             0x00000001
181 #define KEYFLAG_LOC             0x00000002
182 #define KEYFLAG_SIZE    0x00000004
183
184 float centre[3], centroid[3];
185
186 void mirrormenu(void);
187
188 void add_object_draw(int type)  /* for toolbox */
189 {
190         Object *ob;
191         
192         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
193         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
194
195         if ELEM3(curarea->spacetype, SPACE_VIEW3D, SPACE_BUTS, SPACE_INFO) {
196                 if (G.obedit) exit_editmode(1);
197                 ob= add_object(type);
198                 base_init_from_view3d(BASACT, G.vd);
199
200                 allqueue(REDRAWVIEW3D, 0);
201         }
202
203         redraw_test_buttons(BASACT);
204
205         allqueue(REDRAWALL, 0);
206
207         deselect_all_area_oops();
208         set_select_flag_oops();
209         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
210
211 }
212
213 void free_and_unlink_base(Base *base)
214 {
215         if (base==BASACT)
216                 BASACT= NULL;
217         
218         BLI_remlink(&G.scene->base, base);
219         free_libblock_us(&G.main->object, base->object);
220         MEM_freeN(base);
221 }
222
223 void delete_obj(int ok)
224 {
225         Base *base;
226 extern int undo_push(char *);
227         
228         if(G.obpose) return;
229         if(G.obedit) return;
230         if(G.scene->id.lib) return;
231         
232 //if (undo_push("Erase")) return;       
233         
234         base= FIRSTBASE;
235         while(base) {
236                 Base *nbase= base->next;
237
238                 if TESTBASE(base) {
239                         if(ok==0 &&  (ok=okee("Erase selected"))==0) return;
240                         
241                         free_and_unlink_base(base);
242                 }
243                 
244                 base= nbase;
245         }
246         countall();
247
248         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
249         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
250         
251         test_scene_constraints();
252         allqueue(REDRAWVIEW3D, 0);
253         redraw_test_buttons(BASACT);
254         allqueue (REDRAWACTION, 0);
255         allqueue(REDRAWIPO, 0);
256         allqueue(REDRAWDATASELECT, 0);
257         allqueue(REDRAWOOPS, 0);
258         allqueue(REDRAWACTION, 0);
259         allqueue(REDRAWNLA, 0);
260 }
261
262
263 void make_track(void)
264 {
265         Base *base;
266         short mode=0;
267         
268         if(G.scene->id.lib) return;
269         if(G.obedit) {
270                 return;
271         }
272         if(BASACT==0) return;
273
274         mode= pupmenu("Make Track %t|Constraint %x1|Old Track %x2");
275         if (mode == 0){
276                 return;
277         }
278         else if (mode == 1){
279                 bConstraint *con;
280                 bTrackToConstraint *data;
281
282                 base= FIRSTBASE;
283                 while(base) {
284                         if TESTBASELIB(base) {
285                                 if(base!=BASACT) {
286                                         con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
287                                         strcpy (con->name, "AutoTrack");
288
289                                         data = con->data;
290                                         data->tar = BASACT->object;
291
292                                         /* Lamp and Camera track differently by default */
293                                         if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
294                                                 data->reserved1 = TRACK_nZ;
295                                                 data->reserved2 = UP_Y;
296                                         }
297
298                                         add_constraint_to_object(con, base->object);
299                                 }
300                         }
301                         base= base->next;
302                 }
303
304                 test_scene_constraints();
305                 allqueue(REDRAWVIEW3D, 0);
306                 sort_baselist(G.scene);
307         }
308         else if (mode == 2){
309                 base= FIRSTBASE;
310                 while(base) {
311                         if TESTBASELIB(base) {
312                                 if(base!=BASACT) {
313
314                                         base->object->track= BASACT->object;
315                                 }
316                         }
317                         base= base->next;
318                 }
319
320                 test_scene_constraints();
321                 allqueue(REDRAWVIEW3D, 0);
322                 allqueue(REDRAWOOPS, 0);
323                 sort_baselist(G.scene);
324         }
325 }
326
327 void apply_obmat(Object *ob)
328 {
329         float mat[3][3], imat[3][3], tmat[3][3];
330         
331         /* from obmat to loc rot size */
332         
333         if(ob==0) return;
334         Mat3CpyMat4(mat, ob->obmat);
335         
336         VECCOPY(ob->loc, ob->obmat[3]);
337         
338         if(ob->transflag & OB_QUAT) {
339                 Mat3ToQuat(mat, ob->quat);
340                 QuatToMat3(ob->quat, tmat);
341         }
342         else {
343                 Mat3ToEul(mat, ob->rot);
344                 EulToMat3(ob->rot, tmat);
345         }
346         Mat3Inv(imat, tmat);
347         
348         Mat3MulMat3(tmat, imat, mat);
349         
350         ob->size[0]= tmat[0][0];
351         ob->size[1]= tmat[1][1];
352         ob->size[2]= tmat[2][2];
353
354 }
355
356 void clear_parent(void)
357 {
358         Object *par;
359         Base *base;
360         int mode;
361         
362         if(G.obedit) return;
363         if(G.scene->id.lib) return;
364
365         mode= pupmenu("OK? %t|Clear Parent %x1|Clear and Keep Transformation (Clear Track) %x2|Clear Parent Inverse %x3");
366         
367         if(mode<1) return;
368
369         base= FIRSTBASE;
370         while(base) {
371                 if TESTBASELIB(base) {
372                         par= 0;
373                         if(mode==1 || mode==2) {
374                                 if(base->object->type==OB_IKA) {
375                                         Ika *ika= base->object->data;
376                                         ika->parent= 0;
377                                 }
378                                 par= base->object->parent;
379                                 base->object->parent= 0;
380                         
381                                 if(mode==2) {
382                                         base->object->track= 0;
383                                         apply_obmat(base->object);
384                                 }
385                         }
386                         else if(mode==3) {
387                                 Mat4One(base->object->parentinv);
388                         }
389                         
390                         if(par) {
391                                 if(par->type==OB_LATTICE) makeDispList(base->object);
392                                 if(par->type==OB_IKA) makeDispList(base->object);
393                                 if(par->type==OB_ARMATURE) makeDispList(base->object);
394                         }
395                 }
396                 base= base->next;
397         }
398
399         test_scene_constraints();
400         allqueue(REDRAWVIEW3D, 0);
401         allqueue(REDRAWOOPS, 0);
402 }
403
404 void clear_track(void)
405 {
406         Base *base;
407         int mode;
408         
409         if(G.obedit) return;
410         if(G.scene->id.lib) return;
411
412         mode= pupmenu("OK? %t|Clear Track %x1| Clear Track and Keep Transform %x2");
413
414         if(mode<1) return;
415
416         base= FIRSTBASE;
417         while(base) {
418                 if TESTBASELIB(base) {
419                         base->object->track= 0;
420
421                         if(mode==2) {
422                                 apply_obmat(base->object);
423                         }                       
424                 }
425                 base= base->next;
426         }
427         test_scene_constraints();
428         allqueue(REDRAWVIEW3D, 0);
429         allqueue(REDRAWOOPS, 0);
430 }
431
432 void clear_object(char mode)
433 {
434         Base *base;
435         float *v1, *v3, mat[3][3];
436         
437         if(G.obedit) return;
438         if(G.scene->id.lib) return;
439         
440         if(mode=='r' && okee("Clear rotation")==0) return;
441         else if(mode=='g' && okee("Clear location")==0) return;
442         else if(mode=='s' && okee("Clear size")==0) return;
443         else if(mode=='o' && okee("Clear origin")==0) return;
444         
445         if (G.obpose){
446
447                 switch (G.obpose->type){
448                 case OB_ARMATURE:
449                         clear_armature (G.obpose, mode);
450 #if 1
451                         clear_pose_constraint_status(G.obpose);
452                         make_displists_by_armature (G.obpose);
453 #endif
454                         break;
455                 }
456
457                 allqueue(REDRAWVIEW3D, 0);
458                 return;
459         }
460
461         base= FIRSTBASE;
462         while(base) {
463                 if TESTBASELIB(base) {
464                         if(mode=='r') {
465                                 memset(base->object->rot, 0, 3*sizeof(float));
466                                 memset(base->object->drot, 0, 3*sizeof(float));
467                                 QuatOne(base->object->quat);
468                                 QuatOne(base->object->dquat);
469                         }
470                         else if(mode=='g') {
471                                 memset(base->object->loc, 0, 3*sizeof(float));
472                                 memset(base->object->dloc, 0, 3*sizeof(float));
473                         }
474                         else if(mode=='s') {
475                                 memset(base->object->dsize, 0, 3*sizeof(float));
476                                 base->object->size[0]= 1.0;
477                                 base->object->size[1]= 1.0;
478                                 base->object->size[2]= 1.0;
479                         }
480                         else if(mode=='o') {
481                                 if(base->object->parent) {
482                                         v1= base->object->loc;
483                                         v3= base->object->parentinv[3];
484                                         
485                                         Mat3CpyMat4(mat, base->object->parentinv);
486                                         VECCOPY(v3, v1);
487                                         v3[0]= -v3[0];
488                                         v3[1]= -v3[1];
489                                         v3[2]= -v3[2];
490                                         Mat3MulVecfl(mat, v3);
491                                 }
492                         }
493                 }
494                 base= base->next;
495         }
496         
497         allqueue(REDRAWVIEW3D, 0);
498 }
499
500 void reset_slowparents(void)
501 {
502         /* back to original locations */
503         Base *base;
504         
505         base= FIRSTBASE;
506         while(base) {
507                 if(base->object->parent) {
508                         if(base->object->partype & PARSLOW) {
509                                 base->object->partype -= PARSLOW;
510                                 where_is_object(base->object);
511                                 base->object->partype |= PARSLOW;
512                         }
513                 }
514                 base= base->next;
515         }
516 }
517
518 void set_slowparent(void)
519 {
520         Base *base;
521
522         if( okee("Set slow parent")==0 ) return;
523
524         base= FIRSTBASE;
525         while(base) {
526                 if TESTBASELIB(base) {
527                         if(base->object->parent) base->object->partype |= PARSLOW;
528                 }
529                 base= base->next;
530         }
531         
532 }
533
534 void make_vertex_parent(void)
535 {
536         EditMesh *em = G.editMesh;
537         EditVert *eve;
538         Base *base;
539         Nurb *nu;
540         BezTriple *bezt;
541         BPoint *bp;
542         Object *par, *ob;
543         int a, v1=0, v2=0, v3=0, nr=1;
544         
545         /* we need 1 ot 3 selected vertices */
546         
547         if(G.obedit->type==OB_MESH) {
548                 eve= em->verts.first;
549                 while(eve) {
550                         if(eve->f & 1) {
551                                 if(v1==0) v1= nr;
552                                 else if(v2==0) v2= nr;
553                                 else if(v3==0) v3= nr;
554                                 else break;
555                         }
556                         nr++;
557                         eve= eve->next;
558                 }
559         }
560         else if ELEM(G.obedit->type, OB_SURF, OB_CURVE) {
561                 nu= editNurb.first;
562                 while(nu) {
563                         if((nu->type & 7)==CU_BEZIER) {
564                                 bezt= nu->bezt;
565                                 a= nu->pntsu;
566                                 while(a--) {
567                                         if(BEZSELECTED(bezt)) {
568                                                 if(v1==0) v1= nr;
569                                                 else if(v2==0) v2= nr;
570                                                 else if(v3==0) v3= nr;
571                                                 else break;
572                                         }
573                                         nr++;
574                                         bezt++;
575                                 }
576                         }
577                         else {
578                                 bp= nu->bp;
579                                 a= nu->pntsu*nu->pntsv;
580                                 while(a--) {
581                                         if(bp->f1 & SELECT) {
582                                                 if(v1==0) v1= nr;
583                                                 else if(v2==0) v2= nr;
584                                                 else if(v3==0) v3= nr;
585                                                 else break;
586                                         }
587                                         nr++;
588                                         bp++;
589                                 }
590                         }
591                         nu= nu->next;
592                 }
593                 
594         }
595         
596         if( !(v1 && v2==0 && v3==0) && !(v1 && v2 && v3) ) {
597                 error("Select either 1 or 3 vertices to parent to");
598                 return;
599         }
600         
601         if(okee("Make vertex-parent")==0) return;
602         
603         base= FIRSTBASE;
604         while(base) {
605                 if TESTBASELIB(base) {
606                         if(base!=BASACT) {
607                                 ob= base->object;
608                                 par= BASACT->object->parent;
609                                 
610                                 while(par) {
611                                         if(par==ob) break;
612                                         par= par->parent;
613                                 }
614                                 if(par) {
615                                         error("Loop in parents");
616                                 }
617                                 else {
618                                         ob->parent= BASACT->object;
619                                         if(v3) {
620                                                 ob->partype= PARVERT3;
621                                                 ob->par1= v1-1;
622                                                 ob->par2= v2-1;
623                                                 ob->par3= v3-1;
624
625                                                 /* inverse parent matrix */
626                                                 what_does_parent(ob);
627                                                 Mat4Invert(ob->parentinv, workob.obmat);
628                                                 clear_workob();
629                                         }
630                                         else {
631                                                 ob->partype= PARVERT1;
632                                                 ob->par1= v1-1;
633
634                                                 /* inverse parent matrix */
635                                                 what_does_parent(ob);
636                                                 Mat4Invert(ob->parentinv, workob.obmat);
637                                                 clear_workob();
638                                         }
639                                 }
640                         }
641                 }
642                 base= base->next;
643         }
644         allqueue(REDRAWVIEW3D, 0);
645         
646 }
647
648 int test_parent_loop(Object *par, Object *ob)
649 {
650         /* test if 'ob' is a parent somewhere in par's parents */
651         
652         if(par==0) return 0;
653         if(ob == par) return 1;
654         
655         if(par->type==OB_IKA) {
656                 Ika *ika= par->data;
657                 
658                 if( ob == ika->parent ) return 1;
659                 if( test_parent_loop(ika->parent, ob) ) return 1;
660         }
661
662         return test_parent_loop(par->parent, ob);
663
664 }
665
666 void make_parent(void)
667 {
668         Base *base;
669         Object *par;
670         Ika *ika;
671         short qual, ok, mode=0, limbnr=0, effchild=0;
672         char *bonestr=NULL;
673         Bone    *bone=NULL;
674         int     bonenr;
675
676         if(G.scene->id.lib) return;
677         if(G.obedit) {
678                 if ELEM3(G.obedit->type, OB_MESH, OB_CURVE, OB_SURF) make_vertex_parent();
679                 else if (G.obedit->type==OB_ARMATURE) make_bone_parent();
680                 return;
681         }
682         if(BASACT==0) return;
683         
684         qual= G.qual;
685         par= BASACT->object;
686
687         if(par->type==OB_IKA) {
688                 
689                 if(qual & LR_SHIFTKEY)
690                         mode= pupmenu("Make Parent Without Inverse%t|Use Vertex %x1|Use Limb %x2|Use Skeleton %x3");
691                 else 
692                         mode= pupmenu("Make Parent %t|Use Vertex %x1|Use Limb %x2|Use Skeleton %x3");
693                 
694                 if(mode==1) {
695                         draw_ika_nrs(par, 0);
696                         if(button(&limbnr, 0, 99, "Vertex: ")==0) {
697                                 allqueue(REDRAWVIEW3D, 0);
698                                 return;
699                         }
700                 }
701                 else if(mode==2) {
702                         draw_ika_nrs(par, 1);
703                         if(button(&limbnr, 0, 99, "Limb: ")==0) {
704                                 allqueue(REDRAWVIEW3D, 0);
705                                 return;
706                         }
707                 }
708                 else if(mode==3) {
709                         ika= par->data;
710                         if(ika->def==0) {
711                                 error("No skeleton available: use CTRL K");
712                                 return;
713                         }
714                 }
715                 else return;
716
717                 if(mode==1) mode= PARVERT1;
718                 else if(mode==2) mode= PARLIMB;
719                 else if(mode==3) mode= PARSKEL;
720
721                 /* test effchild */
722                 base= FIRSTBASE;
723                 while(base) {
724                         if TESTBASELIB(base) {
725                                 if(base->object->type==OB_IKA && base->object!=par && mode==PARVERT1 ) {
726                                         if(effchild==0) {
727                                                 if(okee("Effector as child")) effchild= 1;
728                                                 else effchild= 2;
729                                         }
730                                 }
731                         }
732                         if(effchild) break;
733                         base= base->next;
734                 }
735         }
736         else if(par->type == OB_CURVE){
737                 bConstraint *con;
738                 bFollowPathConstraint *data;
739
740                 mode= pupmenu("Make Parent %t|Normal Parent %x1|Follow Path %x2");
741                 if (mode == 0){
742                         return;
743                 }
744                 else if (mode == 2){
745
746                         base= FIRSTBASE;
747                         while(base) {
748                                 if TESTBASELIB(base) {
749                                         if(base!=BASACT) {
750                                                 float cmat[4][4], vec[3], size[3];
751
752                                                 con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
753                                                 strcpy (con->name, "AutoPath");
754
755                                                 data = con->data;
756                                                 data->tar = BASACT->object;
757
758                                                 add_constraint_to_object(con, base->object);
759
760                                                 get_constraint_target(con, TARGET_OBJECT, NULL, cmat, size, G.scene->r.cfra - base->object->sf);
761                                                 VecSubf(vec, base->object->obmat[3], cmat[3]);
762
763                                                 base->object->loc[0] = vec[0];
764                                                 base->object->loc[1] = vec[1];
765                                                 base->object->loc[2] = vec[2];
766                                         }
767                                 }
768                                 base= base->next;
769                         }
770
771                         test_scene_constraints();
772                         allqueue(REDRAWVIEW3D, 0);
773                         sort_baselist(G.scene);
774                         return;
775                 }
776         }
777         else if(par->type == OB_ARMATURE){
778
779                         base= FIRSTBASE;
780                         while(base) {
781                                 if TESTBASELIB(base) {
782                                         if(base!=BASACT) {
783                                                 if(base->object->type==OB_MESH) {
784                                                         mode= pupmenu("Make Parent %t|Use Bone %x1|Use Armature %x2|Use Object %x3");
785                                                         break;
786                                                 }
787                                                 else {
788                                                         mode= pupmenu("Make Parent %t|Use Bone %x1|Use Object %x3");
789                                                         break;
790                                                 }
791                                         }
792                                 }
793                                 base= base->next;
794                         }
795                 
796                         switch (mode){
797                         case 1:
798                                 mode=PARBONE;
799                                 /* Make bone popup menu */
800
801                                 bonestr = make_bone_menu(get_armature(par));
802                 //              if(mbutton(&bone, bonestr, 1, 24, "Bone: ")==0) {
803
804                                 bonenr= pupmenu_col(bonestr, 20);
805                                 if (bonestr)
806                                         MEM_freeN (bonestr);
807                                 
808                                 if (bonenr==-1){
809                                         allqueue(REDRAWVIEW3D, 0);
810                                         return;
811                                 }
812
813                                 apply_pose_armature(get_armature(par), par->pose, 0);
814                                 bone=get_indexed_bone(get_armature(par), bonenr); 
815                                 if (!bone){
816                 //                      error ("Invalid bone!");
817                                         allqueue(REDRAWVIEW3D, 0);
818                                         return;
819                                 }
820
821                                 break;
822                         case 2:
823                                 mode=PARSKEL;
824                                 break;
825                         case 3:
826                                 mode=PAROBJECT;
827                                 break;
828                         default:
829                                 return;
830                         }
831         }
832                 else {
833                 if(qual & LR_SHIFTKEY) {
834                         if(okee("Make parent without inverse")==0) return;
835                 }
836                 else {
837                         if(qual & LR_ALTKEY) {
838                                 if(okee("Make vertex parent")==0) return;
839                         }
840                         else if(okee("Make parent")==0) return;
841
842                         /* test effchild */
843                         base= FIRSTBASE;
844                         while(base) {
845                                 if TESTBASELIB(base) {
846                                         if(base->object->type==OB_IKA && base->object != par) {
847                                                 if(effchild==0) {
848                                                         if(okee("Effector as Child")) effchild= 1;
849                                                         else effchild= 2;
850                                                 }
851                                         }
852                                 }
853                                 if(effchild) break;
854                                 base= base->next;
855                         }
856                         
857                         /* now we'll clearparentandkeeptransform all objects */
858                         base= FIRSTBASE;
859                         while(base) {
860                                 if TESTBASELIB(base) {
861                                         if(base!=BASACT && base->object->parent) {
862                                                 if(base->object->type==OB_IKA && effchild==1);
863                                                 else {
864                                                         base->object->parent= 0;
865                                                         apply_obmat(base->object);
866                                                 }
867                                         }
868                                 }
869                                 base= base->next;
870                         }
871                 }
872         }
873         
874         
875         base= FIRSTBASE;
876         while(base) {
877                 if TESTBASELIB(base) {
878                         if(base!=BASACT) {
879                                 
880                                 ok= 1;
881                                 if(base->object->type==OB_IKA) {
882                                 
883                                         if(effchild==1) {
884                                         
885                                                 if( test_parent_loop(par, base->object)==0 ) {
886                                                 
887                                                         Ika *ika= base->object->data;
888                                                         
889                                                         ika->parent= par;
890                                                         ika->par1= limbnr;
891                                                         ika->partype= mode;
892                                                         itterate_ika(base->object);
893                                                         ok= 0;
894                                                 }
895                                                 else {
896                                                         ok= 0;
897                                                         error("Loop in parents");
898                                                 }
899                                         }
900                                 }
901                                 
902                                 if(ok) {
903                                         if( test_parent_loop(par, base->object) ) {
904                                                 error("Loop in parents");
905                                         }
906                                         else {
907                                                 
908                                                 if(par->type==OB_IKA){
909                                                         base->object->partype= mode;
910                                                         base->object->par1= limbnr;
911                                                 }
912                                                 else if (par->type==OB_ARMATURE){
913                                                         base->object->partype= mode;
914                                                         if (bone)
915                                                                 strcpy (base->object->parsubstr, bone->name);
916                                                         else
917                                                                 base->object->parsubstr[0]=0;
918                                                 }
919                                                 
920                                                 else if(qual & LR_ALTKEY) {
921                                                         base->object->partype= PARVERT1;
922                                                 }
923                                                 else {
924                                                         base->object->partype= PAROBJECT;
925                                                 }
926                                                 
927                                                 base->object->parent= par;
928                                                 
929                                                 /* calculate inverse parent matrix? */
930                                                 if( (qual & LR_SHIFTKEY) ) {
931                                                         /* not... */
932                                                         Mat4One(base->object->parentinv);
933                                                         memset(base->object->loc, 0, 3*sizeof(float));
934                                                 }
935                                                 else {
936                                                         if(mode==PARSKEL && par->type == OB_ARMATURE) {
937                                                                 /* Prompt the user as to whether he wants to
938                                                                  * add some vertex groups based on the bones
939                                                                  * in the parent armature.
940                                                                  */
941                                                                 create_vgroups_from_armature(base->object, 
942                                                                                                                          par);
943
944                                                                 base->object->partype= PAROBJECT;
945                                                                 what_does_parent(base->object);
946                                                                 Mat4One (base->object->parentinv);
947                                                                 base->object->partype= mode;
948                                                         }
949                                                         else
950                                                                 what_does_parent(base->object);
951                                                         Mat4Invert(base->object->parentinv, workob.obmat);
952                                                 }
953                                                 
954                                                 if(par->type==OB_LATTICE) makeDispList(base->object);
955                                                 if(par->type==OB_IKA && mode==PARSKEL) makeDispList(base->object);
956                                                 if(par->type==OB_ARMATURE && mode == PARSKEL){
957                                                         verify_defgroups(base->object);
958                                                         makeDispList(base->object);
959                                                 }
960                                         }
961                                 }
962                         }
963                 }
964                 base= base->next;
965         }
966         allqueue(REDRAWVIEW3D, 0);
967         allqueue(REDRAWOOPS, 0);
968         
969         test_scene_constraints();
970         sort_baselist(G.scene);
971 }
972
973
974 void enter_editmode(void)
975 {
976         Base *base;
977         Object *ob;
978         Ika *ika;
979         ID *id;
980         Mesh *me;
981         int ok= 0;
982         bArmature *arm;
983         
984         if(G.scene->id.lib) return;
985         base= BASACT;
986         if(base==0) return;
987         if((base->lay & G.vd->lay)==0) return;
988         
989         ob= base->object;
990         if(ob->data==0) return;
991         
992         id= ob->data;
993         if(id->lib) {
994                 error("Can't edit library data");
995                 return;
996         }
997         
998         if(ob->type==OB_MESH) {
999                 me= get_mesh(ob);
1000                 if( me==0 ) return;
1001                 if(me->id.lib) {
1002                         error("Can't edit library data");
1003                         return;
1004                 }
1005                 ok= 1;
1006                 G.obedit= ob;
1007                 make_editMesh();
1008                 allqueue(REDRAWBUTSLOGIC, 0);
1009                 if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);
1010         }
1011         if (ob->type==OB_ARMATURE){
1012                 arm=base->object->data;
1013                 if (!arm) return;
1014                 if (arm->id.lib){
1015                         error("Can't edit library data");
1016                         return;
1017                 }
1018                 ok=1;
1019                 G.obedit=ob;
1020                 make_editArmature();
1021                 allqueue (REDRAWVIEW3D,0);
1022         }
1023         else if(ob->type==OB_IKA) {     /* grab type */
1024                 base= FIRSTBASE;
1025                 while(base) {
1026                         if TESTBASE(base) {
1027                                 if(base->object->type==OB_IKA) {
1028                                         ika= base->object->data;
1029                                         if(ika->flag & IK_GRABEFF) ika->flag &= ~IK_GRABEFF;
1030                                         else ika->flag |= IK_GRABEFF;
1031                                 }
1032                         }
1033                         base= base->next;
1034                 }
1035                 allqueue(REDRAWVIEW3D, 0);
1036         }
1037         else if(ob->type==OB_FONT) {
1038                 G.obedit= ob;
1039                 ok= 1;
1040                 make_editText();
1041         }
1042         else if(ob->type==OB_MBALL) {
1043                 G.obedit= ob;
1044                 ok= 1;
1045                 make_editMball();
1046         }
1047         else if(ob->type==OB_LATTICE) {
1048                 G.obedit= ob;
1049                 ok= 1;
1050                 make_editLatt();
1051         }
1052         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1053                 ok= 1;
1054                 G.obedit= ob;
1055                 make_editNurb();
1056         }
1057         allqueue(REDRAWBUTSEDIT, 0);
1058         countall();
1059         
1060         if(ok) {
1061                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1062         
1063                 allqueue(REDRAWVIEW3D, 0);
1064         }
1065         else G.obedit= 0;
1066
1067         if (G.obpose)
1068                 exit_posemode (1);
1069         scrarea_queue_headredraw(curarea);
1070 }
1071
1072 void make_displists_by_parent(Object *ob) {
1073         Base *base;
1074         
1075         for (base= FIRSTBASE; base; base= base->next)
1076                 if (ob==base->object->parent)
1077                         makeDispList(base->object);
1078 }
1079
1080 void exit_editmode(int freedata)        /* freedata==0 at render */
1081 {
1082         Base *base;
1083         Object *ob;
1084         Curve *cu;
1085
1086         if(G.obedit==0) return;
1087
1088         if(G.obedit->type==OB_MESH) {
1089
1090                 /* temporal */
1091                 countall();
1092
1093                 if(G.totvert>MESH_MAX_VERTS) {
1094                         error("Too many vertices");
1095                         return;
1096                 }
1097                 load_editMesh();        /* makes new displist */
1098
1099                 if(freedata) free_editMesh();
1100                 
1101                 if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);
1102
1103                 build_particle_system(G.obedit);
1104         }
1105         else if (G.obedit->type==OB_ARMATURE){  
1106                 load_editArmature();
1107                 if (freedata) free_editArmature();
1108         }
1109         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1110                 load_editNurb();
1111                 if(freedata) freeNurblist(&editNurb);
1112         }
1113         else if(G.obedit->type==OB_FONT && freedata==1) {
1114                 load_editText();
1115         }
1116         else if(G.obedit->type==OB_LATTICE) {
1117                 load_editLatt();
1118                 if(freedata) free_editLatt();
1119         }
1120         else if(G.obedit->type==OB_MBALL) {
1121                 load_editMball();
1122                 if(freedata) BLI_freelistN(&editelems);
1123         }
1124
1125         ob= G.obedit;
1126         
1127         /* displist make is different in editmode */
1128         if(freedata) G.obedit= NULL;
1129         makeDispList(ob);
1130
1131         /* has this influence at other objects? */
1132         if(ob->type==OB_CURVE) {
1133
1134                 /* test if ob is use as bevelcurve r textoncurve */
1135                 base= FIRSTBASE;
1136                 while(base) {
1137                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
1138                                 cu= base->object->data;
1139                                 
1140                                 if(cu->textoncurve==ob) {
1141                                         text_to_curve(base->object, 0);
1142                                         makeDispList(base->object);
1143                                 }
1144                                 if(cu->bevobj== ob) {
1145                                         makeDispList(base->object);
1146                                 }
1147                         }
1148                         base= base->next;
1149                 }
1150                 
1151         }
1152         else if(ob->type==OB_LATTICE) {
1153                 make_displists_by_parent(ob);
1154         }
1155
1156         if(freedata) {
1157                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1158         
1159                 countall();
1160                 allqueue(REDRAWVIEW3D, 0);
1161                 allqueue(REDRAWBUTSEDIT, 0);
1162                 allqueue(REDRAWBUTSLOGIC, 0);
1163         }
1164         scrarea_queue_headredraw(curarea);
1165
1166 }
1167
1168 void check_editmode(int type)
1169 {
1170         
1171         if (G.obedit==0 || G.obedit->type==type) return;
1172
1173         exit_editmode(1);
1174 }
1175
1176 static int centremode= 0; /* 0 == do centre, 1 == centre new, 2 == centre cursor */
1177
1178 void docentre(void)
1179 {
1180         EditMesh *em = G.editMesh;
1181         Base *base;
1182         Object *ob;
1183         Mesh *me, *tme;
1184         Curve *cu;
1185 //      BezTriple *bezt;
1186 //      BPoint *bp;
1187         Nurb *nu, *nu1;
1188         EditVert *eve;
1189         float cent[3], centn[3], min[3], max[3], omat[3][3];
1190         int a;
1191         MVert *mvert;
1192
1193         if(G.scene->id.lib) return;
1194
1195         if(G.obedit) {
1196
1197                 INIT_MINMAX(min, max);
1198         
1199                 if(G.obedit->type==OB_MESH) {
1200                         eve= em->verts.first;
1201                         while(eve) {
1202                                 DO_MINMAX(eve->co, min, max);
1203                                 eve= eve->next;
1204                         }
1205                         cent[0]= (min[0]+max[0])/2.0;
1206                         cent[1]= (min[1]+max[1])/2.0;
1207                         cent[2]= (min[2]+max[2])/2.0;
1208                         
1209                         eve= em->verts.first;
1210                         while(eve) {
1211                                 VecSubf(eve->co, eve->co, cent);                        
1212                                 eve= eve->next;
1213                         }
1214                 }
1215         }
1216         
1217         /* reset flags */
1218         base= FIRSTBASE;
1219         while(base) {
1220                 if TESTBASELIB(base) {
1221                         base->object->flag &= ~OB_DONE;
1222                 }
1223                 base= base->next;
1224         }
1225         me= G.main->mesh.first;
1226         while(me) {
1227                 me->flag &= ~ME_ISDONE;
1228                 me= me->id.next;
1229         }
1230         
1231         base= FIRSTBASE;
1232         while(base) {
1233                 
1234                 if TESTBASELIB(base) {
1235                         if((base->object->flag & OB_DONE)==0) {
1236                                 
1237                                 base->object->flag |= OB_DONE;
1238                                 
1239                                 if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1240                                         
1241                                         if(me->key) {
1242                                                 error("Can't change the center of a mesh with vertex keys");
1243                                                 return;
1244                                         }
1245                                         
1246                                         if(centremode==2) {
1247                                                 VECCOPY(cent, give_cursor());
1248                                                 Mat4Invert(base->object->imat, base->object->obmat);
1249                                                 Mat4MulVecfl(base->object->imat, cent);
1250                                         } else {
1251                                                 INIT_MINMAX(min, max);
1252                 
1253                                                 mvert= me->mvert;
1254                                                 for(a=0; a<me->totvert; a++, mvert++) {
1255                                                         DO_MINMAX(mvert->co, min, max);
1256                                                 }
1257                                 
1258                                                 cent[0]= (min[0]+max[0])/2.0;
1259                                                 cent[1]= (min[1]+max[1])/2.0;
1260                                                 cent[2]= (min[2]+max[2])/2.0;
1261                                         }
1262                                                 
1263                                         mvert= me->mvert;
1264                                         for(a=0; a<me->totvert; a++, mvert++) {
1265                                                 VecSubf(mvert->co, mvert->co, cent);
1266                                         }
1267                                         me->flag |= ME_ISDONE;
1268                                         
1269                                         if(centremode) {
1270                                                 Mat3CpyMat4(omat, base->object->obmat);
1271                                                 
1272                                                 VECCOPY(centn, cent);
1273                                                 Mat3MulVecfl(omat, centn);
1274                                                 base->object->loc[0]+= centn[0];
1275                                                 base->object->loc[1]+= centn[1];
1276                                                 base->object->loc[2]+= centn[2];
1277                                                 
1278                                                 /* other users? */
1279                                                 ob= G.main->object.first;
1280                                                 while(ob) {
1281                                                         if((ob->flag & OB_DONE)==0) {
1282                                                                 tme= get_mesh(ob);
1283                                                                 
1284                                                                 if(tme==me) {
1285                                                                         
1286                                                                         ob->flag |= OB_DONE;
1287
1288                                                                         Mat3CpyMat4(omat, ob->obmat);
1289                                                                         VECCOPY(centn, cent);
1290                                                                         Mat3MulVecfl(omat, centn);
1291                                                                         ob->loc[0]+= centn[0];
1292                                                                         ob->loc[1]+= centn[1];
1293                                                                         ob->loc[2]+= centn[2];
1294                                                                         
1295                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
1296                                                                                 mvert= tme->mvert;
1297                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
1298                                                                                         VecSubf(mvert->co, mvert->co, cent);
1299                                                                                 }
1300                                                                                 tme->flag |= ME_ISDONE;
1301                                                                         }
1302                                                                 }
1303                                                         }
1304                                                         
1305                                                         ob= ob->id.next;
1306                                                 }
1307                                         }
1308                                 
1309                                         /* displist of all users, also this one */
1310                                         makeDispList(base->object);
1311                                         
1312                                         /* DO: check all users... */
1313                                         tex_space_mesh(me);
1314                 
1315                                 }
1316                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1317                                                                         
1318                                         if(G.obedit) {
1319                                                 nu1= editNurb.first;
1320                                         }
1321                                         else {
1322                                                 cu= base->object->data;
1323                                                 nu1= cu->nurb.first;
1324                                         }
1325                                         
1326                                         if(centremode==2) {
1327                                                 VECCOPY(cent, give_cursor());
1328                                                 Mat4Invert(base->object->imat, base->object->obmat);
1329                                                 Mat4MulVecfl(base->object->imat, cent);
1330                                                         
1331                                                         /* Curves need to be 2d, never offset in
1332                                                          * Z. Is a somewhat arbitrary restriction, 
1333                                                          * would probably be nice to remove.
1334                                                          */
1335                                                 cent[2]= 0.0;
1336                                         } else {
1337                                                 INIT_MINMAX(min, max);
1338         
1339                                                 nu= nu1;
1340                                                 while(nu) {
1341                                                         minmaxNurb(nu, min, max);
1342                                                         nu= nu->next;
1343                                                 }
1344                                                 
1345                                                 cent[0]= (min[0]+max[0])/2.0;
1346                                                 cent[1]= (min[1]+max[1])/2.0;
1347                                                 cent[2]= (min[2]+max[2])/2.0;
1348                                         }
1349                                         
1350                                         nu= nu1;
1351                                         while(nu) {
1352                                                 if( (nu->type & 7)==1) {
1353                                                         a= nu->pntsu;
1354                                                         while (a--) {
1355                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1356                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1357                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1358                                                         }
1359                                                 }
1360                                                 else {
1361                                                         a= nu->pntsu*nu->pntsv;
1362                                                         while (a--)
1363                                                                 VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1364                                                 }
1365                                                 nu= nu->next;
1366                                         }
1367                         
1368                                         if(centremode && G.obedit==0) {
1369                                                 Mat3CpyMat4(omat, base->object->obmat);
1370                                                 
1371                                                 Mat3MulVecfl(omat, cent);
1372                                                 base->object->loc[0]+= cent[0];
1373                                                 base->object->loc[1]+= cent[1];
1374                                                 base->object->loc[2]+= cent[2];
1375                                         }
1376                         
1377                                         if(G.obedit) {
1378                                                 makeDispList(G.obedit);
1379                                                 break;
1380                                         }
1381                                         else makeDispList(base->object);
1382         
1383                                 }
1384                                 else if(base->object->type==OB_FONT) {
1385                                         /* get from bb */
1386                                         
1387                                         cu= base->object->data;
1388                                         if(cu->bb==0) return;
1389                                         
1390                                         cu->xof= -0.5*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
1391                                         cu->yof= -0.5 -0.5*( cu->bb->vec[0][1] - cu->bb->vec[2][1]);    /* extra 0.5 is the height of above line */
1392                                         
1393                                         /* not really ok, do this better once! */
1394                                         cu->xof /= cu->fsize;
1395                                         cu->yof /= cu->fsize;
1396                                         
1397                                         text_to_curve(base->object, 0);
1398                                         makeDispList(base->object);
1399                                         
1400                                         allqueue(REDRAWBUTSEDIT, 0);
1401                                 }
1402                         }
1403                 }
1404                 base= base->next;
1405         }
1406
1407         allqueue(REDRAWVIEW3D, 0);
1408 }
1409
1410 void docentre_new(void)
1411 {
1412         if(G.scene->id.lib) return;
1413
1414         if(G.obedit) {
1415                 error("Unable to center new in Edit Mode");
1416         }
1417         else {
1418                 centremode= 1;
1419                 docentre();
1420                 centremode= 0;
1421         }
1422 }
1423
1424 void docentre_cursor(void)
1425 {
1426         if(G.scene->id.lib) return;
1427
1428         if(G.obedit) {
1429                 error("Unable to center cursor in Edit Mode");
1430         }
1431         else {
1432                 centremode= 2;
1433                 docentre();
1434                 centremode= 0;
1435         }
1436 }
1437
1438 void movetolayer(void)
1439 {
1440         Base *base;
1441         unsigned int lay= 0, local;
1442
1443         if(G.scene->id.lib) return;
1444
1445         base= FIRSTBASE;
1446         while(base) {
1447                 if TESTBASE(base) lay |= base->lay;
1448                 base= base->next;
1449         }
1450         if(lay==0) return;
1451         lay &= 0xFFFFFF;
1452         
1453         if( movetolayer_buts(&lay)==0 ) return;
1454         if(lay==0) return;
1455
1456         base= FIRSTBASE;
1457         while(base) {
1458                 if TESTBASE(base) {
1459                         local= base->lay & 0xFF000000;
1460                         base->lay= lay + local;
1461
1462                         base->object->lay= lay;
1463                 }
1464                 base= base->next;
1465         }
1466         countall();
1467         allqueue(REDRAWBUTSEDIT, 0);
1468         allqueue(REDRAWVIEW3D, 0);
1469         allqueue(REDRAWOOPS, 0);
1470         allqueue(REDRAWINFO, 0);
1471 }
1472
1473
1474 void special_editmenu(void)
1475 {
1476         extern short editbutflag;
1477         extern float doublimit;
1478         float fac;
1479         int nr,ret;
1480         short randfac;
1481         
1482         if(G.obedit==0) {
1483                 if(G.f & G_FACESELECT) {
1484                         Mesh *me= get_mesh(OBACT);
1485                         TFace *tface;
1486                         int a;
1487                         
1488                         if(me==0 || me->tface==0) return;
1489                         
1490                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|Clr     Tex%x6|         Shared%x7|         Light%x8|         Invisible%x9|         Collision%x10");
1491         
1492                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1493                                 if(tface->flag & SELECT) {
1494                                         switch(nr) {
1495                                         case 1:
1496                                                 tface->mode |= TF_TEX; break;
1497                                         case 2:
1498                                                 tface->mode |= TF_SHAREDCOL; break;
1499                                         case 3:
1500                                                 tface->mode |= TF_LIGHT; break; 
1501                                         case 4:
1502                                                 tface->mode |= TF_INVISIBLE; break;
1503                                         case 5:
1504                                                 tface->mode |= TF_DYNAMIC; break;
1505                                         case 6:
1506                                                 tface->mode &= ~TF_TEX;
1507                                                 tface->tpage= 0;
1508                                                 break;
1509                                         case 7:
1510                                                 tface->mode &= ~TF_SHAREDCOL; break;
1511                                         case 8:
1512                                                 tface->mode &= ~TF_LIGHT; break;
1513                                         case 9:
1514                                                 tface->mode &= ~TF_INVISIBLE; break;
1515                                         case 10:
1516                                                 tface->mode &= ~TF_DYNAMIC; break;
1517                                         }
1518                                 }
1519                         }
1520                         allqueue(REDRAWVIEW3D, 0);
1521                         allqueue(REDRAWBUTSLOGIC, 0);
1522                 }
1523                 else if(G.f & G_VERTEXPAINT) {
1524                         Mesh *me= get_mesh(OBACT);
1525                         
1526                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1527                         
1528                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1529                         if(nr==1) {
1530                                 
1531                                 if(me->tface) tface_to_mcol(me);
1532                                 
1533                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1534                                 do_shared_vertexcol(me);
1535                                 
1536                                 if(me->tface) mcol_to_tface(me, 1);
1537                         }
1538                 }
1539                 else {
1540                         Base *base, *base_select= NULL;
1541
1542                         // Get the active object mesh.
1543                         Mesh *me= get_mesh(OBACT);
1544
1545                         // If the active object is a mesh...
1546                         if (me) {
1547                                 // Bring up a little menu with the boolean operation choices on.
1548                                 nr= pupmenu("Boolean %t|Intersect%x1|Union%x2|Difference%x3");
1549
1550                                 if (nr > 0) {
1551                                         // user has made a choice of a menu element.
1552                                         // All of the boolean functions require 2 mesh objects 
1553                                         // we search through the object list to find the other 
1554                                         // selected item and make sure it is distinct and a mesh.
1555
1556                                         base= FIRSTBASE;
1557                                         while(base) {
1558                                                 if(base->flag & SELECT) {
1559                                                         if(base->object != OBACT) base_select= base;
1560                                                 }
1561
1562                                                 base= base->next;
1563                                         }
1564
1565                                         if (base_select) {
1566                                                 if (get_mesh(base_select->object)) {
1567                                                         waitcursor(1);
1568                                                         ret = NewBooleanMesh(BASACT,base_select,nr);
1569                                                         if (ret==0) {
1570                                                                 error("An internal error occurred -- sorry!");
1571                                                         } else if(ret==-1) {
1572                                                                 error("Selected meshes must have faces to perform boolean operations");
1573                                                         }
1574
1575                                                         waitcursor(0);
1576                                                 } else {
1577                                                         error("Please select 2 meshes");
1578                                                 }
1579                                         } else {
1580                                                 error("Please select 2 meshes");
1581                                         }
1582                                 }
1583
1584                                 allqueue(REDRAWVIEW3D, 0);
1585                         }
1586                 }
1587         }
1588         else if(G.obedit->type==OB_MESH) {
1589
1590                 nr= pupmenu("Specials%t|Subdivide%x1|Subdivide Fractal%x2|Subdivide Smooth%x3|Merge%x4|Remove Doubles%x5|Hide%x6|Reveal%x7|Select Swap%x8|Flip Normals %x9|Smooth %x10|Bevel %x11");
1591                 if(nr>0) waitcursor(1);
1592                 
1593                 switch(nr) {
1594                 case 1:
1595                         undo_push_mesh("Subdivide");
1596                         subdivideflag(1, 0.0, editbutflag);
1597                         break;
1598                 case 2:
1599                         randfac= 10;
1600                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
1601                         fac= -( (float)randfac )/100;
1602                         undo_push_mesh("Subdivide Fractal");
1603                         subdivideflag(1, fac, editbutflag);
1604                         break;
1605                 case 3:
1606                         undo_push_mesh("Subdivide Smooth");
1607                         subdivideflag(1, 0.0, editbutflag | B_SMOOTH);
1608                         break;
1609                 case 4:
1610                         mergemenu();
1611                         break;
1612                 case 5:
1613                         undo_push_mesh("Remove Doubles");
1614                         notice("Removed %d Vertices", removedoublesflag(1, doublimit));
1615                         break;
1616                 case 6:
1617                         hide_mesh(0);
1618                         break;
1619                 case 7:
1620                         reveal_mesh();
1621                         break;
1622                 case 8:
1623                         selectswap_mesh();
1624                         break;
1625                 case 9:
1626                         undo_push_mesh("Flip Normals");
1627                         flip_editnormals();
1628                         break;
1629                 case 10:
1630                         undo_push_mesh("Smooth");
1631                         vertexsmooth();
1632                         break;
1633                 case 11:
1634                         bevel_menu();
1635                         break;
1636                 }               
1637                 
1638                 makeDispList(G.obedit);
1639                 
1640                 if(nr>0) waitcursor(0);
1641                 
1642         }
1643         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1644
1645                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2");
1646                 
1647                 switch(nr) {
1648                 case 1:
1649                         subdivideNurb();
1650                         break;
1651                 case 2:
1652                         switchdirectionNurb2();
1653                         break;
1654                 }
1655         }
1656
1657         countall();
1658         allqueue(REDRAWVIEW3D, 0);
1659         
1660 }
1661
1662 void convertmenu(void)
1663 {
1664         Base *base, *basen, *basact;
1665         Object *ob, *ob1;
1666         Curve *cu;
1667         MetaBall *mb;
1668         Mesh *me;
1669         DispList *dl;
1670         int ok=0, nr = 0, a;
1671         
1672         if(G.scene->id.lib) return;
1673
1674         ob= OBACT;
1675         if(ob==0) return;
1676         if(G.obedit) return;
1677         
1678         basact= BASACT; /* will be restored */
1679                 
1680         if(ob->type==OB_FONT) {
1681                 nr= pupmenu("Convert Font to%t|Curve");
1682                 if(nr>0) ok= 1;
1683         }
1684         else if(ob->type==OB_MBALL) {
1685                 nr= pupmenu("Convert Metaball to%t|Mesh (keep original)");
1686                 if(nr>0) ok= 1;
1687         }
1688         else if(ob->type==OB_CURVE) {
1689                 nr= pupmenu("Convert Curve to%t|Mesh");
1690                 if(nr>0) ok= 1;
1691         }
1692         else if(ob->type==OB_SURF) {
1693                 nr= pupmenu("Convert Nurbs Surface to%t|Mesh");
1694                 if(nr>0) ok= 1;
1695         }
1696         else if(ob->type==OB_MESH && mesh_uses_displist((Mesh*) ob->data)) {
1697                 nr= pupmenu("Convert SubSurf to%t|Mesh (Keep Original)");
1698                 if(nr>0) ok= 1;
1699         }
1700         if(ok==0) return;
1701
1702         /* don't forget multiple users! */
1703
1704         /* reset flags */
1705         base= FIRSTBASE;
1706         while(base) {
1707                 if TESTBASELIB(base) {
1708                         base->object->flag &= ~OB_DONE;
1709                 }
1710                 base= base->next;
1711         }
1712
1713         base= FIRSTBASE;
1714         while(base) {
1715                 if TESTBASELIB(base) {
1716                         
1717                         ob= base->object;
1718                         
1719                         if(ob->flag & OB_DONE);
1720                         else if(ob->type==OB_MESH) {
1721                                 Mesh *oldme= ob->data;
1722                                 
1723                                 if (mesh_uses_displist(oldme)) {
1724                                         DispListMesh *dlm;
1725
1726                                         ob->flag |= OB_DONE;
1727
1728                                         ob1= copy_object(ob);
1729
1730                                         basen= MEM_mallocN(sizeof(Base), "duplibase");
1731                                         *basen= *base;
1732                                         BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
1733                                         basen->object= ob1;
1734                                         basen->flag &= ~SELECT;
1735                                                 
1736                                         me= ob1->data;
1737                                         me->id.us--;
1738                                                 
1739                                         ob1->data= add_mesh();
1740                                         G.totmesh++;
1741                                         ob1->type= OB_MESH;
1742
1743                                         me= ob1->data;
1744                                         me->totcol= oldme->totcol;
1745                                         if(ob1->totcol) {
1746                                                 me->mat= MEM_dupallocN(oldme->mat);
1747                                                 for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1748                                         }
1749                                                 
1750                                         dlm= subsurf_make_dispListMesh_from_mesh(oldme, NULL, oldme->subdiv, oldme->flag);
1751                                         displistmesh_to_mesh(dlm, ob1->data);
1752                                         displistmesh_free(dlm);
1753
1754                                         tex_space_mesh(me);
1755                                 }
1756                         }
1757                         else if(ob->type==OB_FONT) {
1758                                 if(nr==1) {
1759                                 
1760                                         ob->flag |= OB_DONE;
1761                                 
1762                                         ob->type= OB_CURVE;
1763                                         cu= ob->data;
1764                                         
1765                                         if(cu->vfont) {
1766                                                 cu->vfont->id.us--;
1767                                                 cu->vfont= 0;
1768                                         }
1769                                         /* other users */
1770                                         if(cu->id.us>1) {
1771                                                 ob1= G.main->object.first;
1772                                                 while(ob1) {
1773                                                         if(ob1->data==cu) ob1->type= OB_CURVE;
1774                                                         ob1= ob1->id.next;
1775                                                 }
1776                                         }
1777                                 }
1778                         }
1779                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1780                                 if(nr==1) {
1781
1782                                         ob->flag |= OB_DONE;
1783                                         cu= ob->data;
1784                                         
1785                                         dl= cu->disp.first;
1786                                         if(dl==0) makeDispList(ob);
1787
1788                                         nurbs_to_mesh(ob); /* also does users */
1789
1790                                         /* texspace and normals */
1791                                         BASACT= base;
1792                                         enter_editmode();
1793                                         exit_editmode(1);
1794                                         BASACT= basact;
1795                                 }
1796                         }
1797                         else if(ob->type==OB_MBALL) {
1798                         
1799                                 if(nr==1) {
1800                                         ob= find_basis_mball(ob);
1801                                         
1802                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
1803                                         
1804                                                 ob->flag |= OB_DONE;
1805
1806                                                 ob1= copy_object(ob);
1807
1808                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1809                                                 *basen= *base;
1810                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
1811                                                 basen->object= ob1;
1812                                                 basen->flag &= ~SELECT;
1813                                                 
1814                                                 mb= ob1->data;
1815                                                 mb->id.us--;
1816                                                 
1817                                                 ob1->data= add_mesh();
1818                                                 G.totmesh++;
1819                                                 ob1->type= OB_MESH;
1820                                                 
1821                                                 me= ob1->data;
1822                                                 me->totcol= mb->totcol;
1823                                                 if(ob1->totcol) {
1824                                                         me->mat= MEM_dupallocN(mb->mat);
1825                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1826                                                 }
1827                                                 
1828                                                 mball_to_mesh(&ob->disp, ob1->data);
1829                                                 tex_space_mesh(me);
1830                                         }
1831                                 }
1832                         }
1833                 }
1834                 base= base->next;
1835         }
1836         
1837         allqueue(REDRAWVIEW3D, 0);
1838         allqueue(REDRAWOOPS, 0);
1839         allqueue(REDRAWBUTSEDIT, 0);
1840 }
1841
1842         /* Change subdivision properties of mesh object ob, if
1843          * level==-1 then toggle subsurf, else set to level.
1844          */
1845 void flip_subdivison(Object *ob, int level)
1846 {
1847         Mesh *me = ob->data;
1848
1849         if (level == -1) {
1850                 me->flag ^= ME_SUBSURF;
1851         } else {
1852                 me->subdiv = level;
1853         }
1854
1855         allqueue(REDRAWVIEW3D, 0);
1856         allqueue(REDRAWOOPS, 0);
1857         allqueue(REDRAWBUTSEDIT, 0);
1858         makeDispList(ob);
1859 }
1860  
1861 void copymenu_properties(Object *ob)
1862 {       
1863         bProperty *prop, *propn, *propc;
1864         Base *base;
1865         int nr, tot=0;
1866         char *str;
1867         
1868         prop= ob->prop.first;
1869         while(prop) {
1870                 tot++;
1871                 prop= prop->next;
1872         }
1873         
1874         if(tot==0) {
1875                 error("No properties in the active object to copy");
1876                 return;
1877         }
1878         
1879         str= MEM_callocN(24+32*tot, "copymenu prop");
1880         
1881         strcpy(str, "Copy Property %t");
1882         
1883         tot= 0; 
1884         prop= ob->prop.first;
1885         while(prop) {
1886                 tot++;
1887                 strcat(str, " |");
1888                 strcat(str, prop->name);
1889                 prop= prop->next;
1890         }
1891
1892         nr= pupmenu(str);
1893         if(nr>0) {
1894                 tot= 0;
1895                 prop= ob->prop.first;
1896                 while(prop) {
1897                         tot++;
1898                         if(tot==nr) break;
1899                         prop= prop->next;
1900                 }
1901                 if(prop) {
1902                         propc= prop;
1903                         
1904                         base= FIRSTBASE;
1905                         while(base) {
1906                                 if(base != BASACT) {
1907                                         if(TESTBASELIB(base)) {
1908                                                 prop= get_property(base->object, propc->name);
1909                                                 if(prop) {
1910                                                         free_property(prop);
1911                                                         BLI_remlink(&base->object->prop, prop);
1912                                                 }
1913                                                 propn= copy_property(propc);
1914                                                 BLI_addtail(&base->object->prop, propn);
1915                                         }
1916                                 }
1917                                 base= base->next;
1918                         }
1919                 }
1920         }
1921         MEM_freeN(str);
1922         allqueue(REDRAWVIEW3D, 0);
1923 }
1924
1925 void copymenu_logicbricks(Object *ob)
1926 {
1927         Base *base;
1928         
1929         base= FIRSTBASE;
1930         while(base) {
1931                 if(base->object != ob) {
1932                         if(TESTBASELIB(base)) {
1933                                 
1934                                 /* first: free all logic */
1935                                 free_sensors(&base->object->sensors);                           
1936                                 unlink_controllers(&base->object->controllers);
1937                                 free_controllers(&base->object->controllers);
1938                                 unlink_actuators(&base->object->actuators);
1939                                 free_actuators(&base->object->actuators);
1940                                 
1941                                 /* now copy it, this also works without logicbricks! */
1942                                 clear_sca_new_poins_ob(ob);
1943                                 copy_sensors(&base->object->sensors, &ob->sensors);
1944                                 copy_controllers(&base->object->controllers, &ob->controllers);
1945                                 copy_actuators(&base->object->actuators, &ob->actuators);
1946                                 set_sca_new_poins_ob(base->object);
1947                                 
1948                                 /* some menu settings */
1949                                 base->object->scavisflag= ob->scavisflag;
1950                                 base->object->scaflag= ob->scaflag;
1951                                 
1952                         }
1953                 }
1954                 base= base->next;
1955         }
1956 }
1957
1958 void copy_attr_menu()
1959 {
1960         Object *ob;
1961         short event;
1962         char str[256];
1963
1964         /* If you change this menu, don't forget to update the menu in header_view3d.c
1965          * view3d_edit_object_copyattrmenu() and in toolbox.c
1966          */
1967         strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Drawtype%x4|Time Offset%x5|Dupli%x6|%l|Mass%x7|Damping%x8|Properties%x9|Logic Bricks%x10|%l");
1968
1969         if(!(ob=OBACT)) return;
1970         
1971         strcat (str, "|Object Constraints%x22");
1972         
1973         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
1974                 strcat(str, "|Texture Space%x17");
1975         }       
1976         
1977         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1978         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
1979
1980         if(ob->type==OB_MESH){
1981                 strcat(str, "|Subdiv%x21");
1982         }
1983
1984         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
1985
1986         event= pupmenu(str);
1987         if(event<= 0) return;
1988         
1989         copy_attr(event);
1990 }
1991
1992 void copy_attr(short event)
1993 {
1994         Object *ob, *obt;
1995         Base *base;
1996         Curve *cu, *cu1;
1997         void *poin1, *poin2=0;
1998         
1999         if(G.scene->id.lib) return;
2000
2001         if(!(ob=OBACT)) return;
2002         
2003         if(G.obedit) {
2004                 /* obedit_copymenu(); */
2005                 return;
2006         }
2007         
2008         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2009                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
2010                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
2011                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
2012         }       
2013
2014         if(event==9) {
2015                 copymenu_properties(ob);
2016                 return;
2017         }
2018         else if(event==10) {
2019                 copymenu_logicbricks(ob);
2020                 return;
2021         }
2022
2023         base= FIRSTBASE;
2024         while(base) {
2025                 if(base != BASACT) {
2026                         if(TESTBASELIB(base)) {
2027                                 
2028                                 if(event==1) {  /* loc */
2029                                         VECCOPY(base->object->loc, ob->loc);
2030                                         VECCOPY(base->object->dloc, ob->dloc);
2031                                 }
2032                                 else if(event==2) {  /* rot */
2033                                         VECCOPY(base->object->rot, ob->rot);
2034                                         VECCOPY(base->object->drot, ob->drot);
2035                                         VECCOPY(base->object->quat, ob->quat);
2036                                         VECCOPY(base->object->dquat, ob->dquat);
2037                                 }
2038                                 else if(event==3) {  /* size */
2039                                         VECCOPY(base->object->size, ob->size);
2040                                         VECCOPY(base->object->dsize, ob->dsize);
2041                                 }
2042                                 else if(event==4) {  /* drawtype */
2043                                         base->object->dt= ob->dt;
2044                                         base->object->dtx= ob->dtx;
2045                                         }
2046                                 else if(event==5) {  /* time offs */
2047                                         base->object->sf= ob->sf;
2048                                 }
2049                                 else if(event==6) {  /* dupli */
2050                                         base->object->dupon= ob->dupon;
2051                                         base->object->dupoff= ob->dupoff;
2052                                         base->object->dupsta= ob->dupsta;
2053                                         base->object->dupend= ob->dupend;
2054                                         
2055                                         base->object->transflag &= ~OB_DUPLI;
2056                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2057                                 }
2058                                 else if(event==7) {     /* mass */
2059                                         base->object->mass= ob->mass;
2060                                 }
2061                                 else if(event==8) {     /* damping */
2062                                         base->object->damping= ob->damping;
2063                                         base->object->rdamping= ob->rdamping;
2064                                 }
2065                                 else if(event==17) {    /* tex space */
2066                                         obt= base->object;
2067                                         poin1= 0;
2068                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2069                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2070                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2071                                         
2072                                         if(poin1) {
2073                                                 memcpy(poin1, poin2, 4+12+12+12);
2074                                         
2075                                                 if(obt->type==OB_MESH) tex_space_mesh(obt->data);
2076                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2077                                                 else tex_space_curve(obt->data);
2078                                         }
2079                                 }
2080                                 else if(event==18) {    /* font settings */
2081                                         
2082                                         if(base->object->type==ob->type) {
2083                                                 cu= ob->data;
2084                                                 cu1= base->object->data;
2085                                                 
2086                                                 cu1->spacemode= cu->spacemode;
2087                                                 cu1->spacing= cu->spacing;
2088                                                 cu1->linedist= cu->linedist;
2089                                                 cu1->shear= cu->shear;
2090                                                 cu1->fsize= cu->fsize;
2091                                                 cu1->xof= cu->xof;
2092                                                 cu1->yof= cu->yof;
2093                                                 cu1->textoncurve= cu->textoncurve;
2094                                                 if(cu1->vfont) cu1->vfont->id.us--;
2095                                                 cu1->vfont= cu->vfont;
2096                                                 id_us_plus((ID *)cu1->vfont);
2097                                                 text_to_curve(base->object, 0);
2098                                                 
2099                                                 strcpy(cu1->family, cu->family);
2100                                                 
2101                                                 makeDispList(base->object);
2102                                         }
2103                                 }
2104                                 else if(event==19) {    /* bevel settings */
2105                                         
2106                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2107                                                 cu= ob->data;
2108                                                 cu1= base->object->data;
2109                                                 
2110                                                 cu1->bevobj= cu->bevobj;
2111                                                 cu1->width= cu->width;
2112                                                 cu1->bevresol= cu->bevresol;
2113                                                 cu1->ext1= cu->ext1;
2114                                                 cu1->ext2= cu->ext2;
2115                                                 
2116                                                 makeDispList(base->object);
2117                                         }
2118                                 }
2119                                 else if(event==20) {    /* particle settings */
2120                                         PartEff *pa1, *pa2;
2121                                         char *p1, *p2;
2122                                         
2123                                         pa1= give_parteff(ob);
2124                                         pa2= give_parteff(base->object);
2125
2126                                         if(pa1==0 && pa2) {
2127                                                 BLI_remlink( &(base->object->effect), pa2);
2128                                                 free_effect( (Effect *) pa2);
2129                                         }
2130                                         else if(pa1 && pa2==0) {
2131                                                 free_effects(&(base->object->effect));
2132                                                 copy_effects(&(base->object->effect), &ob->effect);
2133                                                 build_particle_system(base->object);
2134                                         }
2135                                         else if(pa1 && pa2) {
2136                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2137                                                 
2138                                                 p1= (char *)pa1; p2= (char *)pa2;
2139                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2140                                                 pa2->keys= 0;
2141                                                 
2142                                                 build_particle_system(base->object);
2143                                         }
2144                                 }
2145                                 else if(event==21){
2146                                         if (base->object->type==OB_MESH) {
2147                                                 Mesh *targetme= base->object->data;
2148                                                 Mesh *sourceme= ob->data;
2149
2150                                                 targetme->flag= (targetme->flag&~ME_SUBSURF) | (sourceme->flag&ME_SUBSURF);
2151                                                 targetme->subsurftype = sourceme->subsurftype;
2152                                                 targetme->subdiv= sourceme->subdiv;
2153                                                 targetme->subdivr= sourceme->subdivr;
2154                                                 makeDispList(base->object);
2155                                         }
2156                                 }
2157                                 else if(event==22){
2158                                         /* Clear the constraints on the target */
2159                                         free_constraints(&base->object->constraints);
2160                                         free_constraint_channels(&base->object->constraintChannels);
2161
2162                                         /* Copy the constraint channels over */
2163                                         copy_constraints(&base->object->constraints, &ob->constraints);
2164                                         if (U.dupflag& USER_DUP_IPO)
2165                                                 copy_constraint_channels(&base->object->constraintChannels, &ob->constraintChannels);
2166                                         else
2167                                                 clone_constraint_channels (&base->object->constraintChannels, &ob->constraintChannels, NULL);
2168
2169                                         base->object->activecon = NULL;
2170                                 }
2171                         }
2172                 }
2173                 base= base->next;
2174         }
2175         
2176         allqueue(REDRAWVIEW3D, 0);
2177         if(event==20) {
2178                 allqueue(REDRAWBUTSOBJECT, 0);
2179         }
2180         
2181 }
2182
2183 void link_to_scene(unsigned short nr)
2184 {       
2185         Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1);
2186         Base *base, *nbase;
2187         
2188         if(sce==0) return;
2189         if(sce->id.lib) return;
2190         
2191         base= FIRSTBASE;
2192         while(base) {
2193                 if(TESTBASE(base)) {
2194                         
2195                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2196                         *nbase= *base;
2197                         BLI_addhead( &(sce->base), nbase);
2198                         id_us_plus((ID *)base->object);
2199                 }
2200                 base= base->next;
2201         }
2202 }
2203
2204 void make_links_menu()
2205 {
2206         Object *ob;
2207         short event=0;
2208         char str[140];
2209         
2210         if(!(ob=OBACT)) return;
2211         
2212         strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4");
2213         
2214         if(ob->type==OB_MESH)
2215                 strcat(str, "|Mesh Data%x2|Materials%x3");
2216         else if(ob->type==OB_CURVE)
2217                 strcat(str, "|Curve Data%x2|Materials%x3");
2218         else if(ob->type==OB_FONT)
2219                 strcat(str, "|Text Data%x2|Materials%x3");
2220         else if(ob->type==OB_SURF)
2221                 strcat(str, "|Surface Data%x2|Materials%x3");
2222         else if(ob->type==OB_MBALL)
2223                 strcat(str, "|Materials%x3");
2224         else if(ob->type==OB_CAMERA)
2225                 strcat(str, "|Camera Data%x2");
2226         else if(ob->type==OB_LAMP)
2227                 strcat(str, "|Lamp Data%x2");
2228         else if(ob->type==OB_LATTICE)
2229                 strcat(str, "|Lattice Data%x2");
2230         else if(ob->type==OB_ARMATURE)
2231                 strcat(str, "|Armature Data%x2");
2232
2233         event= pupmenu(str);
2234
2235         if(event<= 0) return;
2236         
2237         make_links(event);
2238 }
2239
2240 void make_links(short event)
2241 {
2242         Object *ob, *obt;
2243         Base *base, *nbase, *sbase;
2244         Scene *sce = NULL;
2245         ID *id;
2246         Material ***matarar, ***obmatarar, **matar1, **matar2;
2247         int a;
2248         short *totcolp, nr;
2249         char *strp;
2250
2251         if(!(ob=OBACT)) return;
2252
2253         if(event==1) {
2254                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr);
2255                 
2256                 if(strncmp(strp, "DataBrow", 8)==0) {
2257                         MEM_freeN(strp);
2258
2259                         activate_databrowse((ID *)G.scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene );
2260                         
2261                         return;                 
2262                 }
2263                 else {
2264                         event= pupmenu(strp);
2265                         MEM_freeN(strp);
2266                 
2267                         if(event<= 0) return;
2268                 
2269                         nr= 1;
2270                         sce= G.main->scene.first;
2271                         while(sce) {
2272                                 if(nr==event) break;
2273                                 nr++;
2274                                 sce= sce->id.next;
2275                         }
2276                         if(sce==G.scene) {
2277                                 error("This is the current scene");
2278                                 return;
2279                         }
2280                         if(sce==0 || sce->id.lib) return;
2281                         
2282                         /* remember: is needed below */
2283                         event= 1;
2284                 }
2285         }
2286
2287         base= FIRSTBASE;
2288         while(base) {
2289                 if(event==1 || base != BASACT) {
2290                         
2291                         obt= base->object;
2292
2293                         if(TESTBASE(base)) {
2294                                 
2295                                 if(event==1) {          /* to scene */
2296                                         
2297                                         /* test if already linked */
2298                                         sbase= sce->base.first;
2299                                         while(sbase) {
2300                                                 if(sbase->object==base->object) break;
2301                                                 sbase= sbase->next;
2302                                         }
2303                                         if(sbase) {     /* remove */
2304                                                 base= base->next;
2305                                                 continue;
2306                                         }
2307                                         
2308                                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2309                                         *nbase= *base;
2310                                         BLI_addhead( &(sce->base), nbase);
2311                                         id_us_plus((ID *)base->object);
2312                                 }
2313                         }
2314                         if(TESTBASELIB(base)) {
2315                                 if(event==2 || event==5) {  /* obdata */
2316                                         if(ob->type==obt->type) {
2317                                                 
2318                                                         id= obt->data;
2319                                                         id->us--;
2320                                                         
2321                                                         id= ob->data;
2322                                                         id_us_plus(id);
2323                                                         obt->data= id;
2324                                                         
2325                                                         /* if amount of material indices changed: */
2326                                                         test_object_materials(obt->data);
2327                                                 }
2328                                         }
2329                                 else if(event==4) {  /* ob ipo */
2330                                         if(obt->ipo) obt->ipo->id.us--;
2331                                         obt->ipo= ob->ipo;
2332                                         if(obt->ipo) {
2333                                                 id_us_plus((ID *)obt->ipo);
2334                                                 do_ob_ipo(obt);
2335                                         }
2336                                 }
2337                                 else if(event==3) {  /* materials */
2338                                         
2339                                         /* only if obt has no material: make arrays */
2340                                         /* from ob to obt! */
2341                                         
2342                                         obmatarar= give_matarar(ob);
2343                                         matarar= give_matarar(obt);
2344                                         totcolp= give_totcolp(obt);
2345
2346                                         /* if one of the two is zero: no render-able object */                                          
2347                                         if( matarar && obmatarar) {
2348                                                 
2349                                                 /* take care of users! so first a copy of original: */
2350
2351                                                 if(ob->totcol) {
2352                                                         matar1= MEM_dupallocN(ob->mat);
2353                                                         matar2= MEM_dupallocN(*obmatarar);
2354                                                 }
2355                                                 else {
2356                                                         matar1= matar2= 0;
2357                                                 }
2358                                                 
2359                                                 /* remove links from obt */
2360                                                 for(a=0; a<obt->totcol; a++) {
2361                                                         if(obt->mat[a]) obt->mat[a]->id.us--;
2362                                                         if( (*matarar)[a]) (*matarar)[a]->id.us--;
2363                                                 }
2364                                                 
2365                                                 /* free */
2366                                                 if(obt->mat) MEM_freeN(obt->mat);
2367                                                 if(*matarar) MEM_freeN(*matarar);
2368                                                 
2369                                                 /* connect a copy */
2370                                                 obt->mat= matar1;
2371                                                 *matarar= matar2;
2372                                                 obt->totcol= ob->totcol;
2373                                                 *totcolp= ob->totcol;
2374                                         
2375                                                 /* increase users */
2376                                                 for(a=0; a<obt->totcol; a++) {
2377                                                         if(obt->mat[a]) id_us_plus((ID *)obt->mat[a]);
2378                                                         if( (*matarar)[a]) id_us_plus((ID *)(*matarar)[a]);
2379                                                 }
2380
2381                                                 obt->colbits= ob->colbits;
2382                                                 
2383                                                 /* if amount of material indices changed: */
2384                                                 test_object_materials(obt->data);
2385                                         }
2386                                 }
2387                         }
2388                 }
2389                 base= base->next;
2390         }
2391         
2392         allqueue(REDRAWVIEW3D, 0);
2393         allqueue(REDRAWOOPS, 0);
2394         allqueue(REDRAWBUTSHEAD, 0);
2395 }
2396
2397 void make_duplilist_real()
2398 {
2399         Base *base, *basen;
2400         Object *ob;
2401         extern ListBase duplilist;
2402         
2403         if(okee("Make dupli objects real")==0) return;
2404         
2405         base= FIRSTBASE;
2406         while(base) {
2407                 if TESTBASELIB(base) {
2408
2409                         if(base->object->transflag & OB_DUPLI) {
2410                                 
2411                                 make_duplilist(G.scene, base->object);
2412                                 ob= duplilist.first;
2413                                 while(ob) {
2414                                         
2415                                         /* font duplis can have a totcol without material, we get them from parent
2416                                          * should be implemented better...
2417                                          */
2418                                         if(ob->mat==0) ob->totcol= 0;
2419                                         
2420                                         basen= MEM_dupallocN(base);
2421                                         basen->flag &= ~OB_FROMDUPLI;
2422                                         BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2423                                         ob->ipo= 0;             /* make sure apply works */
2424                                         ob->parent= ob->track= 0;
2425                                         ob->disp.first= ob->disp.last= 0;
2426                                         ob->transflag &= ~OB_DUPLI;
2427                                         basen->object= copy_object(ob);
2428                                         
2429                                         apply_obmat(basen->object);
2430                                         ob= ob->id.next;
2431                                 }
2432                                 
2433                                 free_duplilist();
2434                                 
2435                                 base->object->transflag &= ~OB_DUPLI;   
2436                         }
2437                 }
2438                 base= base->next;
2439         }
2440         
2441         allqueue(REDRAWVIEW3D, 0);
2442         allqueue(REDRAWOOPS, 0);
2443 }
2444
2445 void apply_object()
2446 {
2447         Base *base, *basact;
2448         Object *ob;
2449         Mesh *me;
2450         Curve *cu;
2451         Nurb *nu;
2452         BPoint *bp;
2453         BezTriple *bezt;
2454         MVert *mvert;
2455         float mat[3][3];
2456         int a;
2457
2458         if(G.scene->id.lib) return;
2459         if(G.obedit) return;
2460         basact= BASACT;
2461         
2462         if(G.qual & LR_SHIFTKEY) {
2463                 ob= OBACT;
2464                 if(ob==0) return;
2465                 
2466                 if(ob->transflag & OB_DUPLI) make_duplilist_real();
2467                 else if(ob->parent && ob->parent->type==OB_LATTICE) apply_lattice();
2468                 
2469                 return;
2470         }
2471
2472         if(okee("Apply size and rotation")==0) return;
2473
2474         base= FIRSTBASE;
2475         while(base) {
2476                 if TESTBASELIB(base) {
2477                         ob= base->object;
2478         
2479                         if(ob->type==OB_MESH) {
2480                                 object_to_mat3(ob, mat);
2481                                 me= ob->data;
2482                                 
2483                                 if(me->id.us>1) {
2484                                         error("Can't apply to a multi user mesh");
2485                                         return;
2486                                 }
2487                                 if(me->key) {
2488                                         error("Can't apply to a mesh with vertex keys");
2489                                         return;
2490                                 }
2491                                 
2492                                 mvert= me->mvert;
2493                                 for(a=0; a<me->totvert; a++, mvert++) {
2494                                         Mat3MulVecfl(mat, mvert->co);
2495                                 }
2496                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2497                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2498                                 QuatOne(ob->quat);
2499                                 
2500                                 where_is_object(ob);
2501                                 
2502                                 /* texspace and normals */
2503                                 BASACT= base;
2504                                 enter_editmode();
2505                                 exit_editmode(1);
2506                                 BASACT= basact;                         
2507                                 
2508                         }
2509                         else if (ob->type==OB_ARMATURE){
2510                                 bArmature *arm;
2511
2512                                 object_to_mat3(ob, mat);
2513                                 arm= ob->data;
2514                                 if(arm->id.us>1) {
2515                                         error("Can't apply to a multi user armature");
2516                                         return;
2517                                 }
2518
2519                                 apply_rot_armature (ob, mat);
2520                                 /* Reset the object's transforms */
2521                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2522                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2523                                 QuatOne(ob->quat);
2524                                 
2525                                 where_is_object(ob);
2526                         }
2527                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2528                                 object_to_mat3(ob, mat);
2529                                 cu= ob->data;
2530                                 
2531                                 if(cu->id.us>1) {
2532                                         error("Can't apply to a multi user curve");
2533                                         return;
2534                                 }
2535                                 if(cu->key) {
2536                                         error("Can't apply to a curve with vertex keys");
2537                                         return;
2538                                 }
2539                                 
2540                                 nu= cu->nurb.first;
2541                                 while(nu) {
2542                                         if( (nu->type & 7)==1) {
2543                                                 a= nu->pntsu;
2544                                                 bezt= nu->bezt;
2545                                                 while(a--) {
2546                                                         Mat3MulVecfl(mat, bezt->vec[0]);
2547                                                         Mat3MulVecfl(mat, bezt->vec[1]);
2548                                                         Mat3MulVecfl(mat, bezt->vec[2]);
2549                                                         bezt++;
2550                                                 }
2551                                         }
2552                                         else {
2553                                                 a= nu->pntsu*nu->pntsv;
2554                                                 bp= nu->bp;
2555                                                 while(a--) {
2556                                                         Mat3MulVecfl(mat, bp->vec);
2557                                                         bp++;
2558                                                 }
2559                                         }
2560                                         nu= nu->next;
2561                                 }
2562                         
2563                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2564                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2565                                 QuatOne(ob->quat);
2566                                 
2567                                 where_is_object(ob);
2568                                 
2569                                 /* texspace and normals */
2570                                 BASACT= base;
2571                                 enter_editmode();
2572                                 exit_editmode(1);
2573                                 BASACT= basact;
2574                         }
2575                 }
2576                 base= base->next;
2577         }
2578         
2579         allqueue(REDRAWVIEW3D, 0);
2580 }
2581
2582
2583
2584 /* ************ GENERAL  *************** */
2585
2586 static Object *is_a_parent_selected_int(Object *startob, Object *ob, GHash *done_hash) {
2587         if (ob!=startob && TESTBASE(ob))
2588                 return ob;
2589                 
2590         if (BLI_ghash_haskey(done_hash, ob))
2591                 return NULL;
2592         else
2593                 BLI_ghash_insert(done_hash, ob, NULL);
2594         
2595         if (ob->parent) {
2596                 Object *par= is_a_parent_selected_int(startob, ob->parent, done_hash);
2597                 if (par)
2598                         return par;
2599         }
2600
2601         /* IK is more complex in parents... */
2602         
2603         /* XXX, should we be handling armatures or constraints here? - zr */
2604
2605         if(ob->type==OB_IKA) {
2606                 Ika *ika= ob->data;
2607                 
2608                 if (ika->def) {
2609                         int i;
2610                         
2611                         for (i=0; i<ika->totdef; i++) {
2612                                 Deform *def= &ika->def[i];
2613                                 
2614                                 if (def->ob && ob!=def->ob && def->ob!=startob) {
2615                                         Object *par= is_a_parent_selected_int(startob, def->ob, done_hash);
2616                                         if (par)
2617                                                 return par;
2618                                 }
2619                         }
2620                 }
2621                 
2622                 if (ika->parent) {
2623                         Object *par= is_a_parent_selected_int(startob, ika->parent, done_hash);
2624                         if (par)
2625                                 return par;
2626                 }
2627         }
2628         
2629         return NULL;
2630 }
2631
2632 static Object *is_a_parent_selected(Object *ob) {
2633         GHash *gh= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
2634         Object *res= is_a_parent_selected_int(ob, ob, gh);
2635         BLI_ghash_free(gh, NULL, NULL);
2636         
2637         return res;
2638 }
2639
2640 /*** POSE FIGURIN' -- START ***/
2641
2642 static void clear_pose_update_flag(Object *ob) {
2643         /* Clear the flag for each pose channel that indicates that
2644          * pose should be updated on every redraw
2645          */
2646         bPoseChannel *chan;
2647         
2648         if (ob->pose) {
2649                 for (chan = ob->pose->chanbase.first; chan; 
2650                          chan=chan->next){
2651                         chan->flag &= ~PCHAN_TRANS_UPDATE;
2652                 }
2653         }
2654 }
2655
2656 static int pose_flags_reset_done(Object *ob) {
2657         /* Clear the constraint done status for every pose channe;
2658          * that has been flagged as needing constant updating
2659          */
2660         bPoseChannel *chan;
2661         int numreset = 0;
2662
2663         if (ob->pose) {
2664                 for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
2665                         if (chan->flag & PCHAN_TRANS_UPDATE) {
2666                                 chan->flag &= ~PCHAN_DONE;
2667                                 numreset++;
2668                         }
2669
2670                 }
2671         }
2672         return numreset;
2673 }
2674
2675 static int is_ob_constraint_target(Object *ob, ListBase *conlist) {
2676         /* Is this object the target of a constraint in this list? 
2677          */
2678
2679         bConstraint *con;
2680
2681         for (con=conlist->first; con; con=con->next)
2682         {
2683                 if (get_con_target(con) == ob)
2684                         return 1;
2685         }
2686         return 0;
2687
2688 }
2689
2690 int clear_bone_nocalc(Object *ob, Bone *bone, void *ptr) {
2691         /* When we aren't transform()-ing, we'll want to turn off
2692          * the no calc flag for bone bone in case the frame changes,
2693          * or something
2694          */
2695         bone->flag &= ~BONE_NOCALC;
2696         
2697         return 0;
2698 }
2699
2700
2701 static void clear_bone_nocalc_ob(Object *ob) {
2702         /* Let's clear no calc for all of the bones in the whole darn armature
2703          */
2704         bArmature *arm;
2705         arm = get_armature(ob);
2706         if (arm) {
2707                 bone_looper(ob, arm->bonebase.first, NULL, 
2708                                         clear_bone_nocalc);
2709         }
2710
2711 }
2712
2713 int set_bone_nocalc(Object *ob, Bone *bone, void *ptr) {
2714         /* Calculating bone transformation makes thins slow ...
2715          * lets set the no calc flag for a bone by default
2716          */
2717         bone->flag |= BONE_NOCALC;
2718         
2719         return 0;
2720 }
2721
2722 int selected_bone_docalc(Object *ob, Bone *bone, void *ptr) {
2723         /* Let's clear the no calc flag for selected bones.
2724          * This function always returns 1 for non-no calc bones
2725          * (a.k.a., the 'do calc' bones) so that the bone_looper 
2726          * will count these
2727          */
2728         if (bone->flag & BONE_NOCALC) {
2729                 if ( (bone->flag & BONE_SELECTED) ) {
2730                         bone->flag &= ~BONE_NOCALC;
2731                         return 1;
2732                 }
2733                 
2734         }
2735         else {
2736                 return 1;
2737         }
2738         return 0;
2739 }
2740
2741 static Bone *get_parent_bone_docalc(Bone *bone) {
2742         Bone            *parBone;
2743
2744         for (parBone = bone->parent; parBone; parBone=parBone->parent)
2745                 if (~parBone->flag & BONE_NOCALC)
2746                         return parBone;
2747
2748         return NULL;
2749 }
2750
2751 static int is_bone_parent(Bone *childBone, Bone *parBone) {
2752         Bone            *currBone;
2753
2754         for (currBone = childBone->parent; currBone; currBone=currBone->parent)
2755                 if (currBone == parBone)
2756                         return 1;
2757
2758         return 0;
2759 }
2760
2761 static void figure_bone_nocalc_constraint(Bone *conbone, bConstraint *con,
2762                                                                                   Object *ob, bArmature *arm) {
2763         /* If this bone has a constraint with a subtarget that has
2764          * the nocalc flag cleared, then we better clear the no calc flag
2765          * on this bone too (and the whole IK chain if this is an IK
2766          * constraint).
2767          *
2768          * Conversly, if this bone has an IK constraint and the root of
2769          * the chain has the no calc flag cleared, we had best clear that
2770          * flag for the whole chain.
2771          */
2772         Bone *subtarbone;
2773         Bone *parBone;
2774         char *subtar;
2775
2776         subtar = get_con_subtarget_name(con, ob);
2777
2778         if (subtar) {
2779                 if ( (subtarbone = get_named_bone(arm, subtar)) ) {
2780                         if ( (~subtarbone->flag & BONE_NOCALC) ||
2781                                  (get_parent_bone_docalc(subtarbone)) ) {
2782                                 if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2783                                         /* IK target is flaged for updating, so we
2784                                          * must update the whole chain.
2785                                          */
2786                                         ik_chain_looper(ob, conbone, NULL, 
2787                                                                         clear_bone_nocalc);
2788                                 else
2789                                         /* Constraint target is flagged for
2790                                          * updating, so we update this bone only
2791                                          */
2792                                         conbone->flag &= ~BONE_NOCALC;
2793                         }
2794                         else {
2795                                 if ( (parBone = get_parent_bone_docalc(conbone)) ) {
2796                                         /* a parent is flagged for updating */
2797                                         if (!is_bone_parent(subtarbone, parBone)) {
2798                                                 /* if the subtarget is also a child of
2799                                                  * this bone, we needn't worry, other
2800                                                  * wise, we have to update
2801                                                  */
2802                                                 if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2803                                                         ik_chain_looper(ob, conbone, NULL, 
2804                                                                                         clear_bone_nocalc);
2805                                                 else
2806                                                         conbone->flag &= ~BONE_NOCALC;
2807                                         }
2808                                 }
2809
2810                         }
2811                 }
2812         }
2813         else {
2814                 /* no subtarget ... target is regular object */
2815                 if ( (parBone = get_parent_bone_docalc(conbone)) ) {
2816                         /* parent is flagged for updating ... since
2817                          * the target will never move (not a bone)
2818                          * we had better update this bone/chain
2819                          */
2820                         if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2821                                 ik_chain_looper(ob, conbone, NULL, 
2822                                                                 clear_bone_nocalc);
2823                         else
2824                                 conbone->flag &= ~BONE_NOCALC;
2825                 }
2826         }
2827
2828 }
2829
2830 static void figure_bone_nocalc_core(Object *ob, bArmature *arm) {
2831         /* Let's figure out which bones need to be recalculated,
2832          * and which don't. Calculations are based on which bones
2833          * are selected, and the constraints that love them.
2834          */
2835         bPoseChannel *chan;
2836         bConstraint *con;
2837         Bone *conbone;
2838
2839         int numbones, oldnumbones, iterations;
2840
2841         oldnumbones = -1;
2842         numbones    =  0;
2843         iterations  =  0;
2844
2845         /* O.K., lets loop until we don't clear any more no calc bones
2846          */
2847         while (oldnumbones != numbones) {
2848                 /* I wonder if this will ever get executed? */
2849                 if ( (++iterations) == 1000) {
2850                         printf("figurin' nocalc is talking too long\n");
2851                         break;
2852                 }
2853
2854                 oldnumbones = numbones;
2855