2.5: Modifiers & Menus
[blender-staging.git] / source / blender / editors / object / object_edit.c
index e470689f28c4590b2f5e21ae96a472bec1cc0a17..2fb9835f833b59365ec11e3c1a3adf34d6d4bdc0 100644 (file)
@@ -98,6 +98,7 @@
 #include "BKE_mesh.h"
 #include "BKE_nla.h"
 #include "BKE_object.h"
+#include "BKE_paint.h"
 #include "BKE_particle.h"
 #include "BKE_property.h"
 #include "BKE_report.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
+#include "RNA_enum_types.h"
 
 /* for menu/popup icons etc etc*/
 #include "UI_interface.h"
@@ -177,10 +179,6 @@ void ED_base_object_activate(bContext *C, Base *base)
        Scene *scene= CTX_data_scene(C);
        Base *tbase;
        
-       /* activating a non-mesh, should end a couple of modes... */
-       if(base && base->object->type!=OB_MESH)
-               ED_view3d_exit_paint_modes(C);
-       
        /* sets scene->basact */
        BASACT= base;
        
@@ -272,9 +270,6 @@ static Object *object_add_type(bContext *C, int type)
        Scene *scene= CTX_data_scene(C);
        Object *ob;
        
-       /* XXX hrms, this is editor level operator, remove? */
-       ED_view3d_exit_paint_modes(C);
-       
        /* for as long scene has editmode... */
        if (CTX_data_edit_object(C)) 
                ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */
@@ -751,8 +746,6 @@ static int object_delete_exec(bContext *C, wmOperator *op)
        if(CTX_data_edit_object(C)) 
                return OPERATOR_CANCELLED;
        
-       ED_view3d_exit_paint_modes(C);
-
        CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
 
                if(base->object->type==OB_LAMP) islamp= 1;
@@ -1157,7 +1150,7 @@ void ED_object_apply_obmat(Object *ob)
        
 }
 
-int hook_getIndexArray(Object *obedit, int *tot, int **indexar, char *name, float *cent_r)
+int object_hook_index_array(Object *obedit, int *tot, int **indexar, char *name, float *cent_r)
 {
        *indexar= NULL;
        *tot= 0;
@@ -1239,9 +1232,8 @@ static void select_editcurve_hook(Object *obedit, HookModifierData *hmd)
        }
 }
 
