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