- replaced G.{edve,eded,edvl} with G.editMesh, atm just a structure to
[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| ... and keep transform (clr 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| ... 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 1 or 3 vertices");
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                         mode= pupmenu("Make Parent %t|Use Bone %x1|Use Armature %x2|Use Object %x3");
779                         switch (mode){
780                         case 1:
781                                 mode=PARBONE;
782                                 /* Make bone popup menu */
783
784                                 bonestr = make_bone_menu(get_armature(par));
785                 //              if(mbutton(&bone, bonestr, 1, 24, "Bone: ")==0) {
786
787                                 bonenr= pupmenu_col(bonestr, 20);
788                                 if (bonestr)
789                                         MEM_freeN (bonestr);
790                                 
791                                 if (bonenr==-1){
792                                         allqueue(REDRAWVIEW3D, 0);
793                                         return;
794                                 }
795
796                                 apply_pose_armature(get_armature(par), par->pose, 0);
797                                 bone=get_indexed_bone(get_armature(par), bonenr); 
798                                 if (!bone){
799                 //                      error ("Invalid bone!");
800                                         allqueue(REDRAWVIEW3D, 0);
801                                         return;
802                                 }
803
804                                 break;
805                         case 2:
806                                 mode=PARSKEL;
807                                 break;
808                         case 3:
809                                 mode=PAROBJECT;
810                                 break;
811                         default:
812                                 return;
813                         }
814         }
815                 else {
816                 if(qual & LR_SHIFTKEY) {
817                         if(okee("Make Parent without inverse")==0) return;
818                 }
819                 else {
820                         if(qual & LR_ALTKEY) {
821                                 if(okee("Make VertexParent")==0) return;
822                         }
823                         else if(okee("Make Parent")==0) return;
824
825                         /* test effchild */
826                         base= FIRSTBASE;
827                         while(base) {
828                                 if TESTBASELIB(base) {
829                                         if(base->object->type==OB_IKA && base->object != par) {
830                                                 if(effchild==0) {
831                                                         if(okee("Effector as Child")) effchild= 1;
832                                                         else effchild= 2;
833                                                 }
834                                         }
835                                 }
836                                 if(effchild) break;
837                                 base= base->next;
838                         }
839                         
840                         /* now we'll clearparentandkeeptransform all objects */
841                         base= FIRSTBASE;
842                         while(base) {
843                                 if TESTBASELIB(base) {
844                                         if(base!=BASACT && base->object->parent) {
845                                                 if(base->object->type==OB_IKA && effchild==1);
846                                                 else {
847                                                         base->object->parent= 0;
848                                                         apply_obmat(base->object);
849                                                 }
850                                         }
851                                 }
852                                 base= base->next;
853                         }
854                 }
855         }
856         
857         
858         base= FIRSTBASE;
859         while(base) {
860                 if TESTBASELIB(base) {
861                         if(base!=BASACT) {
862                                 
863                                 ok= 1;
864                                 if(base->object->type==OB_IKA) {
865                                 
866                                         if(effchild==1) {
867                                         
868                                                 if( test_parent_loop(par, base->object)==0 ) {
869                                                 
870                                                         Ika *ika= base->object->data;
871                                                         
872                                                         ika->parent= par;
873                                                         ika->par1= limbnr;
874                                                         ika->partype= mode;
875                                                         itterate_ika(base->object);
876                                                         ok= 0;
877                                                 }
878                                                 else {
879                                                         ok= 0;
880                                                         error("Loop in parents");
881                                                 }
882                                         }
883                                 }
884                                 
885                                 if(ok) {
886                                         if( test_parent_loop(par, base->object) ) {
887                                                 error("Loop in parents");
888                                         }
889                                         else {
890                                                 
891                                                 if(par->type==OB_IKA){
892                                                         base->object->partype= mode;
893                                                         base->object->par1= limbnr;
894                                                 }
895                                                 else if (par->type==OB_ARMATURE){
896                                                         base->object->partype= mode;
897                                                         if (bone)
898                                                                 strcpy (base->object->parsubstr, bone->name);
899                                                         else
900                                                                 base->object->parsubstr[0]=0;
901                                                 }
902                                                 
903                                                 else if(qual & LR_ALTKEY) {
904                                                         base->object->partype= PARVERT1;
905                                                 }
906                                                 else {
907                                                         base->object->partype= PAROBJECT;
908                                                 }
909                                                 
910                                                 base->object->parent= par;
911                                                 
912                                                 /* calculate inverse parent matrix? */
913                                                 if( (qual & LR_SHIFTKEY) ) {
914                                                         /* not... */
915                                                         Mat4One(base->object->parentinv);
916                                                         memset(base->object->loc, 0, 3*sizeof(float));
917                                                 }
918                                                 else {
919                                                         if(mode==PARSKEL && par->type == OB_ARMATURE) {
920                                                                 /* Prompt the user as to whether he wants to
921                                                                  * add some vertex groups based on the bones
922                                                                  * in the parent armature.
923                                                                  */
924                                                                 create_vgroups_from_armature(base->object, 
925                                                                                                                          par);
926
927                                                                 base->object->partype= PAROBJECT;
928                                                                 what_does_parent(base->object);
929                                                                 Mat4One (base->object->parentinv);
930                                                                 base->object->partype= mode;
931                                                         }
932                                                         else
933                                                                 what_does_parent(base->object);
934                                                         Mat4Invert(base->object->parentinv, workob.obmat);
935                                                 }
936                                                 
937                                                 if(par->type==OB_LATTICE) makeDispList(base->object);
938                                                 if(par->type==OB_IKA && mode==PARSKEL) makeDispList(base->object);
939                                                 if(par->type==OB_ARMATURE && mode == PARSKEL){
940                                                         verify_defgroups(base->object);
941                                                         makeDispList(base->object);
942                                                 }
943                                         }
944                                 }
945                         }
946                 }
947                 base= base->next;
948         }
949         allqueue(REDRAWVIEW3D, 0);
950         allqueue(REDRAWOOPS, 0);
951         
952         test_scene_constraints();
953         sort_baselist(G.scene);
954 }
955
956
957 void enter_editmode(void)
958 {
959         Base *base;
960         Object *ob;
961         Ika *ika;
962         ID *id;
963         Mesh *me;
964         int ok= 0;
965         bArmature *arm;
966         
967         if(G.scene->id.lib) return;
968         base= BASACT;
969         if(base==0) return;
970         if((base->lay & G.vd->lay)==0) return;
971         
972         ob= base->object;
973         if(ob->data==0) return;
974         
975         id= ob->data;
976         if(id->lib) {
977                 error("Can't edit libdata");
978                 return;
979         }
980         
981         if(ob->type==OB_MESH) {
982                 me= get_mesh(ob);
983                 if( me==0 ) return;
984                 if(me->id.lib) {
985                         error("Can't edit libdata");
986                         return;
987                 }
988                 ok= 1;
989                 G.obedit= ob;
990                 make_editMesh();
991                 allqueue(REDRAWBUTSLOGIC, 0);
992         }
993         if (ob->type==OB_ARMATURE){
994                 arm=base->object->data;
995                 if (!arm) return;
996                 if (arm->id.lib){
997                         error("Can't edit libdata");
998                         return;
999                 }
1000                 ok=1;
1001                 G.obedit=ob;
1002                 make_editArmature();
1003                 allqueue (REDRAWVIEW3D,0);
1004         }
1005         else if(ob->type==OB_IKA) {     /* grab type */
1006                 base= FIRSTBASE;
1007                 while(base) {
1008                         if TESTBASE(base) {
1009                                 if(base->object->type==OB_IKA) {
1010                                         ika= base->object->data;
1011                                         if(ika->flag & IK_GRABEFF) ika->flag &= ~IK_GRABEFF;
1012                                         else ika->flag |= IK_GRABEFF;
1013                                 }
1014                         }
1015                         base= base->next;
1016                 }
1017                 allqueue(REDRAWVIEW3D, 0);
1018         }
1019         else if(ob->type==OB_FONT) {
1020                 G.obedit= ob;
1021                 ok= 1;
1022                 make_editText();
1023         }
1024         else if(ob->type==OB_MBALL) {
1025                 G.obedit= ob;
1026                 ok= 1;
1027                 make_editMball();
1028         }
1029         else if(ob->type==OB_LATTICE) {
1030                 G.obedit= ob;
1031                 ok= 1;
1032                 make_editLatt();
1033         }
1034         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1035                 ok= 1;
1036                 G.obedit= ob;
1037                 make_editNurb();
1038         }
1039         allqueue(REDRAWBUTSEDIT, 0);
1040         countall();
1041         
1042         if(ok) {
1043                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1044         
1045                 allqueue(REDRAWVIEW3D, 0);
1046         }
1047         else G.obedit= 0;
1048
1049         if (G.obpose)
1050                 exit_posemode (1);
1051         scrarea_queue_headredraw(curarea);
1052 }
1053
1054 void make_displists_by_parent(Object *ob) {
1055         Base *base;
1056         
1057         for (base= FIRSTBASE; base; base= base->next)
1058                 if (ob==base->object->parent)
1059                         makeDispList(base->object);
1060 }
1061
1062 void exit_editmode(int freedata)        /* freedata==0 at render */
1063 {
1064         Base *base;
1065         Object *ob;
1066         Curve *cu;
1067
1068         if(G.obedit==0) return;
1069
1070         if(G.obedit->type==OB_MESH) {
1071
1072                 /* temporal */
1073                 countall();
1074
1075                 if(G.totvert>MESH_MAX_VERTS) {
1076                         error("too many vertices");
1077                         return;
1078                 }
1079                 load_editMesh();        /* makes new displist */
1080
1081                 if(freedata) free_editMesh();
1082                 
1083                 if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);
1084
1085                 build_particle_system(G.obedit);
1086         }
1087         else if (G.obedit->type==OB_ARMATURE){  
1088                 load_editArmature();
1089                 if (freedata) free_editArmature();
1090         }
1091         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1092                 load_editNurb();
1093                 if(freedata) freeNurblist(&editNurb);
1094         }
1095         else if(G.obedit->type==OB_FONT && freedata==1) {
1096                 load_editText();
1097         }
1098         else if(G.obedit->type==OB_LATTICE) {
1099                 load_editLatt();
1100                 if(freedata) free_editLatt();
1101         }
1102         else if(G.obedit->type==OB_MBALL) {
1103                 load_editMball();
1104                 if(freedata) BLI_freelistN(&editelems);
1105         }
1106
1107         ob= G.obedit;
1108         
1109         /* displist make is different in editmode */
1110         if(freedata) G.obedit= NULL;
1111         makeDispList(ob);
1112
1113         /* has this influence at other objects? */
1114         if(ob->type==OB_CURVE) {
1115
1116                 /* test if ob is use as bevelcurve r textoncurve */
1117                 base= FIRSTBASE;
1118                 while(base) {
1119                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
1120                                 cu= base->object->data;
1121                                 
1122                                 if(cu->textoncurve==ob) {
1123                                         text_to_curve(base->object, 0);
1124                                         makeDispList(base->object);
1125                                 }
1126                                 if(cu->bevobj== ob) {
1127                                         makeDispList(base->object);
1128                                 }
1129                         }
1130                         base= base->next;
1131                 }
1132                 
1133         }
1134         else if(ob->type==OB_LATTICE) {
1135                 make_displists_by_parent(ob);
1136         }
1137
1138         if(freedata) {
1139                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1140         
1141                 countall();
1142                 allqueue(REDRAWVIEW3D, 0);
1143                 allqueue(REDRAWBUTSEDIT, 0);
1144                 allqueue(REDRAWBUTSLOGIC, 0);
1145         }
1146         scrarea_queue_headredraw(curarea);
1147
1148 }
1149
1150 void check_editmode(int type)
1151 {
1152         
1153         if (G.obedit==0 || G.obedit->type==type) return;
1154
1155         exit_editmode(1);
1156 }
1157
1158 static int centremode= 0; /* 0 == do centre, 1 == centre new, 2 == centre cursor */
1159
1160 void docentre(void)
1161 {
1162         EditMesh *em = G.editMesh;
1163         Base *base;
1164         Object *ob;
1165         Mesh *me, *tme;
1166         Curve *cu;
1167 //      BezTriple *bezt;
1168 //      BPoint *bp;
1169         Nurb *nu, *nu1;
1170         EditVert *eve;
1171         float cent[3], centn[3], min[3], max[3], omat[3][3];
1172         int a;
1173         MVert *mvert;
1174
1175         if(G.scene->id.lib) return;
1176
1177         if(G.obedit) {
1178
1179                 INIT_MINMAX(min, max);
1180         
1181                 if(G.obedit->type==OB_MESH) {
1182                         eve= em->verts.first;
1183                         while(eve) {
1184                                 DO_MINMAX(eve->co, min, max);
1185                                 eve= eve->next;
1186                         }
1187                         cent[0]= (min[0]+max[0])/2.0;
1188                         cent[1]= (min[1]+max[1])/2.0;
1189                         cent[2]= (min[2]+max[2])/2.0;
1190                         
1191                         eve= em->verts.first;
1192                         while(eve) {
1193                                 VecSubf(eve->co, eve->co, cent);                        
1194                                 eve= eve->next;
1195                         }
1196                 }
1197         }
1198         
1199         /* reset flags */
1200         base= FIRSTBASE;
1201         while(base) {
1202                 if TESTBASELIB(base) {
1203                         base->object->flag &= ~OB_DONE;
1204                 }
1205                 base= base->next;
1206         }
1207         me= G.main->mesh.first;
1208         while(me) {
1209                 me->flag &= ~ME_ISDONE;
1210                 me= me->id.next;
1211         }
1212         
1213         base= FIRSTBASE;
1214         while(base) {
1215                 
1216                 if TESTBASELIB(base) {
1217                         if((base->object->flag & OB_DONE)==0) {
1218                                 
1219                                 base->object->flag |= OB_DONE;
1220                                 
1221                                 if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1222                                         
1223                                         if(me->key) {
1224                                                 error("Mesh with vertexkey!");
1225                                                 return;
1226                                         }
1227                                         
1228                                         if(centremode==2) {
1229                                                 VECCOPY(cent, give_cursor());
1230                                                 Mat4Invert(base->object->imat, base->object->obmat);
1231                                                 Mat4MulVecfl(base->object->imat, cent);
1232                                         } else {
1233                                                 INIT_MINMAX(min, max);
1234                 
1235                                                 mvert= me->mvert;
1236                                                 for(a=0; a<me->totvert; a++, mvert++) {
1237                                                         DO_MINMAX(mvert->co, min, max);
1238                                                 }
1239                                 
1240                                                 cent[0]= (min[0]+max[0])/2.0;
1241                                                 cent[1]= (min[1]+max[1])/2.0;
1242                                                 cent[2]= (min[2]+max[2])/2.0;
1243                                         }
1244                                                 
1245                                         mvert= me->mvert;
1246                                         for(a=0; a<me->totvert; a++, mvert++) {
1247                                                 VecSubf(mvert->co, mvert->co, cent);
1248                                         }
1249                                         me->flag |= ME_ISDONE;
1250                                         
1251                                         if(centremode) {
1252                                                 Mat3CpyMat4(omat, base->object->obmat);
1253                                                 
1254                                                 VECCOPY(centn, cent);
1255                                                 Mat3MulVecfl(omat, centn);
1256                                                 base->object->loc[0]+= centn[0];
1257                                                 base->object->loc[1]+= centn[1];
1258                                                 base->object->loc[2]+= centn[2];
1259                                                 
1260                                                 /* other users? */
1261                                                 ob= G.main->object.first;
1262                                                 while(ob) {
1263                                                         if((ob->flag & OB_DONE)==0) {
1264                                                                 tme= get_mesh(ob);
1265                                                                 
1266                                                                 if(tme==me) {
1267                                                                         
1268                                                                         ob->flag |= OB_DONE;
1269
1270                                                                         Mat3CpyMat4(omat, ob->obmat);
1271                                                                         VECCOPY(centn, cent);
1272                                                                         Mat3MulVecfl(omat, centn);
1273                                                                         ob->loc[0]+= centn[0];
1274                                                                         ob->loc[1]+= centn[1];
1275                                                                         ob->loc[2]+= centn[2];
1276                                                                         
1277                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
1278                                                                                 mvert= tme->mvert;
1279                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
1280                                                                                         VecSubf(mvert->co, mvert->co, cent);
1281                                                                                 }
1282                                                                                 tme->flag |= ME_ISDONE;
1283                                                                         }
1284                                                                 }
1285                                                         }
1286                                                         
1287                                                         ob= ob->id.next;
1288                                                 }
1289                                         }
1290                                 
1291                                         /* displist of all users, also this one */
1292                                         makeDispList(base->object);
1293                                         
1294                                         /* DO: check all users... */
1295                                         tex_space_mesh(me);
1296                 
1297                                 }
1298                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1299                                                                         
1300                                         if(G.obedit) {
1301                                                 nu1= editNurb.first;
1302                                         }
1303                                         else {
1304                                                 cu= base->object->data;
1305                                                 nu1= cu->nurb.first;
1306                                         }
1307                                         
1308                                         if(centremode==2) {
1309                                                 VECCOPY(cent, give_cursor());
1310                                                 Mat4Invert(base->object->imat, base->object->obmat);
1311                                                 Mat4MulVecfl(base->object->imat, cent);
1312                                                         
1313                                                         /* Curves need to be 2d, never offset in
1314                                                          * Z. Is a somewhat arbitrary restriction, 
1315                                                          * would probably be nice to remove.
1316                                                          */
1317                                                 cent[2]= 0.0;
1318                                         } else {
1319                                                 INIT_MINMAX(min, max);
1320         
1321                                                 nu= nu1;
1322                                                 while(nu) {
1323                                                         minmaxNurb(nu, min, max);
1324                                                         nu= nu->next;
1325                                                 }
1326                                                 
1327                                                 cent[0]= (min[0]+max[0])/2.0;
1328                                                 cent[1]= (min[1]+max[1])/2.0;
1329                                                 cent[2]= (min[2]+max[2])/2.0;
1330                                         }
1331                                         
1332                                         nu= nu1;
1333                                         while(nu) {
1334                                                 if( (nu->type & 7)==1) {
1335                                                         a= nu->pntsu;
1336                                                         while (a--) {
1337                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1338                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1339                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1340                                                         }
1341                                                 }
1342                                                 else {
1343                                                         a= nu->pntsu*nu->pntsv;
1344                                                         while (a--)
1345                                                                 VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1346                                                 }
1347                                                 nu= nu->next;
1348                                         }
1349                         
1350                                         if(centremode && G.obedit==0) {
1351                                                 Mat3CpyMat4(omat, base->object->obmat);
1352                                                 
1353                                                 Mat3MulVecfl(omat, cent);
1354                                                 base->object->loc[0]+= cent[0];
1355                                                 base->object->loc[1]+= cent[1];
1356                                                 base->object->loc[2]+= cent[2];
1357                                         }
1358                         
1359                                         if(G.obedit) {
1360                                                 makeDispList(G.obedit);
1361                                                 break;
1362                                         }
1363                                         else makeDispList(base->object);
1364         
1365                                 }
1366                                 else if(base->object->type==OB_FONT) {
1367                                         /* get from bb */
1368                                         
1369                                         cu= base->object->data;
1370                                         if(cu->bb==0) return;
1371                                         
1372                                         cu->xof= -0.5*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
1373                                         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 */
1374                                         
1375                                         /* not really ok, do this better once! */
1376                                         cu->xof /= cu->fsize;
1377                                         cu->yof /= cu->fsize;
1378                                         
1379                                         text_to_curve(base->object, 0);
1380                                         makeDispList(base->object);
1381                                         
1382                                         allqueue(REDRAWBUTSEDIT, 0);
1383                                 }
1384                         }
1385                 }
1386                 base= base->next;
1387         }
1388
1389         allqueue(REDRAWVIEW3D, 0);
1390 }
1391
1392 void docentre_new(void)
1393 {
1394         if(G.scene->id.lib) return;
1395
1396         if(G.obedit) {
1397                 error("Unable to perform function in EditMode");
1398         }
1399         else {
1400                 centremode= 1;
1401                 docentre();
1402                 centremode= 0;
1403         }
1404 }
1405
1406 void docentre_cursor(void)
1407 {
1408         if(G.scene->id.lib) return;
1409
1410         if(G.obedit) {
1411                 error("Unable to perform function in EditMode");
1412         }
1413         else {
1414                 centremode= 2;
1415                 docentre();
1416                 centremode= 0;
1417         }
1418 }
1419
1420 void movetolayer(void)
1421 {
1422         Base *base;
1423         unsigned int lay= 0, local;
1424
1425         if(G.scene->id.lib) return;
1426
1427         base= FIRSTBASE;
1428         while(base) {
1429                 if TESTBASE(base) lay |= base->lay;
1430                 base= base->next;
1431         }
1432         if(lay==0) return;
1433         lay &= 0xFFFFFF;
1434         
1435         if( movetolayer_buts(&lay)==0 ) return;
1436         if(lay==0) return;
1437
1438         base= FIRSTBASE;
1439         while(base) {
1440                 if TESTBASE(base) {
1441                         local= base->lay & 0xFF000000;
1442                         base->lay= lay + local;
1443
1444                         base->object->lay= lay;
1445                 }
1446                 base= base->next;
1447         }
1448         countall();
1449         allqueue(REDRAWBUTSEDIT, 0);
1450         allqueue(REDRAWVIEW3D, 0);
1451         allqueue(REDRAWOOPS, 0);
1452         allqueue(REDRAWINFO, 0);
1453 }
1454
1455
1456 void special_editmenu(void)
1457 {
1458         extern short editbutflag;
1459         extern float doublimit;
1460         float fac;
1461         int nr;
1462         short randfac;
1463         
1464         if(G.obedit==0) {
1465                 if(G.f & G_FACESELECT) {
1466                         Mesh *me= get_mesh(OBACT);
1467                         TFace *tface;
1468                         int a;
1469                         
1470                         if(me==0 || me->tface==0) return;
1471                         
1472                         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");
1473         
1474                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1475                                 if(tface->flag & SELECT) {
1476                                         switch(nr) {
1477                                         case 1:
1478                                                 tface->mode |= TF_TEX; break;
1479                                         case 2:
1480                                                 tface->mode |= TF_SHAREDCOL; break;
1481                                         case 3:
1482                                                 tface->mode |= TF_LIGHT; break; 
1483                                         case 4:
1484                                                 tface->mode |= TF_INVISIBLE; break;
1485                                         case 5:
1486                                                 tface->mode |= TF_DYNAMIC; break;
1487                                         case 6:
1488                                                 tface->mode &= ~TF_TEX;
1489                                                 tface->tpage= 0;
1490                                                 break;
1491                                         case 7:
1492                                                 tface->mode &= ~TF_SHAREDCOL; break;
1493                                         case 8:
1494                                                 tface->mode &= ~TF_LIGHT; break;
1495                                         case 9:
1496                                                 tface->mode &= ~TF_INVISIBLE; break;
1497                                         case 10:
1498                                                 tface->mode &= ~TF_DYNAMIC; break;
1499                                         }
1500                                 }
1501                         }
1502                         allqueue(REDRAWVIEW3D, 0);
1503                         allqueue(REDRAWBUTSLOGIC, 0);
1504                 }
1505                 else if(G.f & G_VERTEXPAINT) {
1506                         Mesh *me= get_mesh(OBACT);
1507                         
1508                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1509                         
1510                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1511                         if(nr==1) {
1512                                 
1513                                 if(me->tface) tface_to_mcol(me);
1514                                 
1515                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1516                                 do_shared_vertexcol(me);
1517                                 
1518                                 if(me->tface) mcol_to_tface(me, 1);
1519                         }
1520                 }
1521                 else {
1522                         Base *base, *base_select= NULL;
1523
1524                         // Get the active object mesh.
1525                         Mesh *me= get_mesh(OBACT);
1526
1527                         // If the active object is a mesh...
1528                         if (me) {
1529                                 // Bring up a little menu with the boolean operation choices on.
1530                                 nr= pupmenu("Boolean %t|Intersect%x1|Union%x2|Difference%x3");
1531
1532                                 if (nr > 0) {
1533                                         // user has made a choice of a menu element.
1534                                         // All of the boolean functions require 2 mesh objects 
1535                                         // we search through the object list to find the other 
1536                                         // selected item and make sure it is distinct and a mesh.
1537
1538                                         base= FIRSTBASE;
1539                                         while(base) {
1540                                                 if(base->flag & SELECT) {
1541                                                         if(base->object != OBACT) base_select= base;
1542                                                 }
1543
1544                                                 base= base->next;
1545                                         }
1546
1547                                         if (base_select) {
1548                                                 if (get_mesh(base_select->object)) {
1549                                                         waitcursor(1);
1550
1551                                                         if (!NewBooleanMesh(BASACT,base_select,nr)) {
1552                                                                 error("An internal error occurred -- sorry!");
1553                                                         }
1554
1555                                                         waitcursor(0);
1556                                                 } else {
1557                                                         error("Please select 2 meshes");
1558                                                 }
1559                                         } else {
1560                                                 error("Please select 2 meshes");
1561                                         }
1562                                 }
1563
1564                                 allqueue(REDRAWVIEW3D, 0);
1565                         }
1566                 }
1567         }
1568         else if(G.obedit->type==OB_MESH) {
1569
1570                 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");
1571                 if(nr>0) waitcursor(1);
1572                 
1573                 switch(nr) {
1574                 case 1:
1575                         undo_push_mesh("Subdivide");
1576                         subdivideflag(1, 0.0, editbutflag);
1577                         break;
1578                 case 2:
1579                         randfac= 10;
1580                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
1581                         fac= -( (float)randfac )/100;
1582                         undo_push_mesh("Subdivide Fractal");
1583                         subdivideflag(1, fac, editbutflag);
1584                         break;
1585                 case 3:
1586                         undo_push_mesh("Subdivide Smooth");
1587                         subdivideflag(1, 0.0, editbutflag | B_SMOOTH);
1588                         break;
1589                 case 4:
1590                         mergemenu();
1591                         break;
1592                 case 5:
1593                         undo_push_mesh("Rem Doubles");
1594                         notice("Removed: %d", removedoublesflag(1, doublimit));
1595                         break;
1596                 case 6:
1597                         hide_mesh(0);
1598                         break;
1599                 case 7:
1600                         reveal_mesh();
1601                         break;
1602                 case 8:
1603                         selectswap_mesh();
1604                         break;
1605                 case 9:
1606                         undo_push_mesh("Flip Normals");
1607                         flip_editnormals();
1608                         break;
1609                 case 10:
1610                         undo_push_mesh("Smooth");
1611                         vertexsmooth();
1612                         break;
1613                 case 11:
1614                         bevel_menu();
1615                         break;
1616                 }               
1617                 
1618                 makeDispList(G.obedit);
1619                 
1620                 if(nr>0) waitcursor(0);
1621                 
1622         }
1623         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1624
1625                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2");
1626                 
1627                 switch(nr) {
1628                 case 1:
1629                         subdivideNurb();
1630                         break;
1631                 case 2:
1632                         switchdirectionNurb2();
1633                         break;
1634                 }
1635         }
1636
1637         countall();
1638         allqueue(REDRAWVIEW3D, 0);
1639         
1640 }
1641
1642 void convertmenu(void)
1643 {
1644         Base *base, *basen, *basact;
1645         Object *ob, *ob1;
1646         Curve *cu;
1647         MetaBall *mb;
1648         Mesh *me;
1649         DispList *dl;
1650         int ok=0, nr = 0, a;
1651         
1652         if(G.scene->id.lib) return;
1653
1654         ob= OBACT;
1655         if(ob==0) return;
1656         if(G.obedit) return;
1657         
1658         basact= BASACT; /* will be restored */
1659                 
1660         if(ob->type==OB_FONT) {
1661                 nr= pupmenu("Convert Font to%t|Curve");
1662                 if(nr>0) ok= 1;
1663         }
1664         else if(ob->type==OB_MBALL) {
1665                 nr= pupmenu("Convert MetaBall to%t|Mesh (keep original)");
1666                 if(nr>0) ok= 1;
1667         }
1668         else if(ob->type==OB_CURVE) {
1669                 nr= pupmenu("Convert Curve to%t|Mesh");
1670                 if(nr>0) ok= 1;
1671         }
1672         else if(ob->type==OB_SURF) {
1673                 nr= pupmenu("Convert Nurbs Surf to%t|Mesh");
1674                 if(nr>0) ok= 1;
1675         }
1676         else if(ob->type==OB_MESH && mesh_uses_displist((Mesh*) ob->data)) {
1677                 nr= pupmenu("Convert SubSurf to%t|Mesh (keep original)");
1678                 if(nr>0) ok= 1;
1679         }
1680         if(ok==0) return;
1681
1682         /* don't forget multiple users! */
1683
1684         /* reset flags */
1685         base= FIRSTBASE;
1686         while(base) {
1687                 if TESTBASELIB(base) {
1688                         base->object->flag &= ~OB_DONE;
1689                 }
1690                 base= base->next;
1691         }
1692
1693         base= FIRSTBASE;
1694         while(base) {
1695                 if TESTBASELIB(base) {
1696                         
1697                         ob= base->object;
1698                         
1699                         if(ob->flag & OB_DONE);
1700                         else if(ob->type==OB_MESH) {
1701                                 Mesh *oldme= ob->data;
1702                                 
1703                                 if (mesh_uses_displist(oldme)) {
1704                                         DispListMesh *dlm;
1705
1706                                         ob->flag |= OB_DONE;
1707
1708                                         ob1= copy_object(ob);
1709
1710                                         basen= MEM_mallocN(sizeof(Base), "duplibase");
1711                                         *basen= *base;
1712                                         BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
1713                                         basen->object= ob1;
1714                                         basen->flag &= ~SELECT;
1715                                                 
1716                                         me= ob1->data;
1717                                         me->id.us--;
1718                                                 
1719                                         ob1->data= add_mesh();
1720                                         G.totmesh++;
1721                                         ob1->type= OB_MESH;
1722
1723                                         me= ob1->data;
1724                                         me->totcol= oldme->totcol;
1725                                         if(ob1->totcol) {
1726                                                 me->mat= MEM_dupallocN(oldme->mat);
1727                                                 for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1728                                         }
1729                                                 
1730                                         dlm= subsurf_make_dispListMesh_from_mesh(oldme, NULL, oldme->subdiv, oldme->flag);
1731                                         displistmesh_to_mesh(dlm, ob1->data);
1732                                         displistmesh_free(dlm);
1733
1734                                         tex_space_mesh(me);
1735                                 }
1736                         }
1737                         else if(ob->type==OB_FONT) {
1738                                 if(nr==1) {
1739                                 
1740                                         ob->flag |= OB_DONE;
1741                                 
1742                                         ob->type= OB_CURVE;
1743                                         cu= ob->data;
1744                                         
1745                                         if(cu->vfont) {
1746                                                 cu->vfont->id.us--;
1747                                                 cu->vfont= 0;
1748                                         }
1749                                         /* other users */
1750                                         if(cu->id.us>1) {
1751                                                 ob1= G.main->object.first;
1752                                                 while(ob1) {
1753                                                         if(ob1->data==cu) ob1->type= OB_CURVE;
1754                                                         ob1= ob1->id.next;
1755                                                 }
1756                                         }
1757                                 }
1758                         }
1759                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1760                                 if(nr==1) {
1761
1762                                         ob->flag |= OB_DONE;
1763                                         cu= ob->data;
1764                                         
1765                                         dl= cu->disp.first;
1766                                         if(dl==0) makeDispList(ob);
1767
1768                                         nurbs_to_mesh(ob); /* also does users */
1769
1770                                         /* texspace and normals */
1771                                         BASACT= base;
1772                                         enter_editmode();
1773                                         exit_editmode(1);
1774                                         BASACT= basact;
1775                                 }
1776                         }
1777                         else if(ob->type==OB_MBALL) {
1778                         
1779                                 if(nr==1) {
1780                                         ob= find_basis_mball(ob);
1781                                         
1782                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
1783                                         
1784                                                 ob->flag |= OB_DONE;
1785
1786                                                 ob1= copy_object(ob);
1787
1788                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1789                                                 *basen= *base;
1790                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
1791                                                 basen->object= ob1;
1792                                                 basen->flag &= ~SELECT;
1793                                                 
1794                                                 mb= ob1->data;
1795                                                 mb->id.us--;
1796                                                 
1797                                                 ob1->data= add_mesh();
1798                                                 G.totmesh++;
1799                                                 ob1->type= OB_MESH;
1800                                                 
1801                                                 me= ob1->data;
1802                                                 me->totcol= mb->totcol;
1803                                                 if(ob1->totcol) {
1804                                                         me->mat= MEM_dupallocN(mb->mat);
1805                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1806                                                 }
1807                                                 
1808                                                 mball_to_mesh(&ob->disp, ob1->data);
1809                                                 tex_space_mesh(me);
1810                                         }
1811                                 }
1812                         }
1813                 }
1814                 base= base->next;
1815         }
1816         
1817         allqueue(REDRAWVIEW3D, 0);
1818         allqueue(REDRAWOOPS, 0);
1819         allqueue(REDRAWBUTSEDIT, 0);
1820 }
1821
1822         /* Change subdivision properties of mesh object ob, if
1823          * level==-1 then toggle subsurf, else set to level.
1824          */
1825 void flip_subdivison(Object *ob, int level)
1826 {
1827         Mesh *me = ob->data;
1828
1829         if (level == -1) {
1830                 me->flag ^= ME_SUBSURF;
1831         } else {
1832                 me->subdiv = level;
1833         }
1834
1835         allqueue(REDRAWVIEW3D, 0);
1836         allqueue(REDRAWOOPS, 0);
1837         allqueue(REDRAWBUTSEDIT, 0);
1838         makeDispList(ob);
1839 }
1840  
1841 void copymenu_properties(Object *ob)
1842 {       
1843         bProperty *prop, *propn, *propc;
1844         Base *base;
1845         int nr, tot=0;
1846         char *str;
1847         
1848         prop= ob->prop.first;
1849         while(prop) {
1850                 tot++;
1851                 prop= prop->next;
1852         }
1853         
1854         if(tot==0) {
1855                 error("No properties in Object");
1856                 return;
1857         }
1858         
1859         str= MEM_callocN(24+32*tot, "copymenu prop");
1860         
1861         strcpy(str, "Copy Property %t");
1862         
1863         tot= 0; 
1864         prop= ob->prop.first;
1865         while(prop) {
1866                 tot++;
1867                 strcat(str, " |");
1868                 strcat(str, prop->name);
1869                 prop= prop->next;
1870         }
1871
1872         nr= pupmenu(str);
1873         if(nr>0) {
1874                 tot= 0;
1875                 prop= ob->prop.first;
1876                 while(prop) {
1877                         tot++;
1878                         if(tot==nr) break;
1879                         prop= prop->next;
1880                 }
1881                 if(prop) {
1882                         propc= prop;
1883                         
1884                         base= FIRSTBASE;
1885                         while(base) {
1886                                 if(base != BASACT) {
1887                                         if(TESTBASELIB(base)) {
1888                                                 prop= get_property(base->object, propc->name);
1889                                                 if(prop) {
1890                                                         free_property(prop);
1891                                                         BLI_remlink(&base->object->prop, prop);
1892                                                 }
1893                                                 propn= copy_property(propc);
1894                                                 BLI_addtail(&base->object->prop, propn);
1895                                         }
1896                                 }
1897                                 base= base->next;
1898                         }
1899                 }
1900         }
1901         MEM_freeN(str);
1902         allqueue(REDRAWVIEW3D, 0);
1903 }
1904
1905 void copymenu_logicbricks(Object *ob)
1906 {
1907         Base *base;
1908         
1909         base= FIRSTBASE;
1910         while(base) {
1911                 if(base->object != ob) {
1912                         if(TESTBASELIB(base)) {
1913                                 
1914                                 /* first: free all logic */
1915                                 free_sensors(&base->object->sensors);                           
1916                                 unlink_controllers(&base->object->controllers);
1917                                 free_controllers(&base->object->controllers);
1918                                 unlink_actuators(&base->object->actuators);
1919                                 free_actuators(&base->object->actuators);
1920                                 
1921                                 /* now copy it, this also works without logicbricks! */
1922                                 clear_sca_new_poins_ob(ob);
1923                                 copy_sensors(&base->object->sensors, &ob->sensors);
1924                                 copy_controllers(&base->object->controllers, &ob->controllers);
1925                                 copy_actuators(&base->object->actuators, &ob->actuators);
1926                                 set_sca_new_poins_ob(base->object);
1927                                 
1928                                 /* some menu settings */
1929                                 base->object->scavisflag= ob->scavisflag;
1930                                 base->object->scaflag= ob->scaflag;
1931                                 
1932                         }
1933                 }
1934                 base= base->next;
1935         }
1936 }
1937
1938 void copy_attr_menu()
1939 {
1940         Object *ob;
1941         short event;
1942         char str[256];
1943
1944         /* If you change this menu, don't forget to update the menu in header_view3d.c
1945          * view3d_edit_object_copyattrmenu() and in toolbox.c
1946          */
1947         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");
1948
1949         if(!(ob=OBACT)) return;
1950         
1951         strcat (str, "|Object Constraints%x22");
1952         
1953         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
1954                 strcat(str, "|Texture Space%x17");
1955         }       
1956         
1957         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1958         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
1959
1960         if(ob->type==OB_MESH){
1961                 strcat(str, "|Subdiv%x21");
1962         }
1963
1964         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
1965
1966         event= pupmenu(str);
1967         if(event<= 0) return;
1968         
1969         copy_attr(event);
1970 }
1971
1972 void copy_attr(short event)
1973 {
1974         Object *ob, *obt;
1975         Base *base;
1976         Curve *cu, *cu1;
1977         void *poin1, *poin2=0;
1978         
1979         if(G.scene->id.lib) return;
1980
1981         if(!(ob=OBACT)) return;
1982         
1983         if(G.obedit) {
1984                 /* obedit_copymenu(); */
1985                 return;
1986         }
1987         
1988         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
1989                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
1990                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
1991                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
1992         }       
1993
1994         if(event==9) {
1995                 copymenu_properties(ob);
1996                 return;
1997         }
1998         else if(event==10) {
1999                 copymenu_logicbricks(ob);
2000                 return;
2001         }
2002
2003         base= FIRSTBASE;
2004         while(base) {
2005                 if(base != BASACT) {
2006                         if(TESTBASELIB(base)) {
2007                                 
2008                                 if(event==1) {  /* loc */
2009                                         VECCOPY(base->object->loc, ob->loc);
2010                                         VECCOPY(base->object->dloc, ob->dloc);
2011                                 }
2012                                 else if(event==2) {  /* rot */
2013                                         VECCOPY(base->object->rot, ob->rot);
2014                                         VECCOPY(base->object->drot, ob->drot);
2015                                         VECCOPY(base->object->quat, ob->quat);
2016                                         VECCOPY(base->object->dquat, ob->dquat);
2017                                 }
2018                                 else if(event==3) {  /* size */
2019                                         VECCOPY(base->object->size, ob->size);
2020                                         VECCOPY(base->object->dsize, ob->dsize);
2021                                 }
2022                                 else if(event==4) {  /* drawtype */
2023                                         base->object->dt= ob->dt;
2024                                         base->object->dtx= ob->dtx;
2025                                         }
2026                                 else if(event==5) {  /* time offs */
2027                                         base->object->sf= ob->sf;
2028                                 }
2029                                 else if(event==6) {  /* dupli */
2030                                         base->object->dupon= ob->dupon;
2031                                         base->object->dupoff= ob->dupoff;
2032                                         base->object->dupsta= ob->dupsta;
2033                                         base->object->dupend= ob->dupend;
2034                                         
2035                                         base->object->transflag &= ~OB_DUPLI;
2036                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2037                                 }
2038                                 else if(event==7) {     /* mass */
2039                                         base->object->mass= ob->mass;
2040                                 }
2041                                 else if(event==8) {     /* damping */
2042                                         base->object->damping= ob->damping;
2043                                         base->object->rdamping= ob->rdamping;
2044                                 }
2045                                 else if(event==17) {    /* tex space */
2046                                         obt= base->object;
2047                                         poin1= 0;
2048                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2049                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2050                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2051                                         
2052                                         if(poin1) {
2053                                                 memcpy(poin1, poin2, 4+12+12+12);
2054                                         
2055                                                 if(obt->type==OB_MESH) tex_space_mesh(obt->data);
2056                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2057                                                 else tex_space_curve(obt->data);
2058                                         }
2059                                 }
2060                                 else if(event==18) {    /* font settings */
2061                                         
2062                                         if(base->object->type==ob->type) {
2063                                                 cu= ob->data;
2064                                                 cu1= base->object->data;
2065                                                 
2066                                                 cu1->spacemode= cu->spacemode;
2067                                                 cu1->spacing= cu->spacing;
2068                                                 cu1->linedist= cu->linedist;
2069                                                 cu1->shear= cu->shear;
2070                                                 cu1->fsize= cu->fsize;
2071                                                 cu1->xof= cu->xof;
2072                                                 cu1->yof= cu->yof;
2073                                                 cu1->textoncurve= cu->textoncurve;
2074                                                 if(cu1->vfont) cu1->vfont->id.us--;
2075                                                 cu1->vfont= cu->vfont;
2076                                                 id_us_plus((ID *)cu1->vfont);
2077                                                 text_to_curve(base->object, 0);
2078                                                 
2079                                                 strcpy(cu1->family, cu->family);
2080                                                 
2081                                                 makeDispList(base->object);
2082                                         }
2083                                 }
2084                                 else if(event==19) {    /* bevel settings */
2085                                         
2086                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2087                                                 cu= ob->data;
2088                                                 cu1= base->object->data;
2089                                                 
2090                                                 cu1->bevobj= cu->bevobj;
2091                                                 cu1->width= cu->width;
2092                                                 cu1->bevresol= cu->bevresol;
2093                                                 cu1->ext1= cu->ext1;
2094                                                 cu1->ext2= cu->ext2;
2095                                                 
2096                                                 makeDispList(base->object);
2097                                         }
2098                                 }
2099                                 else if(event==20) {    /* particle settings */
2100                                         PartEff *pa1, *pa2;
2101                                         char *p1, *p2;
2102                                         
2103                                         pa1= give_parteff(ob);
2104                                         pa2= give_parteff(base->object);
2105
2106                                         if(pa1==0 && pa2) {
2107                                                 BLI_remlink( &(base->object->effect), pa2);
2108                                                 free_effect( (Effect *) pa2);
2109                                         }
2110                                         else if(pa1 && pa2==0) {
2111                                                 free_effects(&(base->object->effect));
2112                                                 copy_effects(&(base->object->effect), &ob->effect);
2113                                                 build_particle_system(base->object);
2114                                         }
2115                                         else if(pa1 && pa2) {
2116                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2117                                                 
2118                                                 p1= (char *)pa1; p2= (char *)pa2;
2119                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2120                                                 pa2->keys= 0;
2121                                                 
2122                                                 build_particle_system(base->object);
2123                                         }
2124                                 }
2125                                 else if(event==21){
2126                                         if (base->object->type==OB_MESH) {
2127                                                 Mesh *targetme= base->object->data;
2128                                                 Mesh *sourceme= ob->data;
2129
2130                                                 targetme->flag= (targetme->flag&~ME_SUBSURF) | (sourceme->flag&ME_SUBSURF);
2131                                                 targetme->subsurftype = sourceme->subsurftype;
2132                                                 targetme->subdiv= sourceme->subdiv;
2133                                                 targetme->subdivr= sourceme->subdivr;
2134                                                 makeDispList(base->object);
2135                                         }
2136                                 }
2137                                 else if(event==22){
2138                                         /* Clear the constraints on the target */
2139                                         free_constraints(&base->object->constraints);
2140                                         free_constraint_channels(&base->object->constraintChannels);
2141
2142                                         /* Copy the constraint channels over */
2143                                         copy_constraints(&base->object->constraints, &ob->constraints);
2144                                         if (U.dupflag& USER_DUP_IPO)
2145                                                 copy_constraint_channels(&base->object->constraintChannels, &ob->constraintChannels);
2146                                         else
2147                                                 clone_constraint_channels (&base->object->constraintChannels, &ob->constraintChannels, NULL);
2148
2149                                         base->object->activecon = NULL;
2150                                 }
2151                         }
2152                 }
2153                 base= base->next;
2154         }
2155         
2156         allqueue(REDRAWVIEW3D, 0);
2157         if(event==20) {
2158                 allqueue(REDRAWBUTSOBJECT, 0);
2159         }
2160         
2161 }
2162
2163 void link_to_scene(unsigned short nr)
2164 {       
2165         Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1);
2166         Base *base, *nbase;
2167         
2168         if(sce==0) return;
2169         if(sce->id.lib) return;
2170         
2171         base= FIRSTBASE;
2172         while(base) {
2173                 if(TESTBASE(base)) {
2174                         
2175                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2176                         *nbase= *base;
2177                         BLI_addhead( &(sce->base), nbase);
2178                         id_us_plus((ID *)base->object);
2179                 }
2180                 base= base->next;
2181         }
2182 }
2183
2184 void make_links_menu()
2185 {
2186         Object *ob;
2187         short event=0;
2188         char str[140];
2189         
2190         if(!(ob=OBACT)) return;
2191         
2192         strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4");
2193         
2194         if(ob->type==OB_MESH)
2195                 strcat(str, "|Mesh Data%x2|Materials%x3");
2196         else if(ob->type==OB_CURVE)
2197                 strcat(str, "|Curve Data%x2|Materials%x3");
2198         else if(ob->type==OB_FONT)
2199                 strcat(str, "|Text Data%x2|Materials%x3");
2200         else if(ob->type==OB_SURF)
2201                 strcat(str, "|Surface Data%x2|Materials%x3");
2202         else if(ob->type==OB_MBALL)
2203                 strcat(str, "|Materials%x3");
2204         else if(ob->type==OB_CAMERA)
2205                 strcat(str, "|Camera Data%x2");
2206         else if(ob->type==OB_LAMP)
2207                 strcat(str, "|Lamp Data%x2");
2208         else if(ob->type==OB_LATTICE)
2209                 strcat(str, "|Lattice Data%x2");
2210         else if(ob->type==OB_ARMATURE)
2211                 strcat(str, "|Armature Data%x2");
2212
2213         event= pupmenu(str);
2214
2215         if(event<= 0) return;
2216         
2217         make_links(event);
2218 }
2219
2220 void make_links(short event)
2221 {
2222         Object *ob, *obt;
2223         Base *base, *nbase, *sbase;
2224         Scene *sce = NULL;
2225         ID *id;
2226         Material ***matarar, ***obmatarar, **matar1, **matar2;
2227         int a;
2228         short *totcolp, nr;
2229         char *strp;
2230
2231         if(!(ob=OBACT)) return;
2232
2233         if(event==1) {
2234                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr);
2235                 
2236                 if(strncmp(strp, "DataBrow", 8)==0) {
2237                         MEM_freeN(strp);
2238
2239                         activate_databrowse((ID *)G.scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene );
2240                         
2241                         return;                 
2242                 }
2243                 else {
2244                         event= pupmenu(strp);
2245                         MEM_freeN(strp);
2246                 
2247                         if(event<= 0) return;
2248                 
2249                         nr= 1;
2250                         sce= G.main->scene.first;
2251                         while(sce) {
2252                                 if(nr==event) break;
2253                                 nr++;
2254                                 sce= sce->id.next;
2255                         }
2256                         if(sce==G.scene) {
2257                                 error("This is current scene");
2258                                 return;
2259                         }
2260                         if(sce==0 || sce->id.lib) return;
2261                         
2262                         /* remember: is needed below */
2263                         event= 1;
2264                 }
2265         }
2266
2267         base= FIRSTBASE;
2268         while(base) {
2269                 if(event==1 || base != BASACT) {
2270                         
2271                         obt= base->object;
2272
2273                         if(TESTBASE(base)) {
2274                                 
2275                                 if(event==1) {          /* to scene */
2276                                         
2277                                         /* test if already linked */
2278                                         sbase= sce->base.first;
2279                                         while(sbase) {
2280                                                 if(sbase->object==base->object) break;
2281                                                 sbase= sbase->next;
2282                                         }
2283                                         if(sbase) {     /* remove */
2284                                                 base= base->next;
2285                                                 continue;
2286                                         }
2287                                         
2288                                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2289                                         *nbase= *base;
2290                                         BLI_addhead( &(sce->base), nbase);
2291                                         id_us_plus((ID *)base->object);
2292                                 }
2293                         }
2294                         if(TESTBASELIB(base)) {
2295                                 if(event==2 || event==5) {  /* obdata */
2296                                         if(ob->type==obt->type) {
2297                                                 
2298                                                         id= obt->data;
2299                                                         id->us--;
2300                                                         
2301                                                         id= ob->data;
2302                                                         id_us_plus(id);
2303                                                         obt->data= id;
2304                                                         
2305                                                         /* if amount of material indices changed: */
2306                                                         test_object_materials(obt->data);
2307                                                 }
2308                                         }
2309                                 else if(event==4) {  /* ob ipo */
2310                                         if(obt->ipo) obt->ipo->id.us--;
2311                                         obt->ipo= ob->ipo;
2312                                         if(obt->ipo) {
2313                                                 id_us_plus((ID *)obt->ipo);
2314                                                 do_ob_ipo(obt);
2315                                         }
2316                                 }
2317                                 else if(event==3) {  /* materials */
2318                                         
2319                                         /* only if obt has no material: make arrays */
2320                                         /* from ob to obt! */
2321                                         
2322                                         obmatarar= give_matarar(ob);
2323                                         matarar= give_matarar(obt);
2324                                         totcolp= give_totcolp(obt);
2325
2326                                         /* if one of the two is zero: no render-able object */                                          
2327                                         if( matarar && obmatarar) {
2328                                                 
2329                                                 /* take care of users! so first a copy of original: */
2330
2331                                                 if(ob->totcol) {
2332                                                         matar1= MEM_dupallocN(ob->mat);
2333                                                         matar2= MEM_dupallocN(*obmatarar);
2334                                                 }
2335                                                 else {
2336                                                         matar1= matar2= 0;
2337                                                 }
2338                                                 
2339                                                 /* remove links from obt */
2340                                                 for(a=0; a<obt->totcol; a++) {
2341                                                         if(obt->mat[a]) obt->mat[a]->id.us--;
2342                                                         if( (*matarar)[a]) (*matarar)[a]->id.us--;
2343                                                 }
2344                                                 
2345                                                 /* free */
2346                                                 if(obt->mat) MEM_freeN(obt->mat);
2347                                                 if(*matarar) MEM_freeN(*matarar);
2348                                                 
2349                                                 /* connect a copy */
2350                                                 obt->mat= matar1;
2351                                                 *matarar= matar2;
2352                                                 obt->totcol= ob->totcol;
2353                                                 *totcolp= ob->totcol;
2354                                         
2355                                                 /* increase users */
2356                                                 for(a=0; a<obt->totcol; a++) {
2357                                                         if(obt->mat[a]) id_us_plus((ID *)obt->mat[a]);
2358                                                         if( (*matarar)[a]) id_us_plus((ID *)(*matarar)[a]);
2359                                                 }
2360
2361                                                 obt->colbits= ob->colbits;
2362                                                 
2363                                                 /* if amount of material indices changed: */
2364                                                 test_object_materials(obt->data);
2365                                         }
2366                                 }
2367                         }
2368                 }
2369                 base= base->next;
2370         }
2371         
2372         allqueue(REDRAWVIEW3D, 0);
2373         allqueue(REDRAWOOPS, 0);
2374         allqueue(REDRAWBUTSHEAD, 0);
2375 }
2376
2377 void make_duplilist_real()
2378 {
2379         Base *base, *basen;
2380         Object *ob;
2381         extern ListBase duplilist;
2382         
2383         if(okee("Make dupli's real")==0) return;
2384         
2385         base= FIRSTBASE;
2386         while(base) {
2387                 if TESTBASELIB(base) {
2388
2389                         if(base->object->transflag & OB_DUPLI) {
2390                                 
2391                                 make_duplilist(G.scene, base->object);
2392                                 ob= duplilist.first;
2393                                 while(ob) {
2394                                         
2395                                         /* font duplis can have a totcol without material, we get them from parent
2396                                          * should be implemented better...
2397                                          */
2398                                         if(ob->mat==0) ob->totcol= 0;
2399                                         
2400                                         basen= MEM_dupallocN(base);
2401                                         basen->flag &= ~OB_FROMDUPLI;
2402                                         BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2403                                         ob->ipo= 0;             /* make sure apply works */
2404                                         ob->parent= ob->track= 0;
2405                                         ob->disp.first= ob->disp.last= 0;
2406                                         ob->transflag &= ~OB_DUPLI;
2407                                         basen->object= copy_object(ob);
2408                                         
2409                                         apply_obmat(basen->object);
2410                                         ob= ob->id.next;
2411                                 }
2412                                 
2413                                 free_duplilist();
2414                                 
2415                                 base->object->transflag &= ~OB_DUPLI;   
2416                         }
2417                 }
2418                 base= base->next;
2419         }
2420         
2421         allqueue(REDRAWVIEW3D, 0);
2422         allqueue(REDRAWOOPS, 0);
2423 }
2424
2425 void apply_object()
2426 {
2427         Base *base, *basact;
2428         Object *ob;
2429         Mesh *me;
2430         Curve *cu;
2431         Nurb *nu;
2432         BPoint *bp;
2433         BezTriple *bezt;
2434         MVert *mvert;
2435         float mat[3][3];
2436         int a;
2437
2438         if(G.scene->id.lib) return;
2439         if(G.obedit) return;
2440         basact= BASACT;
2441         
2442         if(G.qual & LR_SHIFTKEY) {
2443                 ob= OBACT;
2444                 if(ob==0) return;
2445                 
2446                 if(ob->transflag & OB_DUPLI) make_duplilist_real();
2447                 else if(ob->parent && ob->parent->type==OB_LATTICE) apply_lattice();
2448                 
2449                 return;
2450         }
2451
2452         if(okee("Apply size/rot")==0) return;
2453
2454         base= FIRSTBASE;
2455         while(base) {
2456                 if TESTBASELIB(base) {
2457                         ob= base->object;
2458         
2459                         if(ob->type==OB_MESH) {
2460                                 object_to_mat3(ob, mat);
2461                                 me= ob->data;
2462                                 
2463                                 if(me->id.us>1) {
2464                                         error("Can't do multi user mesh");
2465                                         return;
2466                                 }
2467                                 if(me->key) {
2468                                         error("Can't do key && mesh");
2469                                         return;
2470                                 }
2471                                 
2472                                 mvert= me->mvert;
2473                                 for(a=0; a<me->totvert; a++, mvert++) {
2474                                         Mat3MulVecfl(mat, mvert->co);
2475                                 }
2476                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2477                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2478                                 QuatOne(ob->quat);
2479                                 
2480                                 where_is_object(ob);
2481                                 
2482                                 /* texspace and normals */
2483                                 BASACT= base;
2484                                 enter_editmode();
2485                                 exit_editmode(1);
2486                                 BASACT= basact;                         
2487                                 
2488                         }
2489                         else if (ob->type==OB_ARMATURE){
2490                                 bArmature *arm;
2491
2492                                 object_to_mat3(ob, mat);
2493                                 arm= ob->data;
2494                                 if(arm->id.us>1) {
2495                                         error("Can't do multi user armature");
2496                                         return;
2497                                 }
2498
2499                                 apply_rot_armature (ob, mat);
2500                                 /* Reset the object's transforms */
2501                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2502                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2503                                 QuatOne(ob->quat);
2504                                 
2505                                 where_is_object(ob);
2506                         }
2507                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2508                                 object_to_mat3(ob, mat);
2509                                 cu= ob->data;
2510                                 
2511                                 if(cu->id.us>1) {
2512                                         error("Can't do multi user curve");
2513                                         return;
2514                                 }
2515                                 if(cu->key) {
2516                                         error("Can't do keys");
2517                                         return;
2518                                 }
2519                                 
2520                                 nu= cu->nurb.first;
2521                                 while(nu) {
2522                                         if( (nu->type & 7)==1) {
2523                                                 a= nu->pntsu;
2524                                                 bezt= nu->bezt;
2525                                                 while(a--) {
2526                                                         Mat3MulVecfl(mat, bezt->vec[0]);
2527                                                         Mat3MulVecfl(mat, bezt->vec[1]);
2528                                                         Mat3MulVecfl(mat, bezt->vec[2]);
2529                                                         bezt++;
2530                                                 }
2531                                         }
2532                                         else {
2533                                                 a= nu->pntsu*nu->pntsv;
2534                                                 bp= nu->bp;
2535                                                 while(a--) {
2536                                                         Mat3MulVecfl(mat, bp->vec);
2537                                                         bp++;
2538                                                 }
2539                                         }
2540                                         nu= nu->next;
2541                                 }
2542                         
2543                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2544                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2545                                 QuatOne(ob->quat);
2546                                 
2547                                 where_is_object(ob);
2548                                 
2549                                 /* texspace and normals */
2550                                 BASACT= base;
2551                                 enter_editmode();
2552                                 exit_editmode(1);
2553                                 BASACT= basact;
2554                         }
2555                 }
2556                 base= base->next;
2557         }
2558         
2559         allqueue(REDRAWVIEW3D, 0);
2560 }
2561
2562
2563
2564 /* ************ GENERAL  *************** */
2565
2566 static Object *is_a_parent_selected_int(Object *startob, Object *ob, GHash *done_hash) {
2567         if (ob!=startob && TESTBASE(ob))
2568                 return ob;
2569                 
2570         if (BLI_ghash_haskey(done_hash, ob))
2571                 return NULL;
2572         else
2573                 BLI_ghash_insert(done_hash, ob, NULL);
2574         
2575         if (ob->parent) {
2576                 Object *par= is_a_parent_selected_int(startob, ob->parent, done_hash);
2577                 if (par)
2578                         return par;
2579         }
2580
2581         /* IK is more complex in parents... */
2582         
2583         /* XXX, should we be handling armatures or constraints here? - zr */
2584
2585         if(ob->type==OB_IKA) {
2586                 Ika *ika= ob->data;
2587                 
2588                 if (ika->def) {
2589                         int i;
2590                         
2591                         for (i=0; i<ika->totdef; i++) {
2592                                 Deform *def= &ika->def[i];
2593                                 
2594                                 if (def->ob && ob!=def->ob && def->ob!=startob) {
2595                                         Object *par= is_a_parent_selected_int(startob, def->ob, done_hash);
2596                                         if (par)
2597                                                 return par;
2598                                 }
2599                         }
2600                 }
2601                 
2602                 if (ika->parent) {
2603                         Object *par= is_a_parent_selected_int(startob, ika->parent, done_hash);
2604                         if (par)
2605                                 return par;
2606                 }
2607         }
2608         
2609         return NULL;
2610 }
2611
2612 static Object *is_a_parent_selected(Object *ob) {
2613         GHash *gh= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
2614         Object *res= is_a_parent_selected_int(ob, ob, gh);
2615         BLI_ghash_free(gh, NULL, NULL);
2616         
2617         return res;
2618 }
2619
2620 /*** POSE FIGURIN' -- START ***/
2621
2622 static void clear_pose_update_flag(Object *ob) {
2623         /* Clear the flag for each pose channel that indicates that
2624          * pose should be updated on every redraw
2625          */
2626         bPoseChannel *chan;
2627         
2628         if (ob->pose) {
2629                 for (chan = ob->pose->chanbase.first; chan; 
2630                          chan=chan->next){
2631                         chan->flag &= ~PCHAN_TRANS_UPDATE;
2632                 }
2633         }
2634 }
2635
2636 static int pose_flags_reset_done(Object *ob) {
2637         /* Clear the constraint done status for every pose channe;
2638          * that has been flagged as needing constant updating
2639          */
2640         bPoseChannel *chan;
2641         int numreset = 0;
2642
2643         if (ob->pose) {
2644                 for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
2645                         if (chan->flag & PCHAN_TRANS_UPDATE) {
2646                                 chan->flag &= ~PCHAN_DONE;
2647                                 numreset++;
2648                         }
2649
2650                 }
2651         }
2652         return numreset;
2653 }
2654
2655 static int is_ob_constraint_target(Object *ob, ListBase *conlist) {
2656         /* Is this object the target of a constraint in this list? 
2657          */
2658
2659         bConstraint *con;
2660
2661         for (con=conlist->first; con; con=con->next)
2662         {
2663                 if (get_con_target(con) == ob)
2664                         return 1;
2665         }
2666         return 0;
2667
2668 }
2669
2670 int clear_bone_nocalc(Object *ob, Bone *bone, void *ptr) {
2671         /* When we aren't transform()-ing, we'll want to turn off
2672          * the no calc flag for bone bone in case the frame changes,
2673          * or something
2674          */
2675         bone->flag &= ~BONE_NOCALC;
2676         
2677         return 0;
2678 }
2679
2680
2681 static void clear_bone_nocalc_ob(Object *ob) {
2682         /* Let's clear no calc for all of the bones in the whole darn armature
2683          */
2684         bArmature *arm;
2685         arm = get_armature(ob);
2686         if (arm) {
2687                 bone_looper(ob, arm->bonebase.first, NULL, 
2688                                         clear_bone_nocalc);
2689         }
2690
2691 }
2692
2693 int set_bone_nocalc(Object *ob, Bone *bone, void *ptr) {
2694         /* Calculating bone transformation makes thins slow ...
2695          * lets set the no calc flag for a bone by default
2696          */
2697         bone->flag |= BONE_NOCALC;
2698         
2699         return 0;
2700 }
2701
2702 int selected_bone_docalc(Object *ob, Bone *bone, void *ptr) {
2703         /* Let's clear the no calc flag for selected bones.
2704          * This function always returns 1 for non-no calc bones
2705          * (a.k.a., the 'do calc' bones) so that the bone_looper 
2706          * will count these
2707          */
2708         if (bone->flag & BONE_NOCALC) {
2709                 if ( (bone->flag & BONE_SELECTED) ) {
2710                         bone->flag &= ~BONE_NOCALC;
2711                         return 1;
2712                 }
2713                 
2714         }
2715         else {
2716                 return 1;
2717         }
2718         return 0;
2719 }
2720
2721 static Bone *get_parent_bone_docalc(Bone *bone) {
2722         Bone            *parBone;
2723
2724         for (parBone = bone->parent; parBone; parBone=parBone->parent)
2725                 if (~parBone->flag & BONE_NOCALC)
2726                         return parBone;
2727
2728         return NULL;
2729 }
2730
2731 static int is_bone_parent(Bone *childBone, Bone *parBone) {
2732         Bone            *currBone;
2733
2734         for (currBone = childBone->parent; currBone; currBone=currBone->parent)
2735                 if (currBone == parBone)
2736                         return 1;
2737
2738         return 0;
2739 }
2740
2741 static void figure_bone_nocalc_constraint(Bone *conbone, bConstraint *con,
2742                                                                                   Object *ob, bArmature *arm) {
2743         /* If this bone has a constraint with a subtarget that has
2744          * the nocalc flag cleared, then we better clear the no calc flag
2745          * on this bone too (and the whole IK chain if this is an IK
2746          * constraint).
2747          *
2748          * Conversly, if this bone has an IK constraint and the root of
2749          * the chain has the no calc flag cleared, we had best clear that
2750          * flag for the whole chain.
2751          */
2752         Bone *subtarbone;
2753         Bone *parBone;
2754         char *subtar;
2755
2756         subtar = get_con_subtarget_name(con, ob);
2757
2758         if (subtar) {
2759                 if ( (subtarbone = get_named_bone(arm, subtar)) ) {
2760                         if ( (~subtarbone->flag & BONE_NOCALC) ||
2761                                  (get_parent_bone_docalc(subtarbone)) ) {
2762                                 if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2763                                         /* IK target is flaged for updating, so we
2764                                          * must update the whole chain.
2765                                          */
2766                                         ik_chain_looper(ob, conbone, NULL, 
2767                                                                         clear_bone_nocalc);
2768                                 else
2769                                         /* Constraint target is flagged for
2770                                          * updating, so we update this bone only
2771                                          */
2772                                         conbone->flag &= ~BONE_NOCALC;
2773                         }
2774                         else {
2775                                 if ( (parBone = get_parent_bone_docalc(conbone)) ) {
2776                                         /* a parent is flagged for updating */
2777                                         if (!is_bone_parent(subtarbone, parBone)) {
2778                                                 /* if the subtarget is also a child of
2779                                                  * this bone, we needn't worry, other
2780                                                  * wise, we have to update
2781                                                  */
2782                                                 if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2783                                                         ik_chain_looper(ob, conbone, NULL, 
2784                                                                                         clear_bone_nocalc);
2785                                                 else
2786                                                         conbone->flag &= ~BONE_NOCALC;
2787                                         }
2788                                 }
2789
2790                         }
2791                 }
2792         }
2793         else {
2794                 /* no subtarget ... target is regular object */
2795                 if ( (parBone = get_parent_bone_docalc(conbone)) ) {
2796                         /* parent is flagged for updating ... since
2797                          * the target will never move (not a bone)
2798                          * we had better update this bone/chain
2799                          */
2800                         if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2801                                 ik_chain_looper(ob, conbone, NULL, 
2802                                                                 clear_bone_nocalc);
2803                         else
2804                                 conbone->flag &= ~BONE_NOCALC;
2805                 }
2806         }
2807
2808 }
2809
2810 static void figure_bone_nocalc_core(Object *ob, bArmature *arm) {
2811         /* Let's figure out which bones need to be recalculated,
2812          * and which don't. Calculations are based on which bones
2813          * are selected, and the constraints that love them.
2814          */
2815         bPoseChannel *chan;
2816         bConstraint *con;
2817         Bone *conbone;
2818
2819         int numbones, oldnumbones, iterations;
2820
2821         oldnumbones = -1;
2822         numbones    =  0;
2823         iterations  =  0;
2824
2825         /* O.K., lets loop until we don't clear any more no calc bones
2826          */
2827         while (oldnumbones != numbones) {
2828                 /* I wonder if this will ever get executed? */
2829                 if ( (++iterations) == 1000) {
2830                         printf("figurin' nocalc is talking too long\n");
2831                         break;
2832                 }
2833
2834                 oldnumbones = numbones;
2835
2836                 /* clear no calc for selected bones and count */
2837                 numbones = bone_looper(ob, arm->bonebase.first, NULL, 
2838                                                            selected_bone_docalc);
2839
2840                 if (ob->pose) {
2841                         for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
2842                                 conbone = get_named_bone(arm, chan->name);
2843                                 if (conbone) {
2844                                         for (con = chan->constraints.first; con; con=con->next) {
2845                                                 figure_bone_nocalc_constraint(conbone, con, ob, arm);
2846                                         }
2847                                 }
2848                         }
2849                 }
2850         }
2851 }
2852
2853 static void figure_bone_nocalc(Object *ob) {
2854         /* Let's figure out which bones need to be recalculated,
2855          * and which don't. Calculations are based on which bones
2856          * are selected, and the constraints that love them.
2857          */
2858         bArmature *arm;
2859
2860         arm = get_armature(ob);
2861         if (!arm) return;
2862
2863         if (arm->flag & ARM_RESTPOS) return;
2864
2865         /* Set no calc for all bones
2866          */
2867         bone_looper(ob, arm->bonebase.first, NULL, 
2868                                 set_bone_nocalc);
2869
2870         figure_bone_nocalc_core(ob, ar