committing working copy
[blender.git] / source / blender / editors / object / object_edit.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30 #include <time.h>
31 #include <float.h>
32 #include <ctype.h>
33 #include <stddef.h> //for offsetof
34
35 #include "MEM_guardedalloc.h"
36
37 #include "IMB_imbuf_types.h"
38
39 #include "DNA_armature_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_property_types.h"
46 #include "DNA_vfont_types.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_math.h"
50 #include "BLI_editVert.h"
51 #include "BLI_ghash.h"
52 #include "BLI_rand.h"
53
54 #include "BKE_action.h"
55 #include "BKE_anim.h"
56 #include "BKE_armature.h"
57 #include "BKE_constraint.h"
58 #include "BKE_context.h"
59 #include "BKE_customdata.h"
60 #include "BKE_blender.h"
61 #include "BKE_cloth.h"
62 #include "BKE_curve.h"
63 #include "BKE_displist.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_DerivedMesh.h"
66 #include "BKE_effect.h"
67 #include "BKE_font.h"
68 #include "BKE_global.h"
69 #include "BKE_group.h"
70 #include "BKE_image.h"
71 #include "BKE_key.h"
72 #include "BKE_lattice.h"
73 #include "BKE_library.h"
74 #include "BKE_main.h"
75 #include "BKE_material.h"
76 #include "BKE_mball.h"
77 #include "BKE_mesh.h"
78 #include "BKE_nla.h"
79 #include "BKE_object.h"
80 #include "BKE_paint.h"
81 #include "BKE_particle.h"
82 #include "BKE_pointcache.h"
83 #include "BKE_property.h"
84 #include "BKE_report.h"
85 #include "BKE_sca.h"
86 #include "BKE_scene.h"
87 #include "BKE_softbody.h"
88 #include "BKE_subsurf.h"
89 #include "BKE_texture.h"
90 #include "BKE_utildefines.h"
91 #include "BKE_modifier.h"
92 #include "BKE_tessmesh.h"
93
94 #include "ED_armature.h"
95 #include "ED_curve.h"
96 #include "ED_mesh.h"
97 #include "ED_mball.h"
98 #include "ED_object.h"
99 #include "ED_screen.h"
100 #include "ED_util.h"
101
102
103 #include "RNA_access.h"
104 #include "RNA_define.h"
105 #include "RNA_enum_types.h"
106
107 /* for menu/popup icons etc etc*/
108
109 #include "UI_interface.h"
110 #include "WM_api.h"
111 #include "WM_types.h"
112
113 #include "object_intern.h"      // own include
114
115 /* ************* XXX **************** */
116 static void error(const char *dummy) {}
117 static void waitcursor(int val) {}
118 static int pupmenu(const char *msg) {return 0;}
119
120 /* port over here */
121 static bContext *C;
122 static void error_libdata() {}
123
124
125 /* find the correct active object per context
126  * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
127 Object *ED_object_active_context(bContext *C)
128 {
129         Object *ob= NULL;
130         if(C) {
131                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
132                 if (!ob) ob= CTX_data_active_object(C);
133         }
134         return ob;
135 }
136
137
138 /* ********* clear/set restrict view *********/
139 static int object_restrictview_clear_exec(bContext *C, wmOperator *op)
140 {
141         ScrArea *sa= CTX_wm_area(C);
142         View3D *v3d= sa->spacedata.first;
143         Scene *scene= CTX_data_scene(C);
144         Base *base;
145         int changed = 0;
146         
147         /* XXX need a context loop to handle such cases */
148         for(base = FIRSTBASE; base; base=base->next){
149                 if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
150                         base->flag |= SELECT;
151                         base->object->flag = base->flag;
152                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
153                         changed = 1;
154                 }
155         }
156         if (changed) {
157                 DAG_scene_sort(scene);
158                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
159         }
160
161         return OPERATOR_FINISHED;
162 }
163
164 void OBJECT_OT_restrictview_clear(wmOperatorType *ot)
165 {
166         
167         /* identifiers */
168         ot->name= "Clear Restrict View";
169         ot->description = "Reveal the object by setting the restrictview flag";
170         ot->idname= "OBJECT_OT_restrictview_clear";
171         
172         /* api callbacks */
173         ot->exec= object_restrictview_clear_exec;
174         ot->poll= ED_operator_view3d_active;
175         
176         /* flags */
177         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
178 }
179
180 static int object_restrictview_set_exec(bContext *C, wmOperator *op)
181 {
182         Scene *scene= CTX_data_scene(C);
183         short changed = 0;
184         int unselected= RNA_boolean_get(op->ptr, "unselected");
185         
186         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
187                 if(!unselected) {
188                         if (base->flag & SELECT){
189                                 base->flag &= ~SELECT;
190                                 base->object->flag = base->flag;
191                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
192                                 changed = 1;
193                                 if (base==BASACT) {
194                                         ED_base_object_activate(C, NULL);
195                                 }
196                         }
197                 }
198                 else {
199                         if (!(base->flag & SELECT)){
200                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
201                                 changed = 1;
202                         }
203                 }       
204         }
205         CTX_DATA_END;
206
207         if (changed) {
208                 DAG_scene_sort(scene);
209                 
210                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
211                 
212         }
213
214         return OPERATOR_FINISHED;
215 }
216
217 void OBJECT_OT_restrictview_set(wmOperatorType *ot)
218 {
219         /* identifiers */
220         ot->name= "Set Restrict View";
221         ot->description = "Hide the object by setting the restrictview flag";
222         ot->idname= "OBJECT_OT_restrictview_set";
223         
224         /* api callbacks */
225         ot->exec= object_restrictview_set_exec;
226         ot->poll= ED_operator_view3d_active;
227         
228         /* flags */
229         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
230         
231         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
232         
233 }
234
235 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
236 static int object_restrictrender_clear_exec(bContext *C, wmOperator *op)
237 {
238         short changed= 0;
239
240         /* XXX need a context loop to handle such cases */
241         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
242                 if(ob->restrictflag & OB_RESTRICT_RENDER) {
243                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
244                         changed= 1;
245                 }
246         }
247         CTX_DATA_END;
248
249         if(changed)
250                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
251
252         return OPERATOR_FINISHED;
253 }
254
255 void OBJECT_OT_restrictrender_clear(wmOperatorType *ot)
256 {
257
258         /* identifiers */
259         ot->name= "Clear Restrict Render";
260         ot->description = "Reveal the render object by setting the restrictrender flag";
261         ot->idname= "OBJECT_OT_restrictrender_clear";
262
263         /* api callbacks */
264         ot->exec= object_restrictrender_clear_exec;
265         ot->poll= ED_operator_view3d_active;
266
267         /* flags */
268         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
269 }
270
271 static int object_restrictrender_set_exec(bContext *C, wmOperator *op)
272 {
273         int unselected= RNA_boolean_get(op->ptr, "unselected");
274
275         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
276                 if(!unselected) {
277                         if (base->flag & SELECT){
278                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
279                         }
280                 }
281                 else {
282                         if (!(base->flag & SELECT)){
283                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
284                         }
285                 }
286         }
287         CTX_DATA_END;
288         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
289         return OPERATOR_FINISHED;
290 }
291
292 void OBJECT_OT_restrictrender_set(wmOperatorType *ot)
293 {
294         /* identifiers */
295         ot->name= "Set Restrict Render";
296         ot->description = "Hide the render object by setting the restrictrender flag";
297         ot->idname= "OBJECT_OT_restrictrender_set";
298
299         /* api callbacks */
300         ot->exec= object_restrictrender_set_exec;
301         ot->poll= ED_operator_view3d_active;
302
303         /* flags */
304         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
305
306         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
307 }
308
309 /* ******************* toggle editmode operator  ***************** */
310
311 void ED_object_exit_editmode(bContext *C, int flag)
312 {
313         /* Note! only in exceptional cases should 'EM_DO_UNDO' NOT be in the flag */
314
315         Scene *scene= CTX_data_scene(C);
316         Object *obedit= CTX_data_edit_object(C);
317         int freedata = flag & EM_FREEDATA;
318         
319         if(obedit==NULL) return;
320         
321         if(flag & EM_WAITCURSOR) waitcursor(1);
322         if(obedit->type==OB_MESH) {
323                 Mesh *me= obedit->data;
324                 
325 //              if(EM_texFaceCheck())
326                 
327 //              if(retopo_mesh_paint_check())
328 //                      retopo_end_okee();
329                 
330                 if(me->edit_btmesh->bm->totvert>MESH_MAX_VERTS) {
331                         error("Too many vertices");
332                         return;
333                 }
334                 
335                 EDBM_LoadEditBMesh(scene, obedit);
336                 
337                 if(freedata) {
338                         EDBM_FreeEditBMesh(me->edit_btmesh);
339                         MEM_freeN(me->edit_btmesh);
340                         me->edit_btmesh= NULL;
341                 }
342                 
343                 if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT) {
344                         mesh_octree_table(NULL, NULL, NULL, 'e');
345                         mesh_mirrtopo_table(NULL, 'e');
346                 }
347         }
348         else if (obedit->type==OB_ARMATURE) {   
349                 ED_armature_from_edit(obedit);
350                 if(freedata)
351                         ED_armature_edit_free(obedit);
352         }
353         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
354                 load_editNurb(obedit);
355                 if(freedata) free_editNurb(obedit);
356         }
357         else if(obedit->type==OB_FONT && freedata) {
358                 load_editText(obedit);
359                 if(freedata) free_editText(obedit);
360         }
361         else if(obedit->type==OB_LATTICE) {
362                 load_editLatt(obedit);
363                 if(freedata) free_editLatt(obedit);
364         }
365         else if(obedit->type==OB_MBALL) {
366                 load_editMball(obedit);
367                 if(freedata) free_editMball(obedit);
368         }
369
370         /* freedata only 0 now on file saves and render */
371         if(freedata) {
372                 ListBase pidlist;
373                 PTCacheID *pid;
374
375                 /* for example; displist make is different in editmode */
376                 scene->obedit= NULL; // XXX for context
377
378                 /* flag object caches as outdated */
379                 BKE_ptcache_ids_from_object(&pidlist, obedit, NULL, 0);
380                 for(pid=pidlist.first; pid; pid=pid->next) {
381                         if(pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
382                                 pid->cache->flag |= PTCACHE_OUTDATED;
383                 }
384                 BLI_freelistN(&pidlist);
385                 
386                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_DEPSGRAPH);
387
388                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
389                 DAG_id_flush_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA);
390         
391                 if(flag & EM_DO_UNDO)
392                         ED_undo_push(C, "Editmode");
393         
394                 if(flag & EM_WAITCURSOR) waitcursor(0);
395         
396                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
397
398                 obedit->mode &= ~OB_MODE_EDIT;
399         }
400 }
401
402
403 void ED_object_enter_editmode(bContext *C, int flag)
404 {
405         Scene *scene= CTX_data_scene(C);
406         Base *base= NULL;
407         Object *ob;
408         ScrArea *sa= CTX_wm_area(C);
409         View3D *v3d= NULL;
410         int ok= 0;
411         
412         if(scene->id.lib) return;
413         
414         if(sa && sa->spacetype==SPACE_VIEW3D)
415                 v3d= sa->spacedata.first;
416         
417         if((flag & EM_IGNORE_LAYER)==0) {
418                 base= CTX_data_active_base(C); /* active layer checked here for view3d */
419
420                 if(base==NULL) return;
421                 else if(v3d && (base->lay & v3d->lay)==0) return;
422         else if(!v3d && (base->lay & scene->lay)==0) return;
423         }
424         else {
425                 base= scene->basact;
426         }
427
428         if (ELEM3(NULL, base, base->object, base->object->data)) return;
429
430         ob = base->object;
431         
432         if (object_data_is_libdata(ob)) {
433                 error_libdata();
434                 return;
435         }
436         
437         if(flag & EM_WAITCURSOR) waitcursor(1);
438
439         ob->restore_mode = ob->mode;
440         ED_object_toggle_modes(C, ob->mode);
441         ob->mode= OB_MODE_EDIT;
442         
443         if(ob->type==OB_MESH) {
444                 Mesh *me= ob->data;
445                 
446                 if(me->pv) mesh_pmv_off(ob, me);
447                 ok= 1;
448                 scene->obedit= ob;      // context sees this
449                 
450                 EDBM_MakeEditBMesh(CTX_data_tool_settings(C), scene, ob);
451
452                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene);
453         }
454         else if (ob->type==OB_ARMATURE){
455                 bArmature *arm= base->object->data;
456                 if (!arm) return;
457                 /*
458                  * The function object_data_is_libdata make a problem here, the
459                  * check for ob->proxy return 0 and let blender enter to edit mode
460                  * this causa a crash when you try leave the edit mode.
461                  * The problem is that i can't remove the ob->proxy check from
462                  * object_data_is_libdata that prevent the bugfix #6614, so
463                  * i add this little hack here.
464                  */
465                 if(arm->id.lib) {
466                         error_libdata();
467                         return;
468                 }
469                 ok=1;
470                 scene->obedit= ob;
471                 ED_armature_to_edit(ob);
472                 /* to ensure all goes in restposition and without striding */
473                 DAG_id_flush_update(&ob->id, OB_RECALC_ALL); // XXX: should this be OB_RECALC_DATA?
474
475                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene);
476         }
477         else if(ob->type==OB_FONT) {
478                 scene->obedit= ob; // XXX for context
479                 ok= 1;
480                  make_editText(ob);
481
482                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene);
483         }
484         else if(ob->type==OB_MBALL) {
485                 scene->obedit= ob; // XXX for context
486                 ok= 1;
487                 make_editMball(ob);
488
489                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene);
490         }
491         else if(ob->type==OB_LATTICE) {
492                 scene->obedit= ob; // XXX for context
493                 ok= 1;
494                 make_editLatt(ob);
495                 
496                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene);
497         }
498         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
499                 ok= 1;
500                 scene->obedit= ob; // XXX for context
501                 make_editNurb(ob);
502                 
503                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene);
504         }
505         
506         if(ok) {
507                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
508         }
509         else {
510                 scene->obedit= NULL; // XXX for context
511                 ob->mode &= ~OB_MODE_EDIT;
512                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
513         }
514         
515         if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
516         if(flag & EM_WAITCURSOR) waitcursor(0);
517 }
518
519 static int editmode_toggle_exec(bContext *C, wmOperator *op)
520 {
521         if(!CTX_data_edit_object(C))
522                 ED_object_enter_editmode(C, EM_WAITCURSOR);
523         else
524                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
525         
526         return OPERATOR_FINISHED;
527 }
528
529 static int editmode_toggle_poll(bContext *C)
530 {
531         Object *ob = CTX_data_active_object(C);
532
533         /* covers proxies too */
534         if(ELEM(NULL, ob, ob->data) || ((ID *)ob->data)->lib)
535                 return 0;
536
537         return ob && (ob->type == OB_MESH || ob->type == OB_ARMATURE ||
538                           ob->type == OB_FONT || ob->type == OB_MBALL ||
539                           ob->type == OB_LATTICE || ob->type == OB_SURF ||
540                           ob->type == OB_CURVE);
541 }
542
543 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
544 {
545         
546         /* identifiers */
547         ot->name= "Toggle Editmode";
548         ot->description = "Toggle object's editmode";
549         ot->idname= "OBJECT_OT_editmode_toggle";
550         
551         /* api callbacks */
552         ot->exec= editmode_toggle_exec;
553         
554         ot->poll= editmode_toggle_poll;
555         
556         /* flags */
557         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
558 }
559
560 /* *************************** */
561
562 static int posemode_exec(bContext *C, wmOperator *op)
563 {
564         Base *base= CTX_data_active_base(C);
565         
566         if(base->object->type==OB_ARMATURE) {
567                 if(base->object==CTX_data_edit_object(C)) {
568                         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
569                         ED_armature_enter_posemode(C, base);
570                 }
571                 else if(base->object->mode & OB_MODE_POSE)
572                         ED_armature_exit_posemode(C, base);
573                 else
574                         ED_armature_enter_posemode(C, base);
575                 
576                 return OPERATOR_FINISHED;
577         }
578         
579         return OPERATOR_PASS_THROUGH;
580 }
581
582 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
583 {
584         /* identifiers */
585         ot->name= "Toggle Pose Mode";
586         ot->idname= "OBJECT_OT_posemode_toggle";
587         ot->description= "Enables or disables posing/selecting bones";
588         
589         /* api callbacks */
590         ot->exec= posemode_exec;
591         ot->poll= ED_operator_object_active_editable;
592         
593         /* flag */
594         ot->flag= OPTYPE_REGISTER;
595 }
596
597 /* *********************** */
598
599 void check_editmode(int type)
600 {
601         Object *obedit= NULL; // XXX
602         
603         if (obedit==NULL || obedit->type==type) return;
604
605 // XXX  ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
606 }
607
608 #if 0
609 // XXX should be in view3d?
610
611 /* context: ob = lamp */
612 /* code should be replaced with proper (custom) transform handles for lamp properties */
613 static void spot_interactive(Object *ob, int mode)
614 {
615         Lamp *la= ob->data;
616         float transfac, dx, dy, ratio, origval;
617         int keep_running= 1, center2d[2];
618         short mval[2], mvalo[2];
619         
620 //      getmouseco_areawin(mval);
621 //      getmouseco_areawin(mvalo);
622         
623         project_int(ob->obmat[3], center2d);
624         if( center2d[0] > 100000 ) {            /* behind camera */
625 //              center2d[0]= curarea->winx/2;
626 //              center2d[1]= curarea->winy/2;
627         }
628
629 //      helpline(mval, center2d);
630         
631         /* ratio is like scaling */
632         dx = (float)(center2d[0] - mval[0]);
633         dy = (float)(center2d[1] - mval[1]);
634         transfac = (float)sqrt( dx*dx + dy*dy);
635         if(transfac==0.0f) transfac= 1.0f;
636         
637         if(mode==1)     
638                 origval= la->spotsize;
639         else if(mode==2)        
640                 origval= la->dist;
641         else if(mode==3)        
642                 origval= la->clipsta;
643         else    
644                 origval= la->clipend;
645         
646         while (keep_running>0) {
647                 
648 //              getmouseco_areawin(mval);
649                 
650                 /* essential for idling subloop */
651                 if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) {
652                         PIL_sleep_ms(2);
653                 }
654                 else {
655                         char str[32];
656                         
657                         dx = (float)(center2d[0] - mval[0]);
658                         dy = (float)(center2d[1] - mval[1]);
659                         ratio = (float)(sqrt( dx*dx + dy*dy))/transfac;
660                         
661                         /* do the trick */
662                         
663                         if(mode==1) {   /* spot */
664                                 la->spotsize = ratio*origval;
665                                 CLAMP(la->spotsize, 1.0f, 180.0f);
666                                 sprintf(str, "Spot size %.2f\n", la->spotsize);
667                         }
668                         else if(mode==2) {      /* dist */
669                                 la->dist = ratio*origval;
670                                 CLAMP(la->dist, 0.01f, 5000.0f);
671                                 sprintf(str, "Distance %.2f\n", la->dist);
672                         }
673                         else if(mode==3) {      /* sta */
674                                 la->clipsta = ratio*origval;
675                                 CLAMP(la->clipsta, 0.001f, 5000.0f);
676                                 sprintf(str, "Distance %.2f\n", la->clipsta);
677                         }
678                         else if(mode==4) {      /* end */
679                                 la->clipend = ratio*origval;
680                                 CLAMP(la->clipend, 0.1f, 5000.0f);
681                                 sprintf(str, "Clip End %.2f\n", la->clipend);
682                         }
683
684                         /* cleanup */
685                         mvalo[0]= mval[0];
686                         mvalo[1]= mval[1];
687                         
688                         /* handle shaded mode */
689 // XXX                  shade_buttons_change_3d();
690
691                         /* DRAW */      
692                         headerprint(str);
693                         force_draw_plus(SPACE_BUTS, 0);
694
695 //                      helpline(mval, center2d);
696                 }
697                 
698                 while( qtest() ) {
699                         short val;
700                         unsigned short event= extern_qread(&val);
701                         
702                         switch (event){
703                                 case ESCKEY:
704                                 case RIGHTMOUSE:
705                                         keep_running= 0;
706                                         break;
707                                 case LEFTMOUSE:
708                                 case SPACEKEY:
709                                 case PADENTER:
710                                 case RETKEY:
711                                         if(val)
712                                                 keep_running= -1;
713                                         break;
714                         }
715                 }
716         }
717
718         if(keep_running==0) {
719                 if(mode==1)     
720                         la->spotsize= origval;
721                 else if(mode==2)        
722                         la->dist= origval;
723                 else if(mode==3)        
724                         la->clipsta= origval;
725                 else    
726                         la->clipend= origval;
727         }
728
729 }
730 #endif
731
732 void special_editmenu(Scene *scene, View3D *v3d)
733 {
734 // XXX  static short numcuts= 2;
735         Object *ob= OBACT;
736         Object *obedit= NULL; // XXX
737         int nr,ret=0;
738         
739         if(ob==NULL) return;
740         
741         if(obedit==NULL) {
742                 
743                 if(ob->mode & OB_MODE_POSE) {
744 // XXX                  pose_special_editmenu();
745                 }
746                 else if(paint_facesel_test(ob)) {
747                         Mesh *me= get_mesh(ob);
748                         MTFace *tface;
749                         MFace *mface;
750                         int a;
751                         
752                         if(me==0 || me->mtface==0) return;
753                         
754                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|         TwoSide%x6|Clr     Tex%x7|         Shared%x8|         Light%x9|         Invisible%x10|         Collision%x11|         TwoSide%x12");
755                         
756                         tface= me->mtface;
757                         mface= me->mface;
758                         for(a=me->totface; a>0; a--, tface++, mface++) {
759                                 if(mface->flag & ME_FACE_SEL) {
760                                         switch(nr) {
761                                         case 1:
762                                                 tface->mode |= TF_TEX; break;
763                                         case 2:
764                                                 tface->mode |= TF_SHAREDCOL; break;
765                                         case 3:
766                                                 tface->mode |= TF_LIGHT; break; 
767                                         case 4:
768                                                 tface->mode |= TF_INVISIBLE; break;
769                                         case 5:
770                                                 tface->mode |= TF_DYNAMIC; break;
771                                         case 6:
772                                                 tface->mode |= TF_TWOSIDE; break;
773                                         case 7:
774                                                 tface->mode &= ~TF_TEX;
775                                                 tface->tpage= 0;
776                                                 break;
777                                         case 8:
778                                                 tface->mode &= ~TF_SHAREDCOL; break;
779                                         case 9:
780                                                 tface->mode &= ~TF_LIGHT; break;
781                                         case 10:
782                                                 tface->mode &= ~TF_INVISIBLE; break;
783                                         case 11:
784                                                 tface->mode &= ~TF_DYNAMIC; break;
785                                         case 12:
786                                                 tface->mode &= ~TF_TWOSIDE; break;
787                                         }
788                                 }
789                         }
790                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
791                 }
792                 else if(ob->mode & OB_MODE_VERTEX_PAINT) {
793                         Mesh *me= get_mesh(ob);
794                         
795                         if(me==0 || (me->mcol==NULL && me->mtface==NULL) ) return;
796                         
797                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
798                         if(nr==1) {
799                                 
800 // XXX                          do_shared_vertexcol(me);
801                                 
802                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
803                         }
804                 }
805                 else if(ob->mode & OB_MODE_WEIGHT_PAINT) {
806                         Object *par= modifiers_isDeformedByArmature(ob);
807
808                         if(par && (par->mode & OB_MODE_POSE)) {
809                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
810
811 // XXX                          if(nr==1 || nr==2)
812 // XXX                                  pose_adds_vgroups(ob, (nr == 2));
813                         }
814                 }
815                 else if(ob->mode & OB_MODE_PARTICLE_EDIT) {
816 #if 0
817                         // XXX
818                         ParticleSystem *psys = PE_get_current(ob);
819                         ParticleEditSettings *pset = PE_settings();
820
821                         if(!psys)
822                                 return;
823
824                         if(pset->selectmode & SCE_SELECT_POINT)
825                                 nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5");
826                         else
827                                 nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5");
828                         
829                         switch(nr) {
830                         case 1:
831 // XXX                          if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return;
832                                 waitcursor(1);
833                                 PE_rekey();
834                                 break;
835                         case 2:
836                                 PE_subdivide();
837                                 break;
838                         case 3:
839                                 PE_select_root();
840                                 break;
841                         case 4:
842                                 PE_select_tip();
843                                 break;
844                         case 5:
845                                 PE_remove_doubles();
846                                 break;
847                         }
848                         
849                         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
850                         
851                         if(nr>0) waitcursor(0);
852 #endif
853                 }
854                 else {
855                         Base *base, *base_select= NULL;
856                         
857                         /* Get the active object mesh. */
858                         Mesh *me= get_mesh(ob);
859
860                         /* Booleans, if the active object is a mesh... */
861                         if (me && ob->id.lib==NULL) {
862                                 
863                                 /* Bring up a little menu with the boolean operation choices on. */
864                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
865
866                                 if (nr > 0) {
867                                         /* user has made a choice of a menu element.
868                                            All of the boolean functions require 2 mesh objects 
869                                            we search through the object list to find the other 
870                                            selected item and make sure it is distinct and a mesh. */
871
872                                         for(base= FIRSTBASE; base; base= base->next) {
873                                                 if(TESTBASELIB(v3d, base)) {
874                                                         if(base->object != ob) base_select= base;
875                                                 }
876                                         }
877
878                                         if (base_select) {
879                                                 if (get_mesh(base_select->object)) {
880                                                         if(nr <= 3){
881                                                                 waitcursor(1);
882 // XXX                                                          ret = NewBooleanMesh(BASACT,base_select,nr);
883                                                                 if (ret==0) {
884                                                                         error("An internal error occurred");
885                                                                 } else if(ret==-1) {
886                                                                         error("Selected meshes must have faces to perform boolean operations");
887                                                                 } else if (ret==-2) {
888                                                                         error("Both meshes must be a closed mesh");
889                                                                 }
890                                                                 waitcursor(0);
891                                                         } else {
892                                                                 BooleanModifierData *bmd = NULL;
893                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
894                                                                 BLI_addtail(&ob->modifiers, bmd);
895                                                                 modifier_unique_name(&ob->modifiers, (ModifierData*)bmd);
896                                                                 bmd->object = base_select->object;
897                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
898                                                                 switch(nr){
899                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
900                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
901                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
902                                                                 }
903 // XXX                                                          do_common_editbuts(B_CHANGEDEP);
904                                                         }                                                               
905                                                 } else {
906                                                         error("Please select 2 meshes");
907                                                 }
908                                         } else {
909                                                 error("Please select 2 meshes");
910                                         }
911                                 }
912
913                         }
914                         else if (ob->type == OB_FONT) {
915                                 /* removed until this gets a decent implementation (ton) */
916 /*                              nr= pupmenu("Split %t|Characters%x1");
917                                 if (nr > 0) {
918                                         switch(nr) {
919                                                 case 1: split_font();
920                                         }
921                                 }
922 */
923                         }                       
924                 }
925         }
926         else if(obedit->type==OB_MESH) {
927         }
928         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
929         }
930         else if(obedit->type==OB_ARMATURE) {
931                 nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6");
932 //              if(nr==1)
933 // XXX                  subdivide_armature(1);
934                 if(nr==2) {
935 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
936                         waitcursor(1);
937 // XXX                  subdivide_armature(numcuts);
938                 }
939 //              else if(nr==3)
940 // XXX                  armature_flip_names();
941                 else if(ELEM3(nr, 4, 5, 6)) {
942 // XXX                  armature_autoside_names(nr-4);
943                 }
944 //              else if(nr == 7)
945 // XXX                  switch_direction_armature();
946         }
947         else if(obedit->type==OB_LATTICE) {
948                 Lattice *lt= obedit->data;
949                 static float weight= 1.0f;
950                 { // XXX
951 // XXX          if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
952                         int a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
953                         BPoint *bp= lt->editlatt->def;
954                         
955                         while(a--) {
956                                 if(bp->f1 & SELECT)
957                                         bp->weight= weight;
958                                 bp++;
959                         }       
960                 }
961         }
962
963 }
964
965 /* Change subdivision or particle properties of mesh object ob, if level==-1
966  * then toggle subsurf, else set to level set allows to toggle multiple
967  * selections */
968
969 static void object_has_subdivision_particles(Object *ob, int *havesubdiv, int *havepart, int depth)
970 {
971         if(ob->type==OB_MESH) {
972                 if(modifiers_findByType(ob, eModifierType_Subsurf))
973                         *havesubdiv= 1;
974                 if(modifiers_findByType(ob, eModifierType_ParticleSystem))
975                         *havepart= 1;
976         }
977
978         if(ob->dup_group && depth <= 4) {
979                 GroupObject *go;
980
981                 for(go= ob->dup_group->gobject.first; go; go= go->next)
982                         object_has_subdivision_particles(go->ob, havesubdiv, havepart, depth+1);
983         }
984 }
985
986 static void object_flip_subdivison_particles(Scene *scene, Object *ob, int *set, int level, int mode, int particles, int depth)
987 {
988         ModifierData *md;
989
990         if(ob->type==OB_MESH) {
991                 if(particles) {
992                         for(md=ob->modifiers.first; md; md=md->next) {
993                                 if(md->type == eModifierType_ParticleSystem) {
994                                         ParticleSystemModifierData *psmd = (ParticleSystemModifierData*)md;
995
996                                         if(*set == -1)
997                                                 *set= psmd->modifier.mode&(mode);
998
999                                         if (*set)
1000                                                 psmd->modifier.mode &= ~(mode);
1001                                         else
1002                                                 psmd->modifier.mode |= (mode);
1003                                 }
1004                         }
1005                 }
1006                 else {
1007                         md = modifiers_findByType(ob, eModifierType_Subsurf);
1008
1009                         if (md) {
1010                                 SubsurfModifierData *smd = (SubsurfModifierData*) md;
1011
1012                                 if (level == -1) {
1013                                         if(*set == -1) 
1014                                                 *set= smd->modifier.mode&(mode);
1015
1016                                         if (*set)
1017                                                 smd->modifier.mode &= ~(mode);
1018                                         else
1019                                                 smd->modifier.mode |= (mode);
1020                                 } else {
1021                                         smd->levels = level;
1022                                 }
1023                         } 
1024                         else if(depth == 0 && *set != 0) {
1025                                 SubsurfModifierData *smd = (SubsurfModifierData*) modifier_new(eModifierType_Subsurf);
1026                                 
1027                                 BLI_addtail(&ob->modifiers, smd);
1028                                 modifier_unique_name(&ob->modifiers, (ModifierData*)smd);
1029                                 
1030                                 if (level!=-1) {
1031                                         smd->levels = level;
1032                                 }
1033                                 
1034                                 if(*set == -1)
1035                                         *set= 1;
1036                         }
1037                 }
1038
1039                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1040         }
1041
1042         if(ob->dup_group && depth<=4) {
1043                 GroupObject *go;
1044
1045                 for(go= ob->dup_group->gobject.first; go; go= go->next)
1046                         object_flip_subdivison_particles(scene, go->ob, set, level, mode, particles, depth+1);
1047         }
1048 }
1049
1050 /* Change subdivision properties of mesh object ob, if
1051 * level==-1 then toggle subsurf, else set to level.
1052 */
1053
1054 void flip_subdivison(Scene *scene, View3D *v3d, int level)
1055 {
1056         Base *base;
1057         int set= -1;
1058         int mode, pupmode, particles= 0, havesubdiv= 0, havepart= 0;
1059         int alt= 0; // XXX
1060         
1061         if(alt)
1062                 mode= eModifierMode_Realtime;
1063         else
1064                 mode= eModifierMode_Render|eModifierMode_Realtime;
1065         
1066         if(level == -1) {
1067                 if (scene->obedit) { // XXX get from context
1068                         object_has_subdivision_particles(scene->obedit, &havesubdiv, &havepart, 0);                     
1069                 } else {
1070                         for(base= scene->base.first; base; base= base->next) {
1071                                 if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) {
1072                                         object_has_subdivision_particles(base->object, &havesubdiv, &havepart, 0);
1073                                 }
1074                         }
1075                 }
1076         }
1077         else
1078                 havesubdiv= 1;
1079         
1080         if(havesubdiv && havepart) {
1081                 pupmode= pupmenu("Switch%t|Subsurf %x1|Particle Systems %x2");
1082                 if(pupmode <= 0)
1083                         return;
1084                 else if(pupmode == 2)
1085                         particles= 1;
1086         }
1087         else if(havepart)
1088                 particles= 1;
1089
1090         if (scene->obedit) {     // XXX get from context
1091                 object_flip_subdivison_particles(scene, scene->obedit, &set, level, mode, particles, 0);
1092         } else {
1093                 for(base= scene->base.first; base; base= base->next) {
1094                         if(((level==-1) && (TESTBASE(v3d, base))) || (TESTBASELIB(v3d, base))) {
1095                                 object_flip_subdivison_particles(scene, base->object, &set, level, mode, particles, 0);
1096                         }
1097                 }
1098         }
1099         
1100         DAG_ids_flush_update(0);
1101 }
1102  
1103 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
1104 {       
1105 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
1106         bProperty *prop;
1107         Base *base;
1108         int nr, tot=0;
1109         char *str;
1110         
1111         prop= ob->prop.first;
1112         while(prop) {
1113                 tot++;
1114                 prop= prop->next;
1115         }
1116         
1117         str= MEM_callocN(50 + 33*tot, "copymenu prop");
1118         
1119         if (tot)
1120                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
1121         else
1122                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
1123         
1124         tot= 0; 
1125         prop= ob->prop.first;
1126         while(prop) {
1127                 tot++;
1128                 strcat(str, "|");
1129                 strcat(str, prop->name);
1130                 prop= prop->next;
1131         }
1132
1133         nr= pupmenu(str);
1134         
1135         if ( nr==1 || nr==2 ) {
1136                 for(base= FIRSTBASE; base; base= base->next) {
1137                         if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
1138                                 if (nr==1) { /* replace */
1139                                         copy_properties( &base->object->prop, &ob->prop );
1140                                 } else {
1141                                         for(prop = ob->prop.first; prop; prop= prop->next ) {
1142                                                 set_ob_property(base->object, prop);
1143                                         }
1144                                 }
1145                         }
1146                 }
1147         } else if(nr>0) {
1148                 prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/
1149                 
1150                 if(prop) {
1151                         for(base= FIRSTBASE; base; base= base->next) {
1152                                 if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
1153                                         set_ob_property(base->object, prop);
1154                                 }
1155                         }
1156                 }
1157         }
1158         MEM_freeN(str);
1159         
1160 }
1161
1162 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
1163 {
1164 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
1165         Base *base;
1166         
1167         for(base= FIRSTBASE; base; base= base->next) {
1168                 if(base->object != ob) {
1169                         if(TESTBASELIB(v3d, base)) {
1170                                 
1171                                 /* first: free all logic */
1172                                 free_sensors(&base->object->sensors);                           
1173                                 unlink_controllers(&base->object->controllers);
1174                                 free_controllers(&base->object->controllers);
1175                                 unlink_actuators(&base->object->actuators);
1176                                 free_actuators(&base->object->actuators);
1177                                 
1178                                 /* now copy it, this also works without logicbricks! */
1179                                 clear_sca_new_poins_ob(ob);
1180                                 copy_sensors(&base->object->sensors, &ob->sensors);
1181                                 copy_controllers(&base->object->controllers, &ob->controllers);
1182                                 copy_actuators(&base->object->actuators, &ob->actuators);
1183                                 set_sca_new_poins_ob(base->object);
1184                                 
1185                                 /* some menu settings */
1186                                 base->object->scavisflag= ob->scavisflag;
1187                                 base->object->scaflag= ob->scaflag;
1188                                 
1189                                 /* set the initial state */
1190                                 base->object->state= ob->state;
1191                                 base->object->init_state= ob->init_state;
1192                         }
1193                 }
1194         }
1195 }
1196
1197 static void copymenu_modifiers(Scene *scene, View3D *v3d, Object *ob)
1198 {
1199         Base *base;
1200         int i, event;
1201         char str[512];
1202         char *errorstr= NULL;
1203
1204         strcpy(str, "Copy Modifiers %t");
1205
1206         sprintf(str+strlen(str), "|All%%x%d|%%l", NUM_MODIFIER_TYPES);
1207
1208         for (i=eModifierType_None+1; i<NUM_MODIFIER_TYPES; i++) {
1209                 ModifierTypeInfo *mti = modifierType_getInfo(i);
1210
1211                 if(ELEM3(i, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue;
1212                 
1213                 if(i == eModifierType_Collision)
1214                         continue;
1215
1216                 if (    (mti->flags&eModifierTypeFlag_AcceptsCVs) || 
1217                                 (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) {
1218                         sprintf(str+strlen(str), "|%s%%x%d", mti->name, i);
1219                 }
1220         }
1221
1222         event = pupmenu(str);
1223         if(event<=0) return;
1224
1225         for (base= FIRSTBASE; base; base= base->next) {
1226                 if(base->object != ob) {
1227                         if(TESTBASELIB(v3d, base)) {
1228                                 ModifierData *md;
1229
1230                                 base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
1231
1232                                 if (base->object->type==ob->type) {
1233                                         /* copy all */
1234                                         if (event==NUM_MODIFIER_TYPES) {
1235                                                 object_free_modifiers(base->object);
1236
1237                                                 for (md=ob->modifiers.first; md; md=md->next) {
1238                                                         ModifierData *nmd = NULL;
1239                                                         
1240                                                         if(ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue;
1241                 
1242                                                         if(md->type == eModifierType_Collision)
1243                                                                 continue;
1244                                                         
1245                                                         nmd = modifier_new(md->type);
1246                                                         modifier_copyData(md, nmd);
1247                                                         BLI_addtail(&base->object->modifiers, nmd);
1248                                                         modifier_unique_name(&base->object->modifiers, nmd);
1249                                                 }
1250
1251                                                 copy_object_particlesystems(base->object, ob);
1252                                                 copy_object_softbody(base->object, ob);
1253                                         } else {
1254                                                 /* copy specific types */
1255                                                 ModifierData *md, *mdn;
1256                                                 
1257                                                 /* remove all with type 'event' */
1258                                                 for (md=base->object->modifiers.first; md; md=mdn) {
1259                                                         mdn= md->next;
1260                                                         if(md->type==event) {
1261                                                                 BLI_remlink(&base->object->modifiers, md);
1262                                                                 modifier_free(md);
1263                                                         }
1264                                                 }
1265                                                 
1266                                                 /* copy all with type 'event' */
1267                                                 for (md=ob->modifiers.first; md; md=md->next) {
1268                                                         if (md->type==event) {
1269                                                                 
1270                                                                 mdn = modifier_new(event);
1271                                                                 BLI_addtail(&base->object->modifiers, mdn);
1272                                                                 modifier_unique_name(&base->object->modifiers, mdn);
1273
1274                                                                 modifier_copyData(md, mdn);
1275                                                         }
1276                                                 }
1277
1278                                                 if(event == eModifierType_ParticleSystem) {
1279                                                         object_free_particlesystems(base->object);
1280                                                         copy_object_particlesystems(base->object, ob);
1281                                                 }
1282                                                 else if(event == eModifierType_Softbody) {
1283                                                         object_free_softbody(base->object);
1284                                                         copy_object_softbody(base->object, ob);
1285                                                 }
1286                                         }
1287                                 }
1288                                 else
1289                                         errorstr= "Did not copy modifiers to other Object types";
1290                         }
1291                 }
1292         }
1293         
1294 //      if(errorstr) notice(errorstr);
1295         
1296         DAG_scene_sort(scene);
1297         
1298 }
1299
1300 /* both pointers should exist */
1301 static void copy_texture_space(Object *to, Object *ob)
1302 {
1303         float *poin1= NULL, *poin2= NULL;
1304         short texflag= 0;
1305         
1306         if(ob->type==OB_MESH) {
1307                 texflag= ((Mesh *)ob->data)->texflag;
1308                 poin2= ((Mesh *)ob->data)->loc;
1309         }
1310         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1311                 texflag= ((Curve *)ob->data)->texflag;
1312                 poin2= ((Curve *)ob->data)->loc;
1313         }
1314         else if(ob->type==OB_MBALL) {
1315                 texflag= ((MetaBall *)ob->data)->texflag;
1316                 poin2= ((MetaBall *)ob->data)->loc;
1317         }
1318         else
1319                 return;
1320                 
1321         if(to->type==OB_MESH) {
1322                 ((Mesh *)to->data)->texflag= texflag;
1323                 poin1= ((Mesh *)to->data)->loc;
1324         }
1325         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
1326                 ((Curve *)to->data)->texflag= texflag;
1327                 poin1= ((Curve *)to->data)->loc;
1328         }
1329         else if(to->type==OB_MBALL) {
1330                 ((MetaBall *)to->data)->texflag= texflag;
1331                 poin1= ((MetaBall *)to->data)->loc;
1332         }
1333         else
1334                 return;
1335         
1336         memcpy(poin1, poin2, 9*sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
1337         
1338         if(to->type==OB_MESH) ;
1339         else if(to->type==OB_MBALL) tex_space_mball(to);
1340         else tex_space_curve(to->data);
1341         
1342 }
1343
1344 void copy_attr(Scene *scene, View3D *v3d, short event)
1345 {
1346         Object *ob;
1347         Base *base;
1348         Curve *cu, *cu1;
1349         Nurb *nu;
1350         int do_scene_sort= 0;
1351         
1352         if(scene->id.lib) return;
1353
1354         if(!(ob=OBACT)) return;
1355         
1356         if(scene->obedit) { // XXX get from context
1357                 /* obedit_copymenu(); */
1358                 return;
1359         }
1360         if(event==9) {
1361                 copymenu_properties(scene, v3d, ob);
1362                 return;
1363         }
1364         else if(event==10) {
1365                 copymenu_logicbricks(scene, v3d, ob);
1366                 return;
1367         }
1368         else if(event==24) {
1369                 copymenu_modifiers(scene, v3d, ob);
1370                 return;
1371         }
1372
1373         for(base= FIRSTBASE; base; base= base->next) {
1374                 if(base != BASACT) {
1375                         if(TESTBASELIB(v3d, base)) {
1376                                 base->object->recalc |= OB_RECALC_OB;
1377                                 
1378                                 if(event==1) {  /* loc */
1379                                         VECCOPY(base->object->loc, ob->loc);
1380                                         VECCOPY(base->object->dloc, ob->dloc);
1381                                 }
1382                                 else if(event==2) {  /* rot */
1383                                         VECCOPY(base->object->rot, ob->rot);
1384                                         VECCOPY(base->object->drot, ob->drot);
1385                                         /* Quats arnt used yet */
1386                                         /*VECCOPY(base->object->quat, ob->quat);
1387                                         VECCOPY(base->object->dquat, ob->dquat);*/
1388                                 }
1389                                 else if(event==3) {  /* size */
1390                                         VECCOPY(base->object->size, ob->size);
1391                                         VECCOPY(base->object->dsize, ob->dsize);
1392                                 }
1393                                 else if(event==4) {  /* drawtype */
1394                                         base->object->dt= ob->dt;
1395                                         base->object->dtx= ob->dtx;
1396                                         base->object->empty_drawtype= ob->empty_drawtype;
1397                                         base->object->empty_drawsize= ob->empty_drawsize;
1398                                 }
1399                                 else if(event==5) {  /* time offs */
1400                                         base->object->sf= ob->sf;
1401                                 }
1402                                 else if(event==6) {  /* dupli */
1403                                         base->object->dupon= ob->dupon;
1404                                         base->object->dupoff= ob->dupoff;
1405                                         base->object->dupsta= ob->dupsta;
1406                                         base->object->dupend= ob->dupend;
1407                                         
1408                                         base->object->transflag &= ~OB_DUPLI;
1409                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
1410
1411                                         base->object->dup_group= ob->dup_group;
1412                                         if(ob->dup_group)
1413                                                 id_lib_extern(&ob->dup_group->id);
1414                                 }
1415                                 else if(event==7) {     /* mass */
1416                                         base->object->mass= ob->mass;
1417                                 }
1418                                 else if(event==8) {     /* damping */
1419                                         base->object->damping= ob->damping;
1420                                         base->object->rdamping= ob->rdamping;
1421                                 }
1422                                 else if(event==11) {    /* all physical attributes */
1423                                         base->object->gameflag = ob->gameflag;
1424                                         base->object->inertia = ob->inertia;
1425                                         base->object->formfactor = ob->formfactor;
1426                                         base->object->damping= ob->damping;
1427                                         base->object->rdamping= ob->rdamping;
1428                                         base->object->min_vel= ob->min_vel;
1429                                         base->object->max_vel= ob->max_vel;
1430                                         if (ob->gameflag & OB_BOUNDS) {
1431                                                 base->object->boundtype = ob->boundtype;
1432                                         }
1433                                         base->object->margin= ob->margin;
1434                                         base->object->bsoft= copy_bulletsoftbody(ob->bsoft);
1435
1436                                 }
1437                                 else if(event==17) {    /* tex space */
1438                                         copy_texture_space(base->object, ob);
1439                                 }
1440                                 else if(event==18) {    /* font settings */
1441                                         
1442                                         if(base->object->type==ob->type) {
1443                                                 cu= ob->data;
1444                                                 cu1= base->object->data;
1445                                                 
1446                                                 cu1->spacemode= cu->spacemode;
1447                                                 cu1->spacing= cu->spacing;
1448                                                 cu1->linedist= cu->linedist;
1449                                                 cu1->shear= cu->shear;
1450                                                 cu1->fsize= cu->fsize;
1451                                                 cu1->xof= cu->xof;
1452                                                 cu1->yof= cu->yof;
1453                                                 cu1->textoncurve= cu->textoncurve;
1454                                                 cu1->wordspace= cu->wordspace;
1455                                                 cu1->ulpos= cu->ulpos;
1456                                                 cu1->ulheight= cu->ulheight;
1457                                                 if(cu1->vfont) cu1->vfont->id.us--;
1458                                                 cu1->vfont= cu->vfont;
1459                                                 id_us_plus((ID *)cu1->vfont);
1460                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
1461                                                 cu1->vfontb= cu->vfontb;
1462                                                 id_us_plus((ID *)cu1->vfontb);
1463                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
1464                                                 cu1->vfonti= cu->vfonti;
1465                                                 id_us_plus((ID *)cu1->vfonti);
1466                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
1467                                                 cu1->vfontbi= cu->vfontbi;
1468                                                 id_us_plus((ID *)cu1->vfontbi);                                         
1469
1470                                                 BKE_text_to_curve(scene, base->object, 0);              /* needed? */
1471
1472                                                 
1473                                                 strcpy(cu1->family, cu->family);
1474                                                 
1475                                                 base->object->recalc |= OB_RECALC_DATA;
1476                                         }
1477                                 }
1478                                 else if(event==19) {    /* bevel settings */
1479                                         
1480                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1481                                                 cu= ob->data;
1482                                                 cu1= base->object->data;
1483                                                 
1484                                                 cu1->bevobj= cu->bevobj;
1485                                                 cu1->taperobj= cu->taperobj;
1486                                                 cu1->width= cu->width;
1487                                                 cu1->bevresol= cu->bevresol;
1488                                                 cu1->ext1= cu->ext1;
1489                                                 cu1->ext2= cu->ext2;
1490                                                 
1491                                                 base->object->recalc |= OB_RECALC_DATA;
1492                                         }
1493                                 }
1494                                 else if(event==25) {    /* curve resolution */
1495
1496                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1497                                                 cu= ob->data;
1498                                                 cu1= base->object->data;
1499                                                 
1500                                                 cu1->resolu= cu->resolu;
1501                                                 cu1->resolu_ren= cu->resolu_ren;
1502                                                 
1503                                                 nu= cu1->nurb.first;
1504                                                 
1505                                                 while(nu) {
1506                                                         nu->resolu= cu1->resolu;
1507                                                         nu= nu->next;
1508                                                 }
1509                                                 
1510                                                 base->object->recalc |= OB_RECALC_DATA;
1511                                         }
1512                                 }
1513                                 else if(event==21){
1514                                         if (base->object->type==OB_MESH) {
1515                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
1516
1517                                                 if (md) {
1518                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
1519
1520                                                         if (!tmd) {
1521                                                                 tmd = modifier_new(eModifierType_Subsurf);
1522                                                                 BLI_addtail(&base->object->modifiers, tmd);
1523                                                         }
1524
1525                                                         modifier_copyData(md, tmd);
1526                                                         base->object->recalc |= OB_RECALC_DATA;
1527                                                 }
1528                                         }
1529                                 }
1530                                 else if(event==22) {
1531                                         /* Copy the constraint channels over */
1532                                         copy_constraints(&base->object->constraints, &ob->constraints, TRUE);
1533                                         
1534                                         do_scene_sort= 1;
1535                                 }
1536                                 else if(event==23) {
1537                                         base->object->softflag= ob->softflag;
1538                                         if(base->object->soft) sbFree(base->object->soft);
1539                                         
1540                                         base->object->soft= copy_softbody(ob->soft);
1541
1542                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1543                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1544                                         }
1545                                 }
1546                                 else if(event==26) {
1547 #if 0 // XXX old animation system
1548                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
1549 #endif // XXX old animation system
1550                                 }
1551                                 else if(event==27) {    /* autosmooth */
1552                                         if (base->object->type==OB_MESH) {
1553                                                 Mesh *me= ob->data;
1554                                                 Mesh *cme= base->object->data;
1555                                                 cme->smoothresh= me->smoothresh;
1556                                                 if(me->flag & ME_AUTOSMOOTH)
1557                                                         cme->flag |= ME_AUTOSMOOTH;
1558                                                 else
1559                                                         cme->flag &= ~ME_AUTOSMOOTH;
1560                                         }
1561                                 }
1562                                 else if(event==28) { /* UV orco */
1563                                         if(ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1564                                                 cu= ob->data;
1565                                                 cu1= base->object->data;
1566                                                 
1567                                                 if(cu->flag & CU_UV_ORCO)
1568                                                         cu1->flag |= CU_UV_ORCO;
1569                                                 else
1570                                                         cu1->flag &= ~CU_UV_ORCO;
1571                                         }               
1572                                 }
1573                                 else if(event==29) { /* protected bits */
1574                                         base->object->protectflag= ob->protectflag;
1575                                 }
1576                                 else if(event==30) { /* index object */
1577                                         base->object->index= ob->index;
1578                                 }
1579                                 else if(event==31) { /* object color */
1580                                         QUATCOPY(base->object->col, ob->col);
1581                                 }
1582                         }
1583                 }
1584         }
1585         
1586         if(do_scene_sort)
1587                 DAG_scene_sort(scene);
1588
1589         DAG_ids_flush_update(0);
1590 }
1591
1592 void copy_attr_menu(Scene *scene, View3D *v3d)
1593 {
1594         Object *ob;
1595         short event;
1596         char str[512];
1597         
1598         if(!(ob=OBACT)) return;
1599         
1600         if (scene->obedit) { // XXX get from context
1601 //              if (ob->type == OB_MESH)
1602 // XXX                  mesh_copy_menu();
1603                 return;
1604         }
1605         
1606         /* Object Mode */
1607         
1608         /* If you change this menu, don't forget to update the menu in header_view3d.c
1609          * view3d_edit_object_copyattrmenu() and in toolbox.c
1610          */
1611         
1612         strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Draw Options%x4|Time Offset%x5|Dupli%x6|Object Color%x31|%l|Mass%x7|Damping%x8|All Physical Attributes%x11|Properties%x9|Logic Bricks%x10|Protected Transform%x29|%l");
1613         
1614         strcat (str, "|Object Constraints%x22");
1615         strcat (str, "|NLA Strips%x26");
1616         
1617 // XXX  if (OB_SUPPORT_MATERIAL(ob)) {
1618 //              strcat(str, "|Texture Space%x17");
1619 //      }       
1620         
1621         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1622         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
1623         
1624         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1625                         strcat(str, "|Curve Resolution%x25");
1626         }
1627
1628         if(ob->type==OB_MESH){
1629                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
1630         }
1631
1632         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
1633         
1634         strcat(str, "|Pass Index%x30");
1635         
1636         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
1637                 strcat(str, "|Modifiers ...%x24");
1638         }
1639
1640         event= pupmenu(str);
1641         if(event<= 0) return;
1642         
1643         copy_attr(scene, v3d, event);
1644 }
1645
1646 /* ********************************************** */
1647 /* Motion Paths */
1648
1649 /* For the object with pose/action: update paths for those that have got them
1650  * This should selectively update paths that exist...
1651  *
1652  * To be called from various tools that do incremental updates 
1653  */
1654 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1655 {
1656         ListBase targets = {NULL, NULL};
1657         
1658         /* loop over objects in scene */
1659         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1660         {
1661                 /* set flag to force recalc, then grab the relevant bones to target */
1662                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1663                 animviz_get_object_motionpaths(ob, &targets);
1664         }
1665         CTX_DATA_END;
1666         
1667         /* recalculate paths, then free */
1668         animviz_calc_motionpaths(scene, &targets);
1669         BLI_freelistN(&targets);
1670 }
1671
1672 /* For the object with pose/action: create path curves for selected bones 
1673  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1674  */
1675 static int object_calculate_paths_exec (bContext *C, wmOperator *op)
1676 {
1677         Scene *scene= CTX_data_scene(C);
1678         
1679         /* set up path data for bones being calculated */
1680         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
1681         {
1682                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1683                 animviz_verify_motionpaths(scene, ob, NULL);
1684         }
1685         CTX_DATA_END;
1686         
1687         /* calculate the bones that now have motionpaths... */
1688         // TODO: only make for the selected bones?
1689         ED_objects_recalculate_paths(C, scene);
1690         
1691         /* notifiers for updates */
1692         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1693         
1694         return OPERATOR_FINISHED; 
1695 }
1696
1697 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
1698 {
1699         /* identifiers */
1700         ot->name= "Calculate Object Paths";
1701         ot->idname= "OBJECT_OT_paths_calculate";
1702         ot->description= "Calculate paths for the selected bones";
1703         
1704         /* api callbacks */
1705         ot->exec= object_calculate_paths_exec;
1706         ot->poll= ED_operator_object_active_editable;
1707         
1708         /* flags */
1709         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1710 }
1711
1712 /* --------- */
1713
1714 /* for the object with pose/action: clear path curves for selected bones only */
1715 void ED_objects_clear_paths(bContext *C, Scene *scene)
1716 {
1717         /* loop over objects in scene */
1718         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1719         {
1720                 if (ob->mpath) {
1721                         animviz_free_motionpath(ob->mpath);
1722                         ob->mpath= NULL;
1723                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1724                 }
1725         }
1726         CTX_DATA_END;
1727 }
1728
1729 /* operator callback for this */
1730 static int object_clear_paths_exec (bContext *C, wmOperator *op)
1731 {
1732         Scene *scene= CTX_data_scene(C);
1733         
1734         /* use the backend function for this */
1735         ED_objects_clear_paths(C, scene);
1736         
1737         /* notifiers for updates */
1738         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1739         
1740         return OPERATOR_FINISHED; 
1741 }
1742
1743 void OBJECT_OT_paths_clear (wmOperatorType *ot)
1744 {
1745         /* identifiers */
1746         ot->name= "Clear Object Paths";
1747         ot->idname= "OBJECT_OT_paths_clear";
1748         ot->description= "Clear path caches for selected bones";
1749         
1750         /* api callbacks */
1751         ot->exec= object_clear_paths_exec;
1752         ot->poll= ED_operator_object_active_editable;
1753         
1754         /* flags */
1755         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1756 }
1757
1758
1759 /********************** Smooth/Flat *********************/
1760
1761 static int shade_smooth_exec(bContext *C, wmOperator *op)
1762 {
1763         Curve *cu;
1764         Nurb *nu;
1765         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1766         int done= 0;
1767
1768         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1769
1770                 if(ob->type==OB_MESH) {
1771                         mesh_set_smooth_flag(ob, !clear);
1772
1773                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1774                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1775
1776                         done= 1;
1777                 }
1778                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
1779                         cu= ob->data;
1780
1781                         for(nu=cu->nurb.first; nu; nu=nu->next) {
1782                                 if(!clear) nu->flag |= ME_SMOOTH;
1783                                 else nu->flag &= ~ME_SMOOTH;
1784                         }
1785
1786                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1787                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1788
1789                         done= 1;
1790                 }
1791         }
1792         CTX_DATA_END;
1793
1794         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
1795 }
1796
1797 static int shade_poll(bContext *C)
1798 {
1799         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
1800 }
1801
1802 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1803 {
1804         /* identifiers */
1805         ot->name= "Shade Flat";
1806         ot->idname= "OBJECT_OT_shade_flat";
1807         
1808         /* api callbacks */
1809         ot->poll= shade_poll;
1810         ot->exec= shade_smooth_exec;
1811
1812         /* flags */
1813         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1814 }
1815
1816 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1817 {
1818         /* identifiers */
1819         ot->name= "Shade Smooth";
1820         ot->idname= "OBJECT_OT_shade_smooth";
1821         
1822         /* api callbacks */
1823         ot->poll= shade_poll;
1824         ot->exec= shade_smooth_exec;
1825         
1826         /* flags */
1827         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1828 }
1829
1830 /* ********************** */
1831
1832 void image_aspect(Scene *scene, View3D *v3d)
1833 {
1834         /* all selected objects with an image map: scale in image aspect */
1835         Base *base;
1836         Object *ob;
1837         Material *ma;
1838         Tex *tex;
1839         float x, y, space;
1840         int a, b, done;
1841         
1842         if(scene->obedit) return; // XXX get from context
1843         if(scene->id.lib) return;
1844         
1845         for(base= FIRSTBASE; base; base= base->next) {
1846                 if(TESTBASELIB(v3d, base)) {
1847                         ob= base->object;
1848                         done= 0;
1849                         
1850                         for(a=1; a<=ob->totcol; a++) {
1851                                 ma= give_current_material(ob, a);
1852                                 if(ma) {
1853                                         for(b=0; b<MAX_MTEX; b++) {
1854                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1855                                                         tex= ma->mtex[b]->tex;
1856                                                         if(tex->type==TEX_IMAGE && tex->ima) {
1857                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
1858                                                                 
1859                                                                 /* texturespace */
1860                                                                 space= 1.0;
1861                                                                 if(ob->type==OB_MESH) {
1862                                                                         float size[3];
1863                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1864                                                                         space= size[0]/size[1];
1865                                                                 }
1866                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1867                                                                         Curve *cu= ob->data;
1868                                                                         space= cu->size[0]/cu->size[1];
1869                                                                 }
1870                                                         
1871                                                                 x= ibuf->x/space;
1872                                                                 y= ibuf->y;
1873                                                                 
1874                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
1875                                                                 else ob->size[1]= ob->size[0]*y/x;
1876                                                                 
1877                                                                 done= 1;
1878                                                                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);                                                             
1879                                                         }
1880                                                 }
1881                                                 if(done) break;
1882                                         }
1883                                 }
1884                                 if(done) break;
1885                         }
1886                 }
1887         }
1888         
1889 }
1890
1891 int vergbaseco(const void *a1, const void *a2)
1892 {
1893         Base **x1, **x2;
1894         
1895         x1= (Base **) a1;
1896         x2= (Base **) a2;
1897         
1898         if( (*x1)->sy > (*x2)->sy ) return 1;
1899         else if( (*x1)->sy < (*x2)->sy) return -1;
1900         else if( (*x1)->sx > (*x2)->sx ) return 1;
1901         else if( (*x1)->sx < (*x2)->sx ) return -1;
1902
1903         return 0;
1904 }
1905
1906
1907 void auto_timeoffs(Scene *scene, View3D *v3d)
1908 {
1909         Base *base, **basesort, **bs;
1910         float start, delta;
1911         int tot=0, a;
1912         short offset=25;
1913
1914         if(BASACT==0 || v3d==NULL) return;
1915 // XXX  if(button(&offset, 0, 1000,"Total time")==0) return;
1916
1917         /* make array of all bases, xco yco (screen) */
1918         for(base= FIRSTBASE; base; base= base->next) {
1919                 if(TESTBASELIB(v3d, base)) {
1920                         tot++;
1921                 }
1922         }
1923
1924         delta= (float)offset/(float)tot;
1925         start= OBACT->sf;
1926
1927         bs= basesort= MEM_mallocN(sizeof(void *)*tot,"autotimeoffs");
1928         for(base= FIRSTBASE; base; base= base->next) {
1929                 if(TESTBASELIB(v3d, base)) {
1930                         *bs= base;
1931                         bs++;
1932                 }
1933         }
1934         qsort(basesort, tot, sizeof(void *), vergbaseco);
1935
1936         bs= basesort;
1937         for(a=0; a<tot; a++) {
1938                 
1939                 (*bs)->object->sf= start;
1940                 start+= delta;
1941
1942                 bs++;
1943         }
1944         MEM_freeN(basesort);
1945
1946 }
1947
1948 void ofs_timeoffs(Scene *scene, View3D *v3d)
1949 {
1950         float offset=0.0f;
1951
1952         if(BASACT==0 || v3d==NULL) return;
1953         
1954 // XXX  if(fbutton(&offset, -10000.0f, 10000.0f, 10, 10, "Offset")==0) return;
1955
1956         /* make array of all bases, xco yco (screen) */
1957         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1958                 ob->sf += offset;
1959                 if (ob->sf < -MAXFRAMEF)                ob->sf = -MAXFRAMEF;
1960                 else if (ob->sf > MAXFRAMEF)    ob->sf = MAXFRAMEF;
1961                 }
1962         CTX_DATA_END;
1963
1964 }
1965
1966
1967 void rand_timeoffs(Scene *scene, View3D *v3d)
1968 {
1969         Base *base;
1970         float rand=0.0f;
1971
1972         if(BASACT==0 || v3d==NULL) return;
1973         
1974 // XXX  if(fbutton(&rand, 0.0f, 10000.0f, 10, 10, "Randomize")==0) return;
1975         
1976         rand *= 2;
1977         
1978         for(base= FIRSTBASE; base; base= base->next) {
1979                 if(TESTBASELIB(v3d, base)) {
1980                         base->object->sf += (BLI_drand()-0.5) * rand;
1981                         if (base->object->sf < -MAXFRAMEF)              base->object->sf = -MAXFRAMEF;
1982                         else if (base->object->sf > MAXFRAMEF)  base->object->sf = MAXFRAMEF;
1983                 }
1984         }
1985
1986 }
1987
1988 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *ptr, int *free)
1989 {       
1990         EnumPropertyItem *input = object_mode_items;
1991         EnumPropertyItem *item= NULL;
1992         Object *ob;
1993         int totitem= 0;
1994         
1995         if(!C) /* needed for docs */
1996                 return object_mode_items;
1997
1998         ob = CTX_data_active_object(C);
1999         while(ob && input->identifier) {
2000                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
2001                                                         (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
2002                                                          (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
2003                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
2004                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
2005                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
2006                          input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
2007                    (input->value == OB_MODE_OBJECT))
2008                         RNA_enum_item_add(&item, &totitem, input);
2009                 ++input;
2010         }
2011
2012         RNA_enum_item_end(&item, &totitem);
2013
2014         *free= 1;
2015
2016         return item;
2017 }
2018
2019 static const char *object_mode_op_string(int mode)
2020 {
2021         if(mode & OB_MODE_EDIT)
2022                 return "OBJECT_OT_editmode_toggle";
2023         if(mode == OB_MODE_SCULPT)
2024                 return "SCULPT_OT_sculptmode_toggle";
2025         if(mode == OB_MODE_VERTEX_PAINT)
2026                 return "PAINT_OT_vertex_paint_toggle";
2027         if(mode == OB_MODE_WEIGHT_PAINT)
2028                 return "PAINT_OT_weight_paint_toggle";
2029         if(mode == OB_MODE_TEXTURE_PAINT)
2030                 return "PAINT_OT_texture_paint_toggle";
2031         if(mode == OB_MODE_PARTICLE_EDIT)
2032                 return "PARTICLE_OT_particle_edit_toggle";
2033         if(mode == OB_MODE_POSE)
2034                 return "OBJECT_OT_posemode_toggle";
2035         return NULL;
2036 }
2037
2038 /* checks the mode to be set is compatible with the object
2039  * should be made into a generic function */
2040 static int object_mode_set_compat(bContext *C, wmOperator *op, Object *ob)
2041 {
2042         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
2043
2044         if(mode == OB_MODE_OBJECT)
2045                 return 1;
2046
2047         if(ob) {
2048                 switch(ob->type) {
2049                 case OB_EMPTY:
2050                 case OB_LAMP:
2051                 case OB_CAMERA:
2052                         return 0;
2053
2054                 case OB_MESH:
2055                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
2056                                 return 1;
2057                         return 0;
2058                 case OB_CURVE:
2059                 case OB_SURF:
2060                 case OB_FONT:
2061                 case OB_MBALL:
2062                         if(mode & (OB_MODE_OBJECT|OB_MODE_EDIT))
2063                                 return 1;
2064                         return 0;
2065                 case OB_LATTICE:
2066                         if(mode & (OB_MODE_OBJECT|OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
2067                                 return 1;
2068                 case OB_ARMATURE:
2069                         if(mode & (OB_MODE_OBJECT|OB_MODE_EDIT|OB_MODE_POSE))
2070                                 return 1;
2071                 }
2072         }
2073
2074         return 0;
2075 }
2076
2077 static int object_mode_set_exec(bContext *C, wmOperator *op)
2078 {
2079         Object *ob= CTX_data_active_object(C);
2080         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
2081         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
2082         int toggle = RNA_boolean_get(op->ptr, "toggle");
2083
2084         if(!ob || !object_mode_set_compat(C, op, ob))
2085                 return OPERATOR_CANCELLED;
2086
2087         /* Exit current mode if it's not the mode we're setting */
2088         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
2089                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
2090
2091         if(mode != OB_MODE_OBJECT) {
2092                 /* Enter new mode */
2093                 if(ob->mode != mode || toggle)
2094                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
2095
2096                 if(toggle) {
2097                         if(ob->mode == mode)
2098                                 /* For toggling, store old mode so we know what to go back to */
2099                                 ob->restore_mode = restore_mode;
2100                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
2101                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
2102                         }
2103                 }
2104         }
2105
2106         return OPERATOR_FINISHED;
2107 }
2108
2109 void OBJECT_OT_mode_set(wmOperatorType *ot)
2110 {
2111         PropertyRNA *prop;
2112
2113         /* identifiers */
2114         ot->name= "Set Object Mode";
2115         ot->description = "Sets the object interaction mode";
2116         ot->idname= "OBJECT_OT_mode_set";
2117         
2118         /* api callbacks */
2119         ot->exec= object_mode_set_exec;
2120         
2121         ot->poll= ED_operator_object_active_editable;
2122         
2123         /* flags */
2124         ot->flag= 0; /* no register/undo here, leave it to operators being called */
2125         
2126         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, 0, "Mode", "");
2127         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
2128
2129         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
2130 }
2131
2132
2133
2134 void ED_object_toggle_modes(bContext *C, int mode)
2135 {
2136         if(mode & OB_MODE_SCULPT)
2137                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2138         if(mode & OB_MODE_VERTEX_PAINT)
2139                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2140         if(mode & OB_MODE_WEIGHT_PAINT)
2141                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2142         if(mode & OB_MODE_TEXTURE_PAINT)
2143                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2144         if(mode & OB_MODE_PARTICLE_EDIT)
2145                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2146         if(mode & OB_MODE_POSE)
2147                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2148         if(mode & OB_MODE_EDIT)
2149                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2150 }
2151
2152 /************************ Game Properties ***********************/
2153
2154 static int game_property_new(bContext *C, wmOperator *op)
2155 {
2156         Object *ob= CTX_data_active_object(C);
2157         bProperty *prop;
2158
2159         if(!ob)
2160                 return OPERATOR_CANCELLED;
2161
2162         prop= new_property(PROP_FLOAT);
2163         BLI_addtail(&ob->prop, prop);
2164         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
2165
2166         WM_event_add_notifier(C, NC_LOGIC, NULL);
2167         return OPERATOR_FINISHED;
2168 }
2169
2170
2171 void OBJECT_OT_game_property_new(wmOperatorType *ot)
2172 {
2173         /* identifiers */
2174         ot->name= "New Game Property";
2175         ot->idname= "OBJECT_OT_game_property_new";
2176
2177         /* api callbacks */
2178         ot->exec= game_property_new;
2179         ot->poll= ED_operator_object_active_editable;
2180
2181         /* flags */
2182         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2183 }
2184
2185 static int game_property_remove(bContext *C, wmOperator *op)
2186 {
2187         Object *ob= CTX_data_active_object(C);
2188         bProperty *prop;
2189         int index= RNA_int_get(op->ptr, "index");
2190
2191         if(!ob)
2192                 return OPERATOR_CANCELLED;
2193
2194         prop= BLI_findlink(&ob->prop, index);
2195
2196         if(prop) {
2197                 BLI_remlink(&ob->prop, prop);
2198                 free_property(prop);
2199
2200                 WM_event_add_notifier(C, NC_LOGIC, NULL);
2201                 return OPERATOR_FINISHED;
2202         }
2203         else {
2204                 return OPERATOR_CANCELLED;
2205         }
2206 }
2207
2208 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
2209 {
2210         /* identifiers */
2211         ot->name= "Remove Game Property";
2212         ot->idname= "OBJECT_OT_game_property_remove";
2213
2214         /* api callbacks */
2215         ot->exec= game_property_remove;
2216         ot->poll= ED_operator_object_active_editable;
2217
2218         /* flags */
2219         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2220
2221         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
2222 }
2223
2224 #define COPY_PROPERTIES_REPLACE 1
2225 #define COPY_PROPERTIES_MERGE   2
2226 #define COPY_PROPERTIES_COPY    3
2227
2228 static EnumPropertyItem game_properties_copy_operations[] ={
2229         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
2230         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
2231         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
2232         {0, NULL, 0, NULL, NULL}};
2233
2234 static EnumPropertyItem gameprops_items[]= {
2235         {0, NULL, 0, NULL, NULL}};
2236
2237 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *ptr, int *free)
2238 {       
2239         Object *ob= ED_object_active_context(C);
2240         EnumPropertyItem tmp = {0, "", 0, "", ""};
2241         EnumPropertyItem *item= NULL;
2242         bProperty *prop;
2243         int a, totitem= 0;
2244         
2245         if(!ob)
2246                 return gameprops_items;
2247
2248         for(a=1, prop= ob->prop.first; prop; prop=prop->next, a++) {
2249                 tmp.value= a;
2250                 tmp.identifier= prop->name;
2251                 tmp.name= prop->name;
2252                 RNA_enum_item_add(&item, &totitem, &tmp);
2253         }
2254
2255         RNA_enum_item_end(&item, &totitem);
2256         *free= 1;
2257
2258         return item;
2259 }
2260
2261 static int game_property_copy_exec(bContext *C, wmOperator *op)
2262 {
2263         Object *ob=ED_object_active_context(C);
2264         bProperty *prop;
2265         int type = RNA_enum_get(op->ptr, "operation");
2266         int propid= RNA_enum_get(op->ptr, "property");
2267
2268         if(propid > 0) { /* copy */
2269                 prop = BLI_findlink(&ob->prop, propid-1);
2270                 
2271                 if(prop) {
2272                         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2273                                 if (ob != ob_iter) {
2274                                         if (ob->data != ob_iter->data)
2275                                                 set_ob_property(ob_iter, prop);
2276                                 }
2277                         } CTX_DATA_END;
2278                 }
2279         }
2280         else if (ELEM(type, COPY_PROPERTIES_REPLACE, COPY_PROPERTIES_MERGE)) {
2281                 CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2282                         if (ob != ob_iter) {
2283                                 if (ob->data != ob_iter->data){
2284                                         if (type == 2) {/* merge */
2285                                                 for(prop = ob->prop.first; prop; prop= prop->next ) {
2286                                                         set_ob_property(ob_iter, prop);
2287                                                 }
2288                                         } else /* replace */
2289                                                 copy_properties( &ob_iter->prop, &ob->prop );
2290                                 }
2291                         }
2292                 }
2293                 CTX_DATA_END;
2294         }
2295
2296         return OPERATOR_FINISHED;
2297 }
2298
2299 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
2300 {
2301         PropertyRNA *prop;
2302         /* identifiers */
2303         ot->name= "Copy Game Property";
2304         ot->idname= "OBJECT_OT_game_property_copy";
2305
2306         /* api callbacks */
2307         ot->exec= game_property_copy_exec;
2308         ot->poll= ED_operator_object_active_editable;
2309
2310         /* flags */
2311         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2312
2313         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
2314         prop=RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
2315         RNA_def_enum_funcs(prop, gameprops_itemf);
2316         ot->prop=prop;
2317 }
2318
2319 static int game_property_clear_exec(bContext *C, wmOperator *op)
2320 {
2321         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2322                 free_properties(&ob_iter->prop);
2323         }
2324         CTX_DATA_END;
2325
2326         WM_event_add_notifier(C, NC_LOGIC, NULL);
2327         return OPERATOR_FINISHED;
2328 }
2329 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
2330 {
2331         /* identifiers */
2332         ot->name= "Clear Game Property";
2333         ot->idname= "OBJECT_OT_game_property_clear";
2334
2335         /* api callbacks */
2336         ot->exec= game_property_clear_exec;
2337         ot->poll= ED_operator_object_active_editable;
2338
2339         /* flags */
2340         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2341 }
2342
2343 /************************ Copy Logic Bricks ***********************/
2344
2345 static int logicbricks_copy_exec(bContext *C, wmOperator *op)
2346 {
2347         Object *ob=ED_object_active_context(C);
2348
2349         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2350                 if(ob != ob_iter) {
2351                         if (ob->data != ob_iter->data){
2352                                 /* first: free all logic */
2353                                 free_sensors(&ob_iter->sensors);                                
2354                                 unlink_controllers(&ob_iter->controllers);
2355                                 free_controllers(&ob_iter->controllers);
2356                                 unlink_actuators(&ob_iter->actuators);
2357                                 free_actuators(&ob_iter->actuators);
2358                         
2359                                 /* now copy it, this also works without logicbricks! */
2360                                 clear_sca_new_poins_ob(ob);
2361                                 copy_sensors(&ob_iter->sensors, &ob->sensors);
2362                                 copy_controllers(&ob_iter->controllers, &ob->controllers);
2363                                 copy_actuators(&ob_iter->actuators, &ob->actuators);
2364                                 set_sca_new_poins_ob(ob_iter);
2365                         
2366                                 /* some menu settings */
2367                                 ob_iter->scavisflag= ob->scavisflag;
2368                                 ob_iter->scaflag= ob->scaflag;
2369                         
2370                                 /* set the initial state */
2371                                 ob_iter->state= ob->state;
2372                                 ob_iter->init_state= ob->init_state;
2373                         }                       
2374                         if(ob_iter->totcol==ob->totcol) {
2375                                 ob_iter->actcol= ob->actcol;
2376                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
2377                         }
2378                 }
2379         }
2380         CTX_DATA_END;
2381
2382         WM_event_add_notifier(C, NC_LOGIC, NULL);
2383
2384         return OPERATOR_FINISHED;
2385 }
2386
2387 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
2388 {
2389         /* identifiers */
2390         ot->name= "Copy Logic Bricks to Selected";
2391         ot->description = "Copy logic bricks to other selected objects.";
2392         ot->idname= "OBJECT_OT_logic_bricks_copy";
2393
2394         /* api callbacks */
2395         ot->exec= logicbricks_copy_exec;
2396         ot->poll= ED_operator_object_active_editable;
2397
2398         /* flags */
2399         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2400 }