Merging r46111 through r46136 from trunk into soc-2011-tomato
[blender-staging.git] / source / blender / editors / object / object_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_edit.c
27  *  \ingroup edobj
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33 #include <time.h>
34 #include <float.h>
35 #include <ctype.h>
36 #include <stddef.h> //for offsetof
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_ghash.h"
44 #include "BLI_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 object with pose/action: 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 the relevant bones to target */
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 /* For the object with pose/action: create path curves for selected bones 
1129  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1130  */
1131 static int object_calculate_paths_exec(bContext *C, wmOperator *op)
1132 {
1133         Scene *scene = CTX_data_scene(C);
1134         
1135         /* set up path data for bones being calculated */
1136         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1137         {
1138                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1139                 animviz_verify_motionpaths(op->reports, scene, ob, NULL);
1140         }
1141         CTX_DATA_END;
1142         
1143         /* calculate the bones that now have motionpaths... */
1144         // TODO: only make for the selected bones?
1145         ED_objects_recalculate_paths(C, scene);
1146         
1147         /* notifiers for updates */
1148         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1149         
1150         return OPERATOR_FINISHED; 
1151 }
1152
1153 void OBJECT_OT_paths_calculate(wmOperatorType *ot)
1154 {
1155         /* identifiers */
1156         ot->name = "Calculate Object Paths";
1157         ot->idname = "OBJECT_OT_paths_calculate";
1158         ot->description = "Calculate paths for the selected bones";
1159         
1160         /* api callbacks */
1161         ot->exec = object_calculate_paths_exec;
1162         ot->poll = ED_operator_object_active_editable;
1163         
1164         /* flags */
1165         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1166 }
1167
1168 /* --------- */
1169
1170 /* for the object with pose/action: clear path curves for selected bones only */
1171 void ED_objects_clear_paths(bContext *C)
1172 {
1173         /* loop over objects in scene */
1174         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1175         {
1176                 if (ob->mpath) {
1177                         animviz_free_motionpath(ob->mpath);
1178                         ob->mpath = NULL;
1179                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1180                 }
1181         }
1182         CTX_DATA_END;
1183 }
1184
1185 /* operator callback for this */
1186 static int object_clear_paths_exec(bContext *C, wmOperator *UNUSED(op))
1187 {       
1188         /* use the backend function for this */
1189         ED_objects_clear_paths(C);
1190         
1191         /* notifiers for updates */
1192         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1193         
1194         return OPERATOR_FINISHED; 
1195 }
1196
1197 void OBJECT_OT_paths_clear(wmOperatorType *ot)
1198 {
1199         /* identifiers */
1200         ot->name = "Clear Object Paths";
1201         ot->idname = "OBJECT_OT_paths_clear";
1202         ot->description = "Clear path caches for selected bones";
1203         
1204         /* api callbacks */
1205         ot->exec = object_clear_paths_exec;
1206         ot->poll = ED_operator_object_active_editable;
1207         
1208         /* flags */
1209         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1210 }
1211
1212
1213 /********************** Smooth/Flat *********************/
1214
1215 static int shade_smooth_exec(bContext *C, wmOperator *op)
1216 {
1217         Curve *cu;
1218         Nurb *nu;
1219         int clear = (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1220         int done = 0;
1221
1222         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1223         {
1224
1225                 if (ob->type == OB_MESH) {
1226                         mesh_set_smooth_flag(ob, !clear);
1227
1228                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1229                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1230
1231                         done = 1;
1232                 }
1233                 else if (ELEM(ob->type, OB_SURF, OB_CURVE)) {
1234                         cu = ob->data;
1235
1236                         for (nu = cu->nurb.first; nu; nu = nu->next) {
1237                                 if (!clear) nu->flag |= ME_SMOOTH;
1238                                 else nu->flag &= ~ME_SMOOTH;
1239                         }
1240
1241                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1242                         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
1243
1244                         done = 1;
1245                 }
1246         }
1247         CTX_DATA_END;
1248
1249         return (done) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1250 }
1251
1252 static int shade_poll(bContext *C)
1253 {
1254         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
1255 }
1256
1257 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1258 {
1259         /* identifiers */
1260         ot->name = "Shade Flat";
1261         ot->description = "Display faces 'flat'";
1262         ot->idname = "OBJECT_OT_shade_flat";
1263         
1264         /* api callbacks */
1265         ot->poll = shade_poll;
1266         ot->exec = shade_smooth_exec;
1267
1268         /* flags */
1269         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1270 }
1271
1272 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1273 {
1274         /* identifiers */
1275         ot->name = "Shade Smooth";
1276         ot->description = "Display faces 'smooth' (using vertex normals)";
1277         ot->idname = "OBJECT_OT_shade_smooth";
1278         
1279         /* api callbacks */
1280         ot->poll = shade_poll;
1281         ot->exec = shade_smooth_exec;
1282         
1283         /* flags */
1284         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1285 }
1286
1287 /* ********************** */
1288
1289 static void UNUSED_FUNCTION(image_aspect) (Scene * scene, View3D * v3d)
1290 {
1291         /* all selected objects with an image map: scale in image aspect */
1292         Base *base;
1293         Object *ob;
1294         Material *ma;
1295         Tex *tex;
1296         float x, y, space;
1297         int a, b, done;
1298         
1299         if (scene->obedit) return;  // XXX get from context
1300         if (scene->id.lib) return;
1301         
1302         for (base = FIRSTBASE; base; base = base->next) {
1303                 if (TESTBASELIB(v3d, base)) {
1304                         ob = base->object;
1305                         done = 0;
1306                         
1307                         for (a = 1; a <= ob->totcol; a++) {
1308                                 ma = give_current_material(ob, a);
1309                                 if (ma) {
1310                                         for (b = 0; b < MAX_MTEX; b++) {
1311                                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1312                                                         tex = ma->mtex[b]->tex;
1313                                                         if (tex->type == TEX_IMAGE && tex->ima) {
1314                                                                 ImBuf *ibuf = BKE_image_get_ibuf(tex->ima, NULL);
1315                                                                 
1316                                                                 /* texturespace */
1317                                                                 space = 1.0;
1318                                                                 if (ob->type == OB_MESH) {
1319                                                                         float size[3];
1320                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1321                                                                         space = size[0] / size[1];
1322                                                                 }
1323                                                                 else if (ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1324                                                                         Curve *cu = ob->data;
1325                                                                         space = cu->size[0] / cu->size[1];
1326                                                                 }
1327                                                         
1328                                                                 x = ibuf->x / space;
1329                                                                 y = ibuf->y;
1330                                                                 
1331                                                                 if (x > y) ob->size[0] = ob->size[1] * x / y;
1332                                                                 else ob->size[1] = ob->size[0] * y / x;
1333                                                                 
1334                                                                 done = 1;
1335                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);                                                               
1336                                                         }
1337                                                 }
1338                                                 if (done) break;
1339                                         }
1340                                 }
1341                                 if (done) break;
1342                         }
1343                 }
1344         }
1345         
1346 }
1347
1348
1349 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1350 {       
1351         EnumPropertyItem *input = object_mode_items;
1352         EnumPropertyItem *item = NULL;
1353         Object *ob;
1354         int totitem = 0;
1355         
1356         if (!C) /* needed for docs */
1357                 return object_mode_items;
1358
1359
1360
1361         ob = CTX_data_active_object(C);
1362         while (ob && input->identifier) {
1363                 if ((input->value == OB_MODE_EDIT && OB_TYPE_SUPPORT_EDITMODE(ob->type)) ||
1364                     (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1365                     (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
1366                     ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
1367                       input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1368                     (input->value == OB_MODE_OBJECT))
1369                 {
1370                         RNA_enum_item_add(&item, &totitem, input);
1371                 }
1372                 ++input;
1373         }
1374
1375         RNA_enum_item_end(&item, &totitem);
1376
1377         *free = 1;
1378
1379         return item;
1380 }
1381
1382 static const char *object_mode_op_string(int mode)
1383 {
1384         if (mode & OB_MODE_EDIT)
1385                 return "OBJECT_OT_editmode_toggle";
1386         if (mode == OB_MODE_SCULPT)
1387                 return "SCULPT_OT_sculptmode_toggle";
1388         if (mode == OB_MODE_VERTEX_PAINT)
1389                 return "PAINT_OT_vertex_paint_toggle";
1390         if (mode == OB_MODE_WEIGHT_PAINT)
1391                 return "PAINT_OT_weight_paint_toggle";
1392         if (mode == OB_MODE_TEXTURE_PAINT)
1393                 return "PAINT_OT_texture_paint_toggle";
1394         if (mode == OB_MODE_PARTICLE_EDIT)
1395                 return "PARTICLE_OT_particle_edit_toggle";
1396         if (mode == OB_MODE_POSE)
1397                 return "OBJECT_OT_posemode_toggle";
1398         return NULL;
1399 }
1400
1401 /* checks the mode to be set is compatible with the object
1402  * should be made into a generic function */
1403 static int object_mode_set_compat(bContext *UNUSED(C), wmOperator *op, Object *ob)
1404 {
1405         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1406
1407         if (ob) {
1408                 if (mode == OB_MODE_OBJECT)
1409                         return 1;
1410
1411                 switch (ob->type) {
1412                         case OB_MESH:
1413                                 if (mode & (OB_MODE_EDIT | OB_MODE_SCULPT | OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT | OB_MODE_PARTICLE_EDIT))
1414                                         return 1;
1415                                 return 0;
1416                         case OB_CURVE:
1417                         case OB_SURF:
1418                         case OB_FONT:
1419                         case OB_MBALL:
1420                                 if (mode & (OB_MODE_EDIT))
1421                                         return 1;
1422                                 return 0;
1423                         case OB_LATTICE:
1424                                 if (mode & (OB_MODE_EDIT | OB_MODE_WEIGHT_PAINT))
1425                                         return 1;
1426                         case OB_ARMATURE:
1427                                 if (mode & (OB_MODE_EDIT | OB_MODE_POSE))
1428                                         return 1;
1429                 }
1430         }
1431
1432         return 0;
1433 }
1434
1435 static int object_mode_set_exec(bContext *C, wmOperator *op)
1436 {
1437         Object *ob = CTX_data_active_object(C);
1438         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1439         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1440         int toggle = RNA_boolean_get(op->ptr, "toggle");
1441
1442         if (!ob || !object_mode_set_compat(C, op, ob))
1443                 return OPERATOR_PASS_THROUGH;
1444
1445         /* Exit current mode if it's not the mode we're setting */
1446         if (ob->mode != OB_MODE_OBJECT && ob->mode != mode)
1447                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
1448
1449         if (mode != OB_MODE_OBJECT) {
1450                 /* Enter new mode */
1451                 if (ob->mode != mode || toggle)
1452                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
1453
1454                 if (toggle) {
1455                         if (ob->mode == mode)
1456                                 /* For toggling, store old mode so we know what to go back to */
1457                                 ob->restore_mode = restore_mode;
1458                         else if (ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1459                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1460                         }
1461                 }
1462         }
1463
1464         return OPERATOR_FINISHED;
1465 }
1466
1467 void OBJECT_OT_mode_set(wmOperatorType *ot)
1468 {
1469         PropertyRNA *prop;
1470
1471         /* identifiers */
1472         ot->name = "Set Object Mode";
1473         ot->description = "Sets the object interaction mode";
1474         ot->idname = "OBJECT_OT_mode_set";
1475         
1476         /* api callbacks */
1477         ot->exec = object_mode_set_exec;
1478         
1479         ot->poll = ED_operator_object_active_editable;
1480         
1481         /* flags */
1482         ot->flag = 0; /* no register/undo here, leave it to operators being called */
1483         
1484         prop = RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
1485         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
1486
1487         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1488 }
1489
1490
1491
1492 void ED_object_toggle_modes(bContext *C, int mode)
1493 {
1494         if (mode & OB_MODE_SCULPT)
1495                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1496         if (mode & OB_MODE_VERTEX_PAINT)
1497                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1498         if (mode & OB_MODE_WEIGHT_PAINT)
1499                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1500         if (mode & OB_MODE_TEXTURE_PAINT)
1501                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1502         if (mode & OB_MODE_PARTICLE_EDIT)
1503                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1504         if (mode & OB_MODE_POSE)
1505                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1506         if (mode & OB_MODE_EDIT)
1507                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1508 }
1509
1510 /************************ Game Properties ***********************/
1511
1512 static int game_property_new(bContext *C, wmOperator *op)
1513 {
1514         Object *ob = CTX_data_active_object(C);
1515         bProperty *prop;
1516         char name[MAX_NAME];
1517         int type = RNA_enum_get(op->ptr, "type");
1518
1519         prop = new_property(type);
1520         BLI_addtail(&ob->prop, prop);
1521
1522         RNA_string_get(op->ptr, "name", name);
1523         if (name[0] != '\0') {
1524                 BLI_strncpy(prop->name, name, sizeof(prop->name));
1525         }
1526
1527         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
1528
1529         WM_event_add_notifier(C, NC_LOGIC, NULL);
1530         return OPERATOR_FINISHED;
1531 }
1532
1533
1534 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1535 {
1536         /* identifiers */
1537         ot->name = "New Game Property";
1538         ot->description = "Create a new property available to the game engine";
1539         ot->idname = "OBJECT_OT_game_property_new";
1540
1541         /* api callbacks */
1542         ot->exec = game_property_new;
1543         ot->poll = ED_operator_object_active_editable;
1544
1545         /* flags */
1546         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1547
1548         RNA_def_enum(ot->srna, "type", gameproperty_type_items, GPROP_FLOAT, "Type", "Type of game property to add");
1549         RNA_def_string(ot->srna, "name", "", MAX_NAME, "Name", "Name of the game property to add");
1550 }
1551
1552 static int game_property_remove(bContext *C, wmOperator *op)
1553 {
1554         Object *ob = CTX_data_active_object(C);
1555         bProperty *prop;
1556         int index = RNA_int_get(op->ptr, "index");
1557
1558         if (!ob)
1559                 return OPERATOR_CANCELLED;
1560
1561         prop = BLI_findlink(&ob->prop, index);
1562
1563         if (prop) {
1564                 BLI_remlink(&ob->prop, prop);
1565                 free_property(prop);
1566
1567                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1568                 return OPERATOR_FINISHED;
1569         }
1570         else {
1571                 return OPERATOR_CANCELLED;
1572         }
1573 }
1574
1575 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1576 {
1577         /* identifiers */
1578         ot->name = "Remove Game Property";
1579         ot->description = "Remove game property";
1580         ot->idname = "OBJECT_OT_game_property_remove";
1581
1582         /* api callbacks */
1583         ot->exec = game_property_remove;
1584         ot->poll = ED_operator_object_active_editable;
1585
1586         /* flags */
1587         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1588
1589         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
1590 }
1591
1592 #define COPY_PROPERTIES_REPLACE 1
1593 #define COPY_PROPERTIES_MERGE   2
1594 #define COPY_PROPERTIES_COPY    3
1595
1596 static EnumPropertyItem game_properties_copy_operations[] = {
1597         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
1598         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
1599         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
1600         {0, NULL, 0, NULL, NULL}};
1601
1602 static EnumPropertyItem gameprops_items[]= {
1603         {0, NULL, 0, NULL, NULL}};
1604
1605 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1606 {       
1607         Object *ob = ED_object_active_context(C);
1608         EnumPropertyItem tmp = {0, "", 0, "", ""};
1609         EnumPropertyItem *item = NULL;
1610         bProperty *prop;
1611         int a, totitem = 0;
1612         
1613         if (!ob)
1614                 return gameprops_items;
1615
1616         for (a = 1, prop = ob->prop.first; prop; prop = prop->next, a++) {
1617                 tmp.value = a;
1618                 tmp.identifier = prop->name;
1619                 tmp.name = prop->name;
1620                 RNA_enum_item_add(&item, &totitem, &tmp);
1621         }
1622
1623         RNA_enum_item_end(&item, &totitem);
1624         *free = 1;
1625
1626         return item;
1627 }
1628
1629 static int game_property_copy_exec(bContext *C, wmOperator *op)
1630 {
1631         Object *ob = ED_object_active_context(C);
1632         bProperty *prop;
1633         int type = RNA_enum_get(op->ptr, "operation");
1634         int propid = RNA_enum_get(op->ptr, "property");
1635
1636         if (propid > 0) { /* copy */
1637                 prop = BLI_findlink(&ob->prop, propid - 1);
1638                 
1639                 if (prop) {
1640                         CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
1641                         {
1642                                 if (ob != ob_iter)
1643                                         set_ob_property(ob_iter, prop);
1644                         } CTX_DATA_END;
1645                 }
1646         }
1647
1648         else {
1649                 CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
1650                 {
1651                         if (ob != ob_iter) {
1652                                 if (type == COPY_PROPERTIES_REPLACE)
1653                                         copy_properties(&ob_iter->prop, &ob->prop);
1654
1655                                 /* merge - the default when calling with no argument */
1656                                 else
1657                                         for (prop = ob->prop.first; prop; prop = prop->next)
1658                                                 set_ob_property(ob_iter, prop);
1659                         }
1660                 }
1661                 CTX_DATA_END;
1662         }
1663
1664         return OPERATOR_FINISHED;
1665 }
1666
1667 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
1668 {
1669         PropertyRNA *prop;
1670         /* identifiers */
1671         ot->name = "Copy Game Property";
1672         ot->idname = "OBJECT_OT_game_property_copy";
1673
1674         /* api callbacks */
1675         ot->exec = game_property_copy_exec;
1676         ot->poll = ED_operator_object_active_editable;
1677
1678         /* flags */
1679         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1680
1681         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
1682         prop = RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
1683         RNA_def_enum_funcs(prop, gameprops_itemf);
1684         ot->prop = prop;
1685 }
1686
1687 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
1688 {
1689         CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
1690         {
1691                 free_properties(&ob_iter->prop);
1692         }
1693         CTX_DATA_END;
1694
1695         WM_event_add_notifier(C, NC_LOGIC, NULL);
1696         return OPERATOR_FINISHED;
1697 }
1698 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
1699 {
1700         /* identifiers */
1701         ot->name = "Clear Game Property";
1702         ot->idname = "OBJECT_OT_game_property_clear";
1703
1704         /* api callbacks */
1705         ot->exec = game_property_clear_exec;
1706         ot->poll = ED_operator_object_active_editable;
1707
1708         /* flags */
1709         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1710 }
1711
1712 /************************ Copy Logic Bricks ***********************/
1713
1714 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
1715 {
1716         Object *ob = ED_object_active_context(C);
1717
1718         CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
1719         {
1720                 if (ob != ob_iter) {
1721                         /* first: free all logic */
1722                         free_sensors(&ob_iter->sensors);                                
1723                         unlink_controllers(&ob_iter->controllers);
1724                         free_controllers(&ob_iter->controllers);
1725                         unlink_actuators(&ob_iter->actuators);
1726                         free_actuators(&ob_iter->actuators);
1727                 
1728                         /* now copy it, this also works without logicbricks! */
1729                         clear_sca_new_poins_ob(ob);
1730                         copy_sensors(&ob_iter->sensors, &ob->sensors);
1731                         copy_controllers(&ob_iter->controllers, &ob->controllers);
1732                         copy_actuators(&ob_iter->actuators, &ob->actuators);
1733                         set_sca_new_poins_ob(ob_iter);
1734                 
1735                         /* some menu settings */
1736                         ob_iter->scavisflag = ob->scavisflag;
1737                         ob_iter->scaflag = ob->scaflag;
1738                 
1739                         /* set the initial state */
1740                         ob_iter->state = ob->state;
1741                         ob_iter->init_state = ob->init_state;
1742
1743                         if (ob_iter->totcol == ob->totcol) {
1744                                 ob_iter->actcol = ob->actcol;
1745                                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob_iter);
1746                         }
1747                 }
1748         }
1749         CTX_DATA_END;
1750
1751         WM_event_add_notifier(C, NC_LOGIC, NULL);
1752
1753         return OPERATOR_FINISHED;
1754 }
1755
1756 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
1757 {
1758         /* identifiers */
1759         ot->name = "Copy Logic Bricks to Selected";
1760         ot->description = "Copy logic bricks to other selected objects";
1761         ot->idname = "OBJECT_OT_logic_bricks_copy";
1762
1763         /* api callbacks */
1764         ot->exec = logicbricks_copy_exec;
1765         ot->poll = ED_operator_object_active_editable;
1766
1767         /* flags */
1768         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1769 }
1770
1771 static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op))
1772 {
1773         Object *ob = ED_object_active_context(C);
1774         
1775         CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
1776         {
1777                 if (ob != ob_iter) {
1778                         ob_iter->gameflag = ob->gameflag;
1779                         ob_iter->gameflag2 = ob->gameflag2;
1780                         ob_iter->inertia = ob->inertia;
1781                         ob_iter->formfactor = ob->formfactor;
1782                         ob_iter->damping = ob->damping;
1783                         ob_iter->rdamping = ob->rdamping;
1784                         ob_iter->min_vel = ob->min_vel;
1785                         ob_iter->max_vel = ob->max_vel;
1786                         ob_iter->obstacleRad = ob->obstacleRad;
1787                         ob_iter->mass = ob->mass;
1788                         ob_iter->anisotropicFriction[0] = ob->anisotropicFriction[0];
1789                         ob_iter->anisotropicFriction[1] = ob->anisotropicFriction[1];
1790                         ob_iter->anisotropicFriction[2] = ob->anisotropicFriction[2];
1791                         ob_iter->collision_boundtype = ob->collision_boundtype;                 
1792                         ob_iter->margin = ob->margin;
1793                         ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft);
1794                         if (ob->restrictflag & OB_RESTRICT_RENDER) 
1795                                 ob_iter->restrictflag |= OB_RESTRICT_RENDER;
1796                         else
1797                                 ob_iter->restrictflag &= ~OB_RESTRICT_RENDER;
1798                 }
1799         }
1800         CTX_DATA_END;
1801         
1802         return OPERATOR_FINISHED;
1803 }
1804
1805 void OBJECT_OT_game_physics_copy(struct wmOperatorType *ot)
1806 {
1807         /* identifiers */
1808         ot->name = "Copy Game Physics Properties to Selected";
1809         ot->description = "Copy game physics properties to other selected objects";
1810         ot->idname = "OBJECT_OT_game_physics_copy";
1811         
1812         /* api callbacks */
1813         ot->exec = game_physics_copy_exec;
1814         ot->poll = ED_operator_object_active_editable;
1815         
1816         /* flags */
1817         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1818 }