doxygen: blender/editors tagged.
[blender.git] / source / blender / editors / object / object_transform.c
index cd0d97eed443348edce0f6e1a4960123820a01cd..c373f30a6ac6f3995c6f2f48725ddcc22763d808 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/editors/object/object_transform.c
+ *  \ingroup edobj
+ */
+
+
 #include <stdlib.h>
+#include <string.h>
 
-#include "DNA_action_types.h"
+#include "DNA_anim_types.h"
 #include "DNA_armature_types.h"
-#include "DNA_curve_types.h"
 #include "DNA_key_types.h"
-#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_screen_types.h"
-#include "DNA_view3d_types.h"
+#include "DNA_group_types.h"
 
-#include "BLI_arithb.h"
+#include "BLI_math.h"
 #include "BLI_editVert.h"
 #include "BLI_listbase.h"
+#include "BLI_utildefines.h"
 
 #include "BKE_context.h"
 #include "BKE_curve.h"
 #include "BKE_depsgraph.h"
-#include "BKE_global.h"
 #include "BKE_main.h"
 #include "BKE_mesh.h"
 #include "BKE_object.h"
 #include "BKE_report.h"
-#include "BKE_utildefines.h"
+#include "BKE_multires.h"
 
 #include "RNA_define.h"
 #include "RNA_access.h"
 #include "WM_api.h"
 #include "WM_types.h"
 
-#include "ED_anim_api.h"
 #include "ED_armature.h"
-#include "ED_curve.h"
+#include "ED_keyframing.h"
 #include "ED_mesh.h"
-#include "ED_object.h"
 #include "ED_screen.h"
 #include "ED_view3d.h"
 
 
 /*************************** Clear Transformation ****************************/
 
-static int object_location_clear_exec(bContext *C, wmOperator *op)
+/* clear location of object */
+static void object_clear_loc(Object *ob)
 {
-       int armature_clear= 0;
+       /* clear location if not locked */
+       if ((ob->protectflag & OB_LOCK_LOCX)==0)
+               ob->loc[0]= ob->dloc[0]= 0.0f;
+       if ((ob->protectflag & OB_LOCK_LOCY)==0)
+               ob->loc[1]= ob->dloc[1]= 0.0f;
+       if ((ob->protectflag & OB_LOCK_LOCZ)==0)
+               ob->loc[2]= ob->dloc[2]= 0.0f;
+}
 
-       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
-               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)
-                               ob->loc[1]= ob->dloc[1]= 0.0f;
-                       if((ob->protectflag & OB_LOCK_LOCZ)==0)
-                               ob->loc[2]= ob->dloc[2]= 0.0f;
+/* clear rotation of object */
+static void object_clear_rot(Object *ob)
+{
+       /* clear rotations that aren't locked */
+       if (ob->protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) {
+               if (ob->protectflag & OB_LOCK_ROT4D) {
+                       /* perform clamping on a component by component basis */
+                       if (ob->rotmode == ROT_MODE_AXISANGLE) {
+                               if ((ob->protectflag & OB_LOCK_ROTW) == 0)
+                                       ob->rotAngle= ob->drotAngle= 0.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTX) == 0)
+                                       ob->rotAxis[0]= ob->drotAxis[0]= 0.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTY) == 0)
+                                       ob->rotAxis[1]= ob->drotAxis[1]= 0.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
+                                       ob->rotAxis[2]= ob->drotAxis[2]= 0.0f;
+                                       
+                               /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
+                               if (IS_EQ(ob->rotAxis[0], ob->rotAxis[1]) && IS_EQ(ob->rotAxis[1], ob->rotAxis[2]))
+                                       ob->rotAxis[1] = 1.0f;
+                               if (IS_EQ(ob->drotAxis[0], ob->drotAxis[1]) && IS_EQ(ob->drotAxis[1], ob->drotAxis[2]))
+                                       ob->drotAxis[1]= 1.0f;
+                       }
+                       else if (ob->rotmode == ROT_MODE_QUAT) {
+                               if ((ob->protectflag & OB_LOCK_ROTW) == 0)
+                                       ob->quat[0]= ob->dquat[0]= 1.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTX) == 0)
+                                       ob->quat[1]= ob->dquat[1]= 0.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTY) == 0)
+                                       ob->quat[2]= ob->dquat[2]= 0.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
+                                       ob->quat[3]= ob->dquat[3]= 0.0f;
+                                       
+                               // TODO: does this quat need normalising now?
+                       }
+                       else {
+                               /* the flag may have been set for the other modes, so just ignore the extra flag... */
+                               if ((ob->protectflag & OB_LOCK_ROTX) == 0)
+                                       ob->rot[0]= ob->drot[0]= 0.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTY) == 0)
+                                       ob->rot[1]= ob->drot[1]= 0.0f;
+                               if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
+                                       ob->rot[2]= ob->drot[2]= 0.0f;
+                       }
+               }
+               else {
+                       /* perform clamping using euler form (3-components) */
+                       // FIXME: deltas are not handled for these cases yet...
+                       float eul[3], oldeul[3], quat1[4] = {0};
+                       
+                       if (ob->rotmode == ROT_MODE_QUAT) {
+                               QUATCOPY(quat1, ob->quat);
+                               quat_to_eul(oldeul, ob->quat);
+                       }
+                       else if (ob->rotmode == ROT_MODE_AXISANGLE) {
+                               axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, ob->rotAxis, ob->rotAngle);
+                       }
+                       else {
+                               copy_v3_v3(oldeul, ob->rot);
+                       }
+                       
+                       eul[0]= eul[1]= eul[2]= 0.0f;
+                       
+                       if (ob->protectflag & OB_LOCK_ROTX)
+                               eul[0]= oldeul[0];
+                       if (ob->protectflag & OB_LOCK_ROTY)
+                               eul[1]= oldeul[1];
+                       if (ob->protectflag & OB_LOCK_ROTZ)
+                               eul[2]= oldeul[2];
+                       
+                       if (ob->rotmode == ROT_MODE_QUAT) {
+                               eul_to_quat(ob->quat, eul);
+                               /* quaternions flip w sign to accumulate rotations correctly */
+                               if ((quat1[0]<0.0f && ob->quat[0]>0.0f) || (quat1[0]>0.0f && ob->quat[0]<0.0f)) {
+                                       mul_qt_fl(ob->quat, -1.0f);
+                               }
+                       }
+                       else if (ob->rotmode == ROT_MODE_AXISANGLE) {
+                               eulO_to_axis_angle(ob->rotAxis, &ob->rotAngle,eul, EULER_ORDER_DEFAULT);
+                       }
+                       else {
+                               copy_v3_v3(ob->rot, eul);
+                       }
+               }
+       }                                                // Duplicated in source/blender/editors/armature/editarmature.c
+       else { 
+               if (ob->rotmode == ROT_MODE_QUAT) {
+                       unit_qt(ob->quat);
+                       unit_qt(ob->dquat);
+               }
+               else if (ob->rotmode == ROT_MODE_AXISANGLE) {
+                       unit_axis_angle(ob->rotAxis, &ob->rotAngle);
+                       unit_axis_angle(ob->drotAxis, &ob->drotAngle);
+               }
+               else {
+                       zero_v3(ob->rot);
+                       zero_v3(ob->drot);
                }
-               ob->recalc |= OB_RECALC_OB;
        }
-       CTX_DATA_END;
+}
+
+/* clear scale of object */
+static void object_clear_scale(Object *ob)
+{
+       /* clear scale factors which are not locked */
+       if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
+               ob->dsize[0]= 0.0f;
+               ob->size[0]= 1.0f;
+       }
+       if ((ob->protectflag & OB_LOCK_SCALEY)==0) {
+               ob->dsize[1]= 0.0f;
+               ob->size[1]= 1.0f;
+       }
+       if ((ob->protectflag & OB_LOCK_SCALEZ)==0) {
+               ob->dsize[2]= 0.0f;
+               ob->size[2]= 1.0f;
+       }
+}
 
-       if(armature_clear==0) /* in this case flush was done */
-               ED_anim_dag_flush_update(C);    
+/* --------------- */
+
+/* generic exec for clear-transform operators */
+static int object_clear_transform_generic_exec(bContext *C, wmOperator *op, 
+               void (*clear_func)(Object*), const char default_ksName[])
+{
+       Main *bmain = CTX_data_main(C);
+       Scene *scene= CTX_data_scene(C);
+       KeyingSet *ks;
        
-       WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
+       /* sanity checks */
+       if ELEM(NULL, clear_func, default_ksName) {
+               BKE_report(op->reports, RPT_ERROR, "Programming error: missing clear transform func or Keying Set Name");
+               return OPERATOR_CANCELLED;
+       }
+       
+       /* get KeyingSet to use */
+       ks = ANIM_get_keyingset_for_autokeying(scene, default_ksName);
+       
+       /* operate on selected objects only if they aren't in weight-paint mode 
+        * (so that object-transform clearing won't be applied at same time as bone-clearing)
+        */
+       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
+       {
+               if (!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
+                       /* run provided clearing function */
+                       clear_func(ob);
+                       
+                       /* auto keyframing */
+                       if (autokeyframe_cfra_can_key(scene, &ob->id)) {
+                               ListBase dsources = {NULL, NULL};
+                               
+                               /* now insert the keyframe(s) using the Keying Set
+                                *      1) add datasource override for the Object
+                                *      2) insert keyframes
+                                *      3) free the extra info 
+                                */
+                               ANIM_relative_keyingset_add_source(&dsources, &ob->id, NULL, NULL); 
+                               ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
+                               BLI_freelistN(&dsources);
+                       }
+                       
+                       /* tag for updates */
+                       ob->recalc |= OB_RECALC_OB;
+               }
+       }
+       CTX_DATA_END;
        
+       /* this is needed so children are also updated */
+       DAG_ids_flush_update(bmain, 0);
+
+       WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
+
        return OPERATOR_FINISHED;
 }
 
+/* --------------- */
+
+
+static int object_location_clear_exec(bContext *C, wmOperator *op)
+{
+       return object_clear_transform_generic_exec(C, op, object_clear_loc, "Location");
+}
+
 void OBJECT_OT_location_clear(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Clear Location";
-       ot->description = "Clear the object's location.";
+       ot->description = "Clear the object's location";
        ot->idname= "OBJECT_OT_location_clear";
        
        /* api callbacks */
        ot->exec= object_location_clear_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -112,99 +283,19 @@ void OBJECT_OT_location_clear(wmOperatorType *ot)
 
 static int object_rotation_clear_exec(bContext *C, wmOperator *op)
 {
-       int armature_clear= 0;
-
-       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
-               if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
-                       if (ob->protectflag & (OB_LOCK_ROTX|OB_LOCK_ROTY|OB_LOCK_ROTZ|OB_LOCK_ROTW)) {
-                               /* check if convert to eulers for locking... */
-                               if (ob->protectflag & OB_LOCK_ROT4D) {
-                                       /* perform clamping on a component by component basis */
-                                       if ((ob->protectflag & OB_LOCK_ROTW) == 0)
-                                               ob->quat[0]= (ob->rotmode == ROT_MODE_AXISANGLE) ? 0.0f : 1.0f;
-                                       if ((ob->protectflag & OB_LOCK_ROTX) == 0)
-                                               ob->quat[1]= 0.0f;
-                                       if ((ob->protectflag & OB_LOCK_ROTY) == 0)
-                                               ob->quat[2]= 0.0f;
-                                       if ((ob->protectflag & OB_LOCK_ROTZ) == 0)
-                                               ob->quat[3]= 0.0f;
-                               }
-                               else {
-                                       /* perform clamping using euler form (3-components) */
-                                       float eul[3], oldeul[3], quat1[4];
-                                       
-                                       if (ob->rotmode == ROT_MODE_QUAT) {
-                                               QUATCOPY(quat1, ob->quat);
-                                               QuatToEul(ob->quat, oldeul);
-                                       }
-                                       else if (ob->rotmode == ROT_MODE_AXISANGLE) {
-                                               AxisAngleToEulO(&ob->quat[1], ob->quat[0], oldeul, EULER_ORDER_DEFAULT);
-                                       }
-                                       else {
-                                               VECCOPY(oldeul, ob->rot);
-                                       }
-                                       
-                                       eul[0]= eul[1]= eul[2]= 0.0f;
-                                       
-                                       if (ob->protectflag & OB_LOCK_ROTX)
-                                               eul[0]= oldeul[0];
-                                       if (ob->protectflag & OB_LOCK_ROTY)
-                                               eul[1]= oldeul[1];
-                                       if (ob->protectflag & OB_LOCK_ROTZ)
-                                               eul[2]= oldeul[2];
-                                       
-                                       if (ob->rotmode == ROT_MODE_QUAT) {
-                                               EulToQuat(eul, ob->quat);
-                                               /* quaternions flip w sign to accumulate rotations correctly */
-                                               if ((quat1[0]<0.0f && ob->quat[0]>0.0f) || (quat1[0]>0.0f && ob->quat[0]<0.0f)) {
-                                                       QuatMulf(ob->quat, -1.0f);
-                                               }
-                                       }
-                                       else if (ob->rotmode == ROT_MODE_AXISANGLE) {
-                                               AxisAngleToEulO(&ob->quat[1], ob->quat[0], oldeul, EULER_ORDER_DEFAULT);
-                                       }
-                                       else {
-                                               VECCOPY(ob->rot, eul);
-                                       }
-                               }
-                       }                                               
-                       else { 
-                               if (ob->rotmode == ROT_MODE_QUAT) {
-                                       ob->quat[1]=ob->quat[2]=ob->quat[3]= 0.0f; 
-                                       ob->quat[0]= 1.0f;
-                               }
-                               else if (ob->rotmode == ROT_MODE_AXISANGLE) {
-                                       /* by default, make rotation of 0 radians around y-axis (roll) */
-                                       ob->quat[0]=ob->quat[1]=ob->quat[3]= 0.0f;
-                                       ob->quat[2]= 1.0f;
-                               }
-                               else {
-                                       ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f;
-                               }
-                       }
-               }
-               ob->recalc |= OB_RECALC_OB;
-       }
-       CTX_DATA_END;
-
-       if(armature_clear==0) /* in this case flush was done */
-               ED_anim_dag_flush_update(C);    
-       
-       WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
-       
-       return OPERATOR_FINISHED;
+       return object_clear_transform_generic_exec(C, op, object_clear_rot, "Rotation");
 }
 
 void OBJECT_OT_rotation_clear(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Clear Rotation";
-       ot->description = "Clear the object's rotation.";
+       ot->description = "Clear the object's rotation";
        ot->idname= "OBJECT_OT_rotation_clear";
        
        /* api callbacks */
        ot->exec= object_rotation_clear_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -212,73 +303,48 @@ void OBJECT_OT_rotation_clear(wmOperatorType *ot)
 
 static int object_scale_clear_exec(bContext *C, wmOperator *op)
 {
-       int armature_clear= 0;
-
-       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
-               if(!(ob->mode & OB_MODE_WEIGHT_PAINT)) {
-                       if((ob->protectflag & OB_LOCK_SCALEX)==0) {
-                               ob->dsize[0]= 0.0f;
-                               ob->size[0]= 1.0f;
-                       }
-                       if((ob->protectflag & OB_LOCK_SCALEY)==0) {
-                               ob->dsize[1]= 0.0f;
-                               ob->size[1]= 1.0f;
-                       }
-                       if((ob->protectflag & OB_LOCK_SCALEZ)==0) {
-                               ob->dsize[2]= 0.0f;
-                               ob->size[2]= 1.0f;
-                       }
-               }
-               ob->recalc |= OB_RECALC_OB;
-       }
-       CTX_DATA_END;
-       
-       if(armature_clear==0) /* in this case flush was done */
-               ED_anim_dag_flush_update(C);    
-       
-       WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
-       
-       return OPERATOR_FINISHED;
+       return object_clear_transform_generic_exec(C, op, object_clear_scale, "Scaling");
 }
 
 void OBJECT_OT_scale_clear(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Clear Scale";
-       ot->description = "Clear the object's scale.";
+       ot->description = "Clear the object's scale";
        ot->idname= "OBJECT_OT_scale_clear";
        
        /* api callbacks */
        ot->exec= object_scale_clear_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-static int object_origin_clear_exec(bContext *C, wmOperator *op)
+/* --------------- */
+
+static int object_origin_clear_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       float *v1, *v3, mat[3][3];
-       int armature_clear= 0;
+       Main *bmain= CTX_data_main(C);
+       float *v1, *v3;
+       float mat[3][3];
 
-       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
-               if(ob->parent) {
+       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
+       {
+               if (ob->parent) {
+                       /* vectors pointed to by v1 and v3 will get modified */
                        v1= ob->loc;
                        v3= ob->parentinv[3];
                        
-                       Mat3CpyMat4(mat, ob->parentinv);
-                       VECCOPY(v3, v1);
-                       v3[0]= -v3[0];
-                       v3[1]= -v3[1];
-                       v3[2]= -v3[2];
-                       Mat3MulVecfl(mat, v3);
+                       copy_m3_m4(mat, ob->parentinv);
+                       negate_v3_v3(v3, v1);
+                       mul_m3_v3(mat, v3);
                }
                ob->recalc |= OB_RECALC_OB;
        }
        CTX_DATA_END;
 
-       if(armature_clear==0) /* in this case flush was done */
-               ED_anim_dag_flush_update(C);    
+       DAG_ids_flush_update(bmain, 0);
        
        WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
        
@@ -289,12 +355,12 @@ void OBJECT_OT_origin_clear(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Clear Origin";
-       ot->description = "Clear the object's origin.";
+       ot->description = "Clear the object's origin";
        ot->idname= "OBJECT_OT_origin_clear";
        
        /* api callbacks */
        ot->exec= object_origin_clear_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -312,9 +378,9 @@ static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob )
        /* a change was made, adjust the children to compensate */
        for(ob_child=bmain->object.first; ob_child; ob_child=ob_child->id.next) {
                if(ob_child->parent == ob) {
-                       ED_object_apply_obmat(ob_child);
+                       object_apply_mat4(ob_child, ob_child->obmat, TRUE, FALSE);
                        what_does_parent(scene, ob_child, &workob);
-                       Mat4Invert(ob_child->parentinv, workob.obmat);
+                       invert_m4_m4(ob_child->parentinv, workob.obmat);
                }
        }
 }
@@ -323,7 +389,6 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
 {
        Main *bmain= CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
-       Object *ob;
        bArmature *arm;
        Mesh *me;
        Curve *cu;
@@ -335,13 +400,12 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
        int a, change = 0;
        
        /* first check if we can execute */
-       CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
-               ob= base->object;
+       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
 
                if(ob->type==OB_MESH) {
                        me= ob->data;
                        
-                       if(me->id.us>1) {
+                       if(ID_REAL_USERS(me) > 1) {
                                BKE_report(reports, RPT_ERROR, "Can't apply to a multi user mesh, doing nothing.");
                                return OPERATOR_CANCELLED;
                        }
@@ -349,7 +413,7 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
                else if(ob->type==OB_ARMATURE) {
                        arm= ob->data;
                        
-                       if(arm->id.us>1) {
+                       if(ID_REAL_USERS(arm) > 1) {
                                BKE_report(reports, RPT_ERROR, "Can't apply to a multi user armature, doing nothing.");
                                return OPERATOR_CANCELLED;
                        }
@@ -357,10 +421,14 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
                else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
                        cu= ob->data;
                        
-                       if(cu->id.us>1) {
+                       if(ID_REAL_USERS(cu) > 1) {
                                BKE_report(reports, RPT_ERROR, "Can't apply to a multi user curve, doing nothing.");
                                return OPERATOR_CANCELLED;
                        }
+                       if(!(cu->flag & CU_3D) && (apply_rot || apply_loc)) {
+                               BKE_report(reports, RPT_ERROR, "Neither rotation nor location could be applied to a 2d curve, doing nothing.");
+                               return OPERATOR_CANCELLED;
+                       }
                        if(cu->key) {
                                BKE_report(reports, RPT_ERROR, "Can't apply to a curve with vertex keys, doing nothing.");
                                return OPERATOR_CANCELLED;
@@ -370,31 +438,40 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
        CTX_DATA_END;
        
        /* now execute */
-       CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
-               ob= base->object;
+       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
 
                /* calculate rotation/scale matrix */
                if(apply_scale && apply_rot)
                        object_to_mat3(ob, rsmat);
                else if(apply_scale)
                        object_scale_to_mat3(ob, rsmat);
-               else if(apply_rot)
+               else if(apply_rot) {
+                       float tmat[3][3], timat[3][3];
+
+                       /* simple rotation matrix */
                        object_rot_to_mat3(ob, rsmat);
+
+                       /* correct for scale, note mul_m3_m3m3 has swapped args! */
+                       object_scale_to_mat3(ob, tmat);
+                       invert_m3_m3(timat, tmat);
+                       mul_m3_m3m3(rsmat, timat, rsmat);
+                       mul_m3_m3m3(rsmat, rsmat, tmat);
+               }
                else
-                       Mat3One(rsmat);
+                       unit_m3(rsmat);
 
-               Mat4CpyMat3(mat, rsmat);
+               copy_m4_m3(mat, rsmat);
 
                /* calculate translation */
                if(apply_loc) {
-                       VecCopyf(mat[3], ob->loc);
+                       copy_v3_v3(mat[3], ob->loc);
 
                        if(!(apply_scale && apply_rot)) {
                                /* correct for scale and rotation that is still applied */
                                object_to_mat3(ob, obmat);
-                               Mat3Inv(iobmat, obmat);
-                               Mat3MulMat3(tmat, rsmat, iobmat);
-                               Mat3MulVecfl(tmat, mat[3]);
+                               invert_m3_m3(iobmat, obmat);
+                               mul_m3_m3m3(tmat, rsmat, iobmat);
+                               mul_m3_v3(tmat, mat[3]);
                        }
                }
 
@@ -402,10 +479,12 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
                if(ob->type==OB_MESH) {
                        me= ob->data;
                        
+                       multiresModifier_scale_disp(scene, ob);
+                       
                        /* adjust data */
                        mvert= me->mvert;
                        for(a=0; a<me->totvert; a++, mvert++)
-                               Mat4MulVecfl(mat, mvert->co);
+                               mul_m4_v3(mat, mvert->co);
                        
                        if(me->key) {
                                KeyBlock *kb;
@@ -414,7 +493,7 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
                                        float *fp= kb->data;
                                        
                                        for(a=0; a<kb->totelem; a++, fp+=3)
-                                               Mat4MulVecfl(mat, fp);
+                                               mul_m4_v3(mat, fp);
                                }
                        }
                        
@@ -427,23 +506,22 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
                else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
                        cu= ob->data;
 
-                       scale = Mat3ToScalef(rsmat);
-                       
+                       scale = mat3_to_scale(rsmat);
+
                        for(nu=cu->nurb.first; nu; nu=nu->next) {
                                if(nu->type == CU_BEZIER) {
                                        a= nu->pntsu;
                                        for(bezt= nu->bezt; a--; bezt++) {
-                                               Mat4MulVecfl(mat, bezt->vec[0]);
-                                               Mat4MulVecfl(mat, bezt->vec[1]);
-                                               Mat4MulVecfl(mat, bezt->vec[2]);
+                                               mul_m4_v3(mat, bezt->vec[0]);
+                                               mul_m4_v3(mat, bezt->vec[1]);
+                                               mul_m4_v3(mat, bezt->vec[2]);
                                                bezt->radius *= scale;
-                                               bezt++;
                                        }
                                }
                                else {
                                        a= nu->pntsu*nu->pntsv;
                                        for(bp= nu->bp; a--; bp++)
-                                               Mat4MulVecfl(mat, bp->vec);
+                                               mul_m4_v3(mat, bp->vec);
                                }
                        }
                }
@@ -451,16 +529,19 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
                        continue;
 
                if(apply_loc)
-                       ob->loc[0]= ob->loc[1]= ob->loc[2]= 0.0f;
+                       zero_v3(ob->loc);
                if(apply_scale)
                        ob->size[0]= ob->size[1]= ob->size[2]= 1.0f;
-               if(apply_rot)
-                       ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0f;
+               if(apply_rot) {
+                       zero_v3(ob->rot);
+                       unit_qt(ob->quat);
+                       unit_axis_angle(ob->rotAxis, &ob->rotAngle);
+               }
 
                where_is_object(scene, ob);
                ignore_parent_tx(bmain, scene, ob);
 
-               DAG_id_flush_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
+               DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
 
                change = 1;
        }
@@ -473,20 +554,19 @@ static int apply_objects_internal(bContext *C, ReportList *reports, int apply_lo
        return OPERATOR_FINISHED;
 }
 
-static int visual_transform_apply_exec(bContext *C, wmOperator *op)
+static int visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Scene *scene= CTX_data_scene(C);
        int change = 0;
        
        CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
                where_is_object(scene, ob);
-
-               VECCOPY(ob->loc, ob->obmat[3]);
-               Mat4ToSize(ob->obmat, ob->size);
-               Mat4ToEul(ob->obmat, ob->rot);
-               
+               object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
                where_is_object(scene, ob);
-               
+
+               /* update for any children that may get moved */
+               DAG_id_tag_update(&ob->id, OB_RECALC_OB);
+       
                change = 1;
        }
        CTX_DATA_END;
@@ -502,12 +582,12 @@ void OBJECT_OT_visual_transform_apply(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Apply Visual Transform";
-       ot->description = "Apply the object's visual transformation to its data.";
+       ot->description = "Apply the object's visual transformation to its data";
        ot->idname= "OBJECT_OT_visual_transform_apply";
        
        /* api callbacks */
        ot->exec= visual_transform_apply_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -522,12 +602,12 @@ void OBJECT_OT_location_apply(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Apply Location";
-       ot->description = "Apply the object's location to its data.";
+       ot->description = "Apply the object's location to its data";
        ot->idname= "OBJECT_OT_location_apply";
        
        /* api callbacks */
        ot->exec= location_apply_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -542,12 +622,12 @@ void OBJECT_OT_scale_apply(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Apply Scale";
-       ot->description = "Apply the object's scale to its data.";
+       ot->description = "Apply the object's scale to its data";
        ot->idname= "OBJECT_OT_scale_apply";
        
        /* api callbacks */
        ot->exec= scale_apply_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
@@ -562,425 +642,322 @@ void OBJECT_OT_rotation_apply(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Apply Rotation";
-       ot->description = "Apply the object's rotation to its data.";
+       ot->description = "Apply the object's rotation to its data";
        ot->idname= "OBJECT_OT_rotation_apply";
        
        /* api callbacks */
        ot->exec= rotation_apply_exec;
-       ot->poll= ED_operator_object_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-/************************ Texture Space Transform ****************************/
-
-void texspace_edit(Scene *scene, View3D *v3d)
-{
-       Base *base;
-       int nr=0;
-       
-       /* first test if from visible and selected objects
-        * texspacedraw is set:
-        */
-       
-       if(scene->obedit) return; // XXX get from context
-       
-       for(base= FIRSTBASE; base; base= base->next) {
-               if(TESTBASELIB(v3d, base)) {
-                       break;
-               }
-       }
-
-       if(base==0) {
-               return;
-       }
-       
-       nr= 0; // XXX pupmenu("Texture Space %t|Grab/Move%x1|Size%x2");
-       if(nr<1) return;
-       
-       for(base= FIRSTBASE; base; base= base->next) {
-               if(TESTBASELIB(v3d, base)) {
-                       base->object->dtx |= OB_TEXSPACE;
-               }
-       }
-       
-
-       if(nr==1) {
-// XXX         initTransform(TFM_TRANSLATION, CTX_TEXTURE);
-// XXX         Transform();
-       }
-       else if(nr==2) {
-// XXX         initTransform(TFM_RESIZE, CTX_TEXTURE);
-// XXX         Transform();
-       }
-       else if(nr==3) {
-// XXX         initTransform(TFM_ROTATION, CTX_TEXTURE);
-// XXX         Transform();
-       }
-}
-
-/************************ Mirror Menu ****************************/
-
-void mirrormenu(void)
-{
-// XXX         initTransform(TFM_MIRROR, CTX_NO_PET);
-// XXX         Transform();
-}
-
 /********************* Set Object Center ************************/
 
-static EnumPropertyItem prop_set_center_types[] = {
-       {0, "CENTER", 0, "ObData to Center", "Move object data around Object center"},
-       {1, "CENTERNEW", 0, "Center New", "Move Object center to center of object data"},
-       {2, "CENTERCURSOR", 0, "Center Cursor", "Move Object Center to position of the 3d cursor"},
-       {0, NULL, 0, NULL, NULL}
+enum {
+       GEOMETRY_TO_ORIGIN=0,
+       ORIGIN_TO_GEOMETRY,
+       ORIGIN_TO_CURSOR
 };
 
-/* 0 == do center, 1 == center new, 2 == center cursor */
-static int object_center_set_exec(bContext *C, wmOperator *op)
+static int object_origin_set_exec(bContext *C, wmOperator *op)
 {
        Main *bmain= CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
-       ScrArea *sa= CTX_wm_area(C);
-       View3D *v3d= sa->spacedata.first;
        Object *obedit= CTX_data_edit_object(C);
-       Object *ob;
-       Mesh *me, *tme;
-       Curve *cu;
-/*     BezTriple *bezt;
-       BPoint *bp; */
-       Nurb *nu, *nu1;
-       EditVert *eve;
-       float cent[3], centn[3], min[3], max[3], omat[3][3];
-       int a, total= 0;
+       Object *tob;
+       float cursor[3], cent[3], cent_neg[3], centn[3], min[3], max[3];
        int centermode = RNA_enum_get(op->ptr, "type");
-       
+       int around = RNA_enum_get(op->ptr, "center"); /* initialized from v3d->around */
+
        /* keep track of what is changed */
        int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
-       MVert *mvert;
 
-       if(scene->id.lib || v3d==NULL){
-               BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed on Lib data");
-                return OPERATOR_CANCELLED;
-       }
-       if (obedit && centermode > 0) {
+       if (obedit && centermode != GEOMETRY_TO_ORIGIN) {
                BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
                return OPERATOR_CANCELLED;
-       }       
-       cent[0]= cent[1]= cent[2]= 0.0; 
-       
-       if(obedit) {
+       }
+       else {
+               /* get the view settings if 'around' isnt set and the view is available */
+               View3D *v3d= CTX_wm_view3d(C);
+               copy_v3_v3(cursor, give_cursor(scene, v3d));
+               if(v3d && !RNA_property_is_set(op->ptr, "around"))
+                       around= v3d->around;
+       }
 
+       zero_v3(cent);
+
+       if(obedit) {
                INIT_MINMAX(min, max);
-       
+
                if(obedit->type==OB_MESH) {
                        Mesh *me= obedit->data;
                        EditMesh *em = BKE_mesh_get_editmesh(me);
+                       EditVert *eve;
 
-                       for(eve= em->verts.first; eve; eve= eve->next) {
-                               if(v3d->around==V3D_CENTROID) {
+                       if(around==V3D_CENTROID) {
+                               int total= 0;
+                               for(eve= em->verts.first; eve; eve= eve->next) {
                                        total++;
-                                       VECADD(cent, cent, eve->co);
+                                       add_v3_v3(cent, eve->co);
                                }
-                               else {
-                                       DO_MINMAX(eve->co, min, max);
+                               if(total) {
+                                       mul_v3_fl(cent, 1.0f/(float)total);
                                }
                        }
-                       
-                       if(v3d->around==V3D_CENTROID) {
-                               VecMulf(cent, 1.0f/(float)total);
-                       }
                        else {
-                               cent[0]= (min[0]+max[0])/2.0f;
-                               cent[1]= (min[1]+max[1])/2.0f;
-                               cent[2]= (min[2]+max[2])/2.0f;
+                               for(eve= em->verts.first; eve; eve= eve->next) {
+                                       DO_MINMAX(eve->co, min, max);
+                               }
+                               mid_v3_v3v3(cent, min, max);
                        }
-                       
-                       for(eve= em->verts.first; eve; eve= eve->next) {
-                               VecSubf(eve->co, eve->co, cent);                        
+
+                       if(!is_zero_v3(cent)) {
+                               for(eve= em->verts.first; eve; eve= eve->next) {
+                                       sub_v3_v3(eve->co, cent);
+                               }
+
+                               recalc_editnormals(em);
+                               tot_change++;
+                               DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
                        }
-                       
-                       recalc_editnormals(em);
-                       tot_change++;
-                       DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
                        BKE_mesh_end_editmesh(me, em);
                }
        }
-       
+
        /* reset flags */
-       CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
-                       base->object->flag &= ~OB_DONE;
+       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
+                       ob->flag &= ~OB_DONE;
        }
        CTX_DATA_END;
-       
-       for (me= G.main->mesh.first; me; me= me->id.next) {
-               me->flag &= ~ME_ISDONE;
+
+       for (tob= bmain->object.first; tob; tob= tob->id.next) {
+               if(tob->data)
+                       ((ID *)tob->data)->flag &= ~LIB_DOIT;
+               if(tob->dup_group)
+                       ((ID *)tob->dup_group)->flag &= ~LIB_DOIT;
        }
-       
-       CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
-               if((base->object->flag & OB_DONE)==0) {
-                       base->object->flag |= OB_DONE;
-                               
-                       if(obedit==NULL && (me=get_mesh(base->object)) ) {
-                               if (me->id.lib) {
-                                       tot_lib_error++;
-                               } else {
-                                       if(centermode==2) {
-                                               VECCOPY(cent, give_cursor(scene, v3d));
-                                               Mat4Invert(base->object->imat, base->object->obmat);
-                                               Mat4MulVecfl(base->object->imat, cent);
-                                       } else {
-                                               INIT_MINMAX(min, max);
-                                               mvert= me->mvert;
-                                               for(a=0; a<me->totvert; a++, mvert++) {
-                                                       DO_MINMAX(mvert->co, min, max);
-                                               }
-                                       
-                                               cent[0]= (min[0]+max[0])/2.0f;
-                                               cent[1]= (min[1]+max[1])/2.0f;
-                                               cent[2]= (min[2]+max[2])/2.0f;
-                                       }
 
-                                       mvert= me->mvert;
-                                       for(a=0; a<me->totvert; a++, mvert++) {
-                                               VecSubf(mvert->co, mvert->co, cent);
+       CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
+               if((ob->flag & OB_DONE)==0) {
+                       int do_inverse_offset = FALSE;
+                       ob->flag |= OB_DONE;
+
+                       if(centermode == ORIGIN_TO_CURSOR) {
+                               copy_v3_v3(cent, cursor);
+                               invert_m4_m4(ob->imat, ob->obmat);
+                               mul_m4_v3(ob->imat, cent);
+                       }
+                       
+                       if(ob->data == NULL) {
+                               /* special support for dupligroups */
+                               if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group && (ob->dup_group->id.flag & LIB_DOIT)==0) {
+                                       if(ob->dup_group->id.lib) {
+                                               tot_lib_error++;
                                        }
-                                       
-                                       if (me->key) {
-                                               KeyBlock *kb;
-                                               for (kb=me->key->block.first; kb; kb=kb->next) {
-                                                       float *fp= kb->data;
-                                                       
-                                                       for (a=0; a<kb->totelem; a++, fp+=3) {
-                                                               VecSubf(fp, fp, cent);
-                                                       }
+                                       else {
+                                               if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
+                                               else {
+                                                       /* only bounds support */
+                                                       INIT_MINMAX(min, max);
+                                                       minmax_object_duplis(scene, ob, min, max);
+                                                       mid_v3_v3v3(cent, min, max);
+                                                       invert_m4_m4(ob->imat, ob->obmat);
+                                                       mul_m4_v3(ob->imat, cent);
                                                }
-                                       }
-                                               
-                                       me->flag |= ME_ISDONE;
-                                               
-                                       if(centermode) {
-                                               Mat3CpyMat4(omat, base->object->obmat);
                                                
-                                               VECCOPY(centn, cent);
-                                               Mat3MulVecfl(omat, centn);
-                                               base->object->loc[0]+= centn[0];
-                                               base->object->loc[1]+= centn[1];
-                                               base->object->loc[2]+= centn[2];
-                                               
-                                               where_is_object(scene, base->object);
-                                               ignore_parent_tx(bmain, scene, base->object);
-                                               
-                                               /* other users? */
-                                               CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
-                                                       ob = base->object;
-                                                       if((ob->flag & OB_DONE)==0) {
-                                                               tme= get_mesh(ob);
-                                                               
-                                                               if(tme==me) {
-                                                                       
-                                                                       ob->flag |= OB_DONE;
-                                                                       ob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
-
-                                                                       Mat3CpyMat4(omat, ob->obmat);
-                                                                       VECCOPY(centn, cent);
-                                                                       Mat3MulVecfl(omat, centn);
-                                                                       ob->loc[0]+= centn[0];
-                                                                       ob->loc[1]+= centn[1];
-                                                                       ob->loc[2]+= centn[2];
-                                                                       
-                                                                       where_is_object(scene, ob);
-                                                                       ignore_parent_tx(bmain, scene, ob);
-                                                                       
-                                                                       if(tme && (tme->flag & ME_ISDONE)==0) {
-                                                                               mvert= tme->mvert;
-                                                                               for(a=0; a<tme->totvert; a++, mvert++) {
-                                                                                       VecSubf(mvert->co, mvert->co, cent);
-                                                                               }
-                                                                               
-                                                                               if (tme->key) {
-                                                                                       KeyBlock *kb;
-                                                                                       for (kb=tme->key->block.first; kb; kb=kb->next) {
-                                                                                               float *fp= kb->data;
-                                                                                               
-                                                                                               for (a=0; a<kb->totelem; a++, fp+=3) {
-                                                                                                       VecSubf(fp, fp, cent);
-                                                                                               }
-                                                                                       }
-                                                                               }
-                                                                               
-                                                                               tme->flag |= ME_ISDONE;
-                                                                       }
-                                                               }
-                                                       }
-                                                       
-                                                       ob= ob->id.next;
-                                               }
-                                               CTX_DATA_END;
+                                               add_v3_v3(ob->dup_group->dupli_ofs, cent);
+
+                                               tot_change++;
+                                               ob->dup_group->id.flag |= LIB_DOIT;
+                                               do_inverse_offset= TRUE;
                                        }
-                                       tot_change++;
                                }
                        }
-                       else if (ELEM(base->object->type, OB_CURVE, OB_SURF)) {
-                               
-                               /* weak code here... (ton) */
-                               if(obedit==base->object) {
-                                       ListBase *editnurb= curve_get_editcurve(obedit);
+                       else if (((ID *)ob->data)->lib) {
+                               tot_lib_error++;
+                       }
 
-                                       nu1= editnurb->first;
-                                       cu= obedit->data;
-                               }
-                               else {
-                                       cu= base->object->data;
-                                       nu1= cu->nurb.first;
-                               }
-                               
-                               if (cu->id.lib) {
-                                       tot_lib_error++;
-                               } else {
-                                       if(centermode==2) {
-                                               VECCOPY(cent, give_cursor(scene, v3d));
-                                               Mat4Invert(base->object->imat, base->object->obmat);
-                                               Mat4MulVecfl(base->object->imat, cent);
-
-                                               /* don't allow Z change if curve is 2D */
-                                               if( !( cu->flag & CU_3D ) )
-                                                       cent[2] = 0.0;
-                                       } 
-                                       else {
-                                               INIT_MINMAX(min, max);
-                                               
-                                               nu= nu1;
-                                               while(nu) {
-                                                       minmaxNurb(nu, min, max);
-                                                       nu= nu->next;
-                                               }
-                                               
-                                               cent[0]= (min[0]+max[0])/2.0f;
-                                               cent[1]= (min[1]+max[1])/2.0f;
-                                               cent[2]= (min[2]+max[2])/2.0f;
-                                       }
-                                       
-                                       nu= nu1;
-                                       while(nu) {
-                                               if(nu->type == CU_BEZIER) {
-                                                       a= nu->pntsu;
-                                                       while (a--) {
-                                                               VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
-                                                               VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
-                                                               VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
-                                                       }
-                                               }
-                                               else {
-                                                       a= nu->pntsu*nu->pntsv;
-                                                       while (a--)
-                                                               VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
-                                               }
-                                               nu= nu->next;
-                                       }
-                       
-                                       if(centermode && obedit==0) {
-                                               Mat3CpyMat4(omat, base->object->obmat);
-                                               
-                                               Mat3MulVecfl(omat, cent);
-                                               base->object->loc[0]+= cent[0];
-                                               base->object->loc[1]+= cent[1];
-                                               base->object->loc[2]+= cent[2];
-                                               
-                                               where_is_object(scene, base->object);
-                                               ignore_parent_tx(bmain, scene, base->object);
-                                       }
-                                       
-                                       tot_change++;
-                                       if(obedit) {
-                                               if (centermode==0) {
-                                                       DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
-                                               }
-                                               break;
+                       if(obedit==NULL && ob->type==OB_MESH) {
+                               Mesh *me= ob->data;
+
+                               if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
+                               else if(around==V3D_CENTROID) { mesh_center_median(me, cent); }
+                               else { mesh_center_bounds(me, cent); }
+
+                               negate_v3_v3(cent_neg, cent);
+                               mesh_translate(me, cent_neg, 1);
+
+                               tot_change++;
+                               me->id.flag |= LIB_DOIT;
+                               do_inverse_offset= TRUE;
+                       }
+                       else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
+                               Curve *cu= ob->data;
+
+                               if(centermode == ORIGIN_TO_CURSOR) { /* done */ }
+                               else if(around==V3D_CENTROID) { curve_center_median(cu, cent); }
+                               else { curve_center_bounds(cu, cent);   }
+
+                               /* don't allow Z change if curve is 2D */
+                               if((ob->type == OB_CURVE) && !(cu->flag & CU_3D))
+                                       cent[2] = 0.0;
+
+                               negate_v3_v3(cent_neg, cent);
+                               curve_translate(cu, cent_neg, 1);
+
+                               tot_change++;
+                               cu->id.flag |= LIB_DOIT;
+                               do_inverse_offset= TRUE;
+
+                               if(obedit) {
+                                       if (centermode == GEOMETRY_TO_ORIGIN) {
+                                               DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
                                        }
+                                       break;
                                }
                        }
-                       else if(base->object->type==OB_FONT) {
+                       else if(ob->type==OB_FONT) {
                                /* get from bb */
-                               
-                               cu= base->object->data;
-                               
-                               if(cu->bb==0) {
+
+                               Curve *cu= ob->data;
+
+                               if(cu->bb==NULL && (centermode != ORIGIN_TO_CURSOR)) {
                                        /* do nothing*/
-                               } else if (cu->id.lib) {
-                                       tot_lib_error++;
-                               } else {
-                                       cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
-                                       cu->yof= -0.5f -0.5f*( cu->bb->vec[0][1] - cu->bb->vec[2][1]);  /* extra 0.5 is the height o above line */
-                                       
-                                       /* not really ok, do this better once! */
-                                       cu->xof /= cu->fsize;
-                                       cu->yof /= cu->fsize;
+                               }
+                               else {
+                                       if(centermode == ORIGIN_TO_CURSOR) {
+                                               /* done */
+                                       }
+                                       else {
+                                               cent[0]= 0.5f * ( cu->bb->vec[4][0] + cu->bb->vec[0][0]);
+                                               cent[1]= 0.5f * ( cu->bb->vec[0][1] + cu->bb->vec[2][1]) - 0.5f;        /* extra 0.5 is the height o above line */
+                                       }
+
+                                       cent[2]= 0.0f;
+
+                                       cu->xof= cu->xof - (cent[0] / cu->fsize);
+                                       cu->yof= cu->yof - (cent[1] / cu->fsize);
 
                                        tot_change++;
+                                       cu->id.flag |= LIB_DOIT;
+                                       do_inverse_offset= TRUE;
                                }
                        }
-                       else if(base->object->type==OB_ARMATURE) {
-                               bArmature *arm = base->object->data;
-                               
-                               if (arm->id.lib) {
-                                       tot_lib_error++;
-                               } else if(arm->id.us>1) {
+                       else if(ob->type==OB_ARMATURE) {
+                               bArmature *arm = ob->data;
+
+                               if(ID_REAL_USERS(arm) > 1) {
                                        /*BKE_report(op->reports, RPT_ERROR, "Can't apply to a multi user armature");
                                        return;*/
                                        tot_multiuser_arm_error++;
-                               } else {
-                                       /* Function to recenter armatures in editarmature.c 
+                               }
+                               else {
+                                       /* Function to recenter armatures in editarmature.c
                                         * Bone + object locations are handled there.
                                         */
-                                       docenter_armature(scene, v3d, base->object, centermode);
+                                       docenter_armature(scene, ob, cursor, centermode, around);
+
                                        tot_change++;
-                                       
-                                       where_is_object(scene, base->object);
-                                       ignore_parent_tx(bmain, scene, base->object);
-                                       
-                                       if(obedit) 
+                                       arm->id.flag |= LIB_DOIT;
+                                       /* do_inverse_offset= TRUE; */ /* docenter_armature() handles this */
+
+                                       where_is_object(scene, ob);
+                                       ignore_parent_tx(bmain, scene, ob);
+
+                                       if(obedit)
                                                break;
                                }
                        }
-                       base->object->recalc= OB_RECALC_OB|OB_RECALC_DATA;
+
+                       /* offset other selected objects */
+                       if(do_inverse_offset && (centermode != GEOMETRY_TO_ORIGIN)) {
+                               /* was the object data modified
+                                * note: the functions above must set 'cent' */
+                               copy_v3_v3(centn, cent);
+                               mul_mat3_m4_v3(ob->obmat, centn); /* ommit translation part */
+                               add_v3_v3(ob->loc, centn);
+
+                               where_is_object(scene, ob);
+                               ignore_parent_tx(bmain, scene, ob);
+                               
+                               /* other users? */
+                               CTX_DATA_BEGIN(C, Object*, ob_other, selected_editable_objects) {
+                                       if(             (ob_other->flag & OB_DONE)==0 &&
+                                                       (       (ob->data && (ob->data == ob_other->data)) ||
+                                                               (ob->dup_group==ob_other->dup_group && (ob->transflag|ob_other->transflag) & OB_DUPLIGROUP) )
+                                       ) {
+                                               ob_other->flag |= OB_DONE;
+                                               ob_other->recalc= OB_RECALC_OB|OB_RECALC_DATA;
+
+                                               copy_v3_v3(centn, cent);
+                                               mul_mat3_m4_v3(ob_other->obmat, centn); /* ommit translation part */
+                                               add_v3_v3(ob_other->loc, centn);
+
+                                               where_is_object(scene, ob_other);
+                                               ignore_parent_tx(bmain, scene, ob_other);
+                                       }
+                               }
+                               CTX_DATA_END;
+                       }
                }
        }
        CTX_DATA_END;
-       
+
+       for (tob= bmain->object.first; tob; tob= tob->id.next) {
+               if(tob->data && (((ID *)tob->data)->flag & LIB_DOIT)) {
+                       tob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
+               }
+       }
+
        if (tot_change) {
-               ED_anim_dag_flush_update(C);
+               DAG_ids_flush_update(bmain, 0);
+               WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
        }
-       
-       /* Warn if any errors occured */
+
+       /* Warn if any errors occurred */
        if (tot_lib_error+tot_multiuser_arm_error) {
-               BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);              
+               BKE_reportf(op->reports, RPT_WARNING, "%i Object(s) Not Centered, %i Changed:",tot_lib_error+tot_multiuser_arm_error, tot_change);
                if (tot_lib_error)
                        BKE_reportf(op->reports, RPT_WARNING, "|%i linked library objects",tot_lib_error);
                if (tot_multiuser_arm_error)
                        BKE_reportf(op->reports, RPT_WARNING, "|%i multiuser armature object(s)",tot_multiuser_arm_error);
        }
-       
+
        return OPERATOR_FINISHED;
 }
 
-void OBJECT_OT_center_set(wmOperatorType *ot)
+void OBJECT_OT_origin_set(wmOperatorType *ot)
 {
+       static EnumPropertyItem prop_set_center_types[] = {
+               {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"},
+               {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", "Move object origin to center of object geometry"},
+               {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", "Move object origin to position of the 3d cursor"},
+               {0, NULL, 0, NULL, NULL}
+       };
+       
+       static EnumPropertyItem prop_set_bounds_types[] = {
+               {V3D_CENTROID, "MEDIAN", 0, "Median Center", ""},
+               {V3D_CENTER, "BOUNDS", 0, "Bounds Center", ""},
+               {0, NULL, 0, NULL, NULL}
+       };
+       
        /* identifiers */
-       ot->name= "Set Center";
-       ot->description = "Set the object's center, by either moving the data, or set to center of data, or use 3d cursor";
-       ot->idname= "OBJECT_OT_center_set";
+       ot->name= "Set Origin";
+       ot->description = "Set the object's origin, by either moving the data, or set to center of data, or use 3d cursor";
+       ot->idname= "OBJECT_OT_origin_set";
        
        /* api callbacks */
        ot->invoke= WM_menu_invoke;
-       ot->exec= object_center_set_exec;
+       ot->exec= object_origin_set_exec;
        
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_scene_editable;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
-       RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
+       ot->prop= RNA_def_enum(ot->srna, "type", prop_set_center_types, 0, "Type", "");
+       RNA_def_enum(ot->srna, "center", prop_set_bounds_types, V3D_CENTROID, "Center", "");
 }