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