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