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