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