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