2.5: Modifiers & Menus
[blender-staging.git] / source / blender / editors / object / object_edit.c
index 35b6e23eaa52efa0e4af4fdef96c75f76bd19d6c..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 "ED_curve.h"
 #include "ED_particle.h"
 #include "ED_mesh.h"
+#include "ED_mball.h"
 #include "ED_object.h"
 #include "ED_screen.h"
 #include "ED_transform.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
+#include "RNA_enum_types.h"
 
 /* for menu/popup icons etc etc*/
 #include "UI_interface.h"
 
 /* ************* XXX **************** */
 static void error() {}
-static void waitcursor() {}
-static int pupmenu() {return 0;}
-static int pupmenu_col() {return 0;}
-static int okee() {return 0;}
+static void waitcursor(int val) {}
+static int pupmenu(const char *msg) {return 0;}
+static int pupmenu_col(const char *msg, int val) {return 0;}
+static int okee(const char *msg) {return 0;}
 
 /* port over here */
 static bContext *C;
@@ -176,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;
        
@@ -271,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 */
@@ -538,6 +534,78 @@ void OBJECT_OT_surface_add(wmOperatorType *ot)
        RNA_def_enum(ot->srna, "type", prop_surface_types, 0, "Primitive", "");
 }
 
+static EnumPropertyItem prop_metaball_types[]= {
+       {MB_BALL, "MBALL_BALL", ICON_META_BALL, "Meta Ball", ""},
+       {MB_TUBE, "MBALL_TUBE", ICON_META_TUBE, "Meta Tube", ""},
+       {MB_PLANE, "MBALL_PLANE", ICON_META_PLANE, "Meta Plane", ""},
+       {MB_CUBE, "MBALL_CUBE", ICON_META_CUBE, "Meta Cube", ""},
+       {MB_ELIPSOID, "MBALL_ELLIPSOID", ICON_META_ELLIPSOID, "Meta Ellipsoid", ""},
+       {0, NULL, 0, NULL, NULL}
+};
+
+static int object_metaball_add_exec(bContext *C, wmOperator *op)
+{
+       Object *obedit= CTX_data_edit_object(C);
+       MetaBall *mball;
+       MetaElem *elem;
+       int newob= 0;
+       
+       if(obedit==NULL || obedit->type!=OB_MBALL) {
+               object_add_type(C, OB_MBALL);
+               ED_object_enter_editmode(C, 0);
+               newob = 1;
+       }
+       else DAG_object_flush_update(CTX_data_scene(C), obedit, OB_RECALC_DATA);
+       
+       obedit= CTX_data_edit_object(C);
+       elem= (MetaElem*)add_metaball_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
+       mball= (MetaBall*)obedit->data;
+       BLI_addtail(mball->editelems, elem);
+       
+       /* userdef */
+       if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
+               ED_object_exit_editmode(C, EM_FREEDATA);
+       }
+       
+       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+       
+       return OPERATOR_FINISHED;
+}
+
+static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       Object *obedit= CTX_data_edit_object(C);
+       uiPopupMenu *pup;
+       uiLayout *layout;
+
+       pup= uiPupMenuBegin(C, op->type->name, 0);
+       layout= uiPupMenuLayout(pup);
+       if(!obedit || obedit->type == OB_MBALL)
+               uiItemsEnumO(layout, op->type->idname, "type");
+       else
+               uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
+       uiPupMenuEnd(C, pup);
+
+       return OPERATOR_CANCELLED;
+}
+
+void OBJECT_OT_metaball_add(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Metaball";
+       ot->description= "Add an metaball object to the scene.";
+       ot->idname= "OBJECT_OT_metaball_add";
+
+       /* api callbacks */
+       ot->invoke= object_metaball_add_invoke;
+       ot->exec= object_metaball_add_exec;
+       ot->poll= ED_operator_scene_editable;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       RNA_def_enum(ot->srna, "type", prop_metaball_types, 0, "Primitive", "");
+}
 static int object_add_text_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
