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