4a96a2e220090c669aa0507fc3e4c6b18d51df87
[blender-staging.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_DATABLOCK(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_DATABLOCK(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_DATABLOCK(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_DATABLOCK(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_DATABLOCK(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_DATABLOCK(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 EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1578 {
1579         EnumPropertyItem *input = rna_enum_object_mode_items;
1580         EnumPropertyItem *item = NULL;
1581         Object *ob;
1582         bGPdata *gpd;
1583         int totitem = 0;
1584
1585         if (!C) /* needed for docs */
1586                 return rna_enum_object_mode_items;
1587
1588         ob = CTX_data_active_object(C);
1589         if (ob) {
1590                 const bool use_mode_particle_edit = (BLI_listbase_is_empty(&ob->particlesystem) == false) ||
1591                                                     (ob->soft != NULL) ||
1592                                                     (modifiers_findByType(ob, eModifierType_Cloth) != NULL);
1593                 while (input->identifier) {
1594                         if ((input->value == OB_MODE_EDIT && OB_TYPE_SUPPORT_EDITMODE(ob->type)) ||
1595                             (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1596                             (input->value == OB_MODE_PARTICLE_EDIT && use_mode_particle_edit) ||
1597                             (ELEM(input->value, OB_MODE_SCULPT, OB_MODE_VERTEX_PAINT,
1598                                    OB_MODE_WEIGHT_PAINT, OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1599                             (input->value == OB_MODE_OBJECT))
1600                         {
1601                                 RNA_enum_item_add(&item, &totitem, input);
1602                         }
1603                         input++;
1604                 }
1605         }
1606         else {
1607                 /* We need at least this one! */
1608                 RNA_enum_items_add_value(&item, &totitem, input, OB_MODE_OBJECT);
1609         }
1610         
1611         /* On top of all the rest, GPencil Stroke Edit Mode
1612          * is available if there's a valid gp datablock...
1613          */
1614         gpd = CTX_data_gpencil_data(C);
1615         if (gpd) {
1616                 RNA_enum_items_add_value(&item, &totitem, rna_enum_object_mode_items, OB_MODE_GPENCIL);
1617         }
1618
1619         RNA_enum_item_end(&item, &totitem);
1620
1621         *r_free = true;
1622
1623         return item;
1624 }
1625
1626 static const char *object_mode_op_string(int mode)
1627 {
1628         if (mode & OB_MODE_EDIT)
1629                 return "OBJECT_OT_editmode_toggle";
1630         if (mode == OB_MODE_SCULPT)
1631                 return "SCULPT_OT_sculptmode_toggle";
1632         if (mode == OB_MODE_VERTEX_PAINT)
1633                 return "PAINT_OT_vertex_paint_toggle";
1634         if (mode == OB_MODE_WEIGHT_PAINT)
1635                 return "PAINT_OT_weight_paint_toggle";
1636         if (mode == OB_MODE_TEXTURE_PAINT)
1637                 return "PAINT_OT_texture_paint_toggle";
1638         if (mode == OB_MODE_PARTICLE_EDIT)
1639                 return "PARTICLE_OT_particle_edit_toggle";
1640         if (mode == OB_MODE_POSE)
1641                 return "OBJECT_OT_posemode_toggle";
1642         if (mode == OB_MODE_GPENCIL)
1643                 return "GPENCIL_OT_editmode_toggle";
1644         return NULL;
1645 }
1646
1647 /* checks the mode to be set is compatible with the object
1648  * should be made into a generic function
1649  */
1650 static bool object_mode_compat_test(Object *ob, ObjectMode mode)
1651 {
1652         if (ob) {
1653                 if (mode == OB_MODE_OBJECT)
1654                         return true;
1655                 else if (mode == OB_MODE_GPENCIL)
1656                         return true; /* XXX: assume this is the case for now... */
1657
1658                 switch (ob->type) {
1659                         case OB_MESH:
1660                                 if (mode & (OB_MODE_EDIT | OB_MODE_SCULPT | OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT |
1661                                             OB_MODE_TEXTURE_PAINT | OB_MODE_PARTICLE_EDIT))
1662                                 {
1663                                         return true;
1664                                 }
1665                                 break;
1666                         case OB_CURVE:
1667                         case OB_SURF:
1668                         case OB_FONT:
1669                         case OB_MBALL:
1670                                 if (mode & (OB_MODE_EDIT))
1671                                         return true;
1672                                 break;
1673                         case OB_LATTICE:
1674                                 if (mode & (OB_MODE_EDIT | OB_MODE_WEIGHT_PAINT))
1675                                         return true;
1676                                 break;
1677                         case OB_ARMATURE:
1678                                 if (mode & (OB_MODE_EDIT | OB_MODE_POSE))
1679                                         return true;
1680                                 break;
1681                 }
1682         }
1683
1684         return false;
1685 }
1686
1687 /**
1688  * Sets the mode to a compatible state (use before entering the mode).
1689  *
1690  * This is so each mode's exec function can call
1691  */
1692 bool ED_object_mode_compat_set(bContext *C, Object *ob, int mode, ReportList *reports)
1693 {
1694         bool ok;
1695         if (!ELEM(ob->mode, mode, OB_MODE_OBJECT)) {
1696                 const char *opstring = object_mode_op_string(ob->mode);
1697                 WM_operator_name_call(C, opstring, WM_OP_EXEC_REGION_WIN, NULL);
1698                 ok = ELEM(ob->mode, mode, OB_MODE_OBJECT);
1699                 if (!ok) {
1700                         wmOperatorType *ot = WM_operatortype_find(opstring, false);
1701                         BKE_reportf(reports, RPT_ERROR, "Unable to execute '%s', error changing modes", ot->name);
1702                 }
1703         }
1704         else {
1705                 ok = true;
1706         }
1707
1708         return ok;
1709 }
1710
1711 static int object_mode_set_poll(bContext *C)
1712 {
1713         /* Since Grease Pencil editmode is also handled here,
1714          * we have a special exception for allowing this operator
1715          * to still work in that case when there's no active object
1716          * so that users can exit editmode this way as per normal.
1717          */
1718         if (ED_operator_object_active_editable(C))
1719                 return true;
1720         else
1721                 return (CTX_data_gpencil_data(C) != NULL);
1722 }
1723
1724 static int object_mode_set_exec(bContext *C, wmOperator *op)
1725 {
1726         Object *ob = CTX_data_active_object(C);
1727         bGPdata *gpd = CTX_data_gpencil_data(C);
1728         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1729         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1730         const bool toggle = RNA_boolean_get(op->ptr, "toggle");
1731         
1732         if (gpd) {
1733                 /* GP Mode is not bound to a specific object. Therefore,
1734                  * we don't want it to be actually saved on any objects,
1735                  * as weirdness can happen if you select other objects,
1736                  * or load old files.
1737                  *
1738                  * Instead, we use the following 2 rules to ensure that
1739                  * the mode selector works as expected:
1740                  *  1) If there's no object, we want to enter editmode.
1741                  *     (i.e. with no object, we're in object mode)
1742                  *  2) Otherwise, exit stroke editmode, so that we can
1743                  *     enter another mode...
1744                  */
1745                 if (!ob || (gpd->flag & GP_DATA_STROKE_EDITMODE)) {
1746                         WM_operator_name_call(C, "GPENCIL_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1747                 }
1748         }
1749         
1750         if (!ob || !object_mode_compat_test(ob, mode))
1751                 return OPERATOR_PASS_THROUGH;
1752
1753         if (ob->mode != mode) {
1754                 /* we should be able to remove this call, each operator calls  */
1755                 ED_object_mode_compat_set(C, ob, mode, op->reports);
1756         }
1757
1758         /* Exit current mode if it's not the mode we're setting */
1759         if (mode != OB_MODE_OBJECT && (ob->mode != mode || toggle)) {
1760                 /* Enter new mode */
1761                 ED_object_toggle_modes(C, mode);
1762         }
1763
1764         if (toggle) {
1765                 /* Special case for Object mode! */
1766                 if (mode == OB_MODE_OBJECT && restore_mode == OB_MODE_OBJECT && ob->restore_mode != OB_MODE_OBJECT) {
1767                         ED_object_toggle_modes(C, ob->restore_mode);
1768                 }
1769                 else if (ob->mode == mode) {
1770                         /* For toggling, store old mode so we know what to go back to */
1771                         ob->restore_mode = restore_mode;
1772                 }
1773                 else if (ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1774                         ED_object_toggle_modes(C, ob->restore_mode);
1775                 }
1776         }
1777
1778         return OPERATOR_FINISHED;
1779 }
1780
1781 void OBJECT_OT_mode_set(wmOperatorType *ot)
1782 {
1783         PropertyRNA *prop;
1784         
1785         /* identifiers */
1786         ot->name = "Set Object Mode";
1787         ot->description = "Sets the object interaction mode";
1788         ot->idname = "OBJECT_OT_mode_set";
1789         
1790         /* api callbacks */
1791         ot->exec = object_mode_set_exec;
1792         
1793         ot->poll = object_mode_set_poll; //ED_operator_object_active_editable;
1794         
1795         /* flags */
1796         ot->flag = 0; /* no register/undo here, leave it to operators being called */
1797         
1798         ot->prop = RNA_def_enum(ot->srna, "mode", rna_enum_object_mode_items, OB_MODE_OBJECT, "Mode", "");
1799         RNA_def_enum_funcs(ot->prop, object_mode_set_itemsf);
1800         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
1801
1802         prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1803         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1804 }
1805
1806
1807
1808 void ED_object_toggle_modes(bContext *C, int mode)
1809 {
1810         if (mode != OB_MODE_OBJECT) {
1811                 const char *opstring = object_mode_op_string(mode);
1812                 if (opstring) {
1813                         WM_operator_name_call(C, opstring, WM_OP_EXEC_REGION_WIN, NULL);
1814                 }
1815         }
1816 }
1817
1818 /************************ Game Properties ***********************/
1819
1820 static int game_property_new_exec(bContext *C, wmOperator *op)
1821 {
1822         Object *ob = CTX_data_active_object(C);
1823         bProperty *prop;
1824         char name[MAX_NAME];
1825         int type = RNA_enum_get(op->ptr, "type");
1826
1827         prop = BKE_bproperty_new(type);
1828         BLI_addtail(&ob->prop, prop);
1829
1830         RNA_string_get(op->ptr, "name", name);
1831         if (name[0] != '\0') {
1832                 BLI_strncpy(prop->name, name, sizeof(prop->name));
1833         }
1834
1835         BLI_uniquename(&ob->prop, prop, DATA_("Property"), '.', offsetof(bProperty, name), sizeof(prop->name));
1836
1837         WM_event_add_notifier(C, NC_LOGIC, NULL);
1838         return OPERATOR_FINISHED;
1839 }
1840
1841
1842 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1843 {
1844         /* identifiers */
1845         ot->name = "New Game Property";
1846         ot->description = "Create a new property available to the game engine";
1847         ot->idname = "OBJECT_OT_game_property_new";
1848
1849         /* api callbacks */
1850         ot->exec = game_property_new_exec;
1851         ot->poll = ED_operator_object_active_editable;
1852
1853         /* flags */
1854         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1855
1856         RNA_def_enum(ot->srna, "type", rna_enum_gameproperty_type_items, GPROP_FLOAT, "Type", "Type of game property to add");
1857         RNA_def_string(ot->srna, "name", NULL, MAX_NAME, "Name", "Name of the game property to add");
1858 }
1859
1860 static int game_property_remove_exec(bContext *C, wmOperator *op)
1861 {
1862         Object *ob = CTX_data_active_object(C);
1863         bProperty *prop;
1864         int index = RNA_int_get(op->ptr, "index");
1865
1866         if (!ob)
1867                 return OPERATOR_CANCELLED;
1868
1869         prop = BLI_findlink(&ob->prop, index);
1870
1871         if (prop) {
1872                 BLI_remlink(&ob->prop, prop);
1873                 BKE_bproperty_free(prop);
1874
1875                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1876                 return OPERATOR_FINISHED;
1877         }
1878         else {
1879                 return OPERATOR_CANCELLED;
1880         }
1881 }
1882
1883 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1884 {
1885         /* identifiers */
1886         ot->name = "Remove Game Property";
1887         ot->description = "Remove game property";
1888         ot->idname = "OBJECT_OT_game_property_remove";
1889
1890         /* api callbacks */
1891         ot->exec = game_property_remove_exec;
1892         ot->poll = ED_operator_object_active_editable;
1893
1894         /* flags */
1895         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1896
1897         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
1898 }
1899
1900 #define GAME_PROPERTY_MOVE_UP    1
1901 #define GAME_PROPERTY_MOVE_DOWN -1
1902
1903 static int game_property_move(bContext *C, wmOperator *op)
1904 {
1905         Object *ob = CTX_data_active_object(C);
1906         bProperty *prop;
1907         bProperty *otherprop = NULL;
1908         const int index = RNA_int_get(op->ptr, "index");
1909         const int dir = RNA_enum_get(op->ptr, "direction");
1910
1911         if (ob == NULL)
1912                 return OPERATOR_CANCELLED;
1913
1914         prop = BLI_findlink(&ob->prop, index);
1915         /* invalid index */
1916         if (prop == NULL)
1917                 return OPERATOR_CANCELLED;
1918
1919         if (dir == GAME_PROPERTY_MOVE_UP) {
1920                 otherprop = prop->prev;
1921         }
1922         else if (dir == GAME_PROPERTY_MOVE_DOWN) {
1923                 otherprop = prop->next;
1924         }
1925         else {
1926                 BLI_assert(0);
1927         }
1928
1929         if (prop && otherprop) {
1930                 BLI_listbase_swaplinks(&ob->prop, prop, otherprop);
1931
1932                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1933                 return OPERATOR_FINISHED;
1934         }
1935         else {
1936                 return OPERATOR_CANCELLED;
1937         }
1938 }
1939
1940 void OBJECT_OT_game_property_move(wmOperatorType *ot)
1941 {
1942         static EnumPropertyItem direction_property_move[] = {
1943                 {GAME_PROPERTY_MOVE_UP,   "UP",   0, "Up",   ""},
1944                 {GAME_PROPERTY_MOVE_DOWN, "DOWN", 0, "Down", ""},
1945                 {0, NULL, 0, NULL, NULL}
1946         };
1947         PropertyRNA *prop;
1948
1949         /* identifiers */
1950         ot->name = "Move Game Property";
1951         ot->description = "Move game property";
1952         ot->idname = "OBJECT_OT_game_property_move";
1953
1954         /* api callbacks */
1955         ot->exec = game_property_move;
1956         ot->poll = ED_operator_object_active_editable;
1957
1958         /* flags */
1959         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1960
1961         /* properties */
1962         prop = RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to move", 0, INT_MAX);
1963         RNA_def_property_flag(prop, PROP_HIDDEN);
1964         RNA_def_enum(ot->srna, "direction", direction_property_move, 0, "Direction",
1965                      "Direction for moving the property");
1966 }
1967
1968 #undef GAME_PROPERTY_MOVE_UP
1969 #undef GAME_PROPERTY_MOVE_DOWN
1970
1971 #define COPY_PROPERTIES_REPLACE 1
1972 #define COPY_PROPERTIES_MERGE   2
1973 #define COPY_PROPERTIES_COPY    3
1974
1975 static EnumPropertyItem game_properties_copy_operations[] = {
1976         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
1977         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
1978         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
1979         {0, NULL, 0, NULL, NULL}
1980 };
1981
1982 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1983 {       
1984         Object *ob = ED_object_active_context(C);
1985         EnumPropertyItem tmp = {0, "", 0, "", ""};
1986         EnumPropertyItem *item = NULL;
1987         bProperty *prop;
1988         int a, totitem = 0;
1989         
1990         if (!ob)
1991                 return DummyRNA_NULL_items;
1992
1993         for (a = 1, prop = ob->prop.first; prop; prop = prop->next, a++) {
1994                 tmp.value = a;
1995                 tmp.identifier = prop->name;
1996                 tmp.name = prop->name;
1997                 RNA_enum_item_add(&item, &totitem, &tmp);
1998         }
1999
2000         RNA_enum_item_end(&item, &totitem);
2001         *r_free = true;
2002
2003         return item;
2004 }
2005
2006 static int game_property_copy_exec(bContext *C, wmOperator *op)
2007 {
2008         Object *ob = ED_object_active_context(C);
2009         bProperty *prop;
2010         int type = RNA_enum_get(op->ptr, "operation");
2011         int propid = RNA_enum_get(op->ptr, "property");
2012
2013         if (propid > 0) { /* copy */
2014                 prop = BLI_findlink(&ob->prop, propid - 1);
2015                 
2016                 if (prop) {
2017                         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2018                         {
2019                                 if (ob != ob_iter)
2020                                         BKE_bproperty_object_set(ob_iter, prop);
2021                         } CTX_DATA_END;
2022                 }
2023         }
2024
2025         else {
2026                 CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2027                 {
2028                         if (ob != ob_iter) {
2029                                 if (type == COPY_PROPERTIES_REPLACE) {
2030                                         BKE_bproperty_copy_list(&ob_iter->prop, &ob->prop);
2031                                 }
2032                                 else {
2033                                         /* merge - the default when calling with no argument */
2034                                         for (prop = ob->prop.first; prop; prop = prop->next) {
2035                                                 BKE_bproperty_object_set(ob_iter, prop);
2036                                         }
2037                                 }
2038                         }
2039                 }
2040                 CTX_DATA_END;
2041         }
2042
2043         return OPERATOR_FINISHED;
2044 }
2045
2046 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
2047 {
2048         PropertyRNA *prop;
2049         /* identifiers */
2050         ot->name = "Copy Game Property";
2051         ot->idname = "OBJECT_OT_game_property_copy";
2052         ot->description = "Copy/merge/replace a game property from active object to all selected objects";
2053
2054         /* api callbacks */
2055         ot->exec = game_property_copy_exec;
2056         ot->poll = ED_operator_object_active_editable;
2057
2058         /* flags */
2059         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2060
2061         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
2062         prop = RNA_def_enum(ot->srna, "property", DummyRNA_NULL_items, 0, "Property", "Properties to copy");
2063         RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_ENUM_NO_TRANSLATE);
2064         RNA_def_enum_funcs(prop, gameprops_itemf);
2065         ot->prop = prop;
2066 }
2067
2068 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
2069 {
2070         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2071         {
2072                 BKE_bproperty_free_list(&ob_iter->prop);
2073         }
2074         CTX_DATA_END;
2075
2076         WM_event_add_notifier(C, NC_LOGIC, NULL);
2077         return OPERATOR_FINISHED;
2078 }
2079 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
2080 {
2081         /* identifiers */
2082         ot->name = "Clear Game Properties";
2083         ot->idname = "OBJECT_OT_game_property_clear";
2084         ot->description = "Remove all game properties from all selected objects";
2085
2086         /* api callbacks */
2087         ot->exec = game_property_clear_exec;
2088         ot->poll = ED_operator_object_active_editable;
2089
2090         /* flags */
2091         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2092 }
2093
2094 /************************ Copy Logic Bricks ***********************/
2095
2096 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
2097 {
2098         Object *ob = ED_object_active_context(C);
2099
2100         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2101         {
2102                 if (ob != ob_iter) {
2103                         /* first: free all logic */
2104                         free_sensors(&ob_iter->sensors);
2105                         unlink_controllers(&ob_iter->controllers);
2106                         free_controllers(&ob_iter->controllers);
2107                         unlink_actuators(&ob_iter->actuators);
2108                         free_actuators(&ob_iter->actuators);
2109                 
2110                         /* now copy it, this also works without logicbricks! */
2111                         clear_sca_new_poins_ob(ob);
2112                         copy_sensors(&ob_iter->sensors, &ob->sensors, 0);
2113                         copy_controllers(&ob_iter->controllers, &ob->controllers, 0);
2114                         copy_actuators(&ob_iter->actuators, &ob->actuators, 0);
2115                         set_sca_new_poins_ob(ob_iter);
2116                 
2117                         /* some menu settings */
2118                         ob_iter->scavisflag = ob->scavisflag;
2119                         ob_iter->scaflag = ob->scaflag;
2120                 
2121                         /* set the initial state */
2122                         ob_iter->state = ob->state;
2123                         ob_iter->init_state = ob->init_state;
2124
2125                         if (ob_iter->totcol == ob->totcol) {
2126                                 ob_iter->actcol = ob->actcol;
2127                                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob_iter);
2128                         }
2129                 }
2130         }
2131         CTX_DATA_END;
2132
2133         WM_event_add_notifier(C, NC_LOGIC, NULL);
2134
2135         return OPERATOR_FINISHED;
2136 }
2137
2138 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
2139 {
2140         /* identifiers */
2141         ot->name = "Copy Logic Bricks to Selected";
2142         ot->description = "Copy logic bricks to other selected objects";
2143         ot->idname = "OBJECT_OT_logic_bricks_copy";
2144
2145         /* api callbacks */
2146         ot->exec = logicbricks_copy_exec;
2147         ot->poll = ED_operator_object_active_editable;
2148
2149         /* flags */
2150         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2151 }
2152
2153 static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op))
2154 {
2155         Object *ob = ED_object_active_context(C);
2156         
2157         CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects)
2158         {
2159                 if (ob != ob_iter) {
2160                         ob_iter->gameflag = ob->gameflag;
2161                         ob_iter->gameflag2 = ob->gameflag2;
2162                         ob_iter->inertia = ob->inertia;
2163                         ob_iter->formfactor = ob->formfactor;
2164                         ob_iter->damping = ob->damping;
2165                         ob_iter->rdamping = ob->rdamping;
2166                         ob_iter->min_vel = ob->min_vel;
2167                         ob_iter->max_vel = ob->max_vel;
2168                         ob_iter->min_angvel = ob->min_angvel;
2169                         ob_iter->max_angvel = ob->max_angvel;
2170                         ob_iter->obstacleRad = ob->obstacleRad;
2171                         ob_iter->mass = ob->mass;
2172                         copy_v3_v3(ob_iter->anisotropicFriction, ob->anisotropicFriction);
2173                         ob_iter->collision_boundtype = ob->collision_boundtype;
2174                         ob_iter->margin = ob->margin;
2175                         ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft, 0);
2176                         if (ob->restrictflag & OB_RESTRICT_RENDER) 
2177                                 ob_iter->restrictflag |= OB_RESTRICT_RENDER;
2178                         else
2179                                 ob_iter->restrictflag &= ~OB_RESTRICT_RENDER;
2180
2181                         ob_iter->col_group = ob->col_group;
2182                         ob_iter->col_mask = ob->col_mask;
2183                 }
2184         }
2185         CTX_DATA_END;
2186         
2187         return OPERATOR_FINISHED;
2188 }
2189
2190 void OBJECT_OT_game_physics_copy(struct wmOperatorType *ot)
2191 {
2192         /* identifiers */
2193         ot->name = "Copy Game Physics Properties to Selected";
2194         ot->description = "Copy game physics properties to other selected objects";
2195         ot->idname = "OBJECT_OT_game_physics_copy";
2196         
2197         /* api callbacks */
2198         ot->exec = game_physics_copy_exec;
2199         ot->poll = ED_operator_object_active_editable;
2200         
2201         /* flags */
2202         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2203 }
2204
2205 /* generic utility function */
2206
2207 bool ED_object_editmode_calc_active_center(Object *obedit, const bool select_only, float r_center[3])
2208 {
2209         switch (obedit->type) {
2210                 case OB_MESH:
2211                 {
2212                         BMEditMesh *em = BKE_editmesh_from_object(obedit);
2213                         BMEditSelection ese;
2214
2215                         if (BM_select_history_active_get(em->bm, &ese)) {
2216                                 BM_editselection_center(&ese, r_center);
2217                                 return true;
2218                         }
2219                         break;
2220                 }
2221                 case OB_ARMATURE:
2222                 {
2223                         bArmature *arm = obedit->data;
2224                         EditBone *ebo = arm->act_edbone;
2225
2226                         if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
2227                                 copy_v3_v3(r_center, ebo->head);
2228                                 return true;
2229                         }
2230
2231                         break;
2232                 }
2233                 case OB_CURVE:
2234                 case OB_SURF:
2235                 {
2236                         Curve *cu = obedit->data;
2237
2238                         if (ED_curve_active_center(cu, r_center)) {
2239                                 return true;
2240                         }
2241                         break;
2242                 }
2243                 case OB_MBALL:
2244                 {
2245                         MetaBall *mb = obedit->data;
2246                         MetaElem *ml_act = mb->lastelem;
2247
2248                         if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
2249                                 copy_v3_v3(r_center, &ml_act->x);
2250                                 return true;
2251                         }
2252                         break;
2253                 }
2254                 case OB_LATTICE:
2255                 {
2256                         BPoint *actbp = BKE_lattice_active_point_get(obedit->data);
2257
2258                         if (actbp) {
2259                                 copy_v3_v3(r_center, actbp->vec);
2260                                 return true;
2261                         }
2262                         break;
2263                 }
2264         }
2265
2266         return false;
2267 }