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