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