svn merge ^/trunk/blender -r44235:44250
[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_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,
1009                "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Draw Options%x4|"
1010                "Time Offset%x5|Dupli%x6|Object Color%x31|%l|Mass%x7|Damping%x8|All Physical Attributes%x11|Properties%x9|"
1011                "Logic Bricks%x10|Protected Transform%x29|%l");
1012         
1013         strcat (str, "|Object Constraints%x22");
1014         strcat (str, "|NLA Strips%x26");
1015         
1016 // XXX  if (OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1017 //              strcat(str, "|Texture Space%x17");
1018 //      }       
1019         
1020         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1021         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
1022         
1023         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1024                         strcat(str, "|Curve Resolution%x25");
1025         }
1026
1027         if(ob->type==OB_MESH){
1028                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
1029         }
1030
1031         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
1032         
1033         strcat(str, "|Pass Index%x30");
1034         
1035         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
1036                 strcat(str, "|Modifiers ...%x24");
1037         }
1038
1039         event= pupmenu(str);
1040         if(event<= 0) return;
1041         
1042         copy_attr(bmain, scene, v3d, event);
1043 }
1044
1045 /* ******************* force field toggle operator ***************** */
1046
1047 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1048 {
1049         Object *ob = CTX_data_active_object(C);
1050
1051         if(ob->pd == NULL)
1052                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1053
1054         if(ob->pd->forcefield == 0)
1055                 ob->pd->forcefield = PFIELD_FORCE;
1056         else
1057                 ob->pd->forcefield = 0;
1058         
1059         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, NULL);
1060
1061         return OPERATOR_FINISHED;
1062 }
1063
1064 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1065 {
1066         
1067         /* identifiers */
1068         ot->name= "Toggle Force Field";
1069         ot->description = "Toggle object's force field";
1070         ot->idname= "OBJECT_OT_forcefield_toggle";
1071         
1072         /* api callbacks */
1073         ot->exec= forcefield_toggle_exec;
1074         ot->poll= ED_operator_object_active_editable;
1075         
1076         /* flags */
1077         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1078 }
1079
1080 /* ********************************************** */
1081 /* Motion Paths */
1082
1083 /* For the object with pose/action: update paths for those that have got them
1084  * This should selectively update paths that exist...
1085  *
1086  * To be called from various tools that do incremental updates 
1087  */
1088 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1089 {
1090         ListBase targets = {NULL, NULL};
1091         
1092         /* loop over objects in scene */
1093         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1094         {
1095                 /* set flag to force recalc, then grab the relevant bones to target */
1096                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1097                 animviz_get_object_motionpaths(ob, &targets);
1098         }
1099         CTX_DATA_END;
1100         
1101         /* recalculate paths, then free */
1102         animviz_calc_motionpaths(scene, &targets);
1103         BLI_freelistN(&targets);
1104 }
1105
1106 /* For the object with pose/action: create path curves for selected bones 
1107  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1108  */
1109 static int object_calculate_paths_exec (bContext *C, wmOperator *op)
1110 {
1111         Scene *scene= CTX_data_scene(C);
1112         
1113         /* set up path data for bones being calculated */
1114         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
1115         {
1116                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1117                 animviz_verify_motionpaths(op->reports, scene, ob, NULL);
1118         }
1119         CTX_DATA_END;
1120         
1121         /* calculate the bones that now have motionpaths... */
1122         // TODO: only make for the selected bones?
1123         ED_objects_recalculate_paths(C, scene);
1124         
1125         /* notifiers for updates */
1126         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1127         
1128         return OPERATOR_FINISHED; 
1129 }
1130
1131 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
1132 {
1133         /* identifiers */
1134         ot->name= "Calculate Object Paths";
1135         ot->idname= "OBJECT_OT_paths_calculate";
1136         ot->description= "Calculate paths for the selected bones";
1137         
1138         /* api callbacks */
1139         ot->exec= object_calculate_paths_exec;
1140         ot->poll= ED_operator_object_active_editable;
1141         
1142         /* flags */
1143         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1144 }
1145
1146 /* --------- */
1147
1148 /* for the object with pose/action: clear path curves for selected bones only */
1149 void ED_objects_clear_paths(bContext *C)
1150 {
1151         /* loop over objects in scene */
1152         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1153         {
1154                 if (ob->mpath) {
1155                         animviz_free_motionpath(ob->mpath);
1156                         ob->mpath= NULL;
1157                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1158                 }
1159         }
1160         CTX_DATA_END;
1161 }
1162
1163 /* operator callback for this */
1164 static int object_clear_paths_exec (bContext *C, wmOperator *UNUSED(op))
1165 {       
1166         /* use the backend function for this */
1167         ED_objects_clear_paths(C);
1168         
1169         /* notifiers for updates */
1170         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1171         
1172         return OPERATOR_FINISHED; 
1173 }
1174
1175 void OBJECT_OT_paths_clear (wmOperatorType *ot)
1176 {
1177         /* identifiers */
1178         ot->name= "Clear Object Paths";
1179         ot->idname= "OBJECT_OT_paths_clear";
1180         ot->description= "Clear path caches for selected bones";
1181         
1182         /* api callbacks */
1183         ot->exec= object_clear_paths_exec;
1184         ot->poll= ED_operator_object_active_editable;
1185         
1186         /* flags */
1187         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1188 }
1189
1190
1191 /********************** Smooth/Flat *********************/
1192
1193 static int shade_smooth_exec(bContext *C, wmOperator *op)
1194 {
1195         Curve *cu;
1196         Nurb *nu;
1197         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1198         int done= 0;
1199
1200         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1201
1202                 if(ob->type==OB_MESH) {
1203                         mesh_set_smooth_flag(ob, !clear);
1204
1205                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1206                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1207
1208                         done= 1;
1209                 }
1210                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
1211                         cu= ob->data;
1212
1213                         for(nu=cu->nurb.first; nu; nu=nu->next) {
1214                                 if(!clear) nu->flag |= ME_SMOOTH;
1215                                 else nu->flag &= ~ME_SMOOTH;
1216                         }
1217
1218                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1219                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1220
1221                         done= 1;
1222                 }
1223         }
1224         CTX_DATA_END;
1225
1226         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
1227 }
1228
1229 static int shade_poll(bContext *C)
1230 {
1231         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
1232 }
1233
1234 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1235 {
1236         /* identifiers */
1237         ot->name= "Shade Flat";
1238         ot->description= "Display faces 'flat'";
1239         ot->idname= "OBJECT_OT_shade_flat";
1240         
1241         /* api callbacks */
1242         ot->poll= shade_poll;
1243         ot->exec= shade_smooth_exec;
1244
1245         /* flags */
1246         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1247 }
1248
1249 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1250 {
1251         /* identifiers */
1252         ot->name= "Shade Smooth";
1253         ot->description= "Display faces 'smooth' (using vertex normals)";
1254         ot->idname= "OBJECT_OT_shade_smooth";
1255         
1256         /* api callbacks */
1257         ot->poll= shade_poll;
1258         ot->exec= shade_smooth_exec;
1259         
1260         /* flags */
1261         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1262 }
1263
1264 /* ********************** */
1265
1266 static void UNUSED_FUNCTION(image_aspect)(Scene *scene, View3D *v3d)
1267 {
1268         /* all selected objects with an image map: scale in image aspect */
1269         Base *base;
1270         Object *ob;
1271         Material *ma;
1272         Tex *tex;
1273         float x, y, space;
1274         int a, b, done;
1275         
1276         if(scene->obedit) return; // XXX get from context
1277         if(scene->id.lib) return;
1278         
1279         for(base= FIRSTBASE; base; base= base->next) {
1280                 if(TESTBASELIB(v3d, base)) {
1281                         ob= base->object;
1282                         done= 0;
1283                         
1284                         for(a=1; a<=ob->totcol; a++) {
1285                                 ma= give_current_material(ob, a);
1286                                 if(ma) {
1287                                         for(b=0; b<MAX_MTEX; b++) {
1288                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1289                                                         tex= ma->mtex[b]->tex;
1290                                                         if(tex->type==TEX_IMAGE && tex->ima) {
1291                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
1292                                                                 
1293                                                                 /* texturespace */
1294                                                                 space= 1.0;
1295                                                                 if(ob->type==OB_MESH) {
1296                                                                         float size[3];
1297                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1298                                                                         space= size[0]/size[1];
1299                                                                 }
1300                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1301                                                                         Curve *cu= ob->data;
1302                                                                         space= cu->size[0]/cu->size[1];
1303                                                                 }
1304                                                         
1305                                                                 x= ibuf->x/space;
1306                                                                 y= ibuf->y;
1307                                                                 
1308                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
1309                                                                 else ob->size[1]= ob->size[0]*y/x;
1310                                                                 
1311                                                                 done= 1;
1312                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);                                                               
1313                                                         }
1314                                                 }
1315                                                 if(done) break;
1316                                         }
1317                                 }
1318                                 if(done) break;
1319                         }
1320                 }
1321         }
1322         
1323 }
1324
1325
1326 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1327 {       
1328         EnumPropertyItem *input = object_mode_items;
1329         EnumPropertyItem *item= NULL;
1330         Object *ob;
1331         int totitem= 0;
1332         
1333         if(!C) /* needed for docs */
1334                 return object_mode_items;
1335
1336         ob = CTX_data_active_object(C);
1337         while(ob && input->identifier) {
1338                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
1339                                                         (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
1340                                                          (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
1341                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1342                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
1343                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
1344                          input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1345                    (input->value == OB_MODE_OBJECT))
1346                         RNA_enum_item_add(&item, &totitem, input);
1347                 ++input;
1348         }
1349
1350         RNA_enum_item_end(&item, &totitem);
1351
1352         *free= 1;
1353
1354         return item;
1355 }
1356
1357 static const char *object_mode_op_string(int mode)
1358 {
1359         if(mode & OB_MODE_EDIT)
1360                 return "OBJECT_OT_editmode_toggle";
1361         if(mode == OB_MODE_SCULPT)
1362                 return "SCULPT_OT_sculptmode_toggle";
1363         if(mode == OB_MODE_VERTEX_PAINT)
1364                 return "PAINT_OT_vertex_paint_toggle";
1365         if(mode == OB_MODE_WEIGHT_PAINT)
1366                 return "PAINT_OT_weight_paint_toggle";
1367         if(mode == OB_MODE_TEXTURE_PAINT)
1368                 return "PAINT_OT_texture_paint_toggle";
1369         if(mode == OB_MODE_PARTICLE_EDIT)
1370                 return "PARTICLE_OT_particle_edit_toggle";
1371         if(mode == OB_MODE_POSE)
1372                 return "OBJECT_OT_posemode_toggle";
1373         return NULL;
1374 }
1375
1376 /* checks the mode to be set is compatible with the object
1377  * should be made into a generic function */
1378 static int object_mode_set_compat(bContext *UNUSED(C), wmOperator *op, Object *ob)
1379 {
1380         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1381
1382         if(ob) {
1383                 if(mode == OB_MODE_OBJECT)
1384                         return 1;
1385
1386                 switch(ob->type) {
1387                 case OB_MESH:
1388                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
1389                                 return 1;
1390                         return 0;
1391                 case OB_CURVE:
1392                 case OB_SURF:
1393                 case OB_FONT:
1394                 case OB_MBALL:
1395                         if(mode & (OB_MODE_EDIT))
1396                                 return 1;
1397                         return 0;
1398                 case OB_LATTICE:
1399                         if(mode & (OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
1400                                 return 1;
1401                 case OB_ARMATURE:
1402                         if(mode & (OB_MODE_EDIT|OB_MODE_POSE))
1403                                 return 1;
1404                 }
1405         }
1406
1407         return 0;
1408 }
1409
1410 static int object_mode_set_exec(bContext *C, wmOperator *op)
1411 {
1412         Object *ob= CTX_data_active_object(C);
1413         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1414         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1415         int toggle = RNA_boolean_get(op->ptr, "toggle");
1416
1417         if(!ob || !object_mode_set_compat(C, op, ob))
1418                 return OPERATOR_PASS_THROUGH;
1419
1420         /* Exit current mode if it's not the mode we're setting */
1421         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
1422                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
1423
1424         if(mode != OB_MODE_OBJECT) {
1425                 /* Enter new mode */
1426                 if(ob->mode != mode || toggle)
1427                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
1428
1429                 if(toggle) {
1430                         if(ob->mode == mode)
1431                                 /* For toggling, store old mode so we know what to go back to */
1432                                 ob->restore_mode = restore_mode;
1433                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1434                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1435                         }
1436                 }
1437         }
1438
1439         return OPERATOR_FINISHED;
1440 }
1441
1442 void OBJECT_OT_mode_set(wmOperatorType *ot)
1443 {
1444         PropertyRNA *prop;
1445
1446         /* identifiers */
1447         ot->name= "Set Object Mode";
1448         ot->description = "Sets the object interaction mode";
1449         ot->idname= "OBJECT_OT_mode_set";
1450         
1451         /* api callbacks */
1452         ot->exec= object_mode_set_exec;
1453         
1454         ot->poll= ED_operator_object_active_editable;
1455         
1456         /* flags */
1457         ot->flag= 0; /* no register/undo here, leave it to operators being called */
1458         
1459         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
1460         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
1461
1462         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1463 }
1464
1465
1466
1467 void ED_object_toggle_modes(bContext *C, int mode)
1468 {
1469         if(mode & OB_MODE_SCULPT)
1470                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1471         if(mode & OB_MODE_VERTEX_PAINT)
1472                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1473         if(mode & OB_MODE_WEIGHT_PAINT)
1474                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1475         if(mode & OB_MODE_TEXTURE_PAINT)
1476                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1477         if(mode & OB_MODE_PARTICLE_EDIT)
1478                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1479         if(mode & OB_MODE_POSE)
1480                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1481         if(mode & OB_MODE_EDIT)
1482                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1483 }
1484
1485 /************************ Game Properties ***********************/
1486
1487 static int game_property_new(bContext *C, wmOperator *op)
1488 {
1489         Object *ob= CTX_data_active_object(C);
1490         bProperty *prop;
1491         char name[MAX_NAME];
1492         int type= RNA_enum_get(op->ptr, "type");
1493
1494         prop= new_property(type);
1495         BLI_addtail(&ob->prop, prop);
1496
1497         RNA_string_get(op->ptr, "name", name);
1498         if (name[0] != '\0') {
1499                 BLI_strncpy(prop->name, name, sizeof(prop->name));
1500         }
1501
1502         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
1503
1504         WM_event_add_notifier(C, NC_LOGIC, NULL);
1505         return OPERATOR_FINISHED;
1506 }
1507
1508
1509 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1510 {
1511         /* identifiers */
1512         ot->name= "New Game Property";
1513         ot->description= "Create a new property available to the game engine";
1514         ot->idname= "OBJECT_OT_game_property_new";
1515
1516         /* api callbacks */
1517         ot->exec= game_property_new;
1518         ot->poll= ED_operator_object_active_editable;
1519
1520         /* flags */
1521         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1522
1523         RNA_def_enum(ot->srna, "type", gameproperty_type_items, GPROP_FLOAT, "Type", "Type of game property to add");
1524         RNA_def_string(ot->srna, "name", "", MAX_NAME, "Name", "Name of the game property to add");
1525 }
1526
1527 static int game_property_remove(bContext *C, wmOperator *op)
1528 {
1529         Object *ob= CTX_data_active_object(C);
1530         bProperty *prop;
1531         int index= RNA_int_get(op->ptr, "index");
1532
1533         if(!ob)
1534                 return OPERATOR_CANCELLED;
1535
1536         prop= BLI_findlink(&ob->prop, index);
1537
1538         if(prop) {
1539                 BLI_remlink(&ob->prop, prop);
1540                 free_property(prop);
1541
1542                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1543                 return OPERATOR_FINISHED;
1544         }
1545         else {
1546                 return OPERATOR_CANCELLED;
1547         }
1548 }
1549
1550 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1551 {
1552         /* identifiers */
1553         ot->name= "Remove Game Property";
1554         ot->description= "Remove game property";
1555         ot->idname= "OBJECT_OT_game_property_remove";
1556
1557         /* api callbacks */
1558         ot->exec= game_property_remove;
1559         ot->poll= ED_operator_object_active_editable;
1560
1561         /* flags */
1562         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1563
1564         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
1565 }
1566
1567 #define COPY_PROPERTIES_REPLACE 1
1568 #define COPY_PROPERTIES_MERGE   2
1569 #define COPY_PROPERTIES_COPY    3
1570
1571 static EnumPropertyItem game_properties_copy_operations[] ={
1572         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
1573         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
1574         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
1575         {0, NULL, 0, NULL, NULL}};
1576
1577 static EnumPropertyItem gameprops_items[]= {
1578         {0, NULL, 0, NULL, NULL}};
1579
1580 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1581 {       
1582         Object *ob= ED_object_active_context(C);
1583         EnumPropertyItem tmp = {0, "", 0, "", ""};
1584         EnumPropertyItem *item= NULL;
1585         bProperty *prop;
1586         int a, totitem= 0;
1587         
1588         if(!ob)
1589                 return gameprops_items;
1590
1591         for(a=1, prop= ob->prop.first; prop; prop=prop->next, a++) {
1592                 tmp.value= a;
1593                 tmp.identifier= prop->name;
1594                 tmp.name= prop->name;
1595                 RNA_enum_item_add(&item, &totitem, &tmp);
1596         }
1597
1598         RNA_enum_item_end(&item, &totitem);
1599         *free= 1;
1600
1601         return item;
1602 }
1603
1604 static int game_property_copy_exec(bContext *C, wmOperator *op)
1605 {
1606         Object *ob=ED_object_active_context(C);
1607         bProperty *prop;
1608         int type = RNA_enum_get(op->ptr, "operation");
1609         int propid= RNA_enum_get(op->ptr, "property");
1610
1611         if(propid > 0) { /* copy */
1612                 prop = BLI_findlink(&ob->prop, propid-1);
1613                 
1614                 if(prop) {
1615                         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1616                                 if (ob != ob_iter)
1617                                         set_ob_property(ob_iter, prop);
1618                         } CTX_DATA_END;
1619                 }
1620         }
1621
1622         else {
1623                 CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1624                         if (ob != ob_iter) {
1625                                 if (type == COPY_PROPERTIES_REPLACE)
1626                                         copy_properties(&ob_iter->prop, &ob->prop);
1627
1628                                 /* merge - the default when calling with no argument */
1629                                 else
1630                                         for(prop = ob->prop.first; prop; prop= prop->next)
1631                                                 set_ob_property(ob_iter, prop);
1632                         }
1633                 }
1634                 CTX_DATA_END;
1635         }
1636
1637         return OPERATOR_FINISHED;
1638 }
1639
1640 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
1641 {
1642         PropertyRNA *prop;
1643         /* identifiers */
1644         ot->name= "Copy Game Property";
1645         ot->idname= "OBJECT_OT_game_property_copy";
1646
1647         /* api callbacks */
1648         ot->exec= game_property_copy_exec;
1649         ot->poll= ED_operator_object_active_editable;
1650
1651         /* flags */
1652         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1653
1654         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
1655         prop=RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
1656         RNA_def_enum_funcs(prop, gameprops_itemf);
1657         ot->prop=prop;
1658 }
1659
1660 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
1661 {
1662         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1663                 free_properties(&ob_iter->prop);
1664         }
1665         CTX_DATA_END;
1666
1667         WM_event_add_notifier(C, NC_LOGIC, NULL);
1668         return OPERATOR_FINISHED;
1669 }
1670 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
1671 {
1672         /* identifiers */
1673         ot->name= "Clear Game Property";
1674         ot->idname= "OBJECT_OT_game_property_clear";
1675
1676         /* api callbacks */
1677         ot->exec= game_property_clear_exec;
1678         ot->poll= ED_operator_object_active_editable;
1679
1680         /* flags */
1681         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1682 }
1683
1684 /************************ Copy Logic Bricks ***********************/
1685
1686 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
1687 {
1688         Object *ob=ED_object_active_context(C);
1689
1690         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1691                 if(ob != ob_iter) {
1692                         /* first: free all logic */
1693                         free_sensors(&ob_iter->sensors);                                
1694                         unlink_controllers(&ob_iter->controllers);
1695                         free_controllers(&ob_iter->controllers);
1696                         unlink_actuators(&ob_iter->actuators);
1697                         free_actuators(&ob_iter->actuators);
1698                 
1699                         /* now copy it, this also works without logicbricks! */
1700                         clear_sca_new_poins_ob(ob);
1701                         copy_sensors(&ob_iter->sensors, &ob->sensors);
1702                         copy_controllers(&ob_iter->controllers, &ob->controllers);
1703                         copy_actuators(&ob_iter->actuators, &ob->actuators);
1704                         set_sca_new_poins_ob(ob_iter);
1705                 
1706                         /* some menu settings */
1707                         ob_iter->scavisflag= ob->scavisflag;
1708                         ob_iter->scaflag= ob->scaflag;
1709                 
1710                         /* set the initial state */
1711                         ob_iter->state= ob->state;
1712                         ob_iter->init_state= ob->init_state;
1713
1714                         if(ob_iter->totcol==ob->totcol) {
1715                                 ob_iter->actcol= ob->actcol;
1716                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
1717                         }
1718                 }
1719         }
1720         CTX_DATA_END;
1721
1722         WM_event_add_notifier(C, NC_LOGIC, NULL);
1723
1724         return OPERATOR_FINISHED;
1725 }
1726
1727 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
1728 {
1729         /* identifiers */
1730         ot->name= "Copy Logic Bricks to Selected";
1731         ot->description = "Copy logic bricks to other selected objects";
1732         ot->idname= "OBJECT_OT_logic_bricks_copy";
1733
1734         /* api callbacks */
1735         ot->exec= logicbricks_copy_exec;
1736         ot->poll= ED_operator_object_active_editable;
1737
1738         /* flags */
1739         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1740 }
1741
1742 static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op))
1743 {
1744         Object *ob=ED_object_active_context(C);
1745         
1746         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1747                 if(ob != ob_iter) {
1748                         ob_iter->gameflag = ob->gameflag;
1749                         ob_iter->gameflag2 = ob->gameflag2;
1750                         ob_iter->inertia = ob->inertia;
1751                         ob_iter->formfactor = ob->formfactor;;
1752                         ob_iter->damping = ob->damping;
1753                         ob_iter->rdamping = ob->rdamping;
1754                         ob_iter->min_vel = ob->min_vel;
1755                         ob_iter->max_vel = ob->max_vel;
1756                         ob_iter->obstacleRad = ob->obstacleRad;
1757                         ob_iter->mass = ob->mass;
1758                         ob_iter->anisotropicFriction[0] = ob->anisotropicFriction[0];
1759                         ob_iter->anisotropicFriction[1] = ob->anisotropicFriction[1];
1760                         ob_iter->anisotropicFriction[2] = ob->anisotropicFriction[2];
1761                         ob_iter->collision_boundtype = ob->collision_boundtype;                 
1762                         ob_iter->margin = ob->margin;
1763                         ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft);
1764                         if(ob->restrictflag & OB_RESTRICT_RENDER) 
1765                                 ob_iter->restrictflag |= OB_RESTRICT_RENDER;
1766                          else
1767                                 ob_iter->restrictflag &= ~OB_RESTRICT_RENDER;
1768                 }
1769         }
1770         CTX_DATA_END;
1771         
1772         return OPERATOR_FINISHED;
1773 }
1774
1775 void OBJECT_OT_game_physics_copy(struct wmOperatorType *ot)
1776 {
1777         /* identifiers */
1778         ot->name= "Copy Game Physics Properties to Selected";
1779         ot->description = "Copy game physics properties to other selected objects";
1780         ot->idname= "OBJECT_OT_game_physics_copy";
1781         
1782         /* api callbacks */
1783         ot->exec= game_physics_copy_exec;
1784         ot->poll= ED_operator_object_active_editable;
1785         
1786         /* flags */
1787         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1788 }