svn merge ^/trunk/blender -r43294:43338
[blender.git] / source / blender / editors / object / object_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_edit.c
27  *  \ingroup edobj
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33 #include <time.h>
34 #include <float.h>
35 #include <ctype.h>
36 #include <stddef.h> //for offsetof
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_editVert.h"
44 #include "BLI_ghash.h"
45 #include "BLI_rand.h"
46
47 #include "DNA_armature_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_group_types.h"
50 #include "DNA_lattice_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_meta_types.h"
53 #include "DNA_property_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_object_force.h"
57 #include "DNA_meshdata_types.h"
58 #include "DNA_vfont_types.h"
59
60 #include "IMB_imbuf_types.h"
61
62 #include "BKE_anim.h"
63 #include "BKE_constraint.h"
64 #include "BKE_context.h"
65 #include "BKE_curve.h"
66 #include "BKE_effect.h"
67 #include "BKE_depsgraph.h"
68 #include "BKE_font.h"
69 #include "BKE_image.h"
70 #include "BKE_library.h"
71 #include "BKE_main.h"
72 #include "BKE_material.h"
73 #include "BKE_mball.h"
74 #include "BKE_mesh.h"
75 #include "BKE_object.h"
76 #include "BKE_paint.h"
77 #include "BKE_pointcache.h"
78 #include "BKE_property.h"
79 #include "BKE_sca.h"
80 #include "BKE_softbody.h"
81 #include "BKE_modifier.h"
82 #include "BKE_tessmesh.h"
83
84 #include "ED_armature.h"
85 #include "ED_curve.h"
86 #include "ED_mesh.h"
87 #include "ED_mball.h"
88 #include "ED_lattice.h"
89 #include "ED_object.h"
90 #include "ED_screen.h"
91 #include "ED_util.h"
92
93
94 #include "RNA_access.h"
95 #include "RNA_define.h"
96 #include "RNA_enum_types.h"
97
98 /* for menu/popup icons etc etc*/
99
100 #include "UI_interface.h"
101 #include "WM_api.h"
102 #include "WM_types.h"
103
104 #include "object_intern.h"      // own include
105
106 /* ************* XXX **************** */
107 static void error(const char *UNUSED(arg)) {}
108 static void waitcursor(int UNUSED(val)) {}
109 static int pupmenu(const char *UNUSED(msg)) {return 0;}
110
111 /* port over here */
112 static void error_libdata(void) {}
113
114 Object *ED_object_context(bContext *C)
115 {
116         return CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
117 }
118
119 /* find the correct active object per context
120  * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
121 Object *ED_object_active_context(bContext *C)
122 {
123         Object *ob= NULL;
124         if(C) {
125                 ob= ED_object_context(C);
126                 if (!ob) ob= CTX_data_active_object(C);
127         }
128         return ob;
129 }
130
131
132 /* ********* clear/set restrict view *********/
133 static int object_hide_view_clear_exec(bContext *C, wmOperator *UNUSED(op))
134 {
135         Main *bmain= CTX_data_main(C);
136         ScrArea *sa= CTX_wm_area(C);
137         View3D *v3d= sa->spacedata.first;
138         Scene *scene= CTX_data_scene(C);
139         Base *base;
140         int changed = 0;
141         
142         /* XXX need a context loop to handle such cases */
143         for(base = FIRSTBASE; base; base=base->next){
144                 if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
145                         base->flag |= SELECT;
146                         base->object->flag = base->flag;
147                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
148                         changed = 1;
149                 }
150         }
151         if (changed) {
152                 DAG_id_type_tag(bmain, ID_OB);
153                 DAG_scene_sort(bmain, scene);
154                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
155         }
156
157         return OPERATOR_FINISHED;
158 }
159
160 void OBJECT_OT_hide_view_clear(wmOperatorType *ot)
161 {
162         
163         /* identifiers */
164         ot->name= "Clear Restrict View";
165         ot->description = "Reveal the object by setting the hide flag";
166         ot->idname= "OBJECT_OT_hide_view_clear";
167         
168         /* api callbacks */
169         ot->exec= object_hide_view_clear_exec;
170         ot->poll= ED_operator_view3d_active;
171         
172         /* flags */
173         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
174 }
175
176 static int object_hide_view_set_exec(bContext *C, wmOperator *op)
177 {
178         Main *bmain= CTX_data_main(C);
179         Scene *scene= CTX_data_scene(C);
180         short changed = 0;
181         const int unselected= RNA_boolean_get(op->ptr, "unselected");
182         
183         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
184                 if(!unselected) {
185                         if (base->flag & SELECT){
186                                 base->flag &= ~SELECT;
187                                 base->object->flag = base->flag;
188                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
189                                 changed = 1;
190                                 if (base==BASACT) {
191                                         ED_base_object_activate(C, NULL);
192                                 }
193                         }
194                 }
195                 else {
196                         if (!(base->flag & SELECT)){
197                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
198                                 changed = 1;
199                         }
200                 }       
201         }
202         CTX_DATA_END;
203
204         if (changed) {
205                 DAG_id_type_tag(bmain, ID_OB);
206                 DAG_scene_sort(bmain, scene);
207                 
208                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
209                 
210         }
211
212         return OPERATOR_FINISHED;
213 }
214
215 void OBJECT_OT_hide_view_set(wmOperatorType *ot)
216 {
217         /* identifiers */
218         ot->name= "Set Restrict View";
219         ot->description = "Hide the object by setting the hide flag";
220         ot->idname= "OBJECT_OT_hide_view_set";
221         
222         /* api callbacks */
223         ot->exec= object_hide_view_set_exec;
224         ot->poll= ED_operator_view3d_active;
225         
226         /* flags */
227         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
228         
229         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
230         
231 }
232
233 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
234 static int object_hide_render_clear_exec(bContext *C, wmOperator *UNUSED(op))
235 {
236         short changed= 0;
237
238         /* XXX need a context loop to handle such cases */
239         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
240                 if(ob->restrictflag & OB_RESTRICT_RENDER) {
241                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
242                         changed= 1;
243                 }
244         }
245         CTX_DATA_END;
246
247         if(changed)
248                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
249
250         return OPERATOR_FINISHED;
251 }
252
253 void OBJECT_OT_hide_render_clear(wmOperatorType *ot)
254 {
255
256         /* identifiers */
257         ot->name= "Clear Restrict Render";
258         ot->description = "Reveal the render object by setting the hide render flag";
259         ot->idname= "OBJECT_OT_hide_render_clear";
260
261         /* api callbacks */
262         ot->exec= object_hide_render_clear_exec;
263         ot->poll= ED_operator_view3d_active;
264
265         /* flags */
266         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
267 }
268
269 static int object_hide_render_set_exec(bContext *C, wmOperator *op)
270 {
271         const int unselected= RNA_boolean_get(op->ptr, "unselected");
272
273         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
274                 if(!unselected) {
275                         if (base->flag & SELECT){
276                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
277                         }
278                 }
279                 else {
280                         if (!(base->flag & SELECT)){
281                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
282                         }
283                 }
284         }
285         CTX_DATA_END;
286         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
287         return OPERATOR_FINISHED;
288 }
289
290 void OBJECT_OT_hide_render_set(wmOperatorType *ot)
291 {
292         /* identifiers */
293         ot->name= "Set Restrict Render";
294         ot->description = "Hide the render object by setting the hide render flag";
295         ot->idname= "OBJECT_OT_hide_render_set";
296
297         /* api callbacks */
298         ot->exec= object_hide_render_set_exec;
299         ot->poll= ED_operator_view3d_active;
300
301         /* flags */
302         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
303
304         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
305 }
306
307 /* ******************* toggle editmode operator  ***************** */
308
309 void ED_object_exit_editmode(bContext *C, int flag)
310 {
311         /* Note! only in exceptional cases should 'EM_DO_UNDO' NOT be in the flag */
312
313         Scene *scene= CTX_data_scene(C);
314         Object *obedit= CTX_data_edit_object(C);
315         int freedata = flag & EM_FREEDATA;
316         
317         if(obedit==NULL) return;
318         
319         if(flag & EM_WAITCURSOR) waitcursor(1);
320         if(obedit->type==OB_MESH) {
321                 Mesh *me= obedit->data;
322                 
323 //              if(EM_texFaceCheck())
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(obedit->restore_mode & OB_MODE_WEIGHT_PAINT) {
338                         mesh_octree_table(NULL, NULL, NULL, 'e');
339                         mesh_mirrtopo_table(NULL, 'e');
340                 }
341         }
342         else if (obedit->type==OB_ARMATURE) {   
343                 ED_armature_from_edit(obedit);
344                 if(freedata)
345                         ED_armature_edit_free(obedit);
346         }
347         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
348                 load_editNurb(obedit);
349                 if(freedata) free_editNurb(obedit);
350         }
351         else if(obedit->type==OB_FONT && freedata) {
352                 load_editText(obedit);
353                 if(freedata) free_editText(obedit);
354         }
355         else if(obedit->type==OB_LATTICE) {
356                 load_editLatt(obedit);
357                 if(freedata) free_editLatt(obedit);
358         }
359         else if(obedit->type==OB_MBALL) {
360                 load_editMball(obedit);
361                 if(freedata) free_editMball(obedit);
362         }
363
364         /* freedata only 0 now on file saves and render */
365         if(freedata) {
366                 ListBase pidlist;
367                 PTCacheID *pid;
368
369                 /* for example; displist make is different in editmode */
370                 scene->obedit= NULL; // XXX for context
371
372                 /* flag object caches as outdated */
373                 BKE_ptcache_ids_from_object(&pidlist, obedit, NULL, 0);
374                 for(pid=pidlist.first; pid; pid=pid->next) {
375                         if(pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
376                                 pid->cache->flag |= PTCACHE_OUTDATED;
377                 }
378                 BLI_freelistN(&pidlist);
379                 
380                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
381
382                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
383                 DAG_id_tag_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA);
384         
385                 if(flag & EM_DO_UNDO)
386                         ED_undo_push(C, "Editmode");
387         
388                 if(flag & EM_WAITCURSOR) waitcursor(0);
389         
390                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
391
392                 obedit->mode &= ~OB_MODE_EDIT;
393         }
394 }
395
396
397 void ED_object_enter_editmode(bContext *C, int flag)
398 {
399         Scene *scene= CTX_data_scene(C);
400         Base *base= NULL;
401         Object *ob;
402         ScrArea *sa= CTX_wm_area(C);
403         View3D *v3d= NULL;
404         int ok= 0;
405         
406         if(scene->id.lib) return;
407         
408         if(sa && sa->spacetype==SPACE_VIEW3D)
409                 v3d= sa->spacedata.first;
410         
411         if((flag & EM_IGNORE_LAYER)==0) {
412                 base= CTX_data_active_base(C); /* active layer checked here for view3d */
413
414                 if(base==NULL) return;
415                 else if(v3d && (base->lay & v3d->lay)==0) return;
416                 else if(!v3d && (base->lay & scene->lay)==0) return;
417         }
418         else {
419                 base= scene->basact;
420         }
421
422         if (ELEM3(NULL, base, base->object, base->object->data)) return;
423
424         ob = base->object;
425         
426         if (object_data_is_libdata(ob)) {
427                 error_libdata();
428                 return;
429         }
430         
431         if(flag & EM_WAITCURSOR) waitcursor(1);
432
433         ob->restore_mode = ob->mode;
434
435         /* note, when switching scenes the object can have editmode data but
436          * not be scene->obedit: bug 22954, this avoids calling self eternally */
437         if((ob->restore_mode & OB_MODE_EDIT)==0)
438                 ED_object_toggle_modes(C, ob->mode);
439
440         ob->mode= OB_MODE_EDIT;
441         
442         if(ob->type==OB_MESH) {
443                 ok= 1;
444                 scene->obedit= ob;      // context sees this
445                 
446                 EDBM_MakeEditBMesh(CTX_data_tool_settings(C), scene, ob);
447
448                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene);
449         }
450         else if (ob->type==OB_ARMATURE){
451                 bArmature *arm= base->object->data;
452                 if (!arm) return;
453                 /*
454                  * The function object_data_is_libdata make a problem here, the
455                  * check for ob->proxy return 0 and let blender enter to edit mode
456                  * this causa a crash when you try leave the edit mode.
457                  * The problem is that i can't remove the ob->proxy check from
458                  * object_data_is_libdata that prevent the bugfix #6614, so
459                  * i add this little hack here.
460                  */
461                 if(arm->id.lib) {
462                         error_libdata();
463                         return;
464                 }
465                 ok=1;
466                 scene->obedit= ob;
467                 ED_armature_to_edit(ob);
468                 /* to ensure all goes in restposition and without striding */
469                 DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME); // XXX: should this be OB_RECALC_DATA?
470
471                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene);
472         }
473         else if(ob->type==OB_FONT) {
474                 scene->obedit= ob; // XXX for context
475                 ok= 1;
476                 make_editText(ob);
477
478                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene);
479         }
480         else if(ob->type==OB_MBALL) {
481                 scene->obedit= ob; // XXX for context
482                 ok= 1;
483                 make_editMball(ob);
484
485                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene);
486         }
487         else if(ob->type==OB_LATTICE) {
488                 scene->obedit= ob; // XXX for context
489                 ok= 1;
490                 make_editLatt(ob);
491                 
492                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene);
493         }
494         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
495                 ok= 1;
496                 scene->obedit= ob; // XXX for context
497                 make_editNurb(ob);
498                 
499                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene);
500         }
501         
502         if(ok) {
503                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
504         }
505         else {
506                 scene->obedit= NULL; // XXX for context
507                 ob->mode &= ~OB_MODE_EDIT;
508                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
509         }
510         
511         if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
512         if(flag & EM_WAITCURSOR) waitcursor(0);
513 }
514
515 static int editmode_toggle_exec(bContext *C, wmOperator *UNUSED(op))
516 {
517         if(!CTX_data_edit_object(C))
518                 ED_object_enter_editmode(C, EM_WAITCURSOR);
519         else
520                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* had EM_DO_UNDO but op flag calls undo too [#24685] */
521         
522         return OPERATOR_FINISHED;
523 }
524
525 static int editmode_toggle_poll(bContext *C)
526 {
527         Object *ob = CTX_data_active_object(C);
528
529         /* covers proxies too */
530         if(ELEM(NULL, ob, ob->data) || ((ID *)ob->data)->lib)
531                 return 0;
532
533         if (ob->restrictflag & OB_RESTRICT_VIEW)
534                 return 0;
535
536         return (ob->type == OB_MESH || ob->type == OB_ARMATURE ||
537                 ob->type == OB_FONT || ob->type == OB_MBALL ||
538                 ob->type == OB_LATTICE || ob->type == OB_SURF ||
539                 ob->type == OB_CURVE);
540 }
541
542 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
543 {
544         
545         /* identifiers */
546         ot->name= "Toggle Editmode";
547         ot->description = "Toggle object's editmode";
548         ot->idname= "OBJECT_OT_editmode_toggle";
549         
550         /* api callbacks */
551         ot->exec= editmode_toggle_exec;
552         
553         ot->poll= editmode_toggle_poll;
554         
555         /* flags */
556         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
557 }
558
559 /* *************************** */
560
561 static int posemode_exec(bContext *C, wmOperator *UNUSED(op))
562 {
563         Base *base= CTX_data_active_base(C);
564         
565         if(base->object->type==OB_ARMATURE) {
566                 if(base->object==CTX_data_edit_object(C)) {
567                         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
568                         ED_armature_enter_posemode(C, base);
569                 }
570                 else if(base->object->mode & OB_MODE_POSE)
571                         ED_armature_exit_posemode(C, base);
572                 else
573                         ED_armature_enter_posemode(C, base);
574                 
575                 return OPERATOR_FINISHED;
576         }
577         
578         return OPERATOR_PASS_THROUGH;
579 }
580
581 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
582 {
583         /* identifiers */
584         ot->name= "Toggle Pose Mode";
585         ot->idname= "OBJECT_OT_posemode_toggle";
586         ot->description= "Enable or disable posing/selecting bones";
587         
588         /* api callbacks */
589         ot->exec= posemode_exec;
590         ot->poll= ED_operator_object_active_editable;
591         
592         /* flag */
593         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
594 }
595
596 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
597 {       
598 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
599         bProperty *prop;
600         Base *base;
601         int nr, tot=0;
602         char *str;
603         
604         prop= ob->prop.first;
605         while(prop) {
606                 tot++;
607                 prop= prop->next;
608         }
609         
610         str= MEM_callocN(50 + 33*tot, "copymenu prop");
611         
612         if (tot)
613                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
614         else
615                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
616         
617         tot= 0; 
618         prop= ob->prop.first;
619         while(prop) {
620                 tot++;
621                 strcat(str, "|");
622                 strcat(str, prop->name);
623                 prop= prop->next;
624         }
625
626         nr= pupmenu(str);
627         
628         if ( nr==1 || nr==2 ) {
629                 for(base= FIRSTBASE; base; base= base->next) {
630                         if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
631                                 if (nr==1) { /* replace */
632                                         copy_properties( &base->object->prop, &ob->prop );
633                                 } else {
634                                         for(prop = ob->prop.first; prop; prop= prop->next ) {
635                                                 set_ob_property(base->object, prop);
636                                         }
637                                 }
638                         }
639                 }
640         } else if(nr>0) {
641                 prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/
642                 
643                 if(prop) {
644                         for(base= FIRSTBASE; base; base= base->next) {
645                                 if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
646                                         set_ob_property(base->object, prop);
647                                 }
648                         }
649                 }
650         }
651         MEM_freeN(str);
652         
653 }
654
655 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
656 {
657 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
658         Base *base;
659         
660         for(base= FIRSTBASE; base; base= base->next) {
661                 if(base->object != ob) {
662                         if(TESTBASELIB(v3d, base)) {
663                                 
664                                 /* first: free all logic */
665                                 free_sensors(&base->object->sensors);                           
666                                 unlink_controllers(&base->object->controllers);
667                                 free_controllers(&base->object->controllers);
668                                 unlink_actuators(&base->object->actuators);
669                                 free_actuators(&base->object->actuators);
670                                 
671                                 /* now copy it, this also works without logicbricks! */
672                                 clear_sca_new_poins_ob(ob);
673                                 copy_sensors(&base->object->sensors, &ob->sensors);
674                                 copy_controllers(&base->object->controllers, &ob->controllers);
675                                 copy_actuators(&base->object->actuators, &ob->actuators);
676                                 set_sca_new_poins_ob(base->object);
677                                 
678                                 /* some menu settings */
679                                 base->object->scavisflag= ob->scavisflag;
680                                 base->object->scaflag= ob->scaflag;
681                                 
682                                 /* set the initial state */
683                                 base->object->state= ob->state;
684                                 base->object->init_state= ob->init_state;
685                         }
686                 }
687         }
688 }
689
690 /* both pointers should exist */
691 static void copy_texture_space(Object *to, Object *ob)
692 {
693         float *poin1= NULL, *poin2= NULL;
694         short texflag= 0;
695         
696         if(ob->type==OB_MESH) {
697                 texflag= ((Mesh *)ob->data)->texflag;
698                 poin2= ((Mesh *)ob->data)->loc;
699         }
700         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
701                 texflag= ((Curve *)ob->data)->texflag;
702                 poin2= ((Curve *)ob->data)->loc;
703         }
704         else if(ob->type==OB_MBALL) {
705                 texflag= ((MetaBall *)ob->data)->texflag;
706                 poin2= ((MetaBall *)ob->data)->loc;
707         }
708         else
709                 return;
710                 
711         if(to->type==OB_MESH) {
712                 ((Mesh *)to->data)->texflag= texflag;
713                 poin1= ((Mesh *)to->data)->loc;
714         }
715         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
716                 ((Curve *)to->data)->texflag= texflag;
717                 poin1= ((Curve *)to->data)->loc;
718         }
719         else if(to->type==OB_MBALL) {
720                 ((MetaBall *)to->data)->texflag= texflag;
721                 poin1= ((MetaBall *)to->data)->loc;
722         }
723         else
724                 return;
725         
726         memcpy(poin1, poin2, 9*sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
727         
728         if(to->type==OB_MESH) ;
729         else if(to->type==OB_MBALL) tex_space_mball(to);
730         else tex_space_curve(to->data);
731         
732 }
733
734 /* UNUSED, keep incase we want to copy functionality for use elsewhere */
735 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
736 {
737         Object *ob;
738         Base *base;
739         Curve *cu, *cu1;
740         Nurb *nu;
741         int do_scene_sort= 0;
742         
743         if(scene->id.lib) return;
744
745         if(!(ob=OBACT)) return;
746         
747         if(scene->obedit) { // XXX get from context
748                 /* obedit_copymenu(); */
749                 return;
750         }
751         if(event==9) {
752                 copymenu_properties(scene, v3d, ob);
753                 return;
754         }
755         else if(event==10) {
756                 copymenu_logicbricks(scene, v3d, ob);
757                 return;
758         }
759         else if(event==24) {
760                 /* moved to object_link_modifiers */
761                 /* copymenu_modifiers(bmain, scene, v3d, ob); */
762                 return;
763         }
764
765         for(base= FIRSTBASE; base; base= base->next) {
766                 if(base != BASACT) {
767                         if(TESTBASELIB(v3d, base)) {
768                                 base->object->recalc |= OB_RECALC_OB;
769                                 
770                                 if(event==1) {  /* loc */
771                                         copy_v3_v3(base->object->loc, ob->loc);
772                                         copy_v3_v3(base->object->dloc, ob->dloc);
773                                 }
774                                 else if(event==2) {  /* rot */
775                                         copy_v3_v3(base->object->rot, ob->rot);
776                                         copy_v3_v3(base->object->drot, ob->drot);
777
778                                         copy_qt_qt(base->object->quat, ob->quat);
779                                         copy_qt_qt(base->object->dquat, ob->dquat);
780                                 }
781                                 else if(event==3) {  /* size */
782                                         copy_v3_v3(base->object->size, ob->size);
783                                         copy_v3_v3(base->object->dscale, ob->dscale);
784                                 }
785                                 else if(event==4) {  /* drawtype */
786                                         base->object->dt= ob->dt;
787                                         base->object->dtx= ob->dtx;
788                                         base->object->empty_drawtype= ob->empty_drawtype;
789                                         base->object->empty_drawsize= ob->empty_drawsize;
790                                 }
791                                 else if(event==5) {  /* time offs */
792                                         base->object->sf= ob->sf;
793                                 }
794                                 else if(event==6) {  /* dupli */
795                                         base->object->dupon= ob->dupon;
796                                         base->object->dupoff= ob->dupoff;
797                                         base->object->dupsta= ob->dupsta;
798                                         base->object->dupend= ob->dupend;
799                                         
800                                         base->object->transflag &= ~OB_DUPLI;
801                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
802
803                                         base->object->dup_group= ob->dup_group;
804                                         if(ob->dup_group)
805                                                 id_lib_extern(&ob->dup_group->id);
806                                 }
807                                 else if(event==7) {     /* mass */
808                                         base->object->mass= ob->mass;
809                                 }
810                                 else if(event==8) {     /* damping */
811                                         base->object->damping= ob->damping;
812                                         base->object->rdamping= ob->rdamping;
813                                 }
814                                 else if(event==11) {    /* all physical attributes */
815                                         base->object->gameflag = ob->gameflag;
816                                         base->object->inertia = ob->inertia;
817                                         base->object->formfactor = ob->formfactor;
818                                         base->object->damping= ob->damping;
819                                         base->object->rdamping= ob->rdamping;
820                                         base->object->min_vel= ob->min_vel;
821                                         base->object->max_vel= ob->max_vel;
822                                         if (ob->gameflag & OB_BOUNDS) {
823                                                 base->object->collision_boundtype = ob->collision_boundtype;
824                                         }
825                                         base->object->margin= ob->margin;
826                                         base->object->bsoft= copy_bulletsoftbody(ob->bsoft);
827
828                                 }
829                                 else if(event==17) {    /* tex space */
830                                         copy_texture_space(base->object, ob);
831                                 }
832                                 else if(event==18) {    /* font settings */
833                                         
834                                         if(base->object->type==ob->type) {
835                                                 cu= ob->data;
836                                                 cu1= base->object->data;
837                                                 
838                                                 cu1->spacemode= cu->spacemode;
839                                                 cu1->spacing= cu->spacing;
840                                                 cu1->linedist= cu->linedist;
841                                                 cu1->shear= cu->shear;
842                                                 cu1->fsize= cu->fsize;
843                                                 cu1->xof= cu->xof;
844                                                 cu1->yof= cu->yof;
845                                                 cu1->textoncurve= cu->textoncurve;
846                                                 cu1->wordspace= cu->wordspace;
847                                                 cu1->ulpos= cu->ulpos;
848                                                 cu1->ulheight= cu->ulheight;
849                                                 if(cu1->vfont) cu1->vfont->id.us--;
850                                                 cu1->vfont= cu->vfont;
851                                                 id_us_plus((ID *)cu1->vfont);
852                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
853                                                 cu1->vfontb= cu->vfontb;
854                                                 id_us_plus((ID *)cu1->vfontb);
855                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
856                                                 cu1->vfonti= cu->vfonti;
857                                                 id_us_plus((ID *)cu1->vfonti);
858                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
859                                                 cu1->vfontbi= cu->vfontbi;
860                                                 id_us_plus((ID *)cu1->vfontbi);                                         
861
862                                                 BKE_text_to_curve(bmain, scene, base->object, 0); /* needed? */
863
864                                                 
865                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
866                                                 
867                                                 base->object->recalc |= OB_RECALC_DATA;
868                                         }
869                                 }
870                                 else if(event==19) {    /* bevel settings */
871                                         
872                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
873                                                 cu= ob->data;
874                                                 cu1= base->object->data;
875                                                 
876                                                 cu1->bevobj= cu->bevobj;
877                                                 cu1->taperobj= cu->taperobj;
878                                                 cu1->width= cu->width;
879                                                 cu1->bevresol= cu->bevresol;
880                                                 cu1->ext1= cu->ext1;
881                                                 cu1->ext2= cu->ext2;
882                                                 
883                                                 base->object->recalc |= OB_RECALC_DATA;
884                                         }
885                                 }
886                                 else if(event==25) {    /* curve resolution */
887
888                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
889                                                 cu= ob->data;
890                                                 cu1= base->object->data;
891                                                 
892                                                 cu1->resolu= cu->resolu;
893                                                 cu1->resolu_ren= cu->resolu_ren;
894                                                 
895                                                 nu= cu1->nurb.first;
896                                                 
897                                                 while(nu) {
898                                                         nu->resolu= cu1->resolu;
899                                                         nu= nu->next;
900                                                 }
901                                                 
902                                                 base->object->recalc |= OB_RECALC_DATA;
903                                         }
904                                 }
905                                 else if(event==21){
906                                         if (base->object->type==OB_MESH) {
907                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
908
909                                                 if (md) {
910                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
911
912                                                         if (!tmd) {
913                                                                 tmd = modifier_new(eModifierType_Subsurf);
914                                                                 BLI_addtail(&base->object->modifiers, tmd);
915                                                         }
916
917                                                         modifier_copyData(md, tmd);
918                                                         base->object->recalc |= OB_RECALC_DATA;
919                                                 }
920                                         }
921                                 }
922                                 else if(event==22) {
923                                         /* Copy the constraint channels over */
924                                         copy_constraints(&base->object->constraints, &ob->constraints, TRUE);
925                                         
926                                         do_scene_sort= 1;
927                                 }
928                                 else if(event==23) {
929                                         base->object->softflag= ob->softflag;
930                                         if(base->object->soft) sbFree(base->object->soft);
931                                         
932                                         base->object->soft= copy_softbody(ob->soft);
933
934                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
935                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
936                                         }
937                                 }
938                                 else if(event==26) {
939 #if 0 // XXX old animation system
940                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
941 #endif // XXX old animation system
942                                 }
943                                 else if(event==27) {    /* autosmooth */
944                                         if (base->object->type==OB_MESH) {
945                                                 Mesh *me= ob->data;
946                                                 Mesh *cme= base->object->data;
947                                                 cme->smoothresh= me->smoothresh;
948                                                 if(me->flag & ME_AUTOSMOOTH)
949                                                         cme->flag |= ME_AUTOSMOOTH;
950                                                 else
951                                                         cme->flag &= ~ME_AUTOSMOOTH;
952                                         }
953                                 }
954                                 else if(event==28) { /* UV orco */
955                                         if(ELEM(base->object->type, OB_CURVE, OB_SURF)) {
956                                                 cu= ob->data;
957                                                 cu1= base->object->data;
958                                                 
959                                                 if(cu->flag & CU_UV_ORCO)
960                                                         cu1->flag |= CU_UV_ORCO;
961                                                 else
962                                                         cu1->flag &= ~CU_UV_ORCO;
963                                         }               
964                                 }
965                                 else if(event==29) { /* protected bits */
966                                         base->object->protectflag= ob->protectflag;
967                                 }
968                                 else if(event==30) { /* index object */
969                                         base->object->index= ob->index;
970                                 }
971                                 else if(event==31) { /* object color */
972                                         copy_v4_v4(base->object->col, ob->col);
973                                 }
974                         }
975                 }
976         }
977         
978         if(do_scene_sort)
979                 DAG_scene_sort(bmain, scene);
980
981         DAG_ids_flush_update(bmain, 0);
982 }
983
984 static void UNUSED_FUNCTION(copy_attr_menu)(Main *bmain, Scene *scene, View3D *v3d)
985 {
986         Object *ob;
987         short event;
988         char str[512];
989         
990         if(!(ob=OBACT)) return;
991         
992         if (scene->obedit) { // XXX get from context
993 //              if (ob->type == OB_MESH)
994 // XXX                  mesh_copy_menu();
995                 return;
996         }
997         
998         /* Object Mode */
999         
1000         /* If you change this menu, don't forget to update the menu in header_view3d.c
1001          * view3d_edit_object_copyattrmenu() and in toolbox.c
1002          */
1003         
1004         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");
1005         
1006         strcat (str, "|Object Constraints%x22");
1007         strcat (str, "|NLA Strips%x26");
1008         
1009 // XXX  if (OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1010 //              strcat(str, "|Texture Space%x17");
1011 //      }       
1012         
1013         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1014         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
1015         
1016         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1017                         strcat(str, "|Curve Resolution%x25");
1018         }
1019
1020         if(ob->type==OB_MESH){
1021                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
1022         }
1023
1024         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
1025         
1026         strcat(str, "|Pass Index%x30");
1027         
1028         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
1029                 strcat(str, "|Modifiers ...%x24");
1030         }
1031
1032         event= pupmenu(str);
1033         if(event<= 0) return;
1034         
1035         copy_attr(bmain, scene, v3d, event);
1036 }
1037
1038 /* ******************* force field toggle operator ***************** */
1039
1040 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1041 {
1042         Object *ob = CTX_data_active_object(C);
1043
1044         if(ob->pd == NULL)
1045                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1046
1047         if(ob->pd->forcefield == 0)
1048                 ob->pd->forcefield = PFIELD_FORCE;
1049         else
1050                 ob->pd->forcefield = 0;
1051         
1052         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, NULL);
1053
1054         return OPERATOR_FINISHED;
1055 }
1056
1057 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1058 {
1059         
1060         /* identifiers */
1061         ot->name= "Toggle Force Field";
1062         ot->description = "Toggle object's force field";
1063         ot->idname= "OBJECT_OT_forcefield_toggle";
1064         
1065         /* api callbacks */
1066         ot->exec= forcefield_toggle_exec;
1067         ot->poll= ED_operator_object_active_editable;
1068         
1069         /* flags */
1070         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1071 }
1072
1073 /* ********************************************** */
1074 /* Motion Paths */
1075
1076 /* For the object with pose/action: update paths for those that have got them
1077  * This should selectively update paths that exist...
1078  *
1079  * To be called from various tools that do incremental updates 
1080  */
1081 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1082 {
1083         ListBase targets = {NULL, NULL};
1084         
1085         /* loop over objects in scene */
1086         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1087         {
1088                 /* set flag to force recalc, then grab the relevant bones to target */
1089                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1090                 animviz_get_object_motionpaths(ob, &targets);
1091         }
1092         CTX_DATA_END;
1093         
1094         /* recalculate paths, then free */
1095         animviz_calc_motionpaths(scene, &targets);
1096         BLI_freelistN(&targets);
1097 }
1098
1099 /* For the object with pose/action: create path curves for selected bones 
1100  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1101  */
1102 static int object_calculate_paths_exec (bContext *C, wmOperator *op)
1103 {
1104         Scene *scene= CTX_data_scene(C);
1105         
1106         /* set up path data for bones being calculated */
1107         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
1108         {
1109                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1110                 animviz_verify_motionpaths(op->reports, scene, ob, NULL);
1111         }
1112         CTX_DATA_END;
1113         
1114         /* calculate the bones that now have motionpaths... */
1115         // TODO: only make for the selected bones?
1116         ED_objects_recalculate_paths(C, scene);
1117         
1118         /* notifiers for updates */
1119         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1120         
1121         return OPERATOR_FINISHED; 
1122 }
1123
1124 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
1125 {
1126         /* identifiers */
1127         ot->name= "Calculate Object Paths";
1128         ot->idname= "OBJECT_OT_paths_calculate";
1129         ot->description= "Calculate paths for the selected bones";
1130         
1131         /* api callbacks */
1132         ot->exec= object_calculate_paths_exec;
1133         ot->poll= ED_operator_object_active_editable;
1134         
1135         /* flags */
1136         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1137 }
1138
1139 /* --------- */
1140
1141 /* for the object with pose/action: clear path curves for selected bones only */
1142 void ED_objects_clear_paths(bContext *C)
1143 {
1144         /* loop over objects in scene */
1145         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1146         {
1147                 if (ob->mpath) {
1148                         animviz_free_motionpath(ob->mpath);
1149                         ob->mpath= NULL;
1150                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1151                 }
1152         }
1153         CTX_DATA_END;
1154 }
1155
1156 /* operator callback for this */
1157 static int object_clear_paths_exec (bContext *C, wmOperator *UNUSED(op))
1158 {       
1159         /* use the backend function for this */
1160         ED_objects_clear_paths(C);
1161         
1162         /* notifiers for updates */
1163         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1164         
1165         return OPERATOR_FINISHED; 
1166 }
1167
1168 void OBJECT_OT_paths_clear (wmOperatorType *ot)
1169 {
1170         /* identifiers */
1171         ot->name= "Clear Object Paths";
1172         ot->idname= "OBJECT_OT_paths_clear";
1173         ot->description= "Clear path caches for selected bones";
1174         
1175         /* api callbacks */
1176         ot->exec= object_clear_paths_exec;
1177         ot->poll= ED_operator_object_active_editable;
1178         
1179         /* flags */
1180         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1181 }
1182
1183
1184 /********************** Smooth/Flat *********************/
1185
1186 static int shade_smooth_exec(bContext *C, wmOperator *op)
1187 {
1188         Curve *cu;
1189         Nurb *nu;
1190         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1191         int done= 0;
1192
1193         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1194
1195                 if(ob->type==OB_MESH) {
1196                         mesh_set_smooth_flag(ob, !clear);
1197
1198                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1199                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1200
1201                         done= 1;
1202                 }
1203                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
1204                         cu= ob->data;
1205
1206                         for(nu=cu->nurb.first; nu; nu=nu->next) {
1207                                 if(!clear) nu->flag |= ME_SMOOTH;
1208                                 else nu->flag &= ~ME_SMOOTH;
1209                         }
1210
1211                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1212                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1213
1214                         done= 1;
1215                 }
1216         }
1217         CTX_DATA_END;
1218
1219         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
1220 }
1221
1222 static int shade_poll(bContext *C)
1223 {
1224         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
1225 }
1226
1227 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1228 {
1229         /* identifiers */
1230         ot->name= "Shade Flat";
1231         ot->description= "Display faces 'flat'";
1232         ot->idname= "OBJECT_OT_shade_flat";
1233         
1234         /* api callbacks */
1235         ot->poll= shade_poll;
1236         ot->exec= shade_smooth_exec;
1237
1238         /* flags */
1239         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1240 }
1241
1242 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1243 {
1244         /* identifiers */
1245         ot->name= "Shade Smooth";
1246         ot->description= "Display faces 'smooth' (using vertex normals)";
1247         ot->idname= "OBJECT_OT_shade_smooth";
1248         
1249         /* api callbacks */
1250         ot->poll= shade_poll;
1251         ot->exec= shade_smooth_exec;
1252         
1253         /* flags */
1254         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1255 }
1256
1257 /* ********************** */
1258
1259 static void UNUSED_FUNCTION(image_aspect)(Scene *scene, View3D *v3d)
1260 {
1261         /* all selected objects with an image map: scale in image aspect */
1262         Base *base;
1263         Object *ob;
1264         Material *ma;
1265         Tex *tex;
1266         float x, y, space;
1267         int a, b, done;
1268         
1269         if(scene->obedit) return; // XXX get from context
1270         if(scene->id.lib) return;
1271         
1272         for(base= FIRSTBASE; base; base= base->next) {
1273                 if(TESTBASELIB(v3d, base)) {
1274                         ob= base->object;
1275                         done= 0;
1276                         
1277                         for(a=1; a<=ob->totcol; a++) {
1278                                 ma= give_current_material(ob, a);
1279                                 if(ma) {
1280                                         for(b=0; b<MAX_MTEX; b++) {
1281                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1282                                                         tex= ma->mtex[b]->tex;
1283                                                         if(tex->type==TEX_IMAGE && tex->ima) {
1284                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
1285                                                                 
1286                                                                 /* texturespace */
1287                                                                 space= 1.0;
1288                                                                 if(ob->type==OB_MESH) {
1289                                                                         float size[3];
1290                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1291                                                                         space= size[0]/size[1];
1292                                                                 }
1293                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1294                                                                         Curve *cu= ob->data;
1295                                                                         space= cu->size[0]/cu->size[1];
1296                                                                 }
1297                                                         
1298                                                                 x= ibuf->x/space;
1299                                                                 y= ibuf->y;
1300                                                                 
1301                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
1302                                                                 else ob->size[1]= ob->size[0]*y/x;
1303                                                                 
1304                                                                 done= 1;
1305                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);                                                               
1306                                                         }
1307                                                 }
1308                                                 if(done) break;
1309                                         }
1310                                 }
1311                                 if(done) break;
1312                         }
1313                 }
1314         }
1315         
1316 }
1317
1318
1319 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1320 {       
1321         EnumPropertyItem *input = object_mode_items;
1322         EnumPropertyItem *item= NULL;
1323         Object *ob;
1324         int totitem= 0;
1325         
1326         if(!C) /* needed for docs */
1327                 return object_mode_items;
1328
1329         ob = CTX_data_active_object(C);
1330         while(ob && input->identifier) {
1331                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
1332                                                         (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
1333                                                          (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
1334                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1335                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
1336                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
1337                          input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1338                    (input->value == OB_MODE_OBJECT))
1339                         RNA_enum_item_add(&item, &totitem, input);
1340                 ++input;
1341         }
1342
1343         RNA_enum_item_end(&item, &totitem);
1344
1345         *free= 1;
1346
1347         return item;
1348 }
1349
1350 static const char *object_mode_op_string(int mode)
1351 {
1352         if(mode & OB_MODE_EDIT)
1353                 return "OBJECT_OT_editmode_toggle";
1354         if(mode == OB_MODE_SCULPT)
1355                 return "SCULPT_OT_sculptmode_toggle";
1356         if(mode == OB_MODE_VERTEX_PAINT)
1357                 return "PAINT_OT_vertex_paint_toggle";
1358         if(mode == OB_MODE_WEIGHT_PAINT)
1359                 return "PAINT_OT_weight_paint_toggle";
1360         if(mode == OB_MODE_TEXTURE_PAINT)
1361                 return "PAINT_OT_texture_paint_toggle";
1362         if(mode == OB_MODE_PARTICLE_EDIT)
1363                 return "PARTICLE_OT_particle_edit_toggle";
1364         if(mode == OB_MODE_POSE)
1365                 return "OBJECT_OT_posemode_toggle";
1366         return NULL;
1367 }
1368
1369 /* checks the mode to be set is compatible with the object
1370  * should be made into a generic function */
1371 static int object_mode_set_compat(bContext *UNUSED(C), wmOperator *op, Object *ob)
1372 {
1373         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1374
1375         if(ob) {
1376                 if(mode == OB_MODE_OBJECT)
1377                         return 1;
1378
1379                 switch(ob->type) {
1380                 case OB_MESH:
1381                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
1382                                 return 1;
1383                         return 0;
1384                 case OB_CURVE:
1385                 case OB_SURF:
1386                 case OB_FONT:
1387                 case OB_MBALL:
1388                         if(mode & (OB_MODE_EDIT))
1389                                 return 1;
1390                         return 0;
1391                 case OB_LATTICE:
1392                         if(mode & (OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
1393                                 return 1;
1394                 case OB_ARMATURE:
1395                         if(mode & (OB_MODE_EDIT|OB_MODE_POSE))
1396                                 return 1;
1397                 }
1398         }
1399
1400         return 0;
1401 }
1402
1403 static int object_mode_set_exec(bContext *C, wmOperator *op)
1404 {
1405         Object *ob= CTX_data_active_object(C);
1406         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1407         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1408         int toggle = RNA_boolean_get(op->ptr, "toggle");
1409
1410         if(!ob || !object_mode_set_compat(C, op, ob))
1411                 return OPERATOR_PASS_THROUGH;
1412
1413         /* Exit current mode if it's not the mode we're setting */
1414         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
1415                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
1416
1417         if(mode != OB_MODE_OBJECT) {
1418                 /* Enter new mode */
1419                 if(ob->mode != mode || toggle)
1420                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
1421
1422                 if(toggle) {
1423                         if(ob->mode == mode)
1424                                 /* For toggling, store old mode so we know what to go back to */
1425                                 ob->restore_mode = restore_mode;
1426                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1427                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1428                         }
1429                 }
1430         }
1431
1432         return OPERATOR_FINISHED;
1433 }
1434
1435 void OBJECT_OT_mode_set(wmOperatorType *ot)
1436 {
1437         PropertyRNA *prop;
1438
1439         /* identifiers */
1440         ot->name= "Set Object Mode";
1441         ot->description = "Sets the object interaction mode";
1442         ot->idname= "OBJECT_OT_mode_set";
1443         
1444         /* api callbacks */
1445         ot->exec= object_mode_set_exec;
1446         
1447         ot->poll= ED_operator_object_active_editable;
1448         
1449         /* flags */
1450         ot->flag= 0; /* no register/undo here, leave it to operators being called */
1451         
1452         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
1453         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
1454
1455         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1456 }
1457
1458
1459
1460 void ED_object_toggle_modes(bContext *C, int mode)
1461 {
1462         if(mode & OB_MODE_SCULPT)
1463                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1464         if(mode & OB_MODE_VERTEX_PAINT)
1465                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1466         if(mode & OB_MODE_WEIGHT_PAINT)
1467                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1468         if(mode & OB_MODE_TEXTURE_PAINT)
1469                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1470         if(mode & OB_MODE_PARTICLE_EDIT)
1471                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1472         if(mode & OB_MODE_POSE)
1473                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1474         if(mode & OB_MODE_EDIT)
1475                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1476 }
1477
1478 /************************ Game Properties ***********************/
1479
1480 static int game_property_new(bContext *C, wmOperator *op)
1481 {
1482         Object *ob= CTX_data_active_object(C);
1483         bProperty *prop;
1484         char name[MAX_NAME];
1485         int type= RNA_enum_get(op->ptr, "type");
1486
1487         prop= new_property(type);
1488         BLI_addtail(&ob->prop, prop);
1489
1490         RNA_string_get(op->ptr, "name", name);
1491         if (name[0] != '\0') {
1492                 BLI_strncpy(prop->name, name, sizeof(prop->name));
1493         }
1494
1495         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
1496
1497         WM_event_add_notifier(C, NC_LOGIC, NULL);
1498         return OPERATOR_FINISHED;
1499 }
1500
1501
1502 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1503 {
1504         /* identifiers */
1505         ot->name= "New Game Property";
1506         ot->description= "Create a new property available to the game engine";
1507         ot->idname= "OBJECT_OT_game_property_new";
1508
1509         /* api callbacks */
1510         ot->exec= game_property_new;
1511         ot->poll= ED_operator_object_active_editable;
1512
1513         /* flags */
1514         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1515
1516         RNA_def_enum(ot->srna, "type", gameproperty_type_items, 2, "Type", "Type of game property to add");
1517         RNA_def_string(ot->srna, "name", "", MAX_NAME, "Name", "Name of the game property to add");
1518 }
1519
1520 static int game_property_remove(bContext *C, wmOperator *op)
1521 {
1522         Object *ob= CTX_data_active_object(C);
1523         bProperty *prop;
1524         int index= RNA_int_get(op->ptr, "index");
1525
1526         if(!ob)
1527                 return OPERATOR_CANCELLED;
1528
1529         prop= BLI_findlink(&ob->prop, index);
1530
1531         if(prop) {
1532                 BLI_remlink(&ob->prop, prop);
1533                 free_property(prop);
1534
1535                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1536                 return OPERATOR_FINISHED;
1537         }
1538         else {
1539                 return OPERATOR_CANCELLED;
1540         }
1541 }
1542
1543 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1544 {
1545         /* identifiers */
1546         ot->name= "Remove Game Property";
1547         ot->description= "Remove game property";
1548         ot->idname= "OBJECT_OT_game_property_remove";
1549
1550         /* api callbacks */
1551         ot->exec= game_property_remove;
1552         ot->poll= ED_operator_object_active_editable;
1553
1554         /* flags */
1555         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1556
1557         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
1558 }
1559
1560 #define COPY_PROPERTIES_REPLACE 1
1561 #define COPY_PROPERTIES_MERGE   2
1562 #define COPY_PROPERTIES_COPY    3
1563
1564 static EnumPropertyItem game_properties_copy_operations[] ={
1565         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
1566         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
1567         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
1568         {0, NULL, 0, NULL, NULL}};
1569
1570 static EnumPropertyItem gameprops_items[]= {
1571         {0, NULL, 0, NULL, NULL}};
1572
1573 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1574 {       
1575         Object *ob= ED_object_active_context(C);
1576         EnumPropertyItem tmp = {0, "", 0, "", ""};
1577         EnumPropertyItem *item= NULL;
1578         bProperty *prop;
1579         int a, totitem= 0;
1580         
1581         if(!ob)
1582                 return gameprops_items;
1583
1584         for(a=1, prop= ob->prop.first; prop; prop=prop->next, a++) {
1585                 tmp.value= a;
1586                 tmp.identifier= prop->name;
1587                 tmp.name= prop->name;
1588                 RNA_enum_item_add(&item, &totitem, &tmp);
1589         }
1590
1591         RNA_enum_item_end(&item, &totitem);
1592         *free= 1;
1593
1594         return item;
1595 }
1596
1597 static int game_property_copy_exec(bContext *C, wmOperator *op)
1598 {
1599         Object *ob=ED_object_active_context(C);
1600         bProperty *prop;
1601         int type = RNA_enum_get(op->ptr, "operation");
1602         int propid= RNA_enum_get(op->ptr, "property");
1603
1604         if(propid > 0) { /* copy */
1605                 prop = BLI_findlink(&ob->prop, propid-1);
1606                 
1607                 if(prop) {
1608                         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1609                                 if (ob != ob_iter)
1610                                         set_ob_property(ob_iter, prop);
1611                         } CTX_DATA_END;
1612                 }
1613         }
1614
1615         else {
1616                 CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1617                         if (ob != ob_iter) {
1618                                 if (type == COPY_PROPERTIES_REPLACE)
1619                                         copy_properties(&ob_iter->prop, &ob->prop);
1620
1621                                 /* merge - the default when calling with no argument */
1622                                 else
1623                                         for(prop = ob->prop.first; prop; prop= prop->next)
1624                                                 set_ob_property(ob_iter, prop);
1625                         }
1626                 }
1627                 CTX_DATA_END;
1628         }
1629
1630         return OPERATOR_FINISHED;
1631 }
1632
1633 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
1634 {
1635         PropertyRNA *prop;
1636         /* identifiers */
1637         ot->name= "Copy Game Property";
1638         ot->idname= "OBJECT_OT_game_property_copy";
1639
1640         /* api callbacks */
1641         ot->exec= game_property_copy_exec;
1642         ot->poll= ED_operator_object_active_editable;
1643
1644         /* flags */
1645         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1646
1647         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
1648         prop=RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
1649         RNA_def_enum_funcs(prop, gameprops_itemf);
1650         ot->prop=prop;
1651 }
1652
1653 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
1654 {
1655         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1656                 free_properties(&ob_iter->prop);
1657         }
1658         CTX_DATA_END;
1659
1660         WM_event_add_notifier(C, NC_LOGIC, NULL);
1661         return OPERATOR_FINISHED;
1662 }
1663 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
1664 {
1665         /* identifiers */
1666         ot->name= "Clear Game Property";
1667         ot->idname= "OBJECT_OT_game_property_clear";
1668
1669         /* api callbacks */
1670         ot->exec= game_property_clear_exec;
1671         ot->poll= ED_operator_object_active_editable;
1672
1673         /* flags */
1674         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1675 }
1676
1677 /************************ Copy Logic Bricks ***********************/
1678
1679 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
1680 {
1681         Object *ob=ED_object_active_context(C);
1682
1683         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1684                 if(ob != ob_iter) {
1685                         /* first: free all logic */
1686                         free_sensors(&ob_iter->sensors);                                
1687                         unlink_controllers(&ob_iter->controllers);
1688                         free_controllers(&ob_iter->controllers);
1689                         unlink_actuators(&ob_iter->actuators);
1690                         free_actuators(&ob_iter->actuators);
1691                 
1692                         /* now copy it, this also works without logicbricks! */
1693                         clear_sca_new_poins_ob(ob);
1694                         copy_sensors(&ob_iter->sensors, &ob->sensors);
1695                         copy_controllers(&ob_iter->controllers, &ob->controllers);
1696                         copy_actuators(&ob_iter->actuators, &ob->actuators);
1697                         set_sca_new_poins_ob(ob_iter);
1698                 
1699                         /* some menu settings */
1700                         ob_iter->scavisflag= ob->scavisflag;
1701                         ob_iter->scaflag= ob->scaflag;
1702                 
1703                         /* set the initial state */
1704                         ob_iter->state= ob->state;
1705                         ob_iter->init_state= ob->init_state;
1706
1707                         if(ob_iter->totcol==ob->totcol) {
1708                                 ob_iter->actcol= ob->actcol;
1709                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
1710                         }
1711                 }
1712         }
1713         CTX_DATA_END;
1714
1715         WM_event_add_notifier(C, NC_LOGIC, NULL);
1716
1717         return OPERATOR_FINISHED;
1718 }
1719
1720 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
1721 {
1722         /* identifiers */
1723         ot->name= "Copy Logic Bricks to Selected";
1724         ot->description = "Copy logic bricks to other selected objects";
1725         ot->idname= "OBJECT_OT_logic_bricks_copy";
1726
1727         /* api callbacks */
1728         ot->exec= logicbricks_copy_exec;
1729         ot->poll= ED_operator_object_active_editable;
1730
1731         /* flags */
1732         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1733 }
1734
1735 static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op))
1736 {
1737         Object *ob=ED_object_active_context(C);
1738         
1739         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1740                 if(ob != ob_iter) {
1741                         ob_iter->gameflag = ob->gameflag;
1742                         ob_iter->gameflag2 = ob->gameflag2;
1743                         ob_iter->inertia = ob->inertia;
1744                         ob_iter->formfactor = ob->formfactor;;
1745                         ob_iter->damping = ob->damping;
1746                         ob_iter->rdamping = ob->rdamping;
1747                         ob_iter->min_vel = ob->min_vel;
1748                         ob_iter->max_vel = ob->max_vel;
1749                         ob_iter->obstacleRad = ob->obstacleRad;
1750                         ob_iter->mass = ob->mass;
1751                         ob_iter->anisotropicFriction[0] = ob->anisotropicFriction[0];
1752                         ob_iter->anisotropicFriction[1] = ob->anisotropicFriction[1];
1753                         ob_iter->anisotropicFriction[2] = ob->anisotropicFriction[2];
1754                         ob_iter->collision_boundtype = ob->collision_boundtype;                 
1755                         ob_iter->margin = ob->margin;
1756                         ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft);
1757                         if(ob->restrictflag & OB_RESTRICT_RENDER) 
1758                                 ob_iter->restrictflag |= OB_RESTRICT_RENDER;
1759                          else
1760                                 ob_iter->restrictflag &= ~OB_RESTRICT_RENDER;
1761                 }
1762         }
1763         CTX_DATA_END;
1764         
1765         return OPERATOR_FINISHED;
1766 }
1767
1768 void OBJECT_OT_game_physics_copy(struct wmOperatorType *ot)
1769 {
1770         /* identifiers */
1771         ot->name= "Copy Game Physics Properties to Selected";
1772         ot->description = "Copy game physics properties to other selected objects";
1773         ot->idname= "OBJECT_OT_game_physics_copy";
1774         
1775         /* api callbacks */
1776         ot->exec= game_physics_copy_exec;
1777         ot->poll= ED_operator_object_active_editable;
1778         
1779         /* flags */
1780         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1781 }