-void obedit_hook_select(Object *ob, HookModifierData *hmd) 
+void object_hook_select(Object *ob, HookModifierData *hmd) 
 {
-       
        if(ob->type==OB_MESH) select_editmesh_hook(ob, hmd);
        else if(ob->type==OB_LATTICE) select_editlattice_hook(ob, hmd);
        else if(ob->type==OB_CURVE) select_editcurve_hook(ob, hmd);
@@ -1325,7 +1317,7 @@ void add_hook(Scene *scene, View3D *v3d, int mode)
                int tot, ok, *indexar;
                char name[32];
                
-               ok = hook_getIndexArray(obedit, &tot, &indexar, name, cent);
+               ok = object_hook_index_array(obedit, &tot, &indexar, name, cent);
                
                if(ok==0) {
                        error("Requires selected vertices or active Vertex Group");
@@ -1388,7 +1380,7 @@ void add_hook(Scene *scene, View3D *v3d, int mode)
                modifier_free(md);
        }
        else if(mode==5) { /* select */
-               obedit_hook_select(obedit, hmd);
+               object_hook_select(obedit, hmd);
        }
        else if(mode==6) { /* clear offset */
                where_is_object(scene, ob);     /* ob is hook->parent */
@@ -2246,7 +2238,7 @@ static int object_location_clear_exec(bContext *C, wmOperator *op)
        int armature_clear= 0;
 
        CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
-               if((G.f & G_WEIGHTPAINT)==0) {
+               if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
                        if ((ob->protectflag & OB_LOCK_LOCX)==0)
                                ob->loc[0]= ob->dloc[0]= 0.0f;
                        if ((ob->protectflag & OB_LOCK_LOCY)==0)
@@ -2289,7 +2281,7 @@ static int object_rotation_clear_exec(bContext *C, wmOperator *op)
        int armature_clear= 0;
 
        CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
-               if((G.f & G_WEIGHTPAINT)==0) {
+               if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
                        /* eulers can only get cleared if they are not protected */
                        if ((ob->protectflag & OB_LOCK_ROTX)==0)
                                ob->rot[0]= ob->drot[0]= 0.0f;
@@ -2333,7 +2325,7 @@ static int object_scale_clear_exec(bContext *C, wmOperator *op)
        int armature_clear= 0;
 
        CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
-               if((G.f & G_WEIGHTPAINT)==0) {
+               if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
                        if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
                                ob->dsize[0]= 0.0f;
                                ob->size[0]= 1.0f;
@@ -2744,7 +2736,6 @@ void make_vertex_parent(Scene *scene, Object *obedit, View3D *v3d)
 /* present menu listing the possible objects within the group to proxify */
 static void proxy_group_objects_menu (bContext *C, wmOperator *op, Object *ob, Group *group)
 {
-       PointerRNA gob_ptr;
        uiPopupMenu *pup;
        uiLayout *layout;
        GroupObject *go;
@@ -2760,22 +2751,16 @@ static void proxy_group_objects_menu (bContext *C, wmOperator *op, Object *ob, G
        pup= uiPupMenuBegin(C, "Make Proxy For:", 0);
        layout= uiPupMenuLayout(pup);
        
-       /* make RNA pointer for object that group belongs to */
-       RNA_id_pointer_create((ID *)ob, &gob_ptr);
-       
        for (go= group->gobject.first; go; go= go->next) {
                if (go->ob) {
-                       PointerRNA props_ptr, ob_ptr;
-                       
-                       /* create pointer for this object */
-                       RNA_id_pointer_create((ID *)go->ob, &ob_ptr);
+                       PointerRNA props_ptr;
                        
                        /* create operator properties, and assign the relevant pointers to that, 
                         * and add a menu entry which uses these props 
                         */
                        WM_operator_properties_create(&props_ptr, op->idname);
-                               RNA_pointer_set(&props_ptr, "object", ob_ptr);
-                               RNA_pointer_set(&props_ptr, "group_object", gob_ptr);
+                               RNA_string_set(&props_ptr, "object", go->ob->id.name+2);
+                               RNA_string_set(&props_ptr, "group_object", go->ob->id.name+2);
                        uiItemFullO(layout, go->ob->id.name+2, 0, op->idname, props_ptr.data, WM_OP_EXEC_REGION_WIN);
                }
        }
@@ -2802,16 +2787,13 @@ static int make_proxy_invoke (bContext *C, wmOperator *op, wmEvent *evt)
        else if (ob->id.lib) {
                uiPopupMenu *pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
                uiLayout *layout= uiPupMenuLayout(pup);
-               PointerRNA ob_ptr, props_ptr;
-               
-               /* create pointer for this object */
-               RNA_id_pointer_create((ID *)ob, &ob_ptr);
+               PointerRNA props_ptr;
                
                /* create operator properties, and assign the relevant pointers to that, 
                 * and add a menu entry which uses these props 
                 */
                WM_operator_properties_create(&props_ptr, op->idname);
-                       RNA_pointer_set(&props_ptr, "object", ob_ptr);
+                       RNA_string_set(&props_ptr, "object", ob->id.name+2);
                uiItemFullO(layout, op->type->name, 0, op->idname, props_ptr.data, WM_OP_EXEC_REGION_WIN);
                
                /* present the menu and be done... */
@@ -2828,11 +2810,39 @@ static int make_proxy_invoke (bContext *C, wmOperator *op, wmEvent *evt)
 
 static int make_proxy_exec (bContext *C, wmOperator *op)
 {
-       PointerRNA ob_ptr= RNA_pointer_get(op->ptr, "object");
-       PointerRNA gob_ptr= RNA_pointer_get(op->ptr, "group_object");
-       Object *ob= ob_ptr.data;
-       Object *gob= gob_ptr.data;
+       Object *ob=NULL, *gob=NULL;
        Scene *scene= CTX_data_scene(C);
+       char ob_name[21], gob_name[21];
+       
+       /* get object and group object
+        *      - firstly names
+        *      - then pointers from context 
+        */
+       RNA_string_get(op->ptr, "object", ob_name);
+       RNA_string_get(op->ptr, "group_object", gob_name);
+       
+       if (gob_name[0]) {
+               Group *group;
+               GroupObject *go;
+               
+               /* active object is group object... */
+               // FIXME: we should get the nominated name instead
+               gob= CTX_data_active_object(C);
+               group= gob->dup_group;
+               
+               /* find the object to affect */
+               for (go= group->gobject.first; go; go= go->next) {
+                       if ((go->ob) && strcmp(go->ob->id.name+2, gob_name)==0) {
+                               ob= go->ob;
+                               break;
+                       }
+               }
+       }
+       else {
+               /* just use the active object for now */
+               // FIXME: we should get the nominated name instead
+               ob= CTX_data_active_object(C);
+       }
        
        if (ob) {
                Object *newob;
@@ -2864,6 +2874,8 @@ static int make_proxy_exec (bContext *C, wmOperator *op)
                /* depsgraph flushes are needed for the new data */
                DAG_scene_sort(scene);
                DAG_object_flush_update(scene, newob, OB_RECALC);
+               
+               WM_event_add_notifier(C, NC_OBJECT, NULL);
        }
        else {
                BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
@@ -2889,8 +2901,8 @@ void OBJECT_OT_proxy_make (wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* properties */
-       RNA_def_pointer(ot->srna, "object", "Object", "Proxy Object", "Lib-linked/grouped object to make a proxy for.");
-       RNA_def_pointer(ot->srna, "group_object", "Object", "Group Object", "Group instancer (if applicable).");
+       RNA_def_string(ot->srna, "object", "", 19, "Proxy Object", "Name of lib-linked/grouped object to make a proxy for.");
+       RNA_def_string(ot->srna, "group_object", "", 19, "Group Object", "Name of group instancer (if applicable).");
 }
 
 /* ******************** make parent operator *********************** */
@@ -3680,7 +3692,7 @@ void ED_object_exit_editmode(bContext *C, int flag)
                        me->edit_mesh= NULL;
                }
                
-               if(G.f & G_WEIGHTPAINT)
+               if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT)
                        mesh_octree_table(obedit, NULL, NULL, 'e');
        }
        else if (obedit->type==OB_ARMATURE) {   
@@ -3704,7 +3716,7 @@ void ED_object_exit_editmode(bContext *C, int flag)
                load_editMball(obedit);
                if(freedata) free_editMball(obedit);
        }
-       
+
        /* freedata only 0 now on file saves */
        if(freedata) {
                /* for example; displist make is different in editmode */
@@ -3719,6 +3731,9 @@ void ED_object_exit_editmode(bContext *C, int flag)
        
                WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
        }
+
+       obedit->mode &= ~OB_MODE_EDIT;
+       ED_object_toggle_modes(C, obedit->restore_mode);
 }
 
 
@@ -3752,7 +3767,10 @@ void ED_object_enter_editmode(bContext *C, int flag)
        
        if(flag & EM_WAITCURSOR) waitcursor(1);
 
-       ED_view3d_exit_paint_modes(C);
+       ob->restore_mode = ob->mode;
+       ED_object_toggle_modes(C, ob->mode);
+
+       ob->mode |= OB_MODE_EDIT;
        
        if(ob->type==OB_MESH) {
                Mesh *me= ob->data;
@@ -3840,6 +3858,16 @@ static int editmode_toggle_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
+static int editmode_toggle_poll(bContext *C)
+{
+       Object *ob = CTX_data_active_object(C);
+
+       return ob && (ob->type == OB_MESH || ob->type == OB_ARMATURE ||
+                     ob->type == OB_FONT || ob->type == OB_MBALL ||
+                     ob->type == OB_LATTICE || ob->type == OB_SURF ||
+                     ob->type == OB_CURVE);
+}
+
 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
 {
        
@@ -3851,7 +3879,7 @@ void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
        /* api callbacks */
        ot->exec= editmode_toggle_exec;
        
-       ot->poll= ED_operator_object_active;
+       ot->poll= editmode_toggle_poll;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -3868,7 +3896,7 @@ static int posemode_exec(bContext *C, wmOperator *op)
                        ED_object_exit_editmode(C, EM_FREEDATA);
                        ED_armature_enter_posemode(C, base);
                }
-               else if(base->object->flag & OB_POSEMODE)
+               else if(base->object->mode & OB_MODE_POSE)
                        ED_armature_exit_posemode(C, base);
                else
                        ED_armature_enter_posemode(C, base);
@@ -4091,10 +4119,10 @@ void special_editmenu(Scene *scene, View3D *v3d)
        
        if(obedit==NULL) {
                
-               if(ob->flag & OB_POSEMODE) {
+               if(ob->mode & OB_MODE_POSE) {
 // XXX                 pose_special_editmenu();
                }
-               else if(FACESEL_PAINT_TEST) {
+               else if(paint_facesel_test(ob)) {
                        Mesh *me= get_mesh(ob);
                        MTFace *tface;
                        MFace *mface;
@@ -4140,7 +4168,7 @@ void special_editmenu(Scene *scene, View3D *v3d)
                        }
                        DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
                }
-               else if(G.f & G_VERTEXPAINT) {
+               else if(ob->mode & OB_MODE_VERTEX_PAINT) {
                        Mesh *me= get_mesh(ob);
                        
                        if(me==0 || (me->mcol==NULL && me->mtface==NULL) ) return;
@@ -4153,17 +4181,17 @@ void special_editmenu(Scene *scene, View3D *v3d)
                                DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
                        }
                }
-               else if(G.f & G_WEIGHTPAINT) {
+               else if(ob->mode & OB_MODE_WEIGHT_PAINT) {
                        Object *par= modifiers_isDeformedByArmature(ob);
 
-                       if(par && (par->flag & OB_POSEMODE)) {
+                       if(par && (par->mode & OB_MODE_POSE)) {
                                nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
 
 // XXX                         if(nr==1 || nr==2)
 // XXX                                 pose_adds_vgroups(ob, (nr == 2));
                        }
                }
-               else if(G.f & G_PARTICLEEDIT) {
+               else if(ob->mode & OB_MODE_PARTICLE_EDIT) {
 #if 0
                        // XXX
                        ParticleSystem *psys = PE_get_current(ob);
@@ -5705,7 +5733,7 @@ void new_id_matar(Material **matar, int totcol)
        }
 }
 
-void single_obdata_users(Scene *scene, View3D *v3d, int flag)
+void single_obdata_users(Scene *scene, int flag)
 {
        Object *ob;
        Lamp *la;
@@ -5828,7 +5856,7 @@ void single_obdata_users(Scene *scene, View3D *v3d, int flag)
        }
 }
 
-void single_ipo_users(Scene *scene, View3D *v3d, int flag)
+void single_ipo_users(Scene *scene, int flag)
 {
 #if 0 // XXX old animation system
        Object *ob;
@@ -5851,7 +5879,7 @@ void single_ipo_users(Scene *scene, View3D *v3d, int flag)
 #endif // XXX old animation system
 }
 
-void single_mat_users(Scene *scene, View3D *v3d, int flag)
+void single_mat_users(Scene *scene, int flag)
 {
        Object *ob;
        Base *base;
@@ -6037,19 +6065,19 @@ void single_user(Scene *scene, View3D *v3d)
        
                else if(nr==2) {
                        single_object_users(scene, v3d, 1);
-                       single_obdata_users(scene, v3d, 1);
+                       single_obdata_users(scene, 1);
                }
                else if(nr==3) {
                        single_object_users(scene, v3d, 1);
-                       single_obdata_users(scene, v3d, 1);
-                       single_mat_users(scene, v3d, 1); /* also tex */
+                       single_obdata_users(scene, 1);
+                       single_mat_users(scene, 1); /* also tex */
                        
                }
                else if(nr==4) {
-                       single_mat_users(scene, v3d, 1);
+                       single_mat_users(scene, 1);
                }
                else if(nr==5) {
-                       single_ipo_users(scene, v3d, 1);
+                       single_ipo_users(scene, 1);
                }
                
                
@@ -6058,6 +6086,20 @@ void single_user(Scene *scene, View3D *v3d)
        }
 }
 
+/* used for copying scenes */
+void ED_object_single_users(Scene *scene, int full)
+{
+    single_object_users(scene, NULL, 0);
+
+    if(full) {
+        single_obdata_users(scene, 0);
+        single_mat_users_expand();
+        single_tex_users_expand();
+    }
+
+       clear_id_newpoins();
+}
+
 /* ************************************************************* */
 
 /* helper for below, ma was checked to be not NULL */
@@ -6262,7 +6304,7 @@ static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag
        int a, didit;
 
        ob= base->object;
-       if(ob->flag & OB_POSEMODE) {
+       if(ob->mode & OB_MODE_POSE) {
                ; /* nothing? */
        }
        else {
@@ -6998,3 +7040,109 @@ void hookmenu(Scene *scene, View3D *v3d)
        if (changed) {
        }       
 }
+
+static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *ptr, int *free)
+{      
+       EnumPropertyItem *input = object_mode_items;
+       EnumPropertyItem *item= NULL;
+       Object *ob;
+       int totitem= 0;
+       
+       if(!C) /* needed for docs */
+               return object_mode_items;
+
+       ob = CTX_data_active_object(C);
+       while(ob && input->identifier) {
+               if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
+                                                   (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
+                                                    (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
+                  (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
+                  (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
+                  ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
+                    input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
+                  (input->value == OB_MODE_OBJECT))
+                       RNA_enum_item_add(&item, &totitem, input);
+               ++input;
+       }
+
+       RNA_enum_item_end(&item, &totitem);
+
+       *free= 1;
+
+       return item;
+}
+
+static const char *object_mode_op_string(int mode)
+{
+       if(mode == OB_MODE_EDIT)
+               return "OBJECT_OT_editmode_toggle";
+       if(mode == OB_MODE_SCULPT)
+               return "SCULPT_OT_sculptmode_toggle";
+       if(mode == OB_MODE_VERTEX_PAINT)
+               return "PAINT_OT_vertex_paint_toggle";
+       if(mode == OB_MODE_WEIGHT_PAINT)
+               return "PAINT_OT_weight_paint_toggle";
+       if(mode == OB_MODE_TEXTURE_PAINT)
+               return "PAINT_OT_texture_paint_toggle";
+       if(mode == OB_MODE_PARTICLE_EDIT)
+               return "PARTICLE_OT_particle_edit_toggle";
+       if(mode == OB_MODE_POSE)
+               return "OBJECT_OT_posemode_toggle";
+       return NULL;
+}
+
+static int object_mode_set_exec(bContext *C, wmOperator *op)
+{
+       Object *ob= CTX_data_active_object(C);
+       int mode = RNA_enum_get(op->ptr, "mode");
+
+       if(!ob)
+               return OPERATOR_CANCELLED;
+
+       /* Exit off current mode */
+       if(ob->mode != OB_MODE_OBJECT)
+               WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
+
+       /* Enter new mode */
+       if(mode != OB_MODE_OBJECT)
+               WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
+
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_mode_set(wmOperatorType *ot)
+{
+       PropertyRNA *prop;
+
+       /* identifiers */
+       ot->name= "Set Object Mode";
+       ot->description = "Sets the object interaction mode.";
+       ot->idname= "OBJECT_OT_mode_set";
+       
+       /* api callbacks */
+       ot->exec= object_mode_set_exec;
+       
+       ot->poll= ED_operator_object_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       prop= RNA_def_enum(ot->srna, "mode", object_mode_items, 0, "Mode", "");
+       RNA_def_enum_funcs(prop, object_mode_set_itemsf);
+}
+
+
+
+void ED_object_toggle_modes(bContext *C, int mode)
+{
+       if(mode & OB_MODE_SCULPT)
+               WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
+       if(mode & OB_MODE_VERTEX_PAINT)
+               WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
+       if(mode & OB_MODE_WEIGHT_PAINT)
+               WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
+       if(mode & OB_MODE_TEXTURE_PAINT)
+               WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
+       if(mode & OB_MODE_PARTICLE_EDIT)
+               WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
+}