Blender  V2.59
object_edit.c
Go to the documentation of this file.
00001 /*
00002  * $Id: object_edit.c 39090 2011-08-06 04:19:30Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * Contributor(s): Blender Foundation, 2002-2008 full recode
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <math.h>
00035 #include <time.h>
00036 #include <float.h>
00037 #include <ctype.h>
00038 #include <stddef.h> //for offsetof
00039 
00040 #include "MEM_guardedalloc.h"
00041 
00042 #include "BLI_blenlib.h"
00043 #include "BLI_math.h"
00044 #include "BLI_utildefines.h"
00045 #include "BLI_editVert.h"
00046 #include "BLI_ghash.h"
00047 #include "BLI_rand.h"
00048 
00049 #include "DNA_armature_types.h"
00050 #include "DNA_curve_types.h"
00051 #include "DNA_group_types.h"
00052 #include "DNA_lattice_types.h"
00053 #include "DNA_material_types.h"
00054 #include "DNA_meta_types.h"
00055 #include "DNA_property_types.h"
00056 #include "DNA_scene_types.h"
00057 #include "DNA_object_types.h"
00058 #include "DNA_object_force.h"
00059 #include "DNA_meshdata_types.h"
00060 #include "DNA_vfont_types.h"
00061 
00062 #include "IMB_imbuf_types.h"
00063 
00064 #include "BKE_anim.h"
00065 #include "BKE_constraint.h"
00066 #include "BKE_context.h"
00067 #include "BKE_curve.h"
00068 #include "BKE_effect.h"
00069 #include "BKE_depsgraph.h"
00070 #include "BKE_font.h"
00071 #include "BKE_image.h"
00072 #include "BKE_library.h"
00073 #include "BKE_main.h"
00074 #include "BKE_material.h"
00075 #include "BKE_mball.h"
00076 #include "BKE_mesh.h"
00077 #include "BKE_object.h"
00078 #include "BKE_paint.h"
00079 #include "BKE_pointcache.h"
00080 #include "BKE_property.h"
00081 #include "BKE_sca.h"
00082 #include "BKE_softbody.h"
00083 #include "BKE_modifier.h"
00084 
00085 #include "ED_armature.h"
00086 #include "ED_curve.h"
00087 #include "ED_mesh.h"
00088 #include "ED_mball.h"
00089 #include "ED_lattice.h"
00090 #include "ED_object.h"
00091 #include "ED_screen.h"
00092 #include "ED_util.h"
00093 
00094 
00095 #include "RNA_access.h"
00096 #include "RNA_define.h"
00097 #include "RNA_enum_types.h"
00098 
00099 /* for menu/popup icons etc etc*/
00100 
00101 #include "UI_interface.h"
00102 #include "WM_api.h"
00103 #include "WM_types.h"
00104 
00105 #include "object_intern.h"      // own include
00106 
00107 /* ************* XXX **************** */
00108 static void error(const char *UNUSED(arg)) {}
00109 static void waitcursor(int UNUSED(val)) {}
00110 static int pupmenu(const char *UNUSED(msg)) {return 0;}
00111 
00112 /* port over here */
00113 static bContext *evil_C;
00114 static void error_libdata(void) {}
00115 
00116 
00117 /* find the correct active object per context
00118  * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
00119 Object *ED_object_active_context(bContext *C)
00120 {
00121         Object *ob= NULL;
00122         if(C) {
00123                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
00124                 if (!ob) ob= CTX_data_active_object(C);
00125         }
00126         return ob;
00127 }
00128 
00129 
00130 /* ********* clear/set restrict view *********/
00131 static int object_hide_view_clear_exec(bContext *C, wmOperator *UNUSED(op))
00132 {
00133         Main *bmain= CTX_data_main(C);
00134         ScrArea *sa= CTX_wm_area(C);
00135         View3D *v3d= sa->spacedata.first;
00136         Scene *scene= CTX_data_scene(C);
00137         Base *base;
00138         int changed = 0;
00139         
00140         /* XXX need a context loop to handle such cases */
00141         for(base = FIRSTBASE; base; base=base->next){
00142                 if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
00143                         base->flag |= SELECT;
00144                         base->object->flag = base->flag;
00145                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
00146                         changed = 1;
00147                 }
00148         }
00149         if (changed) {
00150                 DAG_scene_sort(bmain, scene);
00151                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
00152         }
00153 
00154         return OPERATOR_FINISHED;
00155 }
00156 
00157 void OBJECT_OT_hide_view_clear(wmOperatorType *ot)
00158 {
00159         
00160         /* identifiers */
00161         ot->name= "Clear Restrict View";
00162         ot->description = "Reveal the object by setting the hide flag";
00163         ot->idname= "OBJECT_OT_hide_view_clear";
00164         
00165         /* api callbacks */
00166         ot->exec= object_hide_view_clear_exec;
00167         ot->poll= ED_operator_view3d_active;
00168         
00169         /* flags */
00170         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00171 }
00172 
00173 static int object_hide_view_set_exec(bContext *C, wmOperator *op)
00174 {
00175         Main *bmain= CTX_data_main(C);
00176         Scene *scene= CTX_data_scene(C);
00177         short changed = 0;
00178         const int unselected= RNA_boolean_get(op->ptr, "unselected");
00179         
00180         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
00181                 if(!unselected) {
00182                         if (base->flag & SELECT){
00183                                 base->flag &= ~SELECT;
00184                                 base->object->flag = base->flag;
00185                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
00186                                 changed = 1;
00187                                 if (base==BASACT) {
00188                                         ED_base_object_activate(C, NULL);
00189                                 }
00190                         }
00191                 }
00192                 else {
00193                         if (!(base->flag & SELECT)){
00194                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
00195                                 changed = 1;
00196                         }
00197                 }       
00198         }
00199         CTX_DATA_END;
00200 
00201         if (changed) {
00202                 DAG_scene_sort(bmain, scene);
00203                 
00204                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
00205                 
00206         }
00207 
00208         return OPERATOR_FINISHED;
00209 }
00210 
00211 void OBJECT_OT_hide_view_set(wmOperatorType *ot)
00212 {
00213         /* identifiers */
00214         ot->name= "Set Restrict View";
00215         ot->description = "Hide the object by setting the hide flag";
00216         ot->idname= "OBJECT_OT_hide_view_set";
00217         
00218         /* api callbacks */
00219         ot->exec= object_hide_view_set_exec;
00220         ot->poll= ED_operator_view3d_active;
00221         
00222         /* flags */
00223         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00224         
00225         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
00226         
00227 }
00228 
00229 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
00230 static int object_hide_render_clear_exec(bContext *C, wmOperator *UNUSED(op))
00231 {
00232         short changed= 0;
00233 
00234         /* XXX need a context loop to handle such cases */
00235         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
00236                 if(ob->restrictflag & OB_RESTRICT_RENDER) {
00237                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
00238                         changed= 1;
00239                 }
00240         }
00241         CTX_DATA_END;
00242 
00243         if(changed)
00244                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
00245 
00246         return OPERATOR_FINISHED;
00247 }
00248 
00249 void OBJECT_OT_hide_render_clear(wmOperatorType *ot)
00250 {
00251 
00252         /* identifiers */
00253         ot->name= "Clear Restrict Render";
00254         ot->description = "Reveal the render object by setting the hide render flag";
00255         ot->idname= "OBJECT_OT_hide_render_clear";
00256 
00257         /* api callbacks */
00258         ot->exec= object_hide_render_clear_exec;
00259         ot->poll= ED_operator_view3d_active;
00260 
00261         /* flags */
00262         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00263 }
00264 
00265 static int object_hide_render_set_exec(bContext *C, wmOperator *op)
00266 {
00267         const int unselected= RNA_boolean_get(op->ptr, "unselected");
00268 
00269         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
00270                 if(!unselected) {
00271                         if (base->flag & SELECT){
00272                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
00273                         }
00274                 }
00275                 else {
00276                         if (!(base->flag & SELECT)){
00277                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
00278                         }
00279                 }
00280         }
00281         CTX_DATA_END;
00282         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
00283         return OPERATOR_FINISHED;
00284 }
00285 
00286 void OBJECT_OT_hide_render_set(wmOperatorType *ot)
00287 {
00288         /* identifiers */
00289         ot->name= "Set Restrict Render";
00290         ot->description = "Hide the render object by setting the hide render flag";
00291         ot->idname= "OBJECT_OT_hide_render_set";
00292 
00293         /* api callbacks */
00294         ot->exec= object_hide_render_set_exec;
00295         ot->poll= ED_operator_view3d_active;
00296 
00297         /* flags */
00298         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00299 
00300         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
00301 }
00302 
00303 /* ******************* toggle editmode operator  ***************** */
00304 
00305 void ED_object_exit_editmode(bContext *C, int flag)
00306 {
00307         /* Note! only in exceptional cases should 'EM_DO_UNDO' NOT be in the flag */
00308 
00309         Scene *scene= CTX_data_scene(C);
00310         Object *obedit= CTX_data_edit_object(C);
00311         int freedata = flag & EM_FREEDATA;
00312         
00313         if(obedit==NULL) return;
00314         
00315         if(flag & EM_WAITCURSOR) waitcursor(1);
00316         if(obedit->type==OB_MESH) {
00317                 Mesh *me= obedit->data;
00318                 
00319 //              if(EM_texFaceCheck())
00320                 
00321 //              if(retopo_mesh_paint_check())
00322 //                      retopo_end_okee();
00323                 
00324                 if(me->edit_mesh->totvert>MESH_MAX_VERTS) {
00325                         error("Too many vertices");
00326                         return;
00327                 }
00328                 load_editMesh(scene, obedit);
00329                 
00330                 if(freedata) {
00331                         free_editMesh(me->edit_mesh);
00332                         MEM_freeN(me->edit_mesh);
00333                         me->edit_mesh= NULL;
00334                 }
00335                 
00336                 if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT) {
00337                         mesh_octree_table(NULL, NULL, NULL, 'e');
00338                         mesh_mirrtopo_table(NULL, 'e');
00339                 }
00340         }
00341         else if (obedit->type==OB_ARMATURE) {   
00342                 ED_armature_from_edit(obedit);
00343                 if(freedata)
00344                         ED_armature_edit_free(obedit);
00345         }
00346         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
00347                 load_editNurb(obedit);
00348                 if(freedata) free_editNurb(obedit);
00349         }
00350         else if(obedit->type==OB_FONT && freedata) {
00351                 load_editText(obedit);
00352                 if(freedata) free_editText(obedit);
00353         }
00354         else if(obedit->type==OB_LATTICE) {
00355                 load_editLatt(obedit);
00356                 if(freedata) free_editLatt(obedit);
00357         }
00358         else if(obedit->type==OB_MBALL) {
00359                 load_editMball(obedit);
00360                 if(freedata) free_editMball(obedit);
00361         }
00362 
00363         /* freedata only 0 now on file saves and render */
00364         if(freedata) {
00365                 ListBase pidlist;
00366                 PTCacheID *pid;
00367 
00368                 /* for example; displist make is different in editmode */
00369                 scene->obedit= NULL; // XXX for context
00370 
00371                 /* flag object caches as outdated */
00372                 BKE_ptcache_ids_from_object(&pidlist, obedit, NULL, 0);
00373                 for(pid=pidlist.first; pid; pid=pid->next) {
00374                         if(pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
00375                                 pid->cache->flag |= PTCACHE_OUTDATED;
00376                 }
00377                 BLI_freelistN(&pidlist);
00378                 
00379                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
00380 
00381                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
00382                 DAG_id_tag_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA);
00383         
00384                 if(flag & EM_DO_UNDO)
00385                         ED_undo_push(C, "Editmode");
00386         
00387                 if(flag & EM_WAITCURSOR) waitcursor(0);
00388         
00389                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
00390 
00391                 obedit->mode &= ~OB_MODE_EDIT;
00392         }
00393 }
00394 
00395 
00396 void ED_object_enter_editmode(bContext *C, int flag)
00397 {
00398         Scene *scene= CTX_data_scene(C);
00399         Base *base= NULL;
00400         Object *ob;
00401         ScrArea *sa= CTX_wm_area(C);
00402         View3D *v3d= NULL;
00403         int ok= 0;
00404         
00405         if(scene->id.lib) return;
00406         
00407         if(sa && sa->spacetype==SPACE_VIEW3D)
00408                 v3d= sa->spacedata.first;
00409         
00410         if((flag & EM_IGNORE_LAYER)==0) {
00411                 base= CTX_data_active_base(C); /* active layer checked here for view3d */
00412 
00413                 if(base==NULL) return;
00414                 else if(v3d && (base->lay & v3d->lay)==0) return;
00415                 else if(!v3d && (base->lay & scene->lay)==0) return;
00416         }
00417         else {
00418                 base= scene->basact;
00419         }
00420 
00421         if (ELEM3(NULL, base, base->object, base->object->data)) return;
00422 
00423         ob = base->object;
00424         
00425         if (object_data_is_libdata(ob)) {
00426                 error_libdata();
00427                 return;
00428         }
00429         
00430         if(flag & EM_WAITCURSOR) waitcursor(1);
00431 
00432         ob->restore_mode = ob->mode;
00433 
00434         /* note, when switching scenes the object can have editmode data but
00435          * not be scene->obedit: bug 22954, this avoids calling self eternally */
00436         if((ob->restore_mode & OB_MODE_EDIT)==0)
00437                 ED_object_toggle_modes(C, ob->mode);
00438 
00439         ob->mode= OB_MODE_EDIT;
00440         
00441         if(ob->type==OB_MESH) {
00442                 Mesh *me= ob->data;
00443                 
00444                 if(me->pv) mesh_pmv_off(me);
00445                 ok= 1;
00446                 scene->obedit= ob;      // context sees this
00447                 
00448                 make_editMesh(scene, ob);
00449 
00450                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene);
00451         }
00452         else if (ob->type==OB_ARMATURE){
00453                 bArmature *arm= base->object->data;
00454                 if (!arm) return;
00455                 /*
00456                  * The function object_data_is_libdata make a problem here, the
00457                  * check for ob->proxy return 0 and let blender enter to edit mode
00458                  * this causa a crash when you try leave the edit mode.
00459                  * The problem is that i can't remove the ob->proxy check from
00460                  * object_data_is_libdata that prevent the bugfix #6614, so
00461                  * i add this little hack here.
00462                  */
00463                 if(arm->id.lib) {
00464                         error_libdata();
00465                         return;
00466                 }
00467                 ok=1;
00468                 scene->obedit= ob;
00469                 ED_armature_to_edit(ob);
00470                 /* to ensure all goes in restposition and without striding */
00471                 DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME); // XXX: should this be OB_RECALC_DATA?
00472 
00473                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene);
00474         }
00475         else if(ob->type==OB_FONT) {
00476                 scene->obedit= ob; // XXX for context
00477                 ok= 1;
00478                  make_editText(ob);
00479 
00480                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene);
00481         }
00482         else if(ob->type==OB_MBALL) {
00483                 scene->obedit= ob; // XXX for context
00484                 ok= 1;
00485                 make_editMball(ob);
00486 
00487                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene);
00488         }
00489         else if(ob->type==OB_LATTICE) {
00490                 scene->obedit= ob; // XXX for context
00491                 ok= 1;
00492                 make_editLatt(ob);
00493                 
00494                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene);
00495         }
00496         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
00497                 ok= 1;
00498                 scene->obedit= ob; // XXX for context
00499                 make_editNurb(ob);
00500                 
00501                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene);
00502         }
00503         
00504         if(ok) {
00505                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
00506         }
00507         else {
00508                 scene->obedit= NULL; // XXX for context
00509                 ob->mode &= ~OB_MODE_EDIT;
00510                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
00511         }
00512         
00513         if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
00514         if(flag & EM_WAITCURSOR) waitcursor(0);
00515 }
00516 
00517 static int editmode_toggle_exec(bContext *C, wmOperator *UNUSED(op))
00518 {
00519         if(!CTX_data_edit_object(C))
00520                 ED_object_enter_editmode(C, EM_WAITCURSOR);
00521         else
00522                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* had EM_DO_UNDO but op flag calls undo too [#24685] */
00523         
00524         return OPERATOR_FINISHED;
00525 }
00526 
00527 static int editmode_toggle_poll(bContext *C)
00528 {
00529         Object *ob = CTX_data_active_object(C);
00530 
00531         /* covers proxies too */
00532         if(ELEM(NULL, ob, ob->data) || ((ID *)ob->data)->lib)
00533                 return 0;
00534 
00535         if (ob->restrictflag & OB_RESTRICT_VIEW)
00536                 return 0;
00537 
00538         return (ob->type == OB_MESH || ob->type == OB_ARMATURE ||
00539                           ob->type == OB_FONT || ob->type == OB_MBALL ||
00540                           ob->type == OB_LATTICE || ob->type == OB_SURF ||
00541                           ob->type == OB_CURVE);
00542 }
00543 
00544 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
00545 {
00546         
00547         /* identifiers */
00548         ot->name= "Toggle Editmode";
00549         ot->description = "Toggle object's editmode";
00550         ot->idname= "OBJECT_OT_editmode_toggle";
00551         
00552         /* api callbacks */
00553         ot->exec= editmode_toggle_exec;
00554         
00555         ot->poll= editmode_toggle_poll;
00556         
00557         /* flags */
00558         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00559 }
00560 
00561 /* *************************** */
00562 
00563 static int posemode_exec(bContext *C, wmOperator *UNUSED(op))
00564 {
00565         Base *base= CTX_data_active_base(C);
00566         
00567         if(base->object->type==OB_ARMATURE) {
00568                 if(base->object==CTX_data_edit_object(C)) {
00569                         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
00570                         ED_armature_enter_posemode(C, base);
00571                 }
00572                 else if(base->object->mode & OB_MODE_POSE)
00573                         ED_armature_exit_posemode(C, base);
00574                 else
00575                         ED_armature_enter_posemode(C, base);
00576                 
00577                 return OPERATOR_FINISHED;
00578         }
00579         
00580         return OPERATOR_PASS_THROUGH;
00581 }
00582 
00583 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
00584 {
00585         /* identifiers */
00586         ot->name= "Toggle Pose Mode";
00587         ot->idname= "OBJECT_OT_posemode_toggle";
00588         ot->description= "Enables or disables posing/selecting bones";
00589         
00590         /* api callbacks */
00591         ot->exec= posemode_exec;
00592         ot->poll= ED_operator_object_active_editable;
00593         
00594         /* flag */
00595         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00596 }
00597 
00598 /* *********************** */
00599 
00600 #if 0
00601 // XXX should be in view3d?
00602 
00603 /* context: ob = lamp */
00604 /* code should be replaced with proper (custom) transform handles for lamp properties */
00605 static void spot_interactive(Object *ob, int mode)
00606 {
00607         Lamp *la= ob->data;
00608         float transfac, dx, dy, ratio, origval;
00609         int keep_running= 1, center2d[2];
00610         int mval[2], mvalo[2];
00611         
00612 //      getmouseco_areawin(mval);
00613 //      getmouseco_areawin(mvalo);
00614         
00615         project_int(ob->obmat[3], center2d);
00616         if( center2d[0] > 100000 ) {            /* behind camera */
00617 //              center2d[0]= curarea->winx/2;
00618 //              center2d[1]= curarea->winy/2;
00619         }
00620 
00621 //      helpline(mval, center2d);
00622         
00623         /* ratio is like scaling */
00624         dx = (float)(center2d[0] - mval[0]);
00625         dy = (float)(center2d[1] - mval[1]);
00626         transfac = (float)sqrt( dx*dx + dy*dy);
00627         if(transfac==0.0f) transfac= 1.0f;
00628         
00629         if(mode==1)     
00630                 origval= la->spotsize;
00631         else if(mode==2)        
00632                 origval= la->dist;
00633         else if(mode==3)        
00634                 origval= la->clipsta;
00635         else    
00636                 origval= la->clipend;
00637         
00638         while (keep_running>0) {
00639                 
00640 //              getmouseco_areawin(mval);
00641                 
00642                 /* essential for idling subloop */
00643                 if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) {
00644                         PIL_sleep_ms(2);
00645                 }
00646                 else {
00647                         char str[32];
00648                         
00649                         dx = (float)(center2d[0] - mval[0]);
00650                         dy = (float)(center2d[1] - mval[1]);
00651                         ratio = (float)(sqrt( dx*dx + dy*dy))/transfac;
00652                         
00653                         /* do the trick */
00654                         
00655                         if(mode==1) {   /* spot */
00656                                 la->spotsize = ratio*origval;
00657                                 CLAMP(la->spotsize, 1.0f, 180.0f);
00658                                 sprintf(str, "Spot size %.2f\n", la->spotsize);
00659                         }
00660                         else if(mode==2) {      /* dist */
00661                                 la->dist = ratio*origval;
00662                                 CLAMP(la->dist, 0.01f, 5000.0f);
00663                                 sprintf(str, "Distance %.2f\n", la->dist);
00664                         }
00665                         else if(mode==3) {      /* sta */
00666                                 la->clipsta = ratio*origval;
00667                                 CLAMP(la->clipsta, 0.001f, 5000.0f);
00668                                 sprintf(str, "Distance %.2f\n", la->clipsta);
00669                         }
00670                         else if(mode==4) {      /* end */
00671                                 la->clipend = ratio*origval;
00672                                 CLAMP(la->clipend, 0.1f, 5000.0f);
00673                                 sprintf(str, "Clip End %.2f\n", la->clipend);
00674                         }
00675 
00676                         /* cleanup */
00677                         mvalo[0]= mval[0];
00678                         mvalo[1]= mval[1];
00679                         
00680                         /* handle shaded mode */
00681 // XXX                  shade_buttons_change_3d();
00682 
00683                         /* DRAW */      
00684                         headerprint(str);
00685                         force_draw_plus(SPACE_BUTS, 0);
00686 
00687 //                      helpline(mval, center2d);
00688                 }
00689                 
00690                 while( qtest() ) {
00691                         short val;
00692                         unsigned short event= extern_qread(&val);
00693                         
00694                         switch (event){
00695                                 case ESCKEY:
00696                                 case RIGHTMOUSE:
00697                                         keep_running= 0;
00698                                         break;
00699                                 case LEFTMOUSE:
00700                                 case SPACEKEY:
00701                                 case PADENTER:
00702                                 case RETKEY:
00703                                         if(val)
00704                                                 keep_running= -1;
00705                                         break;
00706                         }
00707                 }
00708         }
00709 
00710         if(keep_running==0) {
00711                 if(mode==1)     
00712                         la->spotsize= origval;
00713                 else if(mode==2)        
00714                         la->dist= origval;
00715                 else if(mode==3)        
00716                         la->clipsta= origval;
00717                 else    
00718                         la->clipend= origval;
00719         }
00720 
00721 }
00722 #endif
00723 
00724 static void special_editmenu(Scene *scene, View3D *v3d)
00725 {
00726 // XXX  static short numcuts= 2;
00727         Object *ob= OBACT;
00728         Object *obedit= NULL; // XXX
00729         int nr,ret=0;
00730         
00731         if(ob==NULL) return;
00732         
00733         if(obedit==NULL) {
00734                 
00735                 if(ob->mode & OB_MODE_POSE) {
00736 // XXX                  pose_special_editmenu();
00737                 }
00738                 else if(paint_facesel_test(ob)) {
00739                         Mesh *me= get_mesh(ob);
00740                         MTFace *tface;
00741                         MFace *mface;
00742                         int a;
00743 
00744                         if(me==NULL || me->mtface==NULL) return;
00745 
00746                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|         TwoSide%x6|Clr     Tex%x7|         Shared%x8|         Light%x9|         Invisible%x10|         Collision%x11|         TwoSide%x12");
00747                         
00748                         tface= me->mtface;
00749                         mface= me->mface;
00750                         for(a=me->totface; a>0; a--, tface++, mface++) {
00751                                 if(mface->flag & ME_FACE_SEL) {
00752                                         switch(nr) {
00753                                         case 1:
00754                                                 tface->mode |= TF_TEX; break;
00755                                         case 2:
00756                                                 tface->mode |= TF_SHAREDCOL; break;
00757                                         case 3:
00758                                                 tface->mode |= TF_LIGHT; break; 
00759                                         case 4:
00760                                                 tface->mode |= TF_INVISIBLE; break;
00761                                         case 5:
00762                                                 tface->mode |= TF_DYNAMIC; break;
00763                                         case 6:
00764                                                 tface->mode |= TF_TWOSIDE; break;
00765                                         case 7:
00766                                                 tface->mode &= ~TF_TEX;
00767                                                 tface->tpage= NULL;
00768                                                 break;
00769                                         case 8:
00770                                                 tface->mode &= ~TF_SHAREDCOL; break;
00771                                         case 9:
00772                                                 tface->mode &= ~TF_LIGHT; break;
00773                                         case 10:
00774                                                 tface->mode &= ~TF_INVISIBLE; break;
00775                                         case 11:
00776                                                 tface->mode &= ~TF_DYNAMIC; break;
00777                                         case 12:
00778                                                 tface->mode &= ~TF_TWOSIDE; break;
00779                                         }
00780                                 }
00781                         }
00782                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
00783                 }
00784                 else if(ob->mode & OB_MODE_VERTEX_PAINT) {
00785                         Mesh *me= get_mesh(ob);
00786                         
00787                         if(me==NULL || (me->mcol==NULL && me->mtface==NULL) ) return;
00788                         
00789                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
00790                         if(nr==1) {
00791                                 
00792 // XXX                          do_shared_vertexcol(me);
00793                                 
00794                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
00795                         }
00796                 }
00797                 else if(ob->mode & OB_MODE_WEIGHT_PAINT) {
00798                         Object *par= modifiers_isDeformedByArmature(ob);
00799 
00800                         if(par && (par->mode & OB_MODE_POSE)) {
00801                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
00802 
00803 // XXX                          if(nr==1 || nr==2)
00804 // XXX                                  pose_adds_vgroups(ob, (nr == 2));
00805                         }
00806                 }
00807                 else if(ob->mode & OB_MODE_PARTICLE_EDIT) {
00808 #if 0
00809                         // XXX
00810                         ParticleSystem *psys = PE_get_current(ob);
00811                         ParticleEditSettings *pset = PE_settings();
00812 
00813                         if(!psys)
00814                                 return;
00815 
00816                         if(pset->selectmode & SCE_SELECT_POINT)
00817                                 nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5");
00818                         else
00819                                 nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5");
00820                         
00821                         switch(nr) {
00822                         case 1:
00823 // XXX                          if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return;
00824                                 waitcursor(1);
00825                                 PE_rekey();
00826                                 break;
00827                         case 2:
00828                                 PE_subdivide();
00829                                 break;
00830                         case 3:
00831                                 PE_select_root();
00832                                 break;
00833                         case 4:
00834                                 PE_select_tip();
00835                                 break;
00836                         case 5:
00837                                 PE_remove_doubles();
00838                                 break;
00839                         }
00840                         
00841                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
00842                         
00843                         if(nr>0) waitcursor(0);
00844 #endif
00845                 }
00846                 else {
00847                         Base *base, *base_select= NULL;
00848                         
00849                         /* Get the active object mesh. */
00850                         Mesh *me= get_mesh(ob);
00851 
00852                         /* Booleans, if the active object is a mesh... */
00853                         if (me && ob->id.lib==NULL) {
00854                                 
00855                                 /* Bring up a little menu with the boolean operation choices on. */
00856                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
00857 
00858                                 if (nr > 0) {
00859                                         /* user has made a choice of a menu element.
00860                                            All of the boolean functions require 2 mesh objects 
00861                                            we search through the object list to find the other 
00862                                            selected item and make sure it is distinct and a mesh. */
00863 
00864                                         for(base= FIRSTBASE; base; base= base->next) {
00865                                                 if(TESTBASELIB(v3d, base)) {
00866                                                         if(base->object != ob) base_select= base;
00867                                                 }
00868                                         }
00869 
00870                                         if (base_select) {
00871                                                 if (get_mesh(base_select->object)) {
00872                                                         if(nr <= 3){
00873                                                                 waitcursor(1);
00874 // XXX                                                          ret = NewBooleanMesh(BASACT,base_select,nr);
00875                                                                 if (ret==0) {
00876                                                                         error("An internal error occurred");
00877                                                                 } else if(ret==-1) {
00878                                                                         error("Selected meshes must have faces to perform boolean operations");
00879                                                                 } else if (ret==-2) {
00880                                                                         error("Both meshes must be a closed mesh");
00881                                                                 }
00882                                                                 waitcursor(0);
00883                                                         } else {
00884                                                                 BooleanModifierData *bmd = NULL;
00885                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
00886                                                                 BLI_addtail(&ob->modifiers, bmd);
00887                                                                 modifier_unique_name(&ob->modifiers, (ModifierData*)bmd);
00888                                                                 bmd->object = base_select->object;
00889                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
00890                                                                 switch(nr){
00891                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
00892                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
00893                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
00894                                                                 }
00895 // XXX                                                          do_common_editbuts(B_CHANGEDEP);
00896                                                         }                                                               
00897                                                 } else {
00898                                                         error("Please select 2 meshes");
00899                                                 }
00900                                         } else {
00901                                                 error("Please select 2 meshes");
00902                                         }
00903                                 }
00904 
00905                         }
00906                         else if (ob->type == OB_FONT) {
00907                                 /* removed until this gets a decent implementation (ton) */
00908 /*                              nr= pupmenu("Split %t|Characters%x1");
00909                                 if (nr > 0) {
00910                                         switch(nr) {
00911                                                 case 1: split_font();
00912                                         }
00913                                 }
00914 */
00915                         }                       
00916                 }
00917         }
00918         else if(obedit->type==OB_MESH) {
00919         }
00920         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
00921         }
00922         else if(obedit->type==OB_ARMATURE) {
00923                 nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6");
00924 //              if(nr==1)
00925 // XXX                  subdivide_armature(1);
00926                 if(nr==2) {
00927 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
00928                         waitcursor(1);
00929 // XXX                  subdivide_armature(numcuts);
00930                 }
00931 //              else if(nr==3)
00932 // XXX                  armature_flip_names();
00933                 else if(ELEM3(nr, 4, 5, 6)) {
00934 // XXX                  armature_autoside_names(nr-4);
00935                 }
00936 //              else if(nr == 7)
00937 // XXX                  switch_direction_armature();
00938         }
00939         else if(obedit->type==OB_LATTICE) {
00940                 Lattice *lt= obedit->data;
00941                 static float weight= 1.0f;
00942                 { // XXX
00943 // XXX          if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
00944                         Lattice *editlt= lt->editlatt->latt;
00945                         int a= editlt->pntsu*editlt->pntsv*editlt->pntsw;
00946                         BPoint *bp= editlt->def;
00947                         
00948                         while(a--) {
00949                                 if(bp->f1 & SELECT)
00950                                         bp->weight= weight;
00951                                 bp++;
00952                         }
00953                 }
00954         }
00955 
00956 }
00957 
00958 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
00959 {       
00960 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
00961         bProperty *prop;
00962         Base *base;
00963         int nr, tot=0;
00964         char *str;
00965         
00966         prop= ob->prop.first;
00967         while(prop) {
00968                 tot++;
00969                 prop= prop->next;
00970         }
00971         
00972         str= MEM_callocN(50 + 33*tot, "copymenu prop");
00973         
00974         if (tot)
00975                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
00976         else
00977                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
00978         
00979         tot= 0; 
00980         prop= ob->prop.first;
00981         while(prop) {
00982                 tot++;
00983                 strcat(str, "|");
00984                 strcat(str, prop->name);
00985                 prop= prop->next;
00986         }
00987 
00988         nr= pupmenu(str);
00989         
00990         if ( nr==1 || nr==2 ) {
00991                 for(base= FIRSTBASE; base; base= base->next) {
00992                         if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
00993                                 if (nr==1) { /* replace */
00994                                         copy_properties( &base->object->prop, &ob->prop );
00995                                 } else {
00996                                         for(prop = ob->prop.first; prop; prop= prop->next ) {
00997                                                 set_ob_property(base->object, prop);
00998                                         }
00999                                 }
01000                         }
01001                 }
01002         } else if(nr>0) {
01003                 prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/
01004                 
01005                 if(prop) {
01006                         for(base= FIRSTBASE; base; base= base->next) {
01007                                 if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
01008                                         set_ob_property(base->object, prop);
01009                                 }
01010                         }
01011                 }
01012         }
01013         MEM_freeN(str);
01014         
01015 }
01016 
01017 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
01018 {
01019 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
01020         Base *base;
01021         
01022         for(base= FIRSTBASE; base; base= base->next) {
01023                 if(base->object != ob) {
01024                         if(TESTBASELIB(v3d, base)) {
01025                                 
01026                                 /* first: free all logic */
01027                                 free_sensors(&base->object->sensors);                           
01028                                 unlink_controllers(&base->object->controllers);
01029                                 free_controllers(&base->object->controllers);
01030                                 unlink_actuators(&base->object->actuators);
01031                                 free_actuators(&base->object->actuators);
01032                                 
01033                                 /* now copy it, this also works without logicbricks! */
01034                                 clear_sca_new_poins_ob(ob);
01035                                 copy_sensors(&base->object->sensors, &ob->sensors);
01036                                 copy_controllers(&base->object->controllers, &ob->controllers);
01037                                 copy_actuators(&base->object->actuators, &ob->actuators);
01038                                 set_sca_new_poins_ob(base->object);
01039                                 
01040                                 /* some menu settings */
01041                                 base->object->scavisflag= ob->scavisflag;
01042                                 base->object->scaflag= ob->scaflag;
01043                                 
01044                                 /* set the initial state */
01045                                 base->object->state= ob->state;
01046                                 base->object->init_state= ob->init_state;
01047                         }
01048                 }
01049         }
01050 }
01051 
01052 /* both pointers should exist */
01053 static void copy_texture_space(Object *to, Object *ob)
01054 {
01055         float *poin1= NULL, *poin2= NULL;
01056         short texflag= 0;
01057         
01058         if(ob->type==OB_MESH) {
01059                 texflag= ((Mesh *)ob->data)->texflag;
01060                 poin2= ((Mesh *)ob->data)->loc;
01061         }
01062         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
01063                 texflag= ((Curve *)ob->data)->texflag;
01064                 poin2= ((Curve *)ob->data)->loc;
01065         }
01066         else if(ob->type==OB_MBALL) {
01067                 texflag= ((MetaBall *)ob->data)->texflag;
01068                 poin2= ((MetaBall *)ob->data)->loc;
01069         }
01070         else
01071                 return;
01072                 
01073         if(to->type==OB_MESH) {
01074                 ((Mesh *)to->data)->texflag= texflag;
01075                 poin1= ((Mesh *)to->data)->loc;
01076         }
01077         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
01078                 ((Curve *)to->data)->texflag= texflag;
01079                 poin1= ((Curve *)to->data)->loc;
01080         }
01081         else if(to->type==OB_MBALL) {
01082                 ((MetaBall *)to->data)->texflag= texflag;
01083                 poin1= ((MetaBall *)to->data)->loc;
01084         }
01085         else
01086                 return;
01087         
01088         memcpy(poin1, poin2, 9*sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
01089         
01090         if(to->type==OB_MESH) ;
01091         else if(to->type==OB_MBALL) tex_space_mball(to);
01092         else tex_space_curve(to->data);
01093         
01094 }
01095 
01096 /* UNUSED, keep incase we want to copy functionality for use elsewhere */
01097 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
01098 {
01099         Object *ob;
01100         Base *base;
01101         Curve *cu, *cu1;
01102         Nurb *nu;
01103         int do_scene_sort= 0;
01104         
01105         if(scene->id.lib) return;
01106 
01107         if(!(ob=OBACT)) return;
01108         
01109         if(scene->obedit) { // XXX get from context
01110                 /* obedit_copymenu(); */
01111                 return;
01112         }
01113         if(event==9) {
01114                 copymenu_properties(scene, v3d, ob);
01115                 return;
01116         }
01117         else if(event==10) {
01118                 copymenu_logicbricks(scene, v3d, ob);
01119                 return;
01120         }
01121         else if(event==24) {
01122                 /* moved to object_link_modifiers */
01123                 /* copymenu_modifiers(bmain, scene, v3d, ob); */
01124                 return;
01125         }
01126 
01127         for(base= FIRSTBASE; base; base= base->next) {
01128                 if(base != BASACT) {
01129                         if(TESTBASELIB(v3d, base)) {
01130                                 base->object->recalc |= OB_RECALC_OB;
01131                                 
01132                                 if(event==1) {  /* loc */
01133                                         VECCOPY(base->object->loc, ob->loc);
01134                                         VECCOPY(base->object->dloc, ob->dloc);
01135                                 }
01136                                 else if(event==2) {  /* rot */
01137                                         VECCOPY(base->object->rot, ob->rot);
01138                                         VECCOPY(base->object->drot, ob->drot);
01139 
01140                                         QUATCOPY(base->object->quat, ob->quat);
01141                                         QUATCOPY(base->object->dquat, ob->dquat);
01142                                 }
01143                                 else if(event==3) {  /* size */
01144                                         VECCOPY(base->object->size, ob->size);
01145                                         VECCOPY(base->object->dsize, ob->dsize);
01146                                 }
01147                                 else if(event==4) {  /* drawtype */
01148                                         base->object->dt= ob->dt;
01149                                         base->object->dtx= ob->dtx;
01150                                         base->object->empty_drawtype= ob->empty_drawtype;
01151                                         base->object->empty_drawsize= ob->empty_drawsize;
01152                                 }
01153                                 else if(event==5) {  /* time offs */
01154                                         base->object->sf= ob->sf;
01155                                 }
01156                                 else if(event==6) {  /* dupli */
01157                                         base->object->dupon= ob->dupon;
01158                                         base->object->dupoff= ob->dupoff;
01159                                         base->object->dupsta= ob->dupsta;
01160                                         base->object->dupend= ob->dupend;
01161                                         
01162                                         base->object->transflag &= ~OB_DUPLI;
01163                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
01164 
01165                                         base->object->dup_group= ob->dup_group;
01166                                         if(ob->dup_group)
01167                                                 id_lib_extern(&ob->dup_group->id);
01168                                 }
01169                                 else if(event==7) {     /* mass */
01170                                         base->object->mass= ob->mass;
01171                                 }
01172                                 else if(event==8) {     /* damping */
01173                                         base->object->damping= ob->damping;
01174                                         base->object->rdamping= ob->rdamping;
01175                                 }
01176                                 else if(event==11) {    /* all physical attributes */
01177                                         base->object->gameflag = ob->gameflag;
01178                                         base->object->inertia = ob->inertia;
01179                                         base->object->formfactor = ob->formfactor;
01180                                         base->object->damping= ob->damping;
01181                                         base->object->rdamping= ob->rdamping;
01182                                         base->object->min_vel= ob->min_vel;
01183                                         base->object->max_vel= ob->max_vel;
01184                                         if (ob->gameflag & OB_BOUNDS) {
01185                                                 base->object->boundtype = ob->boundtype;
01186                                         }
01187                                         base->object->margin= ob->margin;
01188                                         base->object->bsoft= copy_bulletsoftbody(ob->bsoft);
01189 
01190                                 }
01191                                 else if(event==17) {    /* tex space */
01192                                         copy_texture_space(base->object, ob);
01193                                 }
01194                                 else if(event==18) {    /* font settings */
01195                                         
01196                                         if(base->object->type==ob->type) {
01197                                                 cu= ob->data;
01198                                                 cu1= base->object->data;
01199                                                 
01200                                                 cu1->spacemode= cu->spacemode;
01201                                                 cu1->spacing= cu->spacing;
01202                                                 cu1->linedist= cu->linedist;
01203                                                 cu1->shear= cu->shear;
01204                                                 cu1->fsize= cu->fsize;
01205                                                 cu1->xof= cu->xof;
01206                                                 cu1->yof= cu->yof;
01207                                                 cu1->textoncurve= cu->textoncurve;
01208                                                 cu1->wordspace= cu->wordspace;
01209                                                 cu1->ulpos= cu->ulpos;
01210                                                 cu1->ulheight= cu->ulheight;
01211                                                 if(cu1->vfont) cu1->vfont->id.us--;
01212                                                 cu1->vfont= cu->vfont;
01213                                                 id_us_plus((ID *)cu1->vfont);
01214                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
01215                                                 cu1->vfontb= cu->vfontb;
01216                                                 id_us_plus((ID *)cu1->vfontb);
01217                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
01218                                                 cu1->vfonti= cu->vfonti;
01219                                                 id_us_plus((ID *)cu1->vfonti);
01220                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
01221                                                 cu1->vfontbi= cu->vfontbi;
01222                                                 id_us_plus((ID *)cu1->vfontbi);                                         
01223 
01224                                                 BKE_text_to_curve(scene, base->object, 0);              /* needed? */
01225 
01226                                                 
01227                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
01228                                                 
01229                                                 base->object->recalc |= OB_RECALC_DATA;
01230                                         }
01231                                 }
01232                                 else if(event==19) {    /* bevel settings */
01233                                         
01234                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
01235                                                 cu= ob->data;
01236                                                 cu1= base->object->data;
01237                                                 
01238                                                 cu1->bevobj= cu->bevobj;
01239                                                 cu1->taperobj= cu->taperobj;
01240                                                 cu1->width= cu->width;
01241                                                 cu1->bevresol= cu->bevresol;
01242                                                 cu1->ext1= cu->ext1;
01243                                                 cu1->ext2= cu->ext2;
01244                                                 
01245                                                 base->object->recalc |= OB_RECALC_DATA;
01246                                         }
01247                                 }
01248                                 else if(event==25) {    /* curve resolution */
01249 
01250                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
01251                                                 cu= ob->data;
01252                                                 cu1= base->object->data;
01253                                                 
01254                                                 cu1->resolu= cu->resolu;
01255                                                 cu1->resolu_ren= cu->resolu_ren;
01256                                                 
01257                                                 nu= cu1->nurb.first;
01258                                                 
01259                                                 while(nu) {
01260                                                         nu->resolu= cu1->resolu;
01261                                                         nu= nu->next;
01262                                                 }
01263                                                 
01264                                                 base->object->recalc |= OB_RECALC_DATA;
01265                                         }
01266                                 }
01267                                 else if(event==21){
01268                                         if (base->object->type==OB_MESH) {
01269                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
01270 
01271                                                 if (md) {
01272                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
01273 
01274                                                         if (!tmd) {
01275                                                                 tmd = modifier_new(eModifierType_Subsurf);
01276                                                                 BLI_addtail(&base->object->modifiers, tmd);
01277                                                         }
01278 
01279                                                         modifier_copyData(md, tmd);
01280                                                         base->object->recalc |= OB_RECALC_DATA;
01281                                                 }
01282                                         }
01283                                 }
01284                                 else if(event==22) {
01285                                         /* Copy the constraint channels over */
01286                                         copy_constraints(&base->object->constraints, &ob->constraints, TRUE);
01287                                         
01288                                         do_scene_sort= 1;
01289                                 }
01290                                 else if(event==23) {
01291                                         base->object->softflag= ob->softflag;
01292                                         if(base->object->soft) sbFree(base->object->soft);
01293                                         
01294                                         base->object->soft= copy_softbody(ob->soft);
01295 
01296                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
01297                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
01298                                         }
01299                                 }
01300                                 else if(event==26) {
01301 #if 0 // XXX old animation system
01302                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
01303 #endif // XXX old animation system
01304                                 }
01305                                 else if(event==27) {    /* autosmooth */
01306                                         if (base->object->type==OB_MESH) {
01307                                                 Mesh *me= ob->data;
01308                                                 Mesh *cme= base->object->data;
01309                                                 cme->smoothresh= me->smoothresh;
01310                                                 if(me->flag & ME_AUTOSMOOTH)
01311                                                         cme->flag |= ME_AUTOSMOOTH;
01312                                                 else
01313                                                         cme->flag &= ~ME_AUTOSMOOTH;
01314                                         }
01315                                 }
01316                                 else if(event==28) { /* UV orco */
01317                                         if(ELEM(base->object->type, OB_CURVE, OB_SURF)) {
01318                                                 cu= ob->data;
01319                                                 cu1= base->object->data;
01320                                                 
01321                                                 if(cu->flag & CU_UV_ORCO)
01322                                                         cu1->flag |= CU_UV_ORCO;
01323                                                 else
01324                                                         cu1->flag &= ~CU_UV_ORCO;
01325                                         }               
01326                                 }
01327                                 else if(event==29) { /* protected bits */
01328                                         base->object->protectflag= ob->protectflag;
01329                                 }
01330                                 else if(event==30) { /* index object */
01331                                         base->object->index= ob->index;
01332                                 }
01333                                 else if(event==31) { /* object color */
01334                                         QUATCOPY(base->object->col, ob->col);
01335                                 }
01336                         }
01337                 }
01338         }
01339         
01340         if(do_scene_sort)
01341                 DAG_scene_sort(bmain, scene);
01342 
01343         DAG_ids_flush_update(bmain, 0);
01344 }
01345 
01346 static void copy_attr_menu(Main *bmain, Scene *scene, View3D *v3d)
01347 {
01348         Object *ob;
01349         short event;
01350         char str[512];
01351         
01352         if(!(ob=OBACT)) return;
01353         
01354         if (scene->obedit) { // XXX get from context
01355 //              if (ob->type == OB_MESH)
01356 // XXX                  mesh_copy_menu();
01357                 return;
01358         }
01359         
01360         /* Object Mode */
01361         
01362         /* If you change this menu, don't forget to update the menu in header_view3d.c
01363          * view3d_edit_object_copyattrmenu() and in toolbox.c
01364          */
01365         
01366         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");
01367         
01368         strcat (str, "|Object Constraints%x22");
01369         strcat (str, "|NLA Strips%x26");
01370         
01371 // XXX  if (OB_SUPPORT_MATERIAL(ob)) {
01372 //              strcat(str, "|Texture Space%x17");
01373 //      }       
01374         
01375         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
01376         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
01377         
01378         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
01379                         strcat(str, "|Curve Resolution%x25");
01380         }
01381 
01382         if(ob->type==OB_MESH){
01383                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
01384         }
01385 
01386         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
01387         
01388         strcat(str, "|Pass Index%x30");
01389         
01390         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
01391                 strcat(str, "|Modifiers ...%x24");
01392         }
01393 
01394         event= pupmenu(str);
01395         if(event<= 0) return;
01396         
01397         copy_attr(bmain, scene, v3d, event);
01398 }
01399 
01400 /* ******************* force field toggle operator ***************** */
01401 
01402 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
01403 {
01404         Object *ob = CTX_data_active_object(C);
01405 
01406         if(ob->pd == NULL)
01407                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
01408 
01409         if(ob->pd->forcefield == 0)
01410                 ob->pd->forcefield = PFIELD_FORCE;
01411         else
01412                 ob->pd->forcefield = 0;
01413         
01414         return OPERATOR_FINISHED;
01415 }
01416 
01417 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
01418 {
01419         
01420         /* identifiers */
01421         ot->name= "Toggle Force Field";
01422         ot->description = "Toggle object's force field";
01423         ot->idname= "OBJECT_OT_forcefield_toggle";
01424         
01425         /* api callbacks */
01426         ot->exec= forcefield_toggle_exec;
01427         ot->poll= ED_operator_object_active_editable;
01428         
01429         /* flags */
01430         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01431 }
01432 
01433 /* ********************************************** */
01434 /* Motion Paths */
01435 
01436 /* For the object with pose/action: update paths for those that have got them
01437  * This should selectively update paths that exist...
01438  *
01439  * To be called from various tools that do incremental updates 
01440  */
01441 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
01442 {
01443         ListBase targets = {NULL, NULL};
01444         
01445         /* loop over objects in scene */
01446         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
01447         {
01448                 /* set flag to force recalc, then grab the relevant bones to target */
01449                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
01450                 animviz_get_object_motionpaths(ob, &targets);
01451         }
01452         CTX_DATA_END;
01453         
01454         /* recalculate paths, then free */
01455         animviz_calc_motionpaths(scene, &targets);
01456         BLI_freelistN(&targets);
01457 }
01458 
01459 /* For the object with pose/action: create path curves for selected bones 
01460  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
01461  */
01462 static int object_calculate_paths_exec (bContext *C, wmOperator *UNUSED(op))
01463 {
01464         Scene *scene= CTX_data_scene(C);
01465         
01466         /* set up path data for bones being calculated */
01467         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
01468         {
01469                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
01470                 animviz_verify_motionpaths(scene, ob, NULL);
01471         }
01472         CTX_DATA_END;
01473         
01474         /* calculate the bones that now have motionpaths... */
01475         // TODO: only make for the selected bones?
01476         ED_objects_recalculate_paths(C, scene);
01477         
01478         /* notifiers for updates */
01479         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
01480         
01481         return OPERATOR_FINISHED; 
01482 }
01483 
01484 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
01485 {
01486         /* identifiers */
01487         ot->name= "Calculate Object Paths";
01488         ot->idname= "OBJECT_OT_paths_calculate";
01489         ot->description= "Calculate paths for the selected bones";
01490         
01491         /* api callbacks */
01492         ot->exec= object_calculate_paths_exec;
01493         ot->poll= ED_operator_object_active_editable;
01494         
01495         /* flags */
01496         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01497 }
01498 
01499 /* --------- */
01500 
01501 /* for the object with pose/action: clear path curves for selected bones only */
01502 void ED_objects_clear_paths(bContext *C)
01503 {
01504         /* loop over objects in scene */
01505         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
01506         {
01507                 if (ob->mpath) {
01508                         animviz_free_motionpath(ob->mpath);
01509                         ob->mpath= NULL;
01510                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
01511                 }
01512         }
01513         CTX_DATA_END;
01514 }
01515 
01516 /* operator callback for this */
01517 static int object_clear_paths_exec (bContext *C, wmOperator *UNUSED(op))
01518 {       
01519         /* use the backend function for this */
01520         ED_objects_clear_paths(C);
01521         
01522         /* notifiers for updates */
01523         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
01524         
01525         return OPERATOR_FINISHED; 
01526 }
01527 
01528 void OBJECT_OT_paths_clear (wmOperatorType *ot)
01529 {
01530         /* identifiers */
01531         ot->name= "Clear Object Paths";
01532         ot->idname= "OBJECT_OT_paths_clear";
01533         ot->description= "Clear path caches for selected bones";
01534         
01535         /* api callbacks */
01536         ot->exec= object_clear_paths_exec;
01537         ot->poll= ED_operator_object_active_editable;
01538         
01539         /* flags */
01540         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
01541 }
01542 
01543 
01544 /********************** Smooth/Flat *********************/
01545 
01546 static int shade_smooth_exec(bContext *C, wmOperator *op)
01547 {
01548         Curve *cu;
01549         Nurb *nu;
01550         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
01551         int done= 0;
01552 
01553         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
01554 
01555                 if(ob->type==OB_MESH) {
01556                         mesh_set_smooth_flag(ob, !clear);
01557 
01558                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
01559                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
01560 
01561                         done= 1;
01562                 }
01563                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
01564                         cu= ob->data;
01565 
01566                         for(nu=cu->nurb.first; nu; nu=nu->next) {
01567                                 if(!clear) nu->flag |= ME_SMOOTH;
01568                                 else nu->flag &= ~ME_SMOOTH;
01569                         }
01570 
01571                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
01572                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
01573 
01574                         done= 1;
01575                 }
01576         }
01577         CTX_DATA_END;
01578 
01579         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
01580 }
01581 
01582 static int shade_poll(bContext *C)
01583 {
01584         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
01585 }
01586 
01587 void OBJECT_OT_shade_flat(wmOperatorType *ot)
01588 {
01589         /* identifiers */
01590         ot->name= "Shade Flat";
01591         ot->description= "Display faces 'flat'";
01592         ot->idname= "OBJECT_OT_shade_flat";
01593         
01594         /* api callbacks */
01595         ot->poll= shade_poll;
01596         ot->exec= shade_smooth_exec;
01597 
01598         /* flags */
01599         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01600 }
01601 
01602 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
01603 {
01604         /* identifiers */
01605         ot->name= "Shade Smooth";
01606         ot->description= "Display faces 'smooth' (using vertex normals)";
01607         ot->idname= "OBJECT_OT_shade_smooth";
01608         
01609         /* api callbacks */
01610         ot->poll= shade_poll;
01611         ot->exec= shade_smooth_exec;
01612         
01613         /* flags */
01614         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01615 }
01616 
01617 /* ********************** */
01618 
01619 static void image_aspect(Scene *scene, View3D *v3d)
01620 {
01621         /* all selected objects with an image map: scale in image aspect */
01622         Base *base;
01623         Object *ob;
01624         Material *ma;
01625         Tex *tex;
01626         float x, y, space;
01627         int a, b, done;
01628         
01629         if(scene->obedit) return; // XXX get from context
01630         if(scene->id.lib) return;
01631         
01632         for(base= FIRSTBASE; base; base= base->next) {
01633                 if(TESTBASELIB(v3d, base)) {
01634                         ob= base->object;
01635                         done= 0;
01636                         
01637                         for(a=1; a<=ob->totcol; a++) {
01638                                 ma= give_current_material(ob, a);
01639                                 if(ma) {
01640                                         for(b=0; b<MAX_MTEX; b++) {
01641                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
01642                                                         tex= ma->mtex[b]->tex;
01643                                                         if(tex->type==TEX_IMAGE && tex->ima) {
01644                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
01645                                                                 
01646                                                                 /* texturespace */
01647                                                                 space= 1.0;
01648                                                                 if(ob->type==OB_MESH) {
01649                                                                         float size[3];
01650                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
01651                                                                         space= size[0]/size[1];
01652                                                                 }
01653                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
01654                                                                         Curve *cu= ob->data;
01655                                                                         space= cu->size[0]/cu->size[1];
01656                                                                 }
01657                                                         
01658                                                                 x= ibuf->x/space;
01659                                                                 y= ibuf->y;
01660                                                                 
01661                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
01662                                                                 else ob->size[1]= ob->size[0]*y/x;
01663                                                                 
01664                                                                 done= 1;
01665                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);                                                               
01666                                                         }
01667                                                 }
01668                                                 if(done) break;
01669                                         }
01670                                 }
01671                                 if(done) break;
01672                         }
01673                 }
01674         }
01675         
01676 }
01677 
01678 static int vergbaseco(const void *a1, const void *a2)
01679 {
01680         Base **x1, **x2;
01681         
01682         x1= (Base **) a1;
01683         x2= (Base **) a2;
01684         
01685         if( (*x1)->sy > (*x2)->sy ) return 1;
01686         else if( (*x1)->sy < (*x2)->sy) return -1;
01687         else if( (*x1)->sx > (*x2)->sx ) return 1;
01688         else if( (*x1)->sx < (*x2)->sx ) return -1;
01689 
01690         return 0;
01691 }
01692 
01693 
01694 static void auto_timeoffs(Scene *scene, View3D *v3d)
01695 {
01696         Base *base, **basesort, **bs;
01697         float start, delta;
01698         int tot=0, a;
01699         short offset=25;
01700 
01701         if(BASACT==NULL || v3d==NULL) return;
01702 // XXX  if(button(&offset, 0, 1000,"Total time")==0) return;
01703 
01704         /* make array of all bases, xco yco (screen) */
01705         for(base= FIRSTBASE; base; base= base->next) {
01706                 if(TESTBASELIB(v3d, base)) {
01707                         tot++;
01708                 }
01709         }
01710 
01711         delta= (float)offset/(float)tot;
01712         start= OBACT->sf;
01713 
01714         bs= basesort= MEM_mallocN(sizeof(void *)*tot,"autotimeoffs");
01715         for(base= FIRSTBASE; base; base= base->next) {
01716                 if(TESTBASELIB(v3d, base)) {
01717                         *bs= base;
01718                         bs++;
01719                 }
01720         }
01721         qsort(basesort, tot, sizeof(void *), vergbaseco);
01722 
01723         bs= basesort;
01724         for(a=0; a<tot; a++) {
01725                 
01726                 (*bs)->object->sf= start;
01727                 start+= delta;
01728 
01729                 bs++;
01730         }
01731         MEM_freeN(basesort);
01732 
01733 }
01734 
01735 static void ofs_timeoffs(Scene *scene, View3D *v3d)
01736 {
01737         float offset=0.0f;
01738 
01739         if(BASACT==NULL || v3d==NULL) return;
01740         
01741 // XXX  if(fbutton(&offset, -10000.0f, 10000.0f, 10, 10, "Offset")==0) return;
01742 
01743         /* make array of all bases, xco yco (screen) */
01744         CTX_DATA_BEGIN(evil_C, Object*, ob, selected_editable_objects) {
01745                 ob->sf += offset;
01746                 if (ob->sf < -MAXFRAMEF)                ob->sf = -MAXFRAMEF;
01747                 else if (ob->sf > MAXFRAMEF)    ob->sf = MAXFRAMEF;
01748         }
01749         CTX_DATA_END;
01750 
01751 }
01752 
01753 
01754 static void rand_timeoffs(Scene *scene, View3D *v3d)
01755 {
01756         Base *base;
01757         float rand_ofs=0.0f;
01758 
01759         if(BASACT==NULL || v3d==NULL) return;
01760         
01761 // XXX  if(fbutton(&rand_ofs, 0.0f, 10000.0f, 10, 10, "Randomize")==0) return;
01762         
01763         rand_ofs *= 2;
01764         
01765         for(base= FIRSTBASE; base; base= base->next) {
01766                 if(TESTBASELIB(v3d, base)) {
01767                         base->object->sf += ((float)BLI_drand()-0.5f) * rand_ofs;
01768                         if (base->object->sf < -MAXFRAMEF)              base->object->sf = -MAXFRAMEF;
01769                         else if (base->object->sf > MAXFRAMEF)  base->object->sf = MAXFRAMEF;
01770                 }
01771         }
01772 
01773 }
01774 
01775 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
01776 {       
01777         EnumPropertyItem *input = object_mode_items;
01778         EnumPropertyItem *item= NULL;
01779         Object *ob;
01780         int totitem= 0;
01781         
01782         if(!C) /* needed for docs */
01783                 return object_mode_items;
01784 
01785         ob = CTX_data_active_object(C);
01786         while(ob && input->identifier) {
01787                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
01788                                                         (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
01789                                                          (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
01790                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
01791                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
01792                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
01793                          input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
01794                    (input->value == OB_MODE_OBJECT))
01795                         RNA_enum_item_add(&item, &totitem, input);
01796                 ++input;
01797         }
01798 
01799         RNA_enum_item_end(&item, &totitem);
01800 
01801         *free= 1;
01802 
01803         return item;
01804 }
01805 
01806 static const char *object_mode_op_string(int mode)
01807 {
01808         if(mode & OB_MODE_EDIT)
01809                 return "OBJECT_OT_editmode_toggle";
01810         if(mode == OB_MODE_SCULPT)
01811                 return "SCULPT_OT_sculptmode_toggle";
01812         if(mode == OB_MODE_VERTEX_PAINT)
01813                 return "PAINT_OT_vertex_paint_toggle";
01814         if(mode == OB_MODE_WEIGHT_PAINT)
01815                 return "PAINT_OT_weight_paint_toggle";
01816         if(mode == OB_MODE_TEXTURE_PAINT)
01817                 return "PAINT_OT_texture_paint_toggle";
01818         if(mode == OB_MODE_PARTICLE_EDIT)
01819                 return "PARTICLE_OT_particle_edit_toggle";
01820         if(mode == OB_MODE_POSE)
01821                 return "OBJECT_OT_posemode_toggle";
01822         return NULL;
01823 }
01824 
01825 /* checks the mode to be set is compatible with the object
01826  * should be made into a generic function */
01827 static int object_mode_set_compat(bContext *UNUSED(C), wmOperator *op, Object *ob)
01828 {
01829         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
01830 
01831         if(ob) {
01832                 if(mode == OB_MODE_OBJECT)
01833                         return 1;
01834 
01835                 switch(ob->type) {
01836                 case OB_MESH:
01837                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
01838                                 return 1;
01839                         return 0;
01840                 case OB_CURVE:
01841                 case OB_SURF:
01842                 case OB_FONT:
01843                 case OB_MBALL:
01844                         if(mode & (OB_MODE_EDIT))
01845                                 return 1;
01846                         return 0;
01847                 case OB_LATTICE:
01848                         if(mode & (OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
01849                                 return 1;
01850                 case OB_ARMATURE:
01851                         if(mode & (OB_MODE_EDIT|OB_MODE_POSE))
01852                                 return 1;
01853                 }
01854         }
01855 
01856         return 0;
01857 }
01858 
01859 static int object_mode_set_exec(bContext *C, wmOperator *op)
01860 {
01861         Object *ob= CTX_data_active_object(C);
01862         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
01863         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
01864         int toggle = RNA_boolean_get(op->ptr, "toggle");
01865 
01866         if(!ob || !object_mode_set_compat(C, op, ob))
01867                 return OPERATOR_PASS_THROUGH;
01868 
01869         /* Exit current mode if it's not the mode we're setting */
01870         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
01871                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
01872 
01873         if(mode != OB_MODE_OBJECT) {
01874                 /* Enter new mode */
01875                 if(ob->mode != mode || toggle)
01876                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
01877 
01878                 if(toggle) {
01879                         if(ob->mode == mode)
01880                                 /* For toggling, store old mode so we know what to go back to */
01881                                 ob->restore_mode = restore_mode;
01882                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
01883                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
01884                         }
01885                 }
01886         }
01887 
01888         return OPERATOR_FINISHED;
01889 }
01890 
01891 void OBJECT_OT_mode_set(wmOperatorType *ot)
01892 {
01893         PropertyRNA *prop;
01894 
01895         /* identifiers */
01896         ot->name= "Set Object Mode";
01897         ot->description = "Sets the object interaction mode";
01898         ot->idname= "OBJECT_OT_mode_set";
01899         
01900         /* api callbacks */
01901         ot->exec= object_mode_set_exec;
01902         
01903         ot->poll= ED_operator_object_active_editable;
01904         
01905         /* flags */
01906         ot->flag= 0; /* no register/undo here, leave it to operators being called */
01907         
01908         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
01909         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
01910 
01911         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
01912 }
01913 
01914 
01915 
01916 void ED_object_toggle_modes(bContext *C, int mode)
01917 {
01918         if(mode & OB_MODE_SCULPT)
01919                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
01920         if(mode & OB_MODE_VERTEX_PAINT)
01921                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
01922         if(mode & OB_MODE_WEIGHT_PAINT)
01923                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
01924         if(mode & OB_MODE_TEXTURE_PAINT)
01925                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
01926         if(mode & OB_MODE_PARTICLE_EDIT)
01927                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
01928         if(mode & OB_MODE_POSE)
01929                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
01930         if(mode & OB_MODE_EDIT)
01931                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
01932 }
01933 
01934 /************************ Game Properties ***********************/
01935 
01936 static int game_property_new(bContext *C, wmOperator *UNUSED(op))
01937 {
01938         Object *ob= CTX_data_active_object(C);
01939         bProperty *prop;
01940 
01941         if(!ob)
01942                 return OPERATOR_CANCELLED;
01943 
01944         prop= new_property(PROP_FLOAT);
01945         BLI_addtail(&ob->prop, prop);
01946         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
01947 
01948         WM_event_add_notifier(C, NC_LOGIC, NULL);
01949         return OPERATOR_FINISHED;
01950 }
01951 
01952 
01953 void OBJECT_OT_game_property_new(wmOperatorType *ot)
01954 {
01955         /* identifiers */
01956         ot->name= "New Game Property";
01957         ot->description= "Create a new property available to the game engine";
01958         ot->idname= "OBJECT_OT_game_property_new";
01959 
01960         /* api callbacks */
01961         ot->exec= game_property_new;
01962         ot->poll= ED_operator_object_active_editable;
01963 
01964         /* flags */
01965         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
01966 }
01967 
01968 static int game_property_remove(bContext *C, wmOperator *op)
01969 {
01970         Object *ob= CTX_data_active_object(C);
01971         bProperty *prop;
01972         int index= RNA_int_get(op->ptr, "index");
01973 
01974         if(!ob)
01975                 return OPERATOR_CANCELLED;
01976 
01977         prop= BLI_findlink(&ob->prop, index);
01978 
01979         if(prop) {
01980                 BLI_remlink(&ob->prop, prop);
01981                 free_property(prop);
01982 
01983                 WM_event_add_notifier(C, NC_LOGIC, NULL);
01984                 return OPERATOR_FINISHED;
01985         }
01986         else {
01987                 return OPERATOR_CANCELLED;
01988         }
01989 }
01990 
01991 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
01992 {
01993         /* identifiers */
01994         ot->name= "Remove Game Property";
01995         ot->description= "Remove game property";
01996         ot->idname= "OBJECT_OT_game_property_remove";
01997 
01998         /* api callbacks */
01999         ot->exec= game_property_remove;
02000         ot->poll= ED_operator_object_active_editable;
02001 
02002         /* flags */
02003         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
02004 
02005         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
02006 }
02007 
02008 #define COPY_PROPERTIES_REPLACE 1
02009 #define COPY_PROPERTIES_MERGE   2
02010 #define COPY_PROPERTIES_COPY    3
02011 
02012 static EnumPropertyItem game_properties_copy_operations[] ={
02013         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
02014         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
02015         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
02016         {0, NULL, 0, NULL, NULL}};
02017 
02018 static EnumPropertyItem gameprops_items[]= {
02019         {0, NULL, 0, NULL, NULL}};
02020 
02021 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
02022 {       
02023         Object *ob= ED_object_active_context(C);
02024         EnumPropertyItem tmp = {0, "", 0, "", ""};
02025         EnumPropertyItem *item= NULL;
02026         bProperty *prop;
02027         int a, totitem= 0;
02028         
02029         if(!ob)
02030                 return gameprops_items;
02031 
02032         for(a=1, prop= ob->prop.first; prop; prop=prop->next, a++) {
02033                 tmp.value= a;
02034                 tmp.identifier= prop->name;
02035                 tmp.name= prop->name;
02036                 RNA_enum_item_add(&item, &totitem, &tmp);
02037         }
02038 
02039         RNA_enum_item_end(&item, &totitem);
02040         *free= 1;
02041 
02042         return item;
02043 }
02044 
02045 static int game_property_copy_exec(bContext *C, wmOperator *op)
02046 {
02047         Object *ob=ED_object_active_context(C);
02048         bProperty *prop;
02049         int type = RNA_enum_get(op->ptr, "operation");
02050         int propid= RNA_enum_get(op->ptr, "property");
02051 
02052         if(propid > 0) { /* copy */
02053                 prop = BLI_findlink(&ob->prop, propid-1);
02054                 
02055                 if(prop) {
02056                         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
02057                                 if (ob != ob_iter) {
02058                                         if (ob->data != ob_iter->data)
02059                                                 set_ob_property(ob_iter, prop);
02060                                 }
02061                         } CTX_DATA_END;
02062                 }
02063         }
02064 
02065         else {
02066                 CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
02067                         if (ob != ob_iter) {
02068                                 if (ob->data != ob_iter->data){
02069                                         if (type == COPY_PROPERTIES_REPLACE)
02070                                                 copy_properties( &ob_iter->prop, &ob->prop );
02071 
02072                                         /* merge - the default when calling with no argument */
02073                                         else {
02074                                                 for(prop = ob->prop.first; prop; prop= prop->next ) {
02075                                                         set_ob_property(ob_iter, prop);
02076                                                 }
02077                                         }
02078                                 }
02079                         }
02080                 }
02081                 CTX_DATA_END;
02082         }
02083 
02084         return OPERATOR_FINISHED;
02085 }
02086 
02087 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
02088 {
02089         PropertyRNA *prop;
02090         /* identifiers */
02091         ot->name= "Copy Game Property";
02092         ot->idname= "OBJECT_OT_game_property_copy";
02093 
02094         /* api callbacks */
02095         ot->exec= game_property_copy_exec;
02096         ot->poll= ED_operator_object_active_editable;
02097 
02098         /* flags */
02099         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
02100 
02101         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
02102         prop=RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
02103         RNA_def_enum_funcs(prop, gameprops_itemf);
02104         ot->prop=prop;
02105 }
02106 
02107 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
02108 {
02109         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
02110                 free_properties(&ob_iter->prop);
02111         }
02112         CTX_DATA_END;
02113 
02114         WM_event_add_notifier(C, NC_LOGIC, NULL);
02115         return OPERATOR_FINISHED;
02116 }
02117 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
02118 {
02119         /* identifiers */
02120         ot->name= "Clear Game Property";
02121         ot->idname= "OBJECT_OT_game_property_clear";
02122 
02123         /* api callbacks */
02124         ot->exec= game_property_clear_exec;
02125         ot->poll= ED_operator_object_active_editable;
02126 
02127         /* flags */
02128         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
02129 }
02130 
02131 /************************ Copy Logic Bricks ***********************/
02132 
02133 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
02134 {
02135         Object *ob=ED_object_active_context(C);
02136 
02137         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
02138                 if(ob != ob_iter) {
02139                         /* first: free all logic */
02140                         free_sensors(&ob_iter->sensors);                                
02141                         unlink_controllers(&ob_iter->controllers);
02142                         free_controllers(&ob_iter->controllers);
02143                         unlink_actuators(&ob_iter->actuators);
02144                         free_actuators(&ob_iter->actuators);
02145                 
02146                         /* now copy it, this also works without logicbricks! */
02147                         clear_sca_new_poins_ob(ob);
02148                         copy_sensors(&ob_iter->sensors, &ob->sensors);
02149                         copy_controllers(&ob_iter->controllers, &ob->controllers);
02150                         copy_actuators(&ob_iter->actuators, &ob->actuators);
02151                         set_sca_new_poins_ob(ob_iter);
02152                 
02153                         /* some menu settings */
02154                         ob_iter->scavisflag= ob->scavisflag;
02155                         ob_iter->scaflag= ob->scaflag;
02156                 
02157                         /* set the initial state */
02158                         ob_iter->state= ob->state;
02159                         ob_iter->init_state= ob->init_state;
02160 
02161                         if(ob_iter->totcol==ob->totcol) {
02162                                 ob_iter->actcol= ob->actcol;
02163                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
02164                         }
02165                 }
02166         }
02167         CTX_DATA_END;
02168 
02169         WM_event_add_notifier(C, NC_LOGIC, NULL);
02170 
02171         return OPERATOR_FINISHED;
02172 }
02173 
02174 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
02175 {
02176         /* identifiers */
02177         ot->name= "Copy Logic Bricks to Selected";
02178         ot->description = "Copy logic bricks to other selected objects.";
02179         ot->idname= "OBJECT_OT_logic_bricks_copy";
02180 
02181         /* api callbacks */
02182         ot->exec= logicbricks_copy_exec;
02183         ot->poll= ED_operator_object_active_editable;
02184 
02185         /* flags */
02186         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
02187 }