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