@@ -616,7 +684,6 @@ void OBJECT_OT_armature_add(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-
 static int object_primitive_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        uiPopupMenu *pup= uiPupMenuBegin(C, "Add Object", 0);
@@ -625,7 +692,7 @@ static int object_primitive_add_invoke(bContext *C, wmOperator *op, wmEvent *eve
        uiItemMenuEnumO(layout, "Mesh", ICON_OUTLINER_OB_MESH, "OBJECT_OT_mesh_add", "type");
        uiItemMenuEnumO(layout, "Curve", ICON_OUTLINER_OB_CURVE, "OBJECT_OT_curve_add", "type");
        uiItemMenuEnumO(layout, "Surface", ICON_OUTLINER_OB_SURFACE, "OBJECT_OT_surface_add", "type");
-       uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_META, "OBJECT_OT_object_add", "type", OB_MBALL);
+       uiItemMenuEnumO(layout, NULL, ICON_OUTLINER_OB_META, "OBJECT_OT_metaball_add", "type");
        uiItemO(layout, "Text", ICON_OUTLINER_OB_FONT, "OBJECT_OT_text_add");
        uiItemS(layout);
        uiItemO(layout, "Armature", ICON_OUTLINER_OB_ARMATURE, "OBJECT_OT_armature_add");
@@ -679,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;
@@ -747,7 +812,7 @@ static void copy_object_set_idnew(Scene *scene, View3D *v3d, int dupflag)
        
        /* XXX check object pointers */
        for(base= FIRSTBASE; base; base= base->next) {
-               if(TESTBASELIB(v3d, base)) {
+               if(TESTBASELIB_BGMODE(v3d, base)) {
                        ob= base->object;
                        relink_constraints(&ob->constraints);
                        if (ob->pose){
@@ -1085,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;
@@ -1167,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);
@@ -1253,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");
@@ -1316,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 */
@@ -1755,7 +1819,6 @@ static short select_grouped_parent(bContext *C)   /* Makes parent active and de-se
 static short select_grouped_group(bContext *C, Object *ob)     /* Select objects in the same group as the active */
 {
        short changed = 0;
-       Base *base;
        Group *group, *ob_groups[GROUP_MENU_MAX];
        //char str[10 + (24*GROUP_MENU_MAX)];
        //char *p = str;
@@ -2175,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)
@@ -2218,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;
@@ -2262,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;
@@ -2667,68 +2730,128 @@ void make_vertex_parent(Scene *scene, Object *obedit, View3D *v3d)
        DAG_scene_sort(scene);
 }
 
-static Object *group_objects_menu(Group *group)
+
+/* ******************** make proxy operator *********************** */
+
+/* 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)
 {
+       uiPopupMenu *pup;
+       uiLayout *layout;
        GroupObject *go;
-       int len= 0;
-       short a, nr;
-       char *str;
-               
-       for(go= group->gobject.first; go; go= go->next) {
-               if(go->ob)
-                       len++;
+       int len=0;
+       
+       /* check if there are any objects within the group to assign for */
+       for (go= group->gobject.first; go; go= go->next) {
+               if (go->ob) len++;
        }
-       if(len==0) return NULL;
+       if (len==0) return;
        
-       str= MEM_callocN(40+32*len, "menu");
+       /* now create the menu to draw */
+       pup= uiPupMenuBegin(C, "Make Proxy For:", 0);
+       layout= uiPupMenuLayout(pup);
        
-       strcpy(str, "Make Proxy for: %t");
-       a= strlen(str);
-       for(nr=1, go= group->gobject.first; go; go= go->next, nr++) {
-               a+= sprintf(str+a, "|%s %%x%d", go->ob->id.name+2, nr);
+       for (go= group->gobject.first; go; go= go->next) {
+               if (go->ob) {
+                       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_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);
+               }
        }
        
-       a= pupmenu_col(str, 20);
-       MEM_freeN(str);
-       if(a>0) {
-               go= BLI_findlink(&group->gobject, a-1);
-               return go->ob;
-       }
-       return NULL;
+       /* display the menu, and be done */
+       uiPupMenuEnd(C, pup);
 }
 
-
-/* adds empty object to become local replacement data of a library-linked object */
-void make_proxy(Scene *scene)
+/* set the object to proxify */
+static int make_proxy_invoke (bContext *C, wmOperator *op, wmEvent *evt)
 {
-       Object *ob= OBACT;
-       Object *gob= NULL;
-       
-       if(scene->id.lib) return;
-       if(ob==NULL) return;
-       
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_active_object(C);
        
-       if(ob->dup_group && ob->dup_group->id.lib) {
-               gob= ob;
+       /* sanity checks */
+       if (!scene || scene->id.lib || !ob)
+               return OPERATOR_CANCELLED;
+               
+       /* Get object to work on - use a menu if we need to... */
+       if (ob->dup_group && ob->dup_group->id.lib) {
                /* gives menu with list of objects in group */
-               ob= group_objects_menu(ob->dup_group);
+               proxy_group_objects_menu(C, op, ob, ob->dup_group);
        }
-       else if(ob->id.lib) {
-               if(okee("Make Proxy Object")==0)
-               return;
+       else if (ob->id.lib) {
+               uiPopupMenu *pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
+               uiLayout *layout= uiPupMenuLayout(pup);
+               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_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... */
+               uiPupMenuEnd(C, pup);
        }
        else {
-               error("Can only make proxy for a referenced object or group");
-               return;
+               /* error.. cannot continue */
+               BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
        }
        
-       if(ob) {
+       /* this invoke just calls another instance of this operator... */
+       return OPERATOR_CANCELLED;
+}
+
+static int make_proxy_exec (bContext *C, wmOperator *op)
+{
+       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;
                Base *newbase, *oldbase= BASACT;
                char name[32];
                
+               /* Add new object for the proxy */
                newob= add_object(scene, OB_EMPTY);
-               if(gob)
+               if (gob)
                        strcpy(name, gob->id.name+2);
                else
                        strcpy(name, ob->id.name+2);
@@ -2741,32 +2864,68 @@ void make_proxy(Scene *scene)
                newob->lay= newbase->lay;
                
                /* remove base, leave user count of object, it gets linked in object_make_proxy */
-               if(gob==NULL) {
+               if (gob==NULL) {
                        BLI_remlink(&scene->base, oldbase);
                        MEM_freeN(oldbase);
-               }               
+               }
+               
                object_make_proxy(newob, ob, gob);
                
+               /* 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");
+               return OPERATOR_CANCELLED;
+       }
+       
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_proxy_make (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Make Proxy";
+       ot->idname= "OBJECT_OT_proxy_make";
+       ot->description= "Add empty object to become local replacement data of a library-linked object";
+       
+       /* callbacks */
+       ot->invoke= make_proxy_invoke;
+       ot->exec= make_proxy_exec;
+       ot->poll= ED_operator_object_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       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 *********************** */
 
-#define PAR_OBJECT             0
-#define PAR_ARMATURE   1
-#define PAR_BONE               2
-#define PAR_CURVE              3
-#define PAR_FOLLOW             4
-#define PAR_PATH_CONST 5
-#define PAR_LATTICE            6
-#define PAR_VERTEX             7
-#define PAR_TRIA               8
+#define PAR_OBJECT                             0
+#define PAR_ARMATURE                   1
+#define PAR_ARMATURE_NAME              2
+#define PAR_ARMATURE_ENVELOPE  3
+#define PAR_ARMATURE_AUTO              4
+#define PAR_BONE                               5
+#define PAR_CURVE                              6
+#define PAR_FOLLOW                             7
+#define PAR_PATH_CONST                 8
+#define PAR_LATTICE                            9
+#define PAR_VERTEX                             10
+#define PAR_TRIA                               11
 
 static EnumPropertyItem prop_make_parent_types[] = {
        {PAR_OBJECT, "OBJECT", 0, "Object", ""},
        {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
+       {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, "   With Empty Groups", ""},
+       {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, "   With Automatic Weights", ""},
+       {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, "   With Envelope Weights", ""},
        {PAR_BONE, "BONE", 0, "Bone", ""},
        {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
        {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
@@ -2787,6 +2946,22 @@ static int test_parent_loop(Object *par, Object *ob)
        return test_parent_loop(par->parent, ob);
 }
 
+void ED_object_parent(Object *ob, Object *par, int type, const char *substr)
+{
+       if(!par || test_parent_loop(par, ob)) {
+               ob->parent= NULL;
+               ob->partype= PAROBJECT;
+               ob->parsubstr[0]= 0;
+               return;
+       }
+
+       /* this could use some more checks */
+
+       ob->parent= par;
+       ob->partype &= ~PARTYPE;
+       ob->partype |= type;
+       BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
+}
 
 static int parent_set_exec(bContext *C, wmOperator *op)
 {
@@ -2794,6 +2969,7 @@ static int parent_set_exec(bContext *C, wmOperator *op)
        Object *par= CTX_data_active_object(C);
        bPoseChannel *pchan= NULL;
        int partype= RNA_enum_get(op->ptr, "type");
+       int pararm= ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
        
        par->recalc |= OB_RECALC_OB;
        
@@ -2865,24 +3041,20 @@ static int parent_set_exec(bContext *C, wmOperator *op)
                                        ob->loc[0] = vec[0];
                                        ob->loc[1] = vec[1];
                                }
-                               else if(partype==PAR_ARMATURE && ob->type==OB_MESH && par->type == OB_ARMATURE) {
+                               else if(pararm && ob->type==OB_MESH && par->type == OB_ARMATURE) {
+                                       if(partype == PAR_ARMATURE_NAME)
+                                               create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_NAME);
+                                       else if(partype == PAR_ARMATURE_ENVELOPE)
+                                               create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_ENVELOPE);
+                                       else if(partype == PAR_ARMATURE_AUTO)
+                                               create_vgroups_from_armature(scene, ob, par, ARM_GROUPS_AUTO);
                                        
-                                       if(1) {
-                                               /* Prompt the user as to whether he wants to
-                                               * add some vertex groups based on the bones
-                                               * in the parent armature.
-                                               */
-                                               create_vgroups_from_armature(scene, ob, par);
-                                               
-                                               /* get corrected inverse */
-                                               ob->partype= PAROBJECT;
-                                               what_does_parent(scene, ob, &workob);
-                                               
-                                               ob->partype= PARSKEL;
-                                       }
-                                       else
-                                               what_does_parent(scene, ob, &workob);
+                                       /* get corrected inverse */
+                                       ob->partype= PAROBJECT;
+                                       what_does_parent(scene, ob, &workob);
                                        
+                                       ob->partype= PARSKEL;
+
                                        Mat4Invert(ob->parentinv, workob.obmat);
                                }
                                else {
@@ -2893,7 +3065,7 @@ static int parent_set_exec(bContext *C, wmOperator *op)
                                
                                ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
                                
-                               if( ELEM3(partype, PAR_CURVE, PAR_ARMATURE, PAR_LATTICE) )
+                               if( ELEM(partype, PAR_CURVE, PAR_LATTICE) || pararm )
                                        ob->partype= PARSKEL; /* note, dna define, not operator property */
                                else
                                        ob->partype= PAROBJECT; /* note, dna define, not operator property */
@@ -2921,6 +3093,9 @@ static int parent_set_invoke(bContext *C, wmOperator *op, wmEvent *event)
        /* ob becomes parent, make the associated menus */
        if(ob->type==OB_ARMATURE) {
                uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE);
+               uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_NAME);
+               uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_ENVELOPE);
+               uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_ARMATURE_AUTO);
                uiItemEnumO(layout, NULL, 0, "OBJECT_OT_parent_set", "type", PAR_BONE);
        }
        else if(ob->type==OB_CURVE) {
@@ -3517,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) {   
@@ -3538,11 +3713,10 @@ void ED_object_exit_editmode(bContext *C, int flag)
                if(freedata) free_editLatt(obedit);
        }
        else if(obedit->type==OB_MBALL) {
-//             extern ListBase editelems;
-//             load_editMball();
-//             if(freedata) BLI_freelistN(&editelems);
+               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 */
@@ -3557,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);
 }
 
 
@@ -3572,10 +3749,11 @@ void ED_object_enter_editmode(bContext *C, int flag)
        if(scene->id.lib) return;
        if(base==NULL) return;
        
-       if(sa->spacetype==SPACE_VIEW3D)
+       if(sa && sa->spacetype==SPACE_VIEW3D)
                v3d= sa->spacedata.first;
        
-       if((v3d==NULL || (base->lay & v3d->lay))==0) return;
+       if(v3d && (base->lay & v3d->lay)==0) return;
+       else if(!v3d && (base->lay & scene->lay)==0) return;
 
        ob = base->object;
 
@@ -3589,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;
@@ -3629,14 +3810,15 @@ void ED_object_enter_editmode(bContext *C, int flag)
                scene->obedit= ob; // XXX for context
                ok= 1;
                make_editText(ob);
+
                WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene);
        }
        else if(ob->type==OB_MBALL) {
                scene->obedit= ob; // XXX for context
-//             ok= 1;
-// XXX         make_editMball();
+               ok= 1;
+               make_editMball(ob);
+
                WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene);
-               
        }
        else if(ob->type==OB_LATTICE) {
                scene->obedit= ob; // XXX for context
@@ -3676,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)
 {
        
@@ -3687,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;
@@ -3695,6 +3887,42 @@ void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
 
 /* *************************** */
 
+static int posemode_exec(bContext *C, wmOperator *op)
+{
+       Base *base= CTX_data_active_base(C);
+       
+       if(base->object->type==OB_ARMATURE) {
+               if(base->object==CTX_data_edit_object(C)) {
+                       ED_object_exit_editmode(C, EM_FREEDATA);
+                       ED_armature_enter_posemode(C, base);
+               }
+               else if(base->object->mode & OB_MODE_POSE)
+                       ED_armature_exit_posemode(C, base);
+               else
+                       ED_armature_enter_posemode(C, base);
+               
+               return OPERATOR_FINISHED;
+       }
+       
+       return OPERATOR_PASS_THROUGH;
+}
+
+void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
+{
+       /* identifiers */
+       ot->name= "Toggle Pose Mode";
+       ot->idname= "OBJECT_OT_posemode_toggle";
+       ot->description= "Enables or disables posing/selecting bones";
+       
+       /* api callbacks */
+       ot->exec= posemode_exec;
+       ot->poll= ED_operator_object_active;
+       
+       /* flag */
+       ot->flag= OPTYPE_REGISTER;
+}
+
+/* *********************** */
 
 void check_editmode(int type)
 {
@@ -3891,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;
@@ -3940,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;
@@ -3953,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);
@@ -5505,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;
@@ -5628,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;
@@ -5651,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;
@@ -5837,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);
                }
                
                
@@ -5858,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 */
@@ -6062,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 {
@@ -6243,12 +6485,11 @@ static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag
        return basen;
 }
 
-/* single object duplicate, if dupflag==0, fully linked, else it uses U.dupflag */
+/* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
 /* leaves selection of base/object unaltered */
-Base *ED_object_add_duplicate(Scene *scene, Base *base, int usedupflag)
+Base *ED_object_add_duplicate(Scene *scene, Base *base, int dupflag)
 {
        Base *basen;
-       int dupflag= usedupflag?U.dupflag:0;
 
        clear_id_newpoins();
        clear_sca_new_poins();  /* sensor/contr/act */
@@ -6298,8 +6539,8 @@ static int duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        duplicate_exec(C, op);
        
-       RNA_int_set(op->ptr, "mode", TFM_TRANSLATION);
-       WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
+//     RNA_int_set(op->ptr, "mode", TFM_TRANSLATION);
+//     WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
 
        return OPERATOR_FINISHED;
 }
@@ -6326,6 +6567,123 @@ void OBJECT_OT_duplicate(wmOperatorType *ot)
        RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
 }
 
+/* ************************** JOIN *********************** */
+
+static int join_exec(bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_active_object(C);
+
+       if(scene->obedit) {
+               BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode.");
+               return OPERATOR_CANCELLED;
+       }
+       else if(!ob) {
+               BKE_report(op->reports, RPT_ERROR, "Can't join unless there is an active object.");
+               return OPERATOR_CANCELLED;
+       }
+       else if(object_data_is_libdata(ob)) {
+               BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata.");
+               return OPERATOR_CANCELLED;
+       }
+
+       if(ob->type == OB_MESH)
+               return join_mesh_exec(C, op);
+       else if(ELEM(ob->type, OB_CURVE, OB_SURF))
+               return join_curve_exec(C, op);
+       else if(ob->type == OB_ARMATURE)
+               return join_armature_exec(C, op);
+
+       BKE_report(op->reports, RPT_ERROR, "This object type doesn't support joining.");
+
+       return OPERATOR_CANCELLED;
+}
+
+void OBJECT_OT_join(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Join";
+       ot->description = "Join selected objects into active object.";
+       ot->idname= "OBJECT_OT_join";
+       
+       /* api callbacks */
+       ot->exec= join_exec;
+       ot->poll= ED_operator_scene_editable;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+/********************** Smooth/Flat *********************/
+
+static int shade_smooth_exec(bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       Object *ob;
+       Curve *cu;
+       Nurb *nu;
+       int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
+       int done= 0;
+
+       CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
+               ob= base->object;
+
+               if(ob->type==OB_MESH) {
+                       mesh_set_smooth_flag(ob, !clear);
+
+                       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
+                       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
+
+                       done= 1;
+               }
+               else if ELEM(ob->type, OB_SURF, OB_CURVE) {
+                       cu= ob->data;
+
+                       for(nu=cu->nurb.first; nu; nu=nu->next) {
+                               if(!clear) nu->flag |= ME_SMOOTH;
+                               else nu->flag &= ~ME_SMOOTH;
+                               nu= nu->next;
+                       }
+
+                       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
+                       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
+
+                       done= 1;
+               }
+       }
+       CTX_DATA_END;
+
+       return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
+}
+
+void OBJECT_OT_shade_flat(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Shade Flat";
+       ot->idname= "OBJECT_OT_shade_flat";
+       
+       /* api callbacks */
+       ot->exec= shade_smooth_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+void OBJECT_OT_shade_smooth(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Shade Smooth";
+       ot->idname= "OBJECT_OT_shade_smooth";
+       
+       /* api callbacks */
+       ot->exec= shade_smooth_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+
+
 /* ********************** */
 
 void image_aspect(Scene *scene, View3D *v3d)
@@ -6682,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);
+}