b80d4dedc65a746aae9e202e5699d5223c328ac2
[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_ghash.h"
44
45 #include "DNA_armature_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_meta_types.h"
51 #include "DNA_property_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_object_force.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_vfont_types.h"
57 #include "DNA_mesh_types.h"
58
59 #include "IMB_imbuf_types.h"
60
61 #include "BKE_anim.h"
62 #include "BKE_constraint.h"
63 #include "BKE_context.h"
64 #include "BKE_curve.h"
65 #include "BKE_effect.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_font.h"
68 #include "BKE_image.h"
69 #include "BKE_library.h"
70 #include "BKE_main.h"
71 #include "BKE_material.h"
72 #include "BKE_mball.h"
73 #include "BKE_mesh.h"
74 #include "BKE_object.h"
75 #include "BKE_paint.h"
76 #include "BKE_pointcache.h"
77 #include "BKE_property.h"
78 #include "BKE_sca.h"
79 #include "BKE_softbody.h"
80 #include "BKE_modifier.h"
81 #include "BKE_editmesh.h"
82 #include "BKE_report.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 #include "ED_image.h"
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         bool changed = false;
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 = true;
149                 }
150         }
151         if (changed) {
152                 DAG_id_type_tag(bmain, ID_OB);
153                 DAG_relations_tag_update(bmain);
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         bool changed = false;
181         const int unselected = RNA_boolean_get(op->ptr, "unselected");
182         
183         CTX_DATA_BEGIN(C, Base *, base, visible_bases)
184         {
185                 if (!unselected) {
186                         if (base->flag & SELECT) {
187                                 base->flag &= ~SELECT;
188                                 base->object->flag = base->flag;
189                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
190                                 changed = true;
191                                 if (base == BASACT) {
192                                         ED_base_object_activate(C, NULL);
193                                 }
194                         }
195                 }
196                 else {
197                         if (!(base->flag & SELECT)) {
198                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
199                                 changed = true;
200                                 if (base == BASACT) {
201                                         ED_base_object_activate(C, NULL);
202                                 }
203                         }
204                 }
205         }
206         CTX_DATA_END;
207
208         if (changed) {
209                 DAG_id_type_tag(bmain, ID_OB);
210                 DAG_relations_tag_update(bmain);
211                 
212                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
213                 
214         }
215
216         return OPERATOR_FINISHED;
217 }
218
219 void OBJECT_OT_hide_view_set(wmOperatorType *ot)
220 {
221         /* identifiers */
222         ot->name = "Set Restrict View";
223         ot->description = "Hide the object by setting the hide flag";
224         ot->idname = "OBJECT_OT_hide_view_set";
225         
226         /* api callbacks */
227         ot->exec = object_hide_view_set_exec;
228         ot->poll = ED_operator_view3d_active;
229         
230         /* flags */
231         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
232         
233         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
234         
235 }
236
237 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
238 static int object_hide_render_clear_exec(bContext *C, wmOperator *UNUSED(op))
239 {
240         bool changed = false;
241
242         /* XXX need a context loop to handle such cases */
243         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
244         {
245                 if (ob->restrictflag & OB_RESTRICT_RENDER) {
246                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
247                         changed = true;
248                 }
249         }
250         CTX_DATA_END;
251
252         if (changed)
253                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
254
255         return OPERATOR_FINISHED;
256 }
257
258 void OBJECT_OT_hide_render_clear(wmOperatorType *ot)
259 {
260
261         /* identifiers */
262         ot->name = "Clear Restrict Render";
263         ot->description = "Reveal the render object by setting the hide render flag";
264         ot->idname = "OBJECT_OT_hide_render_clear";
265
266         /* api callbacks */
267         ot->exec = object_hide_render_clear_exec;
268         ot->poll = ED_operator_view3d_active;
269
270         /* flags */
271         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
272 }
273
274 static int object_hide_render_set_exec(bContext *C, wmOperator *op)
275 {
276         const int unselected = RNA_boolean_get(op->ptr, "unselected");
277
278         CTX_DATA_BEGIN(C, Base *, base, visible_bases)
279         {
280                 if (!unselected) {
281                         if (base->flag & SELECT) {
282                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
283                         }
284                 }
285                 else {
286                         if (!(base->flag & SELECT)) {
287                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
288                         }
289                 }
290         }
291         CTX_DATA_END;
292         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
293         return OPERATOR_FINISHED;
294 }
295
296 void OBJECT_OT_hide_render_set(wmOperatorType *ot)
297 {
298         /* identifiers */
299         ot->name = "Set Restrict Render";
300         ot->description = "Hide the render object by setting the hide render flag";
301         ot->idname = "OBJECT_OT_hide_render_set";
302
303         /* api callbacks */
304         ot->exec = object_hide_render_set_exec;
305         ot->poll = ED_operator_view3d_active;
306
307         /* flags */
308         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
309
310         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
311 }
312
313 /* ******************* toggle editmode operator  ***************** */
314
315 /**
316  * Load EditMode data back into the object,
317  * optionally freeing the editmode data.
318  */
319 static bool ED_object_editmode_load_ex(Object *obedit, const bool freedata)
320 {
321         if (obedit == NULL) {
322                 return false;
323         }
324
325         if (obedit->type == OB_MESH) {
326                 Mesh *me = obedit->data;
327
328                 if (me->edit_btmesh->bm->totvert > MESH_MAX_VERTS) {
329                         error("Too many vertices");
330                         return false;
331                 }
332
333                 EDBM_mesh_load(obedit);
334
335                 if (freedata) {
336                         EDBM_mesh_free(me->edit_btmesh);
337                         MEM_freeN(me->edit_btmesh);
338                         me->edit_btmesh = NULL;
339                 }
340                 if (obedit->restore_mode & OB_MODE_WEIGHT_PAINT) {
341                         mesh_octree_table(NULL, NULL, NULL, 'e');
342                         mesh_mirrtopo_table(NULL, 'e');
343                 }
344         }
345         else if (obedit->type == OB_ARMATURE) {
346                 ED_armature_from_edit(obedit->data);
347                 if (freedata)
348                         ED_armature_edit_free(obedit->data);
349         }
350         else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
351                 load_editNurb(obedit);
352                 if (freedata) free_editNurb(obedit);
353         }
354         else if (obedit->type == OB_FONT) {
355                 load_editText(obedit);
356                 if (freedata) free_editText(obedit);
357         }
358         else if (obedit->type == OB_LATTICE) {
359                 load_editLatt(obedit);
360                 if (freedata) free_editLatt(obedit);
361         }
362         else if (obedit->type == OB_MBALL) {
363                 load_editMball(obedit);
364                 if (freedata) free_editMball(obedit);
365         }
366
367         return true;
368 }
369
370 bool ED_object_editmode_load(Object *obedit)
371 {
372         return ED_object_editmode_load_ex(obedit, false);
373 }
374
375 void ED_object_editmode_exit(bContext *C, int flag)
376 {
377         /* Note! only in exceptional cases should 'EM_DO_UNDO' NOT be in the flag */
378         /* Note! if 'EM_FREEDATA' isn't in the flag, use ED_object_editmode_load directly */
379         Scene *scene = CTX_data_scene(C);
380         Object *obedit = CTX_data_edit_object(C);
381         const bool freedata = (flag & EM_FREEDATA) != 0;
382
383         if (flag & EM_WAITCURSOR) waitcursor(1);
384
385         if (ED_object_editmode_load_ex(obedit, freedata) == false) {
386                 /* in rare cases (background mode) its possible active object
387                  * is flagged for editmode, without 'obedit' being set [#35489] */
388                 if (UNLIKELY(scene->basact && (scene->basact->object->mode & OB_MODE_EDIT))) {
389                         scene->basact->object->mode &= ~OB_MODE_EDIT;
390                 }
391                 if (flag & EM_WAITCURSOR) waitcursor(0);
392                 return;
393         }
394
395         /* freedata only 0 now on file saves and render */
396         if (freedata) {
397                 ListBase pidlist;
398                 PTCacheID *pid;
399
400                 /* for example; displist make is different in editmode */
401                 scene->obedit = NULL; // XXX for context
402
403                 /* flag object caches as outdated */
404                 BKE_ptcache_ids_from_object(&pidlist, obedit, scene, 0);
405                 for (pid = pidlist.first; pid; pid = pid->next) {
406                         if (pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
407                                 pid->cache->flag |= PTCACHE_OUTDATED;
408                 }
409                 BLI_freelistN(&pidlist);
410                 
411                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
412
413                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
414                 DAG_id_tag_update(&obedit->id, OB_RECALC_OB | OB_RECALC_DATA);
415         
416                 if (flag & EM_DO_UNDO)
417                         ED_undo_push(C, "Editmode");
418
419                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
420
421                 obedit->mode &= ~OB_MODE_EDIT;
422         }
423
424         if (flag & EM_WAITCURSOR) waitcursor(0);
425 }
426
427
428 void ED_object_editmode_enter(bContext *C, int flag)
429 {
430         Scene *scene = CTX_data_scene(C);
431         Base *base = NULL;
432         Object *ob;
433         ScrArea *sa = CTX_wm_area(C);
434         View3D *v3d = NULL;
435         int ok = 0;
436
437         if (scene->id.lib) return;
438
439         if (sa && sa->spacetype == SPACE_VIEW3D)
440                 v3d = sa->spacedata.first;
441
442         if ((flag & EM_IGNORE_LAYER) == 0) {
443                 base = CTX_data_active_base(C); /* active layer checked here for view3d */
444
445                 if (base == NULL) return;
446                 else if (v3d && (base->lay & v3d->lay) == 0) return;
447                 else if (!v3d && (base->lay & scene->lay) == 0) return;
448         }
449         else {
450                 base = scene->basact;
451         }
452
453         if (ELEM3(NULL, base, base->object, base->object->data)) return;
454
455         ob = base->object;
456
457         /* this checks actual object->data, for cases when other scenes have it in editmode context */
458         if ( BKE_object_is_in_editmode(ob) )
459                 return;
460         
461         if (BKE_object_obdata_is_libdata(ob)) {
462                 error_libdata();
463                 return;
464         }
465
466         if (flag & EM_WAITCURSOR) waitcursor(1);
467
468         ob->restore_mode = ob->mode;
469
470         /* note, when switching scenes the object can have editmode data but
471          * not be scene->obedit: bug 22954, this avoids calling self eternally */
472         if ((ob->restore_mode & OB_MODE_EDIT) == 0)
473                 ED_object_toggle_modes(C, ob->mode);
474
475         ob->mode = OB_MODE_EDIT;
476
477         if (ob->type == OB_MESH) {
478                 BMEditMesh *em;
479                 ok = 1;
480                 scene->obedit = ob;  /* context sees this */
481
482                 EDBM_mesh_make(scene->toolsettings, ob);
483
484                 em = BKE_editmesh_from_object(ob);
485                 if (LIKELY(em)) {
486                         /* order doesn't matter */
487                         EDBM_mesh_normals_update(em);
488                         BKE_editmesh_tessface_calc(em);
489
490                         BM_mesh_select_mode_flush(em->bm);
491                 }
492
493                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_MESH, scene);
494         }
495         else if (ob->type == OB_ARMATURE) {
496                 bArmature *arm = ob->data;
497                 if (!arm) return;
498                 /*
499                  * The function BKE_object_obdata_is_libdata make a problem here, the
500                  * check for ob->proxy return 0 and let blender enter to edit mode
501                  * this causes a crash when you try leave the edit mode.
502                  * The problem is that i can't remove the ob->proxy check from
503                  * BKE_object_obdata_is_libdata that prevent the bugfix #6614, so
504                  * i add this little hack here.
505                  */
506                 if (arm->id.lib) {
507                         error_libdata();
508                         return;
509                 }
510                 ok = 1;
511                 scene->obedit = ob;
512                 ED_armature_to_edit(arm);
513                 /* to ensure all goes in restposition and without striding */
514                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); /* XXX: should this be OB_RECALC_DATA? */
515
516                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_ARMATURE, scene);
517         }
518         else if (ob->type == OB_FONT) {
519                 scene->obedit = ob; /* XXX for context */
520                 ok = 1;
521                 make_editText(ob);
522
523                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_TEXT, scene);
524         }
525         else if (ob->type == OB_MBALL) {
526                 scene->obedit = ob; /* XXX for context */
527                 ok = 1;
528                 make_editMball(ob);
529
530                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_MBALL, scene);
531         }
532         else if (ob->type == OB_LATTICE) {
533                 scene->obedit = ob; /* XXX for context */
534                 ok = 1;
535                 make_editLatt(ob);
536
537                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_LATTICE, scene);
538         }
539         else if (ob->type == OB_SURF || ob->type == OB_CURVE) {
540                 ok = 1;
541                 scene->obedit = ob; /* XXX for context */
542                 make_editNurb(ob);
543
544                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_EDITMODE_CURVE, scene);
545         }
546
547         if (ok) {
548                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
549         }
550         else {
551                 scene->obedit = NULL; /* XXX for context */
552                 ob->mode &= ~OB_MODE_EDIT;
553                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, scene);
554         }
555
556         if (flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
557         if (flag & EM_WAITCURSOR) waitcursor(0);
558 }
559
560 static int editmode_toggle_exec(bContext *C, wmOperator *op)
561 {
562         const int mode_flag = OB_MODE_EDIT;
563         const bool is_mode_set = (CTX_data_edit_object(C) != NULL);
564         ToolSettings *toolsettings =  CTX_data_tool_settings(C);
565
566         if (!is_mode_set) {
567                 Scene *scene = CTX_data_scene(C);
568                 if (!ED_object_mode_compat_set(C, scene->basact->object, mode_flag, op->reports)) {
569                         return OPERATOR_CANCELLED;
570                 }
571         }
572
573         if (!is_mode_set)
574                 ED_object_editmode_enter(C, EM_WAITCURSOR);
575         else
576                 ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR);  /* had EM_DO_UNDO but op flag calls undo too [#24685] */
577         
578         ED_space_image_uv_sculpt_update(CTX_wm_manager(C), toolsettings);
579
580         return OPERATOR_FINISHED;
581 }
582
583 static int editmode_toggle_poll(bContext *C)
584 {
585         Object *ob = CTX_data_active_object(C);
586
587         /* covers proxies too */
588         if (ELEM(NULL, ob, ob->data) || ((ID *)ob->data)->lib)
589                 return 0;
590
591         /* if hidden but in edit mode, we still display */
592         if ((ob->restrictflag & OB_RESTRICT_VIEW) && !(ob->mode & OB_MODE_EDIT))
593                 return 0;
594
595         return (ELEM7(ob->type, OB_MESH, OB_ARMATURE, OB_FONT, OB_MBALL, OB_LATTICE, OB_SURF, OB_CURVE));
596 }
597
598 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
599 {
600         
601         /* identifiers */
602         ot->name = "Toggle Editmode";
603         ot->description = "Toggle object's editmode";
604         ot->idname = "OBJECT_OT_editmode_toggle";
605         
606         /* api callbacks */
607         ot->exec = editmode_toggle_exec;
608         ot->poll = editmode_toggle_poll;
609         
610         /* flags */
611         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
612 }
613
614 /* *************************** */
615
616 static int posemode_exec(bContext *C, wmOperator *op)
617 {
618         Base *base = CTX_data_active_base(C);
619         Object *ob = base->object;
620         const int mode_flag = OB_MODE_POSE;
621         const bool is_mode_set = (ob->mode & mode_flag) != 0;
622         
623         if (!is_mode_set) {
624                 if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
625                         return OPERATOR_CANCELLED;
626                 }
627         }
628
629         if (ob->type == OB_ARMATURE) {
630                 if (ob == CTX_data_edit_object(C)) {
631                         ED_object_editmode_exit(C, EM_FREEDATA | EM_DO_UNDO);
632                         ED_armature_enter_posemode(C, base);
633                 }
634                 else if (is_mode_set)
635                         ED_armature_exit_posemode(C, base);
636                 else
637                         ED_armature_enter_posemode(C, base);
638                 
639                 return OPERATOR_FINISHED;
640         }
641         
642         return OPERATOR_PASS_THROUGH;
643 }
644
645 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
646 {
647         /* identifiers */
648         ot->name = "Toggle Pose Mode";
649         ot->idname = "OBJECT_OT_posemode_toggle";
650         ot->description = "Enable or disable posing/selecting bones";
651         
652         /* api callbacks */
653         ot->exec = posemode_exec;
654         ot->poll = ED_operator_object_active_editable;
655         
656         /* flag */
657         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
658 }
659
660 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
661 {       
662 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
663         bProperty *prop;
664         Base *base;
665         int nr, tot = 0;
666         char *str;
667         
668         prop = ob->prop.first;
669         while (prop) {
670                 tot++;
671                 prop = prop->next;
672         }
673         
674         str = MEM_callocN(50 + 33 * tot, "copymenu prop");
675         
676         if (tot)
677                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
678         else
679                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
680         
681         tot = 0;
682         prop = ob->prop.first;
683         while (prop) {
684                 tot++;
685                 strcat(str, "|");
686                 strcat(str, prop->name);
687                 prop = prop->next;
688         }
689
690         nr = pupmenu(str);
691         
692         if (nr == 1 || nr == 2) {
693                 for (base = FIRSTBASE; base; base = base->next) {
694                         if ((base != BASACT) && (TESTBASELIB(v3d, base))) {
695                                 if (nr == 1) { /* replace */
696                                         BKE_bproperty_copy_list(&base->object->prop, &ob->prop);
697                                 }
698                                 else {
699                                         for (prop = ob->prop.first; prop; prop = prop->next) {
700                                                 BKE_bproperty_object_set(base->object, prop);
701                                         }
702                                 }
703                         }
704                 }
705         }
706         else if (nr > 0) {
707                 prop = BLI_findlink(&ob->prop, nr - 4); /* account for first 3 menu items & menu index starting at 1*/
708                 
709                 if (prop) {
710                         for (base = FIRSTBASE; base; base = base->next) {
711                                 if ((base != BASACT) && (TESTBASELIB(v3d, base))) {
712                                         BKE_bproperty_object_set(base->object, prop);
713                                 }
714                         }
715                 }
716         }
717         MEM_freeN(str);
718         
719 }
720
721 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
722 {
723 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
724         Base *base;
725         
726         for (base = FIRSTBASE; base; base = base->next) {
727                 if (base->object != ob) {
728                         if (TESTBASELIB(v3d, base)) {
729                                 
730                                 /* first: free all logic */
731                                 free_sensors(&base->object->sensors);
732                                 unlink_controllers(&base->object->controllers);
733                                 free_controllers(&base->object->controllers);
734                                 unlink_actuators(&base->object->actuators);
735                                 free_actuators(&base->object->actuators);
736                                 
737                                 /* now copy it, this also works without logicbricks! */
738                                 clear_sca_new_poins_ob(ob);
739                                 copy_sensors(&base->object->sensors, &ob->sensors);
740                                 copy_controllers(&base->object->controllers, &ob->controllers);
741                                 copy_actuators(&base->object->actuators, &ob->actuators);
742                                 set_sca_new_poins_ob(base->object);
743                                 
744                                 /* some menu settings */
745                                 base->object->scavisflag = ob->scavisflag;
746                                 base->object->scaflag = ob->scaflag;
747                                 
748                                 /* set the initial state */
749                                 base->object->state = ob->state;
750                                 base->object->init_state = ob->init_state;
751                         }
752                 }
753         }
754 }
755
756 /* both pointers should exist */
757 static void copy_texture_space(Object *to, Object *ob)
758 {
759         float *poin1 = NULL, *poin2 = NULL;
760         short texflag = 0;
761         
762         if (ob->type == OB_MESH) {
763                 texflag = ((Mesh *)ob->data)->texflag;
764                 poin2 = ((Mesh *)ob->data)->loc;
765         }
766         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
767                 texflag = ((Curve *)ob->data)->texflag;
768                 poin2 = ((Curve *)ob->data)->loc;
769         }
770         else if (ob->type == OB_MBALL) {
771                 texflag = ((MetaBall *)ob->data)->texflag;
772                 poin2 = ((MetaBall *)ob->data)->loc;
773         }
774         else
775                 return;
776                 
777         if (to->type == OB_MESH) {
778                 ((Mesh *)to->data)->texflag = texflag;
779                 poin1 = ((Mesh *)to->data)->loc;
780         }
781         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
782                 ((Curve *)to->data)->texflag = texflag;
783                 poin1 = ((Curve *)to->data)->loc;
784         }
785         else if (to->type == OB_MBALL) {
786                 ((MetaBall *)to->data)->texflag = texflag;
787                 poin1 = ((MetaBall *)to->data)->loc;
788         }
789         else
790                 return;
791         
792         memcpy(poin1, poin2, 9 * sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
793         
794         if (to->type == OB_MESH) {
795                 /* pass */
796         }
797         else if (to->type == OB_MBALL) {
798                 BKE_mball_texspace_calc(to);
799         }
800         else {
801                 BKE_curve_texspace_calc(to->data);
802         }
803         
804 }
805
806 /* UNUSED, keep in case we want to copy functionality for use elsewhere */
807 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
808 {
809         Object *ob;
810         Base *base;
811         Curve *cu, *cu1;
812         Nurb *nu;
813         bool do_depgraph_update = false;
814         
815         if (scene->id.lib) return;
816
817         if (!(ob = OBACT)) return;
818         
819         if (scene->obedit) { // XXX get from context
820                 /* obedit_copymenu(); */
821                 return;
822         }
823         if (event == 9) {
824                 copymenu_properties(scene, v3d, ob);
825                 return;
826         }
827         else if (event == 10) {
828                 copymenu_logicbricks(scene, v3d, ob);
829                 return;
830         }
831         else if (event == 24) {
832                 /* moved to BKE_object_link_modifiers */
833                 /* copymenu_modifiers(bmain, scene, v3d, ob); */
834                 return;
835         }
836
837         for (base = FIRSTBASE; base; base = base->next) {
838                 if (base != BASACT) {
839                         if (TESTBASELIB(v3d, base)) {
840                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
841                                 
842                                 if (event == 1) {  /* loc */
843                                         copy_v3_v3(base->object->loc, ob->loc);
844                                         copy_v3_v3(base->object->dloc, ob->dloc);
845                                 }
846                                 else if (event == 2) {  /* rot */
847                                         copy_v3_v3(base->object->rot, ob->rot);
848                                         copy_v3_v3(base->object->drot, ob->drot);
849
850                                         copy_qt_qt(base->object->quat, ob->quat);
851                                         copy_qt_qt(base->object->dquat, ob->dquat);
852                                 }
853                                 else if (event == 3) {  /* size */
854                                         copy_v3_v3(base->object->size, ob->size);
855                                         copy_v3_v3(base->object->dscale, ob->dscale);
856                                 }
857                                 else if (event == 4) {  /* drawtype */
858                                         base->object->dt = ob->dt;
859                                         base->object->dtx = ob->dtx;
860                                         base->object->empty_drawtype = ob->empty_drawtype;
861                                         base->object->empty_drawsize = ob->empty_drawsize;
862                                 }
863                                 else if (event == 5) {  /* time offs */
864                                         base->object->sf = ob->sf;
865                                 }
866                                 else if (event == 6) {  /* dupli */
867                                         base->object->dupon = ob->dupon;
868                                         base->object->dupoff = ob->dupoff;
869                                         base->object->dupsta = ob->dupsta;
870                                         base->object->dupend = ob->dupend;
871                                         
872                                         base->object->transflag &= ~OB_DUPLI;
873                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
874
875                                         base->object->dup_group = ob->dup_group;
876                                         if (ob->dup_group)
877                                                 id_lib_extern(&ob->dup_group->id);
878                                 }
879                                 else if (event == 7) {    /* mass */
880                                         base->object->mass = ob->mass;
881                                 }
882                                 else if (event == 8) {    /* damping */
883                                         base->object->damping = ob->damping;
884                                         base->object->rdamping = ob->rdamping;
885                                 }
886                                 else if (event == 11) {   /* all physical attributes */
887                                         base->object->gameflag = ob->gameflag;
888                                         base->object->inertia = ob->inertia;
889                                         base->object->formfactor = ob->formfactor;
890                                         base->object->damping = ob->damping;
891                                         base->object->rdamping = ob->rdamping;
892                                         base->object->min_vel = ob->min_vel;
893                                         base->object->max_vel = ob->max_vel;
894                                         if (ob->gameflag & OB_BOUNDS) {
895                                                 base->object->collision_boundtype = ob->collision_boundtype;
896                                         }
897                                         base->object->margin = ob->margin;
898                                         base->object->bsoft = copy_bulletsoftbody(ob->bsoft);
899
900                                 }
901                                 else if (event == 17) {   /* tex space */
902                                         copy_texture_space(base->object, ob);
903                                 }
904                                 else if (event == 18) {   /* font settings */
905                                         
906                                         if (base->object->type == ob->type) {
907                                                 cu = ob->data;
908                                                 cu1 = base->object->data;
909
910                                                 cu1->spacemode = cu->spacemode;
911                                                 cu1->spacing = cu->spacing;
912                                                 cu1->linedist = cu->linedist;
913                                                 cu1->shear = cu->shear;
914                                                 cu1->fsize = cu->fsize;
915                                                 cu1->xof = cu->xof;
916                                                 cu1->yof = cu->yof;
917                                                 cu1->textoncurve = cu->textoncurve;
918                                                 cu1->wordspace = cu->wordspace;
919                                                 cu1->ulpos = cu->ulpos;
920                                                 cu1->ulheight = cu->ulheight;
921                                                 if (cu1->vfont) cu1->vfont->id.us--;
922                                                 cu1->vfont = cu->vfont;
923                                                 id_us_plus((ID *)cu1->vfont);
924                                                 if (cu1->vfontb) cu1->vfontb->id.us--;
925                                                 cu1->vfontb = cu->vfontb;
926                                                 id_us_plus((ID *)cu1->vfontb);
927                                                 if (cu1->vfonti) cu1->vfonti->id.us--;
928                                                 cu1->vfonti = cu->vfonti;
929                                                 id_us_plus((ID *)cu1->vfonti);
930                                                 if (cu1->vfontbi) cu1->vfontbi->id.us--;
931                                                 cu1->vfontbi = cu->vfontbi;
932                                                 id_us_plus((ID *)cu1->vfontbi);
933                                                 
934                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
935                                                 
936                                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
937                                         }
938                                 }
939                                 else if (event == 19) {   /* bevel settings */
940                                         
941                                         if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
942                                                 cu = ob->data;
943                                                 cu1 = base->object->data;
944                                                 
945                                                 cu1->bevobj = cu->bevobj;
946                                                 cu1->taperobj = cu->taperobj;
947                                                 cu1->width = cu->width;
948                                                 cu1->bevresol = cu->bevresol;
949                                                 cu1->ext1 = cu->ext1;
950                                                 cu1->ext2 = cu->ext2;
951                                                 
952                                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
953                                         }
954                                 }
955                                 else if (event == 25) {   /* curve resolution */
956
957                                         if (ELEM(base->object->type, OB_CURVE, OB_FONT)) {
958                                                 cu = ob->data;
959                                                 cu1 = base->object->data;
960                                                 
961                                                 cu1->resolu = cu->resolu;
962                                                 cu1->resolu_ren = cu->resolu_ren;
963                                                 
964                                                 nu = cu1->nurb.first;
965                                                 
966                                                 while (nu) {
967                                                         nu->resolu = cu1->resolu;
968                                                         nu = nu->next;
969                                                 }
970                                                 
971                                                 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
972                                         }
973                                 }
974                                 else if (event == 21) {
975                                         if (base->object->type == OB_MESH) {
976                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
977
978                                                 if (md) {
979                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
980
981                                                         if (!tmd) {
982                                                                 tmd = modifier_new(eModifierType_Subsurf);
983                                                                 BLI_addtail(&base->object->modifiers, tmd);
984                                                         }
985
986                                                         modifier_copyData(md, tmd);
987                                                         DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
988                                                 }
989                                         }
990                                 }
991                                 else if (event == 22) {
992                                         /* Copy the constraint channels over */
993                                         BKE_copy_constraints(&base->object->constraints, &ob->constraints, TRUE);
994                                         
995                                         do_depgraph_update = true;
996                                 }
997                                 else if (event == 23) {
998                                         base->object->softflag = ob->softflag;
999                                         if (base->object->soft) sbFree(base->object->soft);
1000                                         
1001                                         base->object->soft = copy_softbody(ob->soft, FALSE);
1002
1003                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1004                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1005                                         }
1006                                 }
1007                                 else if (event == 26) {
1008 #if 0 // XXX old animation system
1009                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
1010 #endif // XXX old animation system
1011                                 }
1012                                 else if (event == 27) {   /* autosmooth */
1013                                         if (base->object->type == OB_MESH) {
1014                                                 Mesh *me = ob->data;
1015                                                 Mesh *cme = base->object->data;
1016                                                 cme->smoothresh = me->smoothresh;
1017                                                 if (me->flag & ME_AUTOSMOOTH)
1018                                                         cme->flag |= ME_AUTOSMOOTH;
1019                                                 else
1020                                                         cme->flag &= ~ME_AUTOSMOOTH;
1021                                         }
1022                                 }
1023                                 else if (event == 28) { /* UV orco */
1024                                         if (ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1025                                                 cu = ob->data;
1026                                                 cu1 = base->object->data;
1027                                                 
1028                                                 if (cu->flag & CU_UV_ORCO)
1029                                                         cu1->flag |= CU_UV_ORCO;
1030                                                 else
1031                                                         cu1->flag &= ~CU_UV_ORCO;
1032                                         }
1033                                 }
1034                                 else if (event == 29) { /* protected bits */
1035                                         base->object->protectflag = ob->protectflag;
1036                                 }
1037                                 else if (event == 30) { /* index object */
1038                                         base->object->index = ob->index;
1039                                 }
1040                                 else if (event == 31) { /* object color */
1041                                         copy_v4_v4(base->object->col, ob->col);
1042                                 }
1043                         }
1044                 }
1045         }
1046         
1047         if (do_depgraph_update)
1048                 DAG_relations_tag_update(bmain);
1049 }
1050
1051 static void UNUSED_FUNCTION(copy_attr_menu) (Main *bmain, Scene *scene, View3D *v3d)
1052 {
1053         Object *ob;
1054         short event;
1055         char str[512];
1056         
1057         if (!(ob = OBACT)) return;
1058         
1059         if (scene->obedit) { /* XXX get from context */
1060 /*              if (ob->type == OB_MESH) */
1061 /* XXX                  mesh_copy_menu(); */
1062                 return;
1063         }
1064         
1065         /* Object Mode */
1066         
1067         /* If you change this menu, don't forget to update the menu in header_view3d.c
1068          * view3d_edit_object_copyattrmenu() and in toolbox.c
1069          */
1070         
1071         strcpy(str,
1072                "Copy Attributes %t|Location %x1|Rotation %x2|Size %x3|Draw Options %x4|"
1073                "Time Offset %x5|Dupli %x6|Object Color %x31|%l|Mass %x7|Damping %x8|All Physical Attributes %x11|Properties %x9|"
1074                "Logic Bricks %x10|Protected Transform %x29|%l");
1075         
1076         strcat(str, "|Object Constraints %x22");
1077         strcat(str, "|NLA Strips %x26");
1078         
1079 /* XXX  if (OB_TYPE_SUPPORT_MATERIAL(ob->type)) { */
1080 /*              strcat(str, "|Texture Space %x17"); */
1081 /*      } */
1082         
1083         if (ob->type == OB_FONT) strcat(str, "|Font Settings %x18|Bevel Settings %x19");
1084         if (ob->type == OB_CURVE) strcat(str, "|Bevel Settings %x19|UV Orco %x28");
1085         
1086         if ((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1087                 strcat(str, "|Curve Resolution %x25");
1088         }
1089
1090         if (ob->type == OB_MESH) {
1091                 strcat(str, "|Subsurf Settings %x21|AutoSmooth %x27");
1092         }
1093
1094         if (ob->soft) strcat(str, "|Soft Body Settings %x23");
1095         
1096         strcat(str, "|Pass Index %x30");
1097         
1098         if (ob->type == OB_MESH || ob->type == OB_CURVE || ob->type == OB_LATTICE || ob->type == OB_SURF) {
1099                 strcat(str, "|Modifiers ... %x24");
1100         }
1101
1102         event = pupmenu(str);
1103         if (event <= 0) return;
1104         
1105         copy_attr(bmain, scene, v3d, event);
1106 }
1107
1108 /* ******************* force field toggle operator ***************** */
1109
1110 void ED_object_check_force_modifiers(Main *bmain, Scene *scene, Object *object)
1111 {
1112         PartDeflect *pd = object->pd;
1113         ModifierData *md = modifiers_findByType(object, eModifierType_Surface);
1114
1115         /* add/remove modifier as needed */
1116         if (!md) {
1117                 if (pd && (pd->shape == PFIELD_SHAPE_SURFACE) && ELEM(pd->forcefield, PFIELD_GUIDE, PFIELD_TEXTURE) == 0)
1118                         if (ELEM4(object->type, OB_MESH, OB_SURF, OB_FONT, OB_CURVE))
1119                                 ED_object_modifier_add(NULL, bmain, scene, object, NULL, eModifierType_Surface);
1120         }
1121         else {
1122                 if (!pd || pd->shape != PFIELD_SHAPE_SURFACE || pd->forcefield != PFIELD_FORCE)
1123                         ED_object_modifier_remove(NULL, bmain, object, md);
1124         }
1125 }
1126
1127 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1128 {
1129         Object *ob = CTX_data_active_object(C);
1130
1131         if (ob->pd == NULL)
1132                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1133
1134         if (ob->pd->forcefield == 0)
1135                 ob->pd->forcefield = PFIELD_FORCE;
1136         else
1137                 ob->pd->forcefield = 0;
1138         
1139         ED_object_check_force_modifiers(CTX_data_main(C), CTX_data_scene(C), ob);
1140         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1141         WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
1142
1143         return OPERATOR_FINISHED;
1144 }
1145
1146 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1147 {
1148         
1149         /* identifiers */
1150         ot->name = "Toggle Force Field";
1151         ot->description = "Toggle object's force field";
1152         ot->idname = "OBJECT_OT_forcefield_toggle";
1153         
1154         /* api callbacks */
1155         ot->exec = forcefield_toggle_exec;
1156         ot->poll = ED_operator_object_active_editable;
1157         
1158         /* flags */
1159         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1160 }
1161
1162 /* ********************************************** */
1163 /* Motion Paths */
1164
1165 /* For the objects with animation: update paths for those that have got them
1166  * This should selectively update paths that exist...
1167  *
1168  * To be called from various tools that do incremental updates 
1169  */
1170 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1171 {
1172         ListBase targets = {NULL, NULL};
1173         
1174         /* loop over objects in scene */
1175         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1176         {
1177                 /* set flag to force recalc, then grab path(s) from object */
1178                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1179                 animviz_get_object_motionpaths(ob, &targets);
1180         }
1181         CTX_DATA_END;
1182         
1183         /* recalculate paths, then free */
1184         animviz_calc_motionpaths(scene, &targets);
1185         BLI_freelistN(&targets);
1186 }
1187
1188
1189 /* show popup to determine settings */
1190 static int object_calculate_paths_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1191 {
1192         Object *ob = CTX_data_active_object(C);
1193         
1194         if (ob == NULL)
1195                 return OPERATOR_CANCELLED;
1196         
1197         /* set default settings from existing/stored settings */
1198         {
1199                 bAnimVizSettings *avs = &ob->avs;
1200                 
1201                 RNA_int_set(op->ptr, "start_frame", avs->path_sf);
1202                 RNA_int_set(op->ptr, "end_frame", avs->path_ef);
1203         }
1204         
1205         /* show popup dialog to allow editing of range... */
1206         /* FIXME: hardcoded dimensions here are just arbitrary */
1207         return WM_operator_props_dialog_popup(C, op, 10 * UI_UNIT_X, 10 * UI_UNIT_Y);
1208 }
1209
1210 /* Calculate/recalculate whole paths (avs.path_sf to avs.path_ef) */
1211 static int object_calculate_paths_exec(bContext *C, wmOperator *op)
1212 {
1213         Scene *scene = CTX_data_scene(C);
1214         int start = RNA_int_get(op->ptr, "start_frame");
1215         int end = RNA_int_get(op->ptr, "end_frame");
1216         
1217         /* set up path data for bones being calculated */
1218         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1219         {
1220                 bAnimVizSettings *avs = &ob->avs;
1221                 
1222                 /* grab baking settings from operator settings */
1223                 avs->path_sf = start;
1224                 avs->path_ef = end;
1225                 
1226                 /* verify that the selected object has the appropriate settings */
1227                 animviz_verify_motionpaths(op->reports, scene, ob, NULL);
1228         }
1229         CTX_DATA_END;
1230         
1231         /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1232         ED_objects_recalculate_paths(C, scene);
1233         
1234         /* notifiers for updates */
1235         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1236         
1237         return OPERATOR_FINISHED; 
1238 }
1239
1240 void OBJECT_OT_paths_calculate(wmOperatorType *ot)
1241 {
1242         /* identifiers */
1243         ot->name = "Calculate Object Paths";
1244         ot->idname = "OBJECT_OT_paths_calculate";
1245         ot->description = "Calculate motion paths for the selected objects";
1246         
1247         /* api callbacks */
1248         ot->invoke = object_calculate_paths_invoke;
1249         ot->exec = object_calculate_paths_exec;
1250         ot->poll = ED_operator_object_active_editable;
1251         
1252         /* flags */
1253         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1254         
1255         /* properties */
1256         RNA_def_int(ot->srna, "start_frame", 1, MINAFRAME, MAXFRAME, "Start", 
1257                     "First frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1258         RNA_def_int(ot->srna, "end_frame", 250, MINAFRAME, MAXFRAME, "End", 
1259                     "Last frame to calculate object paths on", MINFRAME, MAXFRAME / 2.0);
1260 }
1261
1262 /* --------- */
1263
1264 static int object_update_paths_exec(bContext *C, wmOperator *UNUSED(op))
1265 {
1266         Scene *scene = CTX_data_scene(C);
1267         
1268         if (scene == NULL)
1269                 return OPERATOR_CANCELLED;
1270                 
1271         /* calculate the paths for objects that have them (and are tagged to get refreshed) */
1272         ED_objects_recalculate_paths(C, scene);
1273         
1274         /* notifiers for updates */
1275         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1276         
1277         return OPERATOR_FINISHED;
1278 }
1279
1280 void OBJECT_OT_paths_update(wmOperatorType *ot)
1281 {
1282         /* identifiers */
1283         ot->name = "Update Object Paths";
1284         ot->idname = "OBJECT_OT_paths_update";
1285         ot->description = "Recalculate paths for selected objects";
1286         
1287         /* api callbakcs */
1288         ot->exec = object_update_paths_exec;
1289         ot->poll = ED_operator_object_active_editable; /* TODO: this should probably check for existing paths */
1290         
1291         /* flags */
1292         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1293 }
1294
1295 /* --------- */
1296
1297 /* Clear motion paths for selected objects only */
1298 void ED_objects_clear_paths(bContext *C)
1299 {
1300         /* loop over objects in scene */
1301         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1302         {
1303                 if (ob->mpath) {
1304                         animviz_free_motionpath(ob->mpath);
1305                         ob->mpath = NULL;
1306                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1307                 }
1308         }
1309         CTX_DATA_END;
1310 }
1311
1312 /* operator callback for this */
1313 static int object_clear_paths_exec(bContext *C, wmOperator *UNUSED(op))
1314 {       
1315         /* use the backend function for this */
1316         ED_objects_clear_paths(C);
1317         
1318         /* notifiers for updates */
1319         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1320         
1321         return OPERATOR_FINISHED; 
1322 }
1323
1324 void OBJECT_OT_paths_clear(wmOperatorType *ot)
1325 {
1326         /* identifiers */
1327         ot->name = "Clear Object Paths";
1328         ot->idname = "OBJECT_OT_paths_clear";
1329         ot->description = "Clear path caches for selected objects";
1330         
1331         /* api callbacks */
1332         ot->exec = object_clear_paths_exec;
1333         ot->poll = ED_operator_object_active_editable;
1334         
1335         /* flags */
1336         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1337 }
1338
1339
1340 /********************** Smooth/Flat *********************/
1341
1342 static int shade_smooth_exec(bContext *C, wmOperator *op)
1343 {
1344         ID *data;
1345         Curve *cu;
1346         Nurb *nu;
1347         int clear = (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1348         bool done = false, linked_data = false;
1349
1350         CTX_DATA_BEGIN(C, Object *, ob, selected_editable_objects)
1351         {
1352                 data = ob->data;
1353
1354                 if (data && data->lib) {
1355                         linked_data = true;
1356                         continue;
1357                 }
1358
1359                 if (ob->type == OB_MESH) {
1360                         BKE_mesh_smooth_flag_set(ob, !clear);
1361
1362                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1363                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1364
1365                         done = true;
1366                 }
1367                 else if (ELEM(ob->type, OB_SURF, OB_CURVE)) {
1368                         cu = ob->data;
1369
1370                         for (nu = cu->nurb.first; nu; nu = nu->next) {
1371                                 if (!clear) nu->flag |= ME_SMOOTH;
1372                                 else nu->flag &= ~ME_SMOOTH;
1373                         }
1374
1375                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1376                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1377
1378                         done = true;
1379                 }
1380         }
1381         CTX_DATA_END;
1382
1383         if (linked_data)
1384                 BKE_report(op->reports, RPT_WARNING, "Can't edit linked mesh or curve data.");
1385
1386         return (done) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1387 }
1388
1389 static int shade_poll(bContext *C)
1390 {
1391         return (CTX_data_edit_object(C) == NULL);
1392 }
1393
1394 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1395 {
1396         /* identifiers */
1397         ot->name = "Shade Flat";
1398         ot->description = "Render and display faces uniform, using Face Normals";
1399         ot->idname = "OBJECT_OT_shade_flat";
1400         
1401         /* api callbacks */
1402         ot->poll = shade_poll;
1403         ot->exec = shade_smooth_exec;
1404
1405         /* flags */
1406         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1407 }
1408
1409 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1410 {
1411         /* identifiers */
1412         ot->name = "Shade Smooth";
1413         ot->description = "Render and display faces smooth, using interpolated Vertex Normals";
1414         ot->idname = "OBJECT_OT_shade_smooth";
1415         
1416         /* api callbacks */
1417         ot->poll = shade_poll;
1418         ot->exec = shade_smooth_exec;
1419         
1420         /* flags */
1421         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1422 }
1423
1424 /* ********************** */
1425
1426 static void UNUSED_FUNCTION(image_aspect) (Scene *scene, View3D *v3d)
1427 {
1428         /* all selected objects with an image map: scale in image aspect */
1429         Base *base;
1430         Object *ob;
1431         Material *ma;
1432         Tex *tex;
1433         float x, y, space;
1434         int a, b, done;
1435         
1436         if (scene->obedit) return;  // XXX get from context
1437         if (scene->id.lib) return;
1438         
1439         for (base = FIRSTBASE; base; base = base->next) {
1440                 if (TESTBASELIB(v3d, base)) {
1441                         ob = base->object;
1442                         done = FALSE;
1443                         
1444                         for (a = 1; a <= ob->totcol; a++) {
1445                                 ma = give_current_material(ob, a);
1446                                 if (ma) {
1447                                         for (b = 0; b < MAX_MTEX; b++) {
1448                                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1449                                                         tex = ma->mtex[b]->tex;
1450                                                         if (tex->type == TEX_IMAGE && tex->ima) {
1451                                                                 ImBuf *ibuf = BKE_image_acquire_ibuf(tex->ima, NULL, NULL);
1452                                                                 
1453                                                                 /* texturespace */
1454                                                                 space = 1.0;
1455                                                                 if (ob->type == OB_MESH) {
1456                                                                         float size[3];
1457                                                                         BKE_mesh_texspace_get(ob->data, NULL, NULL, size);
1458                                                                         space = size[0] / size[1];
1459                                                                 }
1460                                                                 else if (ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1461                                                                         float size[3];
1462                                                                         BKE_curve_texspace_get(ob->data, NULL, NULL, size);
1463                                                                         space = size[0] / size[1];
1464                                                                 }
1465                                                         
1466                                                                 x = ibuf->x / space;
1467                                                                 y = ibuf->y;
1468                                                                 
1469                                                                 if (x > y) ob->size[0] = ob->size[1] * x / y;
1470                                                                 else ob->size[1] = ob->size[0] * y / x;
1471                                                                 
1472                                                                 done = TRUE;
1473                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1474
1475                                                                 BKE_image_release_ibuf(tex->ima, ibuf, NULL);
1476                                                         }
1477                                                 }
1478                                                 if (done) break;
1479                                         }
1480                                 }
1481                                 if (done) break;
1482                         }
1483                 }
1484         }
1485         
1486 }
1487
1488 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1489 {
1490         EnumPropertyItem *input = object_mode_items;
1491         EnumPropertyItem *item = NULL;
1492         Object *ob;
1493         int totitem = 0;
1494
1495         if (!C) /* needed for docs */
1496                 return object_mode_items;
1497
1498         ob = CTX_data_active_object(C);
1499         if (ob) {
1500                 const bool use_mode_particle_edit = (ob->particlesystem.first != NULL) || (ob->soft != NULL) ||
1501                                                     (modifiers_findByType(ob, eModifierType_Cloth) != NULL);
1502                 while (input->identifier) {
1503                         if ((input->value == OB_MODE_EDIT && OB_TYPE_SUPPORT_EDITMODE(ob->type)) ||
1504                             (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1505                             (input->value == OB_MODE_PARTICLE_EDIT && use_mode_particle_edit) ||
1506                             (ELEM4(input->value, OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT,
1507                                    OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1508                             (input->value == OB_MODE_OBJECT))
1509                         {
1510                                 RNA_enum_item_add(&item, &totitem, input);
1511                         }
1512                         input++;
1513                 }
1514         }
1515         else {
1516                 /* We need at least this one! */
1517                 RNA_enum_items_add_value(&item, &totitem, input, OB_MODE_OBJECT);
1518         }
1519
1520         RNA_enum_item_end(&item, &totitem);
1521
1522         *r_free = true;
1523
1524         return item;
1525 }
1526
1527 static const char *object_mode_op_string(int mode)
1528 {
1529         if (mode & OB_MODE_EDIT)
1530                 return "OBJECT_OT_editmode_toggle";
1531         if (mode == OB_MODE_SCULPT)
1532                 return "SCULPT_OT_sculptmode_toggle";
1533         if (mode == OB_MODE_VERTEX_PAINT)
1534                 return "PAINT_OT_vertex_paint_toggle";
1535         if (mode == OB_MODE_WEIGHT_PAINT)
1536                 return "PAINT_OT_weight_paint_toggle";
1537         if (mode == OB_MODE_TEXTURE_PAINT)
1538                 return "PAINT_OT_texture_paint_toggle";
1539         if (mode == OB_MODE_PARTICLE_EDIT)
1540                 return "PARTICLE_OT_particle_edit_toggle";
1541         if (mode == OB_MODE_POSE)
1542                 return "OBJECT_OT_posemode_toggle";
1543         return NULL;
1544 }
1545
1546 /* checks the mode to be set is compatible with the object
1547  * should be made into a generic function
1548  */
1549 static bool object_mode_compat_test(Object *ob, ObjectMode mode)
1550 {
1551         if (ob) {
1552                 if (mode == OB_MODE_OBJECT)
1553                         return true;
1554
1555                 switch (ob->type) {
1556                         case OB_MESH:
1557                                 if (mode & (OB_MODE_EDIT | OB_MODE_SCULPT | OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT |
1558                                             OB_MODE_TEXTURE_PAINT | OB_MODE_PARTICLE_EDIT))
1559                                 {
1560                                         return true;
1561                                 }
1562                                 break;
1563                         case OB_CURVE:
1564                         case OB_SURF:
1565                         case OB_FONT:
1566                         case OB_MBALL:
1567                                 if (mode & (OB_MODE_EDIT))
1568                                         return true;
1569                                 break;
1570                         case OB_LATTICE:
1571                                 if (mode & (OB_MODE_EDIT | OB_MODE_WEIGHT_PAINT))
1572                                         return true;
1573                                 break;
1574                         case OB_ARMATURE:
1575                                 if (mode & (OB_MODE_EDIT | OB_MODE_POSE))
1576                                         return true;
1577                                 break;
1578                 }
1579         }
1580
1581         return false;
1582 }
1583
1584 /**
1585  * Sets the mode to a compatible state (use before entering the mode).
1586  *
1587  * This is so each mode's exec function can call
1588  */
1589 bool ED_object_mode_compat_set(bContext *C, Object *ob, int mode, ReportList *reports)
1590 {
1591         bool ok;
1592         if (!ELEM(ob->mode, mode, OB_MODE_OBJECT)) {
1593                 const char *opstring = object_mode_op_string(ob->mode);
1594                 WM_operator_name_call(C, opstring, WM_OP_EXEC_REGION_WIN, NULL);
1595                 ok = ELEM(ob->mode, mode, OB_MODE_OBJECT);
1596                 if (!ok) {
1597                         wmOperatorType *ot = WM_operatortype_find(opstring, false);
1598                         BKE_reportf(reports, RPT_ERROR, "Unable to execute '%s', error changing modes", ot->name);
1599                 }
1600         }
1601         else {
1602                 ok = true;
1603         }
1604
1605         return ok;
1606 }
1607
1608 static int object_mode_set_exec(bContext *C, wmOperator *op)
1609 {
1610         Object *ob = CTX_data_active_object(C);
1611         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1612         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1613         int toggle = RNA_boolean_get(op->ptr, "toggle");
1614
1615         if (!ob || !object_mode_compat_test(ob, mode))
1616                 return OPERATOR_PASS_THROUGH;
1617
1618         if (ob->mode != mode) {
1619                 /* we should be able to remove this call, each operator calls  */
1620                 ED_object_mode_compat_set(C, ob, mode, op->reports);
1621         }
1622
1623         /* Exit current mode if it's not the mode we're setting */
1624         if (mode != OB_MODE_OBJECT && (ob->mode != mode || toggle)) {
1625                 /* Enter new mode */
1626                 WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
1627         }
1628
1629         if (toggle) {
1630                 /* Special case for Object mode! */
1631                 if (mode == OB_MODE_OBJECT && restore_mode == OB_MODE_OBJECT && ob->restore_mode != OB_MODE_OBJECT) {
1632                         WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1633                 }
1634                 else if (ob->mode == mode) {
1635                         /* For toggling, store old mode so we know what to go back to */
1636                         ob->restore_mode = restore_mode;
1637                 }
1638                 else if (ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1639                         WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1640                 }
1641         }
1642
1643         return OPERATOR_FINISHED;
1644 }
1645
1646 void OBJECT_OT_mode_set(wmOperatorType *ot)
1647 {
1648         PropertyRNA *prop;
1649         
1650         /* identifiers */
1651         ot->name = "Set Object Mode";
1652         ot->description = "Sets the object interaction mode";
1653         ot->idname = "OBJECT_OT_mode_set";
1654         
1655         /* api callbacks */
1656         ot->exec = object_mode_set_exec;
1657         
1658         ot->poll = ED_operator_object_active_editable;
1659         
1660         /* flags */
1661         ot->flag = 0; /* no register/undo here, leave it to operators being called */
1662         
1663         ot->prop = RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
1664         RNA_def_enum_funcs(ot->prop, object_mode_set_itemsf);
1665         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1666
1667         prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1668         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1669 }
1670
1671
1672
1673 void ED_object_toggle_modes(bContext *C, int mode)
1674 {
1675         /* Couldn't we use object_mode_op_string() here?
1676          * Also, if several bits are set in mode, several toggle ops will be called, is this expected?
1677          * If so, would be nice to explain why. ;) --mont29
1678          */
1679         if (mode & OB_MODE_SCULPT)
1680                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1681         if (mode & OB_MODE_VERTEX_PAINT)
1682                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1683         if (mode & OB_MODE_WEIGHT_PAINT)
1684                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1685         if (mode & OB_MODE_TEXTURE_PAINT)
1686                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1687         if (mode & OB_MODE_PARTICLE_EDIT)
1688                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1689         if (mode & OB_MODE_POSE)
1690                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1691         if (mode & OB_MODE_EDIT)
1692                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1693 }
1694
1695 /************************ Game Properties ***********************/
1696
1697 static int game_property_new_exec(bContext *C, wmOperator *op)
1698 {
1699         Object *ob = CTX_data_active_object(C);
1700         bProperty *prop;
1701         char name[MAX_NAME];
1702         int type = RNA_enum_get(op->ptr, "type");
1703
1704         prop = BKE_bproperty_new(type);
1705         BLI_addtail(&ob->prop, prop);
1706
1707         RNA_string_get(op->ptr, "name", name);
1708         if (name[0] != '\0') {
1709                 BLI_strncpy(prop->name, name, sizeof(prop->name));
1710         }
1711
1712         BKE_bproperty_unique(NULL, prop, 0); // make_unique_prop_names(prop->name);
1713
1714         WM_event_add_notifier(C, NC_LOGIC, NULL);
1715         return OPERATOR_FINISHED;
1716 }
1717
1718
1719 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1720 {
1721         /* identifiers */
1722         ot->name = "New Game Property";
1723         ot->description = "Create a new property available to the game engine";
1724         ot->idname = "OBJECT_OT_game_property_new";
1725
1726         /* api callbacks */
1727         ot->exec = game_property_new_exec;
1728         ot->poll = ED_operator_object_active_editable;
1729
1730         /* flags */
1731         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1732
1733         RNA_def_enum(ot->srna, "type", gameproperty_type_items, GPROP_FLOAT, "Type", "Type of game property to add");
1734         RNA_def_string(ot->srna, "name", "", MAX_NAME, "Name", "Name of the game property to add");
1735 }
1736
1737 static int game_property_remove_exec(bContext *C, wmOperator *op)
1738 {
1739         Object *ob = CTX_data_active_object(C);
1740         bProperty *prop;
1741         int index = RNA_int_get(op->ptr, "index");
1742
1743         if (!ob)
1744                 return OPERATOR_CANCELLED;
1745
1746         prop = BLI_findlink(&ob->prop, index);
1747
1748         if (prop) {
1749                 BLI_remlink(&ob->prop, prop);
1750                 BKE_bproperty_free(prop);
1751
1752                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1753                 return OPERATOR_FINISHED;
1754         }
1755         else {
1756                 return OPERATOR_CANCELLED;
1757         }
1758 }
1759
1760 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1761 {
1762         /* identifiers */
1763         ot->name = "Remove Game Property";
1764         ot->description = "Remove game property";
1765         ot->idname = "OBJECT_OT_game_property_remove";
1766
1767         /* api callbacks */
1768         ot->exec = game_property_remove_exec;
1769         ot->poll = ED_operator_object_active_editable;
1770
1771         /* flags */
1772         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1773
1774         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
1775 }
1776
1777 #define COPY_PROPERTIES_REPLACE 1
1778 #define COPY_PROPERTIES_MERGE   2
1779 #define COPY_PROPERTIES_COPY    3
1780
1781 static EnumPropertyItem game_properties_copy_operations[] = {
1782         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
1783         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
1784         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
1785         {0, NULL, 0, NULL, NULL}
1786 };
1787
1788 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1789 {       
1790         Object *ob = ED_object_active_context(C);
1791         EnumPropertyItem tmp = {0, "", 0, "", ""};
1792         EnumPropertyItem *item = NULL;
1793         bProperty *prop;
1794         int a, totitem = 0;
1795         
1796         if (!ob)
1797                 return DummyRNA_NULL_items;
1798
1799         for (a = 1, prop = ob->prop.first; prop; prop = prop->next, a++) {
1800                 tmp.value = a;
1801                 tmp.identifier = prop->name;
1802                 tmp.name = prop->name;
1803                 RNA_enum_item_add(&item, &totitem, &tmp);
1804         }
1805
1806         RNA_enum_item_end(&item, &totitem);
1807         *r_free = true;
1808
1809         return item;
1810 }
1811
1812 static int game_property_copy_exec(bContext *C, wmOperator *op)
1813 {
1814         Object *ob = ED_object_active_context(C);
1815         bProperty *prop;
1816         int type = RNA_enum_get(op->ptr, "operation");
1817         int propid = RNA_enum_get(op->ptr, "property");
1818
1819         if (propid > 0) { /* copy */
1820                 prop = BLI_findlink(&ob->prop, propid - 1);
1821                 
1822                 if (prop) {
1823                         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1824                         {
1825                                 if (ob != ob_iter)
1826                                         BKE_bproperty_object_set(ob_iter, prop);
1827                         } CTX_DATA_END;
1828                 }
1829         }
1830
1831         else {
1832                 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1833                 {
1834                         if (ob != ob_iter) {
1835                                 if (type == COPY_PROPERTIES_REPLACE) {
1836                                         BKE_bproperty_copy_list(&ob_iter->prop, &ob->prop);
1837                                 }
1838                                 else {
1839                                         /* merge - the default when calling with no argument */
1840                                         for (prop = ob->prop.first; prop; prop = prop->next) {
1841                                                 BKE_bproperty_object_set(ob_iter, prop);
1842                                         }
1843                                 }
1844                         }
1845                 }
1846                 CTX_DATA_END;
1847         }
1848
1849         return OPERATOR_FINISHED;
1850 }
1851
1852 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
1853 {
1854         PropertyRNA *prop;
1855         /* identifiers */
1856         ot->name = "Copy Game Property";
1857         ot->idname = "OBJECT_OT_game_property_copy";
1858         ot->description = "Copy/merge/replace a game property from active object to all selected objects";
1859
1860         /* api callbacks */
1861         ot->exec = game_property_copy_exec;
1862         ot->poll = ED_operator_object_active_editable;
1863
1864         /* flags */
1865         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1866
1867         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
1868         prop = RNA_def_enum(ot->srna, "property", DummyRNA_NULL_items, 0, "Property", "Properties to copy");
1869         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1870         RNA_def_enum_funcs(prop, gameprops_itemf);
1871         ot->prop = prop;
1872 }
1873
1874 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
1875 {
1876         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1877         {
1878                 BKE_bproperty_free_list(&ob_iter->prop);
1879         }
1880         CTX_DATA_END;
1881
1882         WM_event_add_notifier(C, NC_LOGIC, NULL);
1883         return OPERATOR_FINISHED;
1884 }
1885 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
1886 {
1887         /* identifiers */
1888         ot->name = "Clear Game Properties";
1889         ot->idname = "OBJECT_OT_game_property_clear";
1890         ot->description = "Remove all game properties from all selected objects";
1891
1892         /* api callbacks */
1893         ot->exec = game_property_clear_exec;
1894         ot->poll = ED_operator_object_active_editable;
1895
1896         /* flags */
1897         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1898 }
1899
1900 /************************ Copy Logic Bricks ***********************/
1901
1902 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
1903 {
1904         Object *ob = ED_object_active_context(C);
1905
1906         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1907         {
1908                 if (ob != ob_iter) {
1909                         /* first: free all logic */
1910                         free_sensors(&ob_iter->sensors);
1911                         unlink_controllers(&ob_iter->controllers);
1912                         free_controllers(&ob_iter->controllers);
1913                         unlink_actuators(&ob_iter->actuators);
1914                         free_actuators(&ob_iter->actuators);
1915                 
1916                         /* now copy it, this also works without logicbricks! */
1917                         clear_sca_new_poins_ob(ob);
1918                         copy_sensors(&ob_iter->sensors, &ob->sensors);
1919                         copy_controllers(&ob_iter->controllers, &ob->controllers);
1920                         copy_actuators(&ob_iter->actuators, &ob->actuators);
1921                         set_sca_new_poins_ob(ob_iter);
1922                 
1923                         /* some menu settings */
1924                         ob_iter->scavisflag = ob->scavisflag;
1925                         ob_iter->scaflag = ob->scaflag;
1926                 
1927                         /* set the initial state */
1928                         ob_iter->state = ob->state;
1929                         ob_iter->init_state = ob->init_state;
1930
1931                         if (ob_iter->totcol == ob->totcol) {
1932                                 ob_iter->actcol = ob->actcol;
1933                                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob_iter);
1934                         }
1935                 }
1936         }
1937         CTX_DATA_END;
1938
1939         WM_event_add_notifier(C, NC_LOGIC, NULL);
1940
1941         return OPERATOR_FINISHED;
1942 }
1943
1944 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
1945 {
1946         /* identifiers */
1947         ot->name = "Copy Logic Bricks to Selected";
1948         ot->description = "Copy logic bricks to other selected objects";
1949         ot->idname = "OBJECT_OT_logic_bricks_copy";
1950
1951         /* api callbacks */
1952         ot->exec = logicbricks_copy_exec;
1953         ot->poll = ED_operator_object_active_editable;
1954
1955         /* flags */
1956         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1957 }
1958
1959 static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op))
1960 {
1961         Object *ob = ED_object_active_context(C);
1962         
1963         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
1964         {
1965                 if (ob != ob_iter) {
1966                         ob_iter->gameflag = ob->gameflag;
1967                         ob_iter->gameflag2 = ob->gameflag2;
1968                         ob_iter->inertia = ob->inertia;
1969                         ob_iter->formfactor = ob->formfactor;
1970                         ob_iter->damping = ob->damping;
1971                         ob_iter->rdamping = ob->rdamping;
1972                         ob_iter->min_vel = ob->min_vel;
1973                         ob_iter->max_vel = ob->max_vel;
1974                         ob_iter->obstacleRad = ob->obstacleRad;
1975                         ob_iter->mass = ob->mass;
1976                         copy_v3_v3(ob_iter->anisotropicFriction, ob->anisotropicFriction);
1977                         ob_iter->collision_boundtype = ob->collision_boundtype;
1978                         ob_iter->margin = ob->margin;
1979                         ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft);
1980                         if (ob->restrictflag & OB_RESTRICT_RENDER) 
1981                                 ob_iter->restrictflag |= OB_RESTRICT_RENDER;
1982                         else
1983                                 ob_iter->restrictflag &= ~OB_RESTRICT_RENDER;
1984
1985                         ob_iter->col_group = ob->col_group;
1986                         ob_iter->col_mask = ob->col_mask;
1987                 }
1988         }
1989         CTX_DATA_END;
1990         
1991         return OPERATOR_FINISHED;
1992 }
1993
1994 void OBJECT_OT_game_physics_copy(struct wmOperatorType *ot)
1995 {
1996         /* identifiers */
1997         ot->name = "Copy Game Physics Properties to Selected";
1998         ot->description = "Copy game physics properties to other selected objects";
1999         ot->idname = "OBJECT_OT_game_physics_copy";
2000         
2001         /* api callbacks */
2002         ot->exec = game_physics_copy_exec;
2003         ot->poll = ED_operator_object_active_editable;
2004         
2005         /* flags */
2006         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2007 }