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