Merge branch 'master' into blender2.8
authorBastien Montagne <montagne29@wanadoo.fr>
Wed, 13 Jun 2018 13:36:24 +0000 (15:36 +0200)
committerBastien Montagne <montagne29@wanadoo.fr>
Wed, 13 Jun 2018 13:36:24 +0000 (15:36 +0200)
Conflicts:
source/blender/blenkernel/BKE_pointcache.h
source/blender/blenkernel/intern/object.c
source/blender/blenkernel/intern/pointcache.c
source/blender/editors/include/ED_particle.h
source/blender/editors/physics/particle_edit.c
source/blender/editors/physics/particle_edit_undo.c
source/blender/editors/physics/particle_object.c
source/blender/editors/physics/physics_intern.h
source/blender/editors/physics/physics_pointcache.c
source/blender/editors/space_time/space_time.c
source/blender/editors/space_view3d/drawobject.c
source/blender/editors/space_view3d/view3d_edit.c
source/blender/editors/transform/transform_conversions.c
source/blender/editors/transform/transform_generics.c
source/blender/editors/transform/transform_manipulator.c
source/blender/makesrna/intern/rna_object.c
source/blender/makesrna/intern/rna_object_force.c
source/blender/makesrna/intern/rna_sculpt_paint.c
source/blenderplayer/bad_level_call_stubs/stubs.c

1  2 
source/blender/blenkernel/BKE_pointcache.h
source/blender/editors/physics/particle_edit.c
source/blender/editors/physics/particle_edit_undo.c
source/blender/editors/transform/transform_conversions.c
source/blender/editors/transform/transform_generics.c
source/blender/makesrna/intern/rna_sculpt_paint.c

@@@ -90,7 -90,6 +90,7 @@@ struct ParticleKey
  struct ParticleSystem;
  struct PointCache;
  struct Scene;
 +struct ViewLayer;
  struct SmokeModifierData;
  struct SoftBody;
  struct RigidBodyWorld;
@@@ -187,8 -186,6 +187,8 @@@ typedef struct PTCacheID 
  typedef struct PTCacheBaker {
        struct Main *bmain;
        struct Scene *scene;
 +      struct ViewLayer *view_layer;
 +      struct Depsgraph *depsgraph;
        int bake;
        int render;
        int anim_init;
@@@ -277,8 -274,8 +277,9 @@@ void BKE_ptcache_id_from_smoke(PTCacheI
  void BKE_ptcache_id_from_dynamicpaint(PTCacheID *pid, struct Object *ob, struct DynamicPaintSurface *surface);
  void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, struct Object *ob, struct RigidBodyWorld *rbw);
  
- void BKE_ptcache_ids_from_object(struct ListBase *lb, struct Object *ob, struct Scene *scene, int duplis);
 +PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct PointCache *cache);
 -        struct Main *bmain, struct ListBase *lb, struct Object *ob, struct Scene *scene, int duplis);
+ void BKE_ptcache_ids_from_object(
++        struct ListBase *lb, struct Object *ob, struct Scene *scene, int duplis);
  
  /***************** Global funcs ****************************/
  void BKE_ptcache_remove(void);
@@@ -321,7 -318,7 +322,7 @@@ struct PointCache *BKE_ptcache_copy_lis
  /********************** Baking *********************/
  
  /* Bakes cache with cache_step sized jumps in time, not accurate but very fast. */
 -void BKE_ptcache_quick_cache_all(struct Main *bmain, struct Scene *scene);
 +void BKE_ptcache_quick_cache_all(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer);
  
  /* Bake cache or simulate to current frame with settings defined in the baker. */
  void BKE_ptcache_bake(struct PTCacheBaker *baker);
  #include "BLI_math.h"
  #include "BLI_lasso_2d.h"
  #include "BLI_listbase.h"
 +#include "BLI_string.h"
  #include "BLI_kdtree.h"
  #include "BLI_rand.h"
  #include "BLI_utildefines.h"
  
  #include "BKE_context.h"
 -#include "BKE_depsgraph.h"
  #include "BKE_DerivedMesh.h"
  #include "BKE_global.h"
  #include "BKE_object.h"
 +#include "BKE_library.h"
+ #include "BKE_main.h"
  #include "BKE_mesh.h"
 +#include "BKE_mesh_runtime.h"
  #include "BKE_modifier.h"
  #include "BKE_particle.h"
  #include "BKE_report.h"
 +#include "BKE_scene.h"
  #include "BKE_bvhutils.h"
  #include "BKE_pointcache.h"
  
 +#include "DEG_depsgraph.h"
 +
  #include "BIF_gl.h"
 -#include "BIF_glutil.h"
  
  #include "ED_object.h"
  #include "ED_physics.h"
  #include "ED_mesh.h"
  #include "ED_particle.h"
 +#include "ED_screen.h"
  #include "ED_view3d.h"
  
 +#include "GPU_immediate.h"
 +#include "GPU_immediate_util.h"
 +
  #include "UI_resources.h"
  
  #include "WM_api.h"
  #include "WM_types.h"
 +#include "WM_message.h"
 +#include "WM_toolsystem.h"
  
  #include "RNA_access.h"
  #include "RNA_define.h"
  
 +#include "DEG_depsgraph_query.h"
 +
  #include "physics_intern.h"
  
  #include "particle_edit_utildefines.h"
  
  int PE_poll(bContext *C)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
  
 -      if (!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT))
 +      if (!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT)) {
                return 0;
 -
 -      return (PE_get_current(bmain, scene, ob) != NULL);
 +      }
 +      return (PE_get_current(scene, ob) != NULL);
  }
  
  int PE_hair_poll(bContext *C)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
        PTCacheEdit *edit;
  
 -      if (!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT))
 +      if (!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT)) {
                return 0;
 -
 -      edit= PE_get_current(bmain, scene, ob);
 +      }
-       edit= PE_get_current(scene, ob);
++      edit = PE_get_current(scene, ob);
  
        return (edit && edit->psys);
  }
@@@ -195,30 -186,12 +196,30 @@@ static float pe_brush_size_get(const Sc
        return brush->size * U.pixelsize;
  }
  
 +PTCacheEdit *PE_get_current_from_psys(ParticleSystem *psys)
 +{
 +      if (psys->part && psys->part->type == PART_HAIR) {
 +              if ((psys->flag & PSYS_HAIR_DYNAMICS) != 0 &&
 +                  (psys->pointcache->flag & PTCACHE_BAKED) != 0)
 +              {
 +                      return psys->pointcache->edit;
 +              }
 +              else {
 +                      return psys->edit;
 +              }
 +      }
 +      else if (psys->pointcache->flag & PTCACHE_BAKED) {
 +              return psys->pointcache->edit;
 +      }
 +      return NULL;
 +}
  
  /* always gets at least the first particlesystem even if PSYS_CURRENT flag is not set
   *
   * note: this function runs on poll, therefor it can runs many times a second
   * keep it fast! */
 -static PTCacheEdit *pe_get_current(Main *bmain, Scene *scene, Object *ob, int create)
 +static PTCacheEdit *pe_get_current(
 +        Depsgraph *depsgraph, Scene *scene, Object *ob, int create)
  {
        ParticleEditSettings *pset= PE_settings(scene);
        PTCacheEdit *edit = NULL;
        pset->scene = scene;
        pset->object = ob;
  
 -      BKE_ptcache_ids_from_object(bmain, &pidlist, ob, NULL, 0);
 +      BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
  
        /* in the case of only one editable thing, set pset->edittype accordingly */
        if (BLI_listbase_is_single(&pidlist)) {
                                if (psys->part && psys->part->type == PART_HAIR) {
                                        if (psys->flag & PSYS_HAIR_DYNAMICS && psys->pointcache->flag & PTCACHE_BAKED) {
                                                if (create && !psys->pointcache->edit)
 -                                                      PE_create_particle_edit(bmain, scene, ob, pid->cache, NULL);
 +                                                      PE_create_particle_edit(depsgraph, scene, ob, pid->cache, NULL);
                                                edit = pid->cache->edit;
                                        }
                                        else {
 -                                              if (create && !psys->edit && psys->flag & PSYS_HAIR_DONE)
 -                                                      PE_create_particle_edit(bmain, scene, ob, NULL, psys);
 +                                              if (create && !psys->edit) {
 +                                                      if (psys->flag & PSYS_HAIR_DONE) {
 +                                                              PE_create_particle_edit(depsgraph, scene, ob, NULL, psys);
 +                                                      }
 +                                              }
                                                edit = psys->edit;
                                        }
                                }
                                else {
                                        if (create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit)
 -                                              PE_create_particle_edit(bmain, scene, ob, pid->cache, psys);
 +                                              PE_create_particle_edit(depsgraph, scene, ob, pid->cache, psys);
                                        edit = pid->cache->edit;
                                }
  
                        if (create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit) {
                                pset->flag |= PE_FADE_TIME;
                                // NICE TO HAVE but doesn't work: pset->brushtype = PE_BRUSH_COMB;
 -                              PE_create_particle_edit(bmain, scene, ob, pid->cache, NULL);
 +                              PE_create_particle_edit(depsgraph, scene, ob, pid->cache, NULL);
                        }
                        edit = pid->cache->edit;
                        break;
                        if (create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit) {
                                pset->flag |= PE_FADE_TIME;
                                // NICE TO HAVE but doesn't work: pset->brushtype = PE_BRUSH_COMB;
 -                              PE_create_particle_edit(bmain, scene, ob, pid->cache, NULL);
 +                              PE_create_particle_edit(depsgraph, scene, ob, pid->cache, NULL);
                        }
                        edit = pid->cache->edit;
                        break;
        return edit;
  }
  
 -PTCacheEdit *PE_get_current(Main *bmain, Scene *scene, Object *ob)
 +PTCacheEdit *PE_get_current(Scene *scene, Object *ob)
  {
 -      return pe_get_current(bmain, scene, ob, 0);
 +      return pe_get_current(NULL, scene, ob, 0);
  }
  
 -PTCacheEdit *PE_create_current(Main *bmain, Scene *scene, Object *ob)
 +PTCacheEdit *PE_create_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
  {
 -      return pe_get_current(bmain, scene, ob, 1);
 +      return pe_get_current(depsgraph, scene, ob, 1);
  }
  
 -void PE_current_changed(Main *bmain, Scene *scene, Object *ob)
 +void PE_current_changed(Depsgraph *depsgraph, Scene *scene, Object *ob)
  {
 -      if (ob->mode == OB_MODE_PARTICLE_EDIT)
 -              PE_create_current(bmain, scene, ob);
 +      if (ob->mode == OB_MODE_PARTICLE_EDIT) {
 +              PE_create_current(depsgraph, scene, ob);
 +      }
  }
  
  void PE_hide_keys_time(Scene *scene, PTCacheEdit *edit, float cfra)
@@@ -362,15 -331,14 +363,16 @@@ static int pe_x_mirror(Object *ob
  
  typedef struct PEData {
        ViewContext vc;
 -      bglMats mats;
  
 +      const bContext *context;
+       Main *bmain;
        Scene *scene;
 +      ViewLayer *view_layer;
        Object *ob;
 -      DerivedMesh *dm;
 +      Mesh *mesh;
        PTCacheEdit *edit;
        BVHTreeFromMesh shape_bvh;
 +      Depsgraph *depsgraph;
  
        const int *mval;
        rcti *rect;
@@@ -400,11 -368,10 +402,12 @@@ static void PE_set_data(bContext *C, PE
  {
        memset(data, 0, sizeof(*data));
  
 -      data->scene= CTX_data_scene(C);
 -      data->ob= CTX_data_active_object(C);
 -      data->edit= PE_get_current(data->bmain, data->scene, data->ob);
+       data->bmain = CTX_data_main(C);
 +      data->scene = CTX_data_scene(C);
 +      data->view_layer = CTX_data_view_layer(C);
 +      data->ob = CTX_data_active_object(C);
 +      data->depsgraph = CTX_data_depsgraph(C);
 +      data->edit = PE_get_current(data->scene, data->ob);
  }
  
  static void PE_set_view3d_data(bContext *C, PEData *data)
        PE_set_data(C, data);
  
        ED_view3d_viewcontext_init(C, &data->vc);
 -      /* note, the object argument means the modelview matrix does not account for the objects matrix, use viewmat rather than (obmat * viewmat) */
 -      view3d_get_transformation(data->vc.ar, data->vc.rv3d, NULL, &data->mats);
  
        if (V3D_IS_ZBUF(data->vc.v3d)) {
                if (data->vc.v3d->flag & V3D_INVALID_BACKBUF) {
  
  static bool PE_create_shape_tree(PEData *data, Object *shapeob)
  {
 -      DerivedMesh *dm = shapeob->derivedFinal;
 +      Mesh *mesh = BKE_object_get_evaluated_mesh(data->depsgraph, shapeob);
  
        memset(&data->shape_bvh, 0, sizeof(data->shape_bvh));
  
 -      if (!dm) {
 +      if (!mesh) {
                return false;
        }
  
 -      return (bvhtree_from_mesh_get(&data->shape_bvh, dm, BVHTREE_FROM_LOOPTRI, 4) != NULL);
 +      return (BKE_bvhtree_from_mesh_get(&data->shape_bvh, mesh, BVHTREE_FROM_LOOPTRI, 4) != NULL);
  }
  
  static void PE_free_shape_tree(PEData *data)
@@@ -451,6 -420,7 +454,6 @@@ static bool key_test_depth(PEData *data
  {
        View3D *v3d= data->vc.v3d;
        ViewDepths *vd = data->vc.rv3d->depths;
 -      double ux, uy, uz;
        float depth;
  
        /* nothing to do */
        }
  #endif
  
 -      gluProject(co[0], co[1], co[2], data->mats.modelview, data->mats.projection,
 -                 (GLint *)data->mats.viewport, &ux, &uy, &uz);
 -
        /* check if screen_co is within bounds because brush_cut uses out of screen coords */
        if (screen_co[0] >= 0 && screen_co[0] < vd->w && screen_co[1] >= 0 && screen_co[1] < vd->h) {
                BLI_assert(vd && vd->depths);
        else
                return 0;
  
 -      if ((float)uz - 0.00001f > depth)
 +      float win[3];
 +      ED_view3d_project(data->vc.ar, co, win);
 +
 +      if (win[2] - 0.00001f > depth)
                return 0;
        else
                return 1;
@@@ -670,7 -640,7 +673,7 @@@ static void foreach_mouse_hit_key(PEDat
                                if (selected==0 || key->flag & PEK_SELECT) {
                                        if (key_inside_circle(data, data->rad, KEY_WCO, &data->dist)) {
                                                if (edit->psys && !(edit->psys->flag & PSYS_GLOBAL_HAIR)) {
 -                                                      psys_mat_hair_to_global(data->ob, psmd->dm_final, psys->part->from, psys->particles + p, mat);
 +                                                      psys_mat_hair_to_global(data->ob, psmd->mesh_final, psys->part->from, psys->particles + p, mat);
                                                        invert_m4_m4(imat, mat);
                                                }
  
                                if (selected==0 || key->flag & PEK_SELECT) {
                                        if (key_inside_circle(data, data->rad, KEY_WCO, &data->dist)) {
                                                if (edit->psys && !(edit->psys->flag & PSYS_GLOBAL_HAIR)) {
 -                                                      psys_mat_hair_to_global(data->ob, psmd->dm_final, psys->part->from, psys->particles + p, mat);
 +                                                      psys_mat_hair_to_global(data->ob, psmd->mesh_final, psys->part->from, psys->particles + p, mat);
                                                        invert_m4_m4(imat, mat);
                                                }
  
@@@ -772,7 -742,7 +775,7 @@@ static void PE_update_mirror_cache(Obje
        psmd= psys_get_modifier(ob, psys);
        totpart= psys->totpart;
  
 -      if (!psmd->dm_final)
 +      if (!psmd->mesh_final)
                return;
  
        tree= BLI_kdtree_new(totpart);
        /* insert particles into kd tree */
        LOOP_PARTICLES {
                key = pa->hair;
 -              psys_mat_hair_to_orco(ob, psmd->dm_final, psys->part->from, pa, mat);
 +              psys_mat_hair_to_orco(ob, psmd->mesh_final, psys->part->from, pa, mat);
                copy_v3_v3(co, key->co);
                mul_m4_v3(mat, co);
                BLI_kdtree_insert(tree, p, co);
  
        LOOP_PARTICLES {
                key = pa->hair;
 -              psys_mat_hair_to_orco(ob, psmd->dm_final, psys->part->from, pa, mat);
 +              psys_mat_hair_to_orco(ob, psmd->mesh_final, psys->part->from, pa, mat);
                copy_v3_v3(co, key->co);
                mul_m4_v3(mat, co);
                co[0] = -co[0];
        BLI_kdtree_free(tree);
  }
  
 -static void PE_mirror_particle(Object *ob, DerivedMesh *dm, ParticleSystem *psys, ParticleData *pa, ParticleData *mpa)
 +static void PE_mirror_particle(Object *ob, Mesh *mesh, ParticleSystem *psys, ParticleData *pa, ParticleData *mpa)
  {
        HairKey *hkey, *mhkey;
        PTCacheEditPoint *point, *mpoint;
        }
  
        /* mirror positions and tags */
 -      psys_mat_hair_to_orco(ob, dm, psys->part->from, pa, mat);
 -      psys_mat_hair_to_orco(ob, dm, psys->part->from, mpa, mmat);
 +      psys_mat_hair_to_orco(ob, mesh, psys->part->from, pa, mat);
 +      psys_mat_hair_to_orco(ob, mesh, psys->part->from, mpa, mmat);
        invert_m4_m4(immat, mmat);
  
        hkey=pa->hair;
@@@ -909,7 -879,7 +912,7 @@@ static void PE_apply_mirror(Object *ob
        edit= psys->edit;
        psmd= psys_get_modifier(ob, psys);
  
 -      if (!psmd->dm_final)
 +      if (!psmd->mesh_final)
                return;
  
        if (!edit->mirror_cache)
         * to avoid doing mirror twice */
        LOOP_POINTS {
                if (point->flag & PEP_EDIT_RECALC) {
 -                      PE_mirror_particle(ob, psmd->dm_final, psys, psys->particles + p, NULL);
 +                      PE_mirror_particle(ob, psmd->mesh_final, psys, psys->particles + p, NULL);
  
                        if (edit->mirror_cache[p] != -1)
                                edit->points[edit->mirror_cache[p]].flag &= ~PEP_EDIT_RECALC;
@@@ -957,11 -927,11 +960,11 @@@ static void pe_deflect_emitter(Scene *s
        psys = edit->psys;
        psmd = psys_get_modifier(ob, psys);
  
 -      if (!psmd->dm_final)
 +      if (!psmd->mesh_final)
                return;
  
        LOOP_EDITED_POINTS {
 -              psys_mat_hair_to_object(ob, psmd->dm_final, psys->part->from, psys->particles + p, hairmat);
 +              psys_mat_hair_to_object(ob, psmd->mesh_final, psys->part->from, psys->particles + p, hairmat);
  
                LOOP_KEYS {
                        mul_m4_v3(hairmat, key->co);
@@@ -1104,14 -1074,14 +1107,14 @@@ void recalc_lengths(PTCacheEdit *edit
  }
  
  /* calculate a tree for finding nearest emitter's vertice */
 -void recalc_emitter_field(Object *ob, ParticleSystem *psys)
 +void recalc_emitter_field(Depsgraph *UNUSED(depsgraph), Object *ob, ParticleSystem *psys)
  {
 -      DerivedMesh *dm=psys_get_modifier(ob, psys)->dm_final;
 -      PTCacheEdit *edit= psys->edit;
 +      Mesh *mesh = psys_get_modifier(ob, psys)->mesh_final;
 +      PTCacheEdit *edit = psys->edit;
        float *vec, *nor;
        int i, totface /*, totvert*/;
  
 -      if (!dm)
 +      if (!mesh)
                return;
  
        if (edit->emitter_cosnos)
  
        BLI_kdtree_free(edit->emitter_field);
  
 -      totface=dm->getNumTessFaces(dm);
 +      totface = mesh->totface;
        /*totvert=dm->getNumVerts(dm);*/ /*UNSUED*/
  
        edit->emitter_cosnos=MEM_callocN(totface*6*sizeof(float), "emitter cosnos");
        nor=vec+3;
  
        for (i=0; i<totface; i++, vec+=6, nor+=6) {
 -              MFace *mface=dm->getTessFaceData(dm, i, CD_MFACE);
 +              MFace *mface = &mesh->mface[i];
                MVert *mvert;
  
 -              mvert=dm->getVertData(dm, mface->v1, CD_MVERT);
 +              mvert = &mesh->mvert[mface->v1];
                copy_v3_v3(vec, mvert->co);
                VECCOPY(nor, mvert->no);
  
 -              mvert=dm->getVertData(dm, mface->v2, CD_MVERT);
 +              mvert = &mesh->mvert[mface->v2];
                add_v3_v3v3(vec, vec, mvert->co);
                VECADD(nor, nor, mvert->no);
  
 -              mvert=dm->getVertData(dm, mface->v3, CD_MVERT);
 +              mvert = &mesh->mvert[mface->v3];
                add_v3_v3v3(vec, vec, mvert->co);
                VECADD(nor, nor, mvert->no);
  
                if (mface->v4) {
 -                      mvert=dm->getVertData(dm, mface->v4, CD_MVERT);
 +                      mvert = &mesh->mvert[mface->v4];
                        add_v3_v3v3(vec, vec, mvert->co);
                        VECADD(nor, nor, mvert->no);
  
        BLI_kdtree_balance(edit->emitter_field);
  }
  
 -static void PE_update_selection(Main *bmain, Scene *scene, Object *ob, int useflag)
 +static void PE_update_selection(Depsgraph *depsgraph, Scene *scene, Object *ob, int useflag)
  {
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
        HairKey *hkey;
        POINT_P; KEY_K;
  
                }
        }
  
 -      psys_cache_edit_paths(scene, ob, edit, CFRA, G.is_rendering);
 +      psys_cache_edit_paths(depsgraph, scene, ob, edit, CFRA, G.is_rendering);
  
  
        /* disable update flag */
        LOOP_POINTS
                point->flag &= ~PEP_EDIT_RECALC;
 +
 +      DEG_id_tag_update(&ob->id, DEG_TAG_SELECT_UPDATE);
  }
  
 -void update_world_cos(Object *ob, PTCacheEdit *edit)
 +void update_world_cos(Depsgraph *UNUSED(depsgraph), Object *ob, PTCacheEdit *edit)
  {
        ParticleSystem *psys = edit->psys;
 -      ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
 +      ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
        POINT_P; KEY_K;
        float hairmat[4][4];
  
 -      if (psys==0 || psys->edit==0 || psmd->dm_final==NULL)
 +      if (psys == 0 || psys->edit == 0 || psmd->mesh_final == NULL)
                return;
  
        LOOP_POINTS {
                if (!(psys->flag & PSYS_GLOBAL_HAIR))
 -                      psys_mat_hair_to_global(ob, psmd->dm_final, psys->part->from, psys->particles+p, hairmat);
 +                      psys_mat_hair_to_global(ob, psmd->mesh_final, psys->part->from, psys->particles+p, hairmat);
  
                LOOP_KEYS {
                        copy_v3_v3(key->world_co, key->co);
@@@ -1272,12 -1240,12 +1275,12 @@@ static void update_velocities(PTCacheEd
        }
  }
  
 -void PE_update_object(Main *bmain, Scene *scene, Object *ob, int useflag)
 +void PE_update_object(Depsgraph *depsgraph, Scene *scene, Object *ob, int useflag)
  {
        /* use this to do partial particle updates, not usable when adding or
         * removing, then a full redo is necessary and calling this may crash */
        ParticleEditSettings *pset= PE_settings(scene);
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
        POINT_P;
  
        if (!edit)
        if (pe_x_mirror(ob))
                PE_apply_mirror(ob, edit->psys);
        if (edit->psys)
 -              update_world_cos(ob, edit);
 +              update_world_cos(depsgraph, ob, edit);
        if (pset->flag & PE_AUTO_VELOCITY)
                update_velocities(edit);
        PE_hide_keys_time(scene, edit, CFRA);
  
        /* regenerate path caches */
 -      psys_cache_edit_paths(scene, ob, edit, CFRA, G.is_rendering);
 +      psys_cache_edit_paths(depsgraph, scene, ob, edit, CFRA, G.is_rendering);
  
        /* disable update flag */
        LOOP_POINTS {
@@@ -1411,10 -1379,10 +1414,10 @@@ static void select_action_apply(PTCache
  
  static int pe_select_all_exec(bContext *C, wmOperator *op)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
        Object *ob= CTX_data_active_object(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        POINT_P; KEY_K;
        int action = RNA_enum_get(op->ptr, "action");
  
                }
        }
  
 -      PE_update_selection(bmain, scene, ob, 1);
 +      PE_update_selection(depsgraph, scene, ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
  
        return OPERATOR_FINISHED;
@@@ -1464,10 -1432,11 +1467,10 @@@ void PARTICLE_OT_select_all(wmOperatorT
  
  int PE_mouse_particles(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle)
  {
 -      Main *bmain = CTX_data_main(C);
        PEData data;
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        POINT_P; KEY_K;
  
        if (!PE_start_edit(edit))
        else
                for_mouse_hit_keys(&data, toggle_key_select, 1);
  
 -      PE_update_selection(bmain, scene, ob, 1);
 +      PE_update_selection(data.depsgraph, scene, ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -1535,7 -1504,7 +1538,7 @@@ static int select_roots_exec(bContext *
        data.select_action = action;
        foreach_point(&data, select_root);
  
 -      PE_update_selection(data.bmain, data.scene, data.ob, 1);
 +      PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -1600,7 -1569,7 +1603,7 @@@ static int select_tips_exec(bContext *C
        data.select_action = action;
        foreach_point(&data, select_tip);
  
 -      PE_update_selection(data.bmain, data.scene, data.ob, 1);
 +      PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -1638,8 -1607,6 +1641,6 @@@ static int select_random_exec(bContext 
  {
        PEData data;
        int type;
-       Scene *scene;
-       Object *ob;
  
        /* used by LOOP_VISIBLE_POINTS, LOOP_VISIBLE_KEYS and LOOP_KEYS */
        PTCacheEdit *edit;
  
        PE_set_data(C, &data);
        data.select_action = SEL_SELECT;
-       scene = CTX_data_scene(C);
-       ob = CTX_data_active_object(C);
-       edit = PE_get_current(scene, ob);
 -      edit = PE_get_current(data.bmain, data.scene, data.ob);
++      edit = PE_get_current(data.scene, data.ob);
  
        rng = BLI_rng_new_srandom(seed);
  
  
        BLI_rng_free(rng);
  
 -      PE_update_selection(data.bmain, data.scene, data.ob, 1);
 +      PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -1728,7 -1693,7 +1727,7 @@@ static int select_linked_exec(bContext 
        data.select= !RNA_boolean_get(op->ptr, "deselect");
  
        for_mouse_hit_keys(&data, select_keys, 1);  /* nearest only */
 -      PE_update_selection(data.bmain, data.scene, data.ob, 1);
 +      PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -1775,9 -1740,10 +1774,9 @@@ void PE_deselect_all_visible(PTCacheEdi
  
  int PE_border_select(bContext *C, rcti *rect, bool select, bool extend)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        PEData data;
  
        if (!PE_start_edit(edit))
  
        for_mouse_hit_keys(&data, select_key, 0);
  
 -      PE_update_selection(bmain, scene, ob, 1);
 +      PE_update_selection(data.depsgraph, scene, ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
  
        return OPERATOR_FINISHED;
  
  int PE_circle_select(bContext *C, int selecting, const int mval[2], float rad)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        PEData data;
  
        if (!PE_start_edit(edit))
  
        for_mouse_hit_keys(&data, select_key, 0);
  
 -      PE_update_selection(bmain, scene, ob, 1);
 +      PE_update_selection(data.depsgraph, scene, ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
  
        return OPERATOR_FINISHED;
  
  int PE_lasso_select(bContext *C, const int mcords[][2], const short moves, bool extend, bool select)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
        ARegion *ar= CTX_wm_region(C);
        ParticleEditSettings *pset= PE_settings(scene);
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
        ParticleSystem *psys = edit->psys;
        ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
        POINT_P; KEY_K;
  
        LOOP_VISIBLE_POINTS {
                if (edit->psys && !(psys->flag & PSYS_GLOBAL_HAIR))
 -                      psys_mat_hair_to_global(ob, psmd->dm_final, psys->part->from, psys->particles + p, mat);
 +                      psys_mat_hair_to_global(ob, psmd->mesh_final, psys->part->from, psys->particles + p, mat);
  
                if (pset->selectmode==SCE_SELECT_POINT) {
                        LOOP_KEYS {
                }
        }
  
 -      PE_update_selection(bmain, scene, ob, 1);
 +      PE_update_selection(data.depsgraph, scene, ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
  
        return OPERATOR_FINISHED;
  
  static int hide_exec(bContext *C, wmOperator *op)
  {
 -      Main *bmain = CTX_data_main(C);
        Object *ob= CTX_data_active_object(C);
        Scene *scene= CTX_data_scene(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
 +
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        POINT_P; KEY_K;
  
 +
        if (RNA_enum_get(op->ptr, "unselected")) {
                LOOP_UNSELECTED_POINTS {
                        point->flag |= PEP_HIDE;
                }
        }
  
 -      PE_update_selection(bmain, scene, ob, 1);
 +      PE_update_selection(depsgraph, scene, ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
  
        return OPERATOR_FINISHED;
@@@ -1970,10 -1936,10 +1969,10 @@@ void PARTICLE_OT_hide(wmOperatorType *o
  
  static int reveal_exec(bContext *C, wmOperator *op)
  {
 -      Main *bmain = CTX_data_main(C);
        Object *ob= CTX_data_active_object(C);
        Scene *scene= CTX_data_scene(C);
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        const bool select = RNA_boolean_get(op->ptr, "select");
        POINT_P; KEY_K;
  
                }
        }
  
 -      PE_update_selection(bmain, scene, ob, 1);
 +      PE_update_selection(depsgraph, scene, ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
  
        return OPERATOR_FINISHED;
@@@ -2050,7 -2016,7 +2049,7 @@@ static int select_less_exec(bContext *C
        PE_set_data(C, &data);
        foreach_point(&data, select_less_keys);
  
 -      PE_update_selection(data.bmain, data.scene, data.ob, 1);
 +      PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -2112,7 -2078,7 +2111,7 @@@ static int select_more_exec(bContext *C
        PE_set_data(C, &data);
        foreach_point(&data, select_more_keys);
  
 -      PE_update_selection(data.bmain, data.scene, data.ob, 1);
 +      PE_update_selection(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -2148,10 -2114,9 +2147,10 @@@ static void rekey_particle(PEData *data
        float dval, sta, end;
        int k;
  
 -      sim.scene= data->scene;
 -      sim.ob= data->ob;
 -      sim.psys= edit->psys;
 +      sim.depsgraph = data->depsgraph;
 +      sim.scene = data->scene;
 +      sim.ob = data->ob;
 +      sim.psys = edit->psys;
  
        pa->flag |= PARS_REKEY;
  
@@@ -2210,7 -2175,7 +2209,7 @@@ static int rekey_exec(bContext *C, wmOp
        foreach_selected_point(&data, rekey_particle);
  
        recalc_lengths(data.edit);
 -      PE_update_object(data.bmain, data.scene, data.ob, 1);
 +      PE_update_object(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -2235,11 -2200,11 +2234,11 @@@ void PARTICLE_OT_rekey(wmOperatorType *
        RNA_def_int(ot->srna, "keys_number", 2, 2, INT_MAX, "Number of Keys", "", 2, 100);
  }
  
 -static void rekey_particle_to_time(Main *bmain, Scene *scene, Object *ob, int pa_index, float path_time)
 +static void rekey_particle_to_time(const bContext *C, Scene *scene, Object *ob, int pa_index, float path_time)
  {
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        ParticleSystem *psys;
 -      ParticleSimulationData sim= {0};
 +      ParticleSimulationData sim = {0};
        ParticleData *pa;
        ParticleKey state;
        HairKey *new_keys, *key;
  
        psys = edit->psys;
  
 -      sim.scene= scene;
 -      sim.ob= ob;
 -      sim.psys= psys;
 +      sim.depsgraph = CTX_data_depsgraph(C);
 +      sim.scene = scene;
 +      sim.ob = ob;
 +      sim.psys = psys;
  
        pa= psys->particles + pa_index;
  
@@@ -2298,7 -2262,7 +2297,7 @@@ static int remove_tagged_particles(Obje
                psmd= psys_get_modifier(ob, psys);
  
                LOOP_TAGGED_POINTS {
 -                      PE_mirror_particle(ob, psmd->dm_final, psys, psys->particles + p, NULL);
 +                      PE_mirror_particle(ob, psmd->mesh_final, psys, psys->particles + p, NULL);
                }
        }
  
@@@ -2378,7 -2342,7 +2377,7 @@@ static void remove_tagged_keys(Object *
  
                LOOP_POINTS {
                        LOOP_TAGGED_KEYS {
 -                              PE_mirror_particle(ob, psmd->dm_final, psys, psys->particles + p, NULL);
 +                              PE_mirror_particle(ob, psmd->mesh_final, psys, psys->particles + p, NULL);
                                break;
                        }
                }
@@@ -2468,10 -2432,9 +2467,10 @@@ static void subdivide_particle(PEData *
        short totnewkey=0;
        float endtime;
  
 -      sim.scene= data->scene;
 -      sim.ob= data->ob;
 -      sim.psys= edit->psys;
 +      sim.depsgraph = data->depsgraph;
 +      sim.scene = data->scene;
 +      sim.ob = data->ob;
 +      sim.psys = edit->psys;
  
        for (k=0, ekey=point->keys; k<pa->totkey-1; k++, ekey++) {
                if (ekey->flag&PEK_SELECT && (ekey+1)->flag&PEK_SELECT)
@@@ -2543,7 -2506,7 +2542,7 @@@ static int subdivide_exec(bContext *C, 
        foreach_point(&data, subdivide_particle);
  
        recalc_lengths(data.edit);
 -      PE_update_object(data.bmain, data.scene, data.ob, 1);
 +      PE_update_object(data.depsgraph, data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -2568,9 -2531,10 +2567,9 @@@ void PARTICLE_OT_subdivide(wmOperatorTy
  
  static int remove_doubles_exec(bContext *C, wmOperator *op)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        ParticleSystem *psys = edit->psys;
        ParticleSystemModifierData *psmd;
        KDTree *tree;
  
                /* insert particles into kd tree */
                LOOP_SELECTED_POINTS {
 -                      psys_mat_hair_to_object(ob, psmd->dm_final, psys->part->from, psys->particles+p, mat);
 +                      psys_mat_hair_to_object(ob, psmd->mesh_final, psys->part->from, psys->particles+p, mat);
                        copy_v3_v3(co, point->keys->co);
                        mul_m4_v3(mat, co);
                        BLI_kdtree_insert(tree, p, co);
  
                /* tag particles to be removed */
                LOOP_SELECTED_POINTS {
 -                      psys_mat_hair_to_object(ob, psmd->dm_final, psys->part->from, psys->particles+p, mat);
 +                      psys_mat_hair_to_object(ob, psmd->mesh_final, psys->part->from, psys->particles+p, mat);
                        copy_v3_v3(co, point->keys->co);
                        mul_m4_v3(mat, co);
  
  
        BKE_reportf(op->reports, RPT_INFO, "Removed %d double particles", totremoved);
  
 -      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
  
        return OPERATOR_FINISHED;
@@@ -2660,10 -2624,11 +2659,10 @@@ void PARTICLE_OT_remove_doubles(wmOpera
  
  static int weight_set_exec(bContext *C, wmOperator *op)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        ParticleEditSettings *pset= PE_settings(scene);
        Object *ob= CTX_data_active_object(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        ParticleSystem *psys = edit->psys;
        POINT_P;
        KEY_K;
                }
        }
  
 -      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
  
        return OPERATOR_FINISHED;
@@@ -2716,27 -2681,24 +2715,27 @@@ static void brush_drawcursor(bContext *
        ParticleEditSettings *pset= PE_settings(scene);
        ParticleBrushData *brush;
  
 -      if (pset->brushtype < 0)
 +      if (pset->brushtype < 0) {
                return;
 +      }
  
 -      brush= &pset->brush[pset->brushtype];
 +      brush = &pset->brush[pset->brushtype];
  
        if (brush) {
 -              glPushMatrix();
 +              unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +              immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
  
 -              glTranslatef((float)x, (float)y, 0.0f);
 +              immUniformColor4ub(255, 255, 255, 128);
  
 -              glColor4ub(255, 255, 255, 128);
                glEnable(GL_LINE_SMOOTH);
                glEnable(GL_BLEND);
 -              glutil_draw_lined_arc(0.0, M_PI*2.0, pe_brush_size_get(scene, brush), 40);
 +
 +              imm_draw_circle_wire_2d(pos, (float)x, (float)y, pe_brush_size_get(scene, brush), 40);
 +
                glDisable(GL_BLEND);
                glDisable(GL_LINE_SMOOTH);
  
 -              glPopMatrix();
 +              immUnbindProgram();
        }
  }
  
@@@ -2793,7 -2755,7 +2792,7 @@@ static int delete_exec(bContext *C, wmO
                recalc_lengths(data.edit);
        }
  
 -      DAG_id_tag_update(&data.ob->id, OB_RECALC_DATA);
 +      DEG_id_tag_update(&data.ob->id, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, data.ob);
  
        return OPERATOR_FINISHED;
@@@ -2820,12 -2782,11 +2819,12 @@@ void PARTICLE_OT_delete(wmOperatorType 
  
  /*************************** mirror operator **************************/
  
 -static void PE_mirror_x(Main *bmain, Scene *scene, Object *ob, int tagged)
 +static void PE_mirror_x(
 +        Scene *scene, Object *ob, int tagged)
  {
        Mesh *me= (Mesh *)(ob->data);
        ParticleSystemModifierData *psmd;
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
        ParticleSystem *psys = edit->psys;
        ParticleData *pa, *newpa, *new_pars;
        PTCacheEditPoint *newpoint, *new_points;
                return;
  
        psmd= psys_get_modifier(ob, psys);
 -      if (!psmd->dm_final)
 +      if (!psmd->mesh_final)
                return;
  
 -      const bool use_dm_final_indices = (psys->part->use_modifier_stack && !psmd->dm_final->deformedOnly);
 +      const bool use_dm_final_indices = (psys->part->use_modifier_stack && !psmd->mesh_final->runtime.deformed_only);
  
        /* NOTE: this is not nice to use tessfaces but hard to avoid since pa->num uses tessfaces */
        BKE_mesh_tessface_ensure(me);
  
 -      /* Note: In case psys uses DM tessface indices, we mirror final DM itself, not orig mesh. Avoids an (impossible)
 -       *       dm -> orig -> dm tessface indices conversion... */
 -      mirrorfaces = mesh_get_x_mirror_faces(ob, NULL, use_dm_final_indices ? psmd->dm_final : NULL);
 +      /* Note: In case psys uses Mesh tessface indices, we mirror final Mesh itself, not orig mesh. Avoids an (impossible)
 +       *       mesh -> orig -> mesh tessface indices conversion... */
 +      mirrorfaces = mesh_get_x_mirror_faces__real_mesh(ob, NULL, use_dm_final_indices ? psmd->mesh_final : NULL);
  
        if (!edit->mirror_cache)
                PE_update_mirror_cache(ob, psys);
                        if (point_is_selected(point)) {
                                if (edit->mirror_cache[p] != -1) {
                                        /* already has a mirror, don't need to duplicate */
 -                                      PE_mirror_particle(ob, psmd->dm_final, psys, pa, NULL);
 +                                      PE_mirror_particle(ob, psmd->mesh_final, psys, pa, NULL);
                                        continue;
                                }
                                else
        }
  
        if (newtotpart != psys->totpart) {
 -              MFace *mtessface = use_dm_final_indices ? psmd->dm_final->getTessFaceArray(psmd->dm_final) : me->mface;
 +              MFace *mtessface = use_dm_final_indices ? psmd->mesh_final->mface : me->mface;
  
                /* allocate new arrays and copy existing */
                new_pars= MEM_callocN(newtotpart*sizeof(ParticleData), "ParticleData new");
                        }
                        else {
                                newpa->num_dmcache = psys_particle_dm_face_lookup(
 -                                                       psmd->dm_final, psmd->dm_deformed, newpa->num, newpa->fuv, NULL);
 +                                                       psmd->mesh_final, psmd->mesh_original, newpa->num, newpa->fuv, NULL);
                        }
  
                        /* update edit key pointers */
                        }
  
                        /* map key positions as mirror over x axis */
 -                      PE_mirror_particle(ob, psmd->dm_final, psys, pa, newpa);
 +                      PE_mirror_particle(ob, psmd->mesh_final, psys, pa, newpa);
  
                        newpa++;
                        newpoint++;
  
  static int mirror_exec(bContext *C, wmOperator *UNUSED(op))
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
  
 -      PE_mirror_x(bmain, scene, ob, 0);
 +      PE_mirror_x(scene, ob, 0);
  
 -      update_world_cos(ob, edit);
 +      update_world_cos(CTX_data_depsgraph(C), ob, edit);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
 -      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
  
        return OPERATOR_FINISHED;
  }
@@@ -3112,7 -3074,7 +3111,7 @@@ static void brush_cut(PEData *data, in
                        edit->points[pa_index].flag |= PEP_TAG;
                }
                else {
 -                      rekey_particle_to_time(data->bmain, data->scene, ob, pa_index, cut_time);
 +                      rekey_particle_to_time(data->context, data->scene, ob, pa_index, cut_time);
                        edit->points[pa_index].flag |= PEP_EDIT_RECALC;
                }
        }
@@@ -3165,7 -3127,7 +3164,7 @@@ static void brush_puff(PEData *data, in
        }
  
        if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
 -              psys_mat_hair_to_global(data->ob, data->dm, psys->part->from, psys->particles + point_index, mat);
 +              psys_mat_hair_to_global(data->ob, data->mesh, psys->part->from, psys->particles + point_index, mat);
                invert_m4_m4(imat, mat);
        }
        else {
@@@ -3360,39 -3322,34 +3359,39 @@@ static void intersect_dm_quad_weights(c
  }
  
  /* check intersection with a derivedmesh */
 -static int particle_intersect_dm(Scene *scene, Object *ob, DerivedMesh *dm,
 +static int particle_intersect_mesh(const bContext *C, Scene *scene, Object *ob, Mesh *mesh,
                                   float *vert_cos,
                                   const float co1[3], const float co2[3],
                                   float *min_d, int *min_face, float *min_w,
                                   float *face_minmax, float *pa_minmax,
                                   float radius, float *ipoint)
  {
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
        MFace *mface= NULL;
        MVert *mvert= NULL;
        int i, totface, intersect=0;
        float cur_d, cur_uv[2], v1[3], v2[3], v3[3], v4[3], min[3], max[3], p_min[3], p_max[3];
        float cur_ipoint[3];
  
 -      if (dm == NULL) {
 +      if (mesh == NULL) {
                psys_disable_all(ob);
  
 -              dm=mesh_get_derived_final(scene, ob, 0);
 +              /* TODO(Sybren): port to Mesh when we have decided how to handle derivedFinal and derivedDeform */
 +              DerivedMesh *dm = mesh_get_derived_final(depsgraph, scene, ob, 0);
                if (dm == NULL)
 -                      dm=mesh_get_derived_deform(scene, ob, 0);
 +                      dm = mesh_get_derived_deform(depsgraph, scene, ob, 0);
  
                psys_enable_all(ob);
  
                if (dm == NULL)
                        return 0;
 +
 +              mesh = BKE_id_new_nomain(ID_ME, NULL);
 +              DM_to_mesh(dm, mesh, ob, CD_MASK_EVERYTHING, false);
        }
  
        /* BMESH_ONLY, deform dm may not have tessface */
 -      DM_ensure_tessface(dm);
 +      BKE_mesh_tessface_ensure(mesh);
  
  
        if (pa_minmax==0) {
                copy_v3_v3(p_max, pa_minmax+3);
        }
  
 -      totface=dm->getNumTessFaces(dm);
 -      mface=dm->getTessFaceDataArray(dm, CD_MFACE);
 -      mvert=dm->getVertDataArray(dm, CD_MVERT);
 +      totface = mesh->totface;
 +      mface = mesh->mface;
 +      mvert = mesh->mvert;
  
        /* lets intersect the faces */
        for (i=0; i<totface; i++, mface++) {
        return intersect;
  }
  
 -static int brush_add(PEData *data, short number)
 +static int brush_add(const bContext *C, PEData *data, short number)
  {
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
        Scene *scene= data->scene;
        Object *ob= data->ob;
 -      DerivedMesh *dm;
 +      Mesh *mesh;
        PTCacheEdit *edit = data->edit;
        ParticleSystem *psys= edit->psys;
        ParticleData *add_pars;
  
        rng = BLI_rng_new_srandom(psys->seed+data->mval[0]+data->mval[1]);
  
 +      sim.depsgraph = depsgraph;
        sim.scene= scene;
        sim.ob= ob;
        sim.psys= psys;
  
        timestep= psys_get_timestep(&sim);
  
 -      if (psys->part->use_modifier_stack || psmd->dm_final->deformedOnly) {
 -              dm = psmd->dm_final;
 +      if (psys->part->use_modifier_stack || psmd->mesh_final->runtime.deformed_only) {
 +              mesh = psmd->mesh_final;
        }
        else {
 -              dm = psmd->dm_deformed;
 +              mesh = psmd->mesh_original;
        }
 -      BLI_assert(dm);
 +      BLI_assert(mesh);
  
        for (i=0; i<number; i++) {
                if (number>1) {
  
                mco[0] = data->mval[0] + dmx;
                mco[1] = data->mval[1] + dmy;
 -              ED_view3d_win_to_segment(data->vc.ar, data->vc.v3d, mco, co1, co2, true);
 +              ED_view3d_win_to_segment(depsgraph, data->vc.ar, data->vc.v3d, mco, co1, co2, true);
  
                mul_m4_v3(imat, co1);
                mul_m4_v3(imat, co2);
                min_d=2.0;
  
                /* warning, returns the derived mesh face */
 -              if (particle_intersect_dm(scene, ob, dm, 0, co1, co2, &min_d, &add_pars[n].num_dmcache, add_pars[n].fuv, 0, 0, 0, 0)) {
 -                      if (psys->part->use_modifier_stack && !psmd->dm_final->deformedOnly) {
 +              if (particle_intersect_mesh(C, scene, ob, mesh, 0, co1, co2, &min_d, &add_pars[n].num_dmcache, add_pars[n].fuv, 0, 0, 0, 0)) {
 +                      if (psys->part->use_modifier_stack && !psmd->mesh_final->runtime.deformed_only) {
                                add_pars[n].num = add_pars[n].num_dmcache;
                                add_pars[n].num_dmcache = DMCACHE_ISCHILD;
                        }
 -                      else if (dm == psmd->dm_deformed) {
 +                      else if (mesh == psmd->mesh_original) {
                                /* Final DM is not same topology as orig mesh, we have to map num_dmcache to real final dm. */
                                add_pars[n].num = add_pars[n].num_dmcache;
                                add_pars[n].num_dmcache = psys_particle_dm_face_lookup(
 -                                                            psmd->dm_final, psmd->dm_deformed,
 +                                                            psmd->mesh_final, psmd->mesh_original,
                                                              add_pars[n].num, add_pars[n].fuv, NULL);
                        }
                        else {
                        tree=BLI_kdtree_new(psys->totpart);
  
                        for (i=0, pa=psys->particles; i<totpart; i++, pa++) {
 -                              psys_particle_on_dm(psmd->dm_final, psys->part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, cur_co, 0, 0, 0, 0, 0);
 +                              psys_particle_on_dm(psmd->mesh_final, psys->part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, cur_co, 0, 0, 0, 0);
                                BLI_kdtree_insert(tree, i, cur_co);
                        }
  
                                int w, maxw;
                                float maxd, totw=0.0, weight[3];
  
 -                              psys_particle_on_dm(psmd->dm_final, psys->part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, co1, 0, 0, 0, 0, 0);
 +                              psys_particle_on_dm(psmd->mesh_final, psys->part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, co1, 0, 0, 0, 0);
                                maxw = BLI_kdtree_find_nearest_n(tree, co1, ptn, 3);
  
                                maxd= ptn[maxw-1].dist;
                                }
                        }
                        for (k=0, hkey=pa->hair; k<pset->totaddkey; k++, hkey++) {
 -                              psys_mat_hair_to_global(ob, psmd->dm_final, psys->part->from, pa, hairmat);
 +                              psys_mat_hair_to_global(ob, psmd->mesh_final, psys->part->from, pa, hairmat);
                                invert_m4_m4(imat, hairmat);
                                mul_m4_v3(imat, hkey->co);
                        }
  
  typedef struct BrushEdit {
        Scene *scene;
 +      ViewLayer *view_layer;
        Object *ob;
        PTCacheEdit *edit;
  
  
  static int brush_edit_init(bContext *C, wmOperator *op)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
 +      ViewLayer *view_layer = CTX_data_view_layer(C);
        Object *ob= CTX_data_active_object(C);
        ParticleEditSettings *pset= PE_settings(scene);
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        ARegion *ar= CTX_wm_region(C);
        BrushEdit *bedit;
        float min[3], max[3];
  
        /* set the 'distance factor' for grabbing (used in comb etc) */
        INIT_MINMAX(min, max);
 -      PE_minmax(bmain, scene, min, max);
 +      PE_minmax(scene, view_layer, min, max);
        mid_v3_v3v3(min, min, max);
  
        bedit= MEM_callocN(sizeof(BrushEdit), "BrushEdit");
        op->customdata= bedit;
  
        bedit->scene= scene;
 +      bedit->view_layer = view_layer;
        bedit->ob= ob;
        bedit->edit= edit;
  
  
  static void brush_edit_apply(bContext *C, wmOperator *op, PointerRNA *itemptr)
  {
+       Main *bmain = CTX_data_main(C);
        BrushEdit *bedit= op->customdata;
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
        Scene *scene= bedit->scene;
        Object *ob= bedit->ob;
        PTCacheEdit *edit= bedit->edit;
             (sqrtf(dx * dx + dy * dy) > pset->brush[PE_BRUSH_ADD].step) : (dx != 0 || dy != 0)) || bedit->first)
        {
                PEData data= bedit->data;
 +              data.context = C; // TODO(mai): why isnt this set in bedit->data?
  
                view3d_operator_needs_opengl(C);
                selected= (short)count_selected_keys(scene, edit);
                                case PE_BRUSH_PUFF:
                                {
                                        if (edit->psys) {
 -                                              data.dm= psmd->dm_final;
 +                                              data.mesh = psmd->mesh_final;
                                                data.mval= mval;
                                                data.rad= pe_brush_size_get(scene, brush);
                                                data.select= selected;
                                        if (edit->psys && edit->psys->part->from==PART_FROM_FACE) {
                                                data.mval= mval;
  
 -                                              added= brush_add(&data, brush->count);
 +                                              added= brush_add(C, &data, brush->count);
  
                                                if (pset->flag & PE_KEEP_LENGTHS)
                                                        recalc_lengths(edit);
                                case PE_BRUSH_WEIGHT:
                                {
                                        if (edit->psys) {
 -                                              data.dm= psmd->dm_final;
 +                                              data.mesh = psmd->mesh_final;
                                                data.mval= mval;
                                                data.rad= pe_brush_size_get(scene, brush);
  
  
                        if (ELEM(pset->brushtype, PE_BRUSH_ADD, PE_BRUSH_CUT) && (added || removed)) {
                                if (pset->brushtype == PE_BRUSH_ADD && pe_x_mirror(ob))
 -                                      PE_mirror_x(bmain, scene, ob, 1);
 +                                      PE_mirror_x(scene, ob, 1);
  
 -                              update_world_cos(ob, edit);
 +                              update_world_cos(depsgraph, ob, edit);
                                psys_free_path_cache(NULL, edit);
 -                              DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +                              DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +                      }
 +                      else {
 +                              PE_update_object(depsgraph, scene, ob, 1);
                        }
 -                      else
 -                              PE_update_object(bmain, scene, ob, 1);
                }
  
                if (edit->psys) {
                        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
 +                      BKE_particle_batch_cache_dirty(edit->psys, BKE_PARTICLE_BATCH_DIRTY_ALL);
 +                      DEG_id_tag_update(&ob->id, DEG_TAG_COPY_ON_WRITE);
                }
                else {
 -                      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +                      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
                        WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
                }
  
@@@ -4217,7 -4166,7 +4217,7 @@@ static void shape_cut(PEData *data, in
                        edit->points[pa_index].flag |= PEP_TAG;
                }
                else {
 -                      rekey_particle_to_time(data->bmain, data->scene, ob, pa_index, cut_time);
 +                      rekey_particle_to_time(data->context, data->scene, ob, pa_index, cut_time);
                        edit->points[pa_index].flag |= PEP_EDIT_RECALC;
                }
        }
  
  static int shape_cut_exec(bContext *C, wmOperator *UNUSED(op))
  {
 -      Main *bmain = CTX_data_main(C);
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
        Scene *scene = CTX_data_scene(C);
        Object *ob = CTX_data_active_object(C);
        ParticleEditSettings *pset = PE_settings(scene);
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
        Object *shapeob = pset->shape_object;
        int selected = count_selected_keys(scene, edit);
        int lock_root = pset->flag & PE_LOCK_FIRST;
                recalc_lengths(edit);
  
                if (removed) {
 -                      update_world_cos(ob, edit);
 +                      update_world_cos(depsgraph, ob, edit);
                        psys_free_path_cache(NULL, edit);
 -                      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +                      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +              }
 +              else {
 +                      PE_update_object(data.depsgraph, scene, ob, 1);
                }
 -              else
 -                      PE_update_object(bmain, scene, ob, 1);
  
                if (edit->psys) {
                        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
 +                      BKE_particle_batch_cache_dirty(edit->psys, BKE_PARTICLE_BATCH_DIRTY_ALL);
 +                      DEG_id_tag_update(&ob->id, DEG_TAG_COPY_ON_WRITE);
                }
                else {
 -                      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +                      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
                        WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
                }
  
@@@ -4303,10 -4249,10 +4303,10 @@@ void PARTICLE_OT_shape_cut(wmOperatorTy
  
  /************************ utilities ******************************/
  
 -int PE_minmax(Main *bmain, Scene *scene, float min[3], float max[3])
 +int PE_minmax(Scene *scene, ViewLayer *view_layer, float min[3], float max[3])
  {
 -      Object *ob= OBACT;
 -      PTCacheEdit *edit= PE_get_current(bmain, scene, ob);
 +      Object *ob= OBACT(view_layer);
 +      PTCacheEdit *edit= PE_get_current(scene, ob);
        ParticleSystem *psys;
        ParticleSystemModifierData *psmd = NULL;
        POINT_P; KEY_K;
  
        LOOP_VISIBLE_POINTS {
                if (psys)
 -                      psys_mat_hair_to_global(ob, psmd->dm_final, psys->part->from, psys->particles+p, mat);
 +                      psys_mat_hair_to_global(ob, psmd->mesh_final, psys->part->from, psys->particles+p, mat);
  
                LOOP_SELECTED_KEYS {
                        copy_v3_v3(co, key->co);
  /************************ particle edit toggle operator ************************/
  
  /* initialize needed data for bake edit */
 -void PE_create_particle_edit(Main *bmain, Scene *scene, Object *ob, PointCache *cache, ParticleSystem *psys)
 +void PE_create_particle_edit(
 +        Depsgraph *depsgraph, Scene *scene, Object *ob, PointCache *cache, ParticleSystem *psys)
  {
        PTCacheEdit *edit;
        ParticleSystemModifierData *psmd = (psys) ? psys_get_modifier(ob, psys) : NULL;
        int totpoint;
  
        /* no psmd->dm happens in case particle system modifier is not enabled */
 -      if (!(psys && psmd && psmd->dm_final) && !cache)
 +      if (!(psys && psmd && psmd->mesh_final) && !cache)
                return;
  
        if (cache && cache->flag & PTCACHE_DISK_CACHE)
                                }
                                pa++;
                        }
 -                      update_world_cos(ob, edit);
 +                      update_world_cos(depsgraph, ob, edit);
                }
                else {
                        PTCacheMem *pm;
  
                recalc_lengths(edit);
                if (psys && !cache)
 -                      recalc_emitter_field(ob, psys);
 -              PE_update_object(bmain, scene, ob, 1);
 +                      recalc_emitter_field(depsgraph, ob, psys);
 +
 +              PE_update_object(depsgraph, scene, ob, 1);
        }
  }
  
@@@ -4468,8 -4412,7 +4468,8 @@@ static int particle_edit_toggle_poll(bC
  
  static int particle_edit_toggle_exec(bContext *C, wmOperator *op)
  {
 -      Main *bmain = CTX_data_main(C);
 +      struct wmMsgBus *mbus = CTX_wm_message_bus(C);
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
        Scene *scene = CTX_data_scene(C);
        Object *ob = CTX_data_active_object(C);
        const int mode_flag = OB_MODE_PARTICLE_EDIT;
  
        if (!is_mode_set) {
                PTCacheEdit *edit;
 +
 +              /* Particle edit mode requires original object to have all strands
 +               * cached. A bit annoying to do update here, but is simpler than
 +               * rewriting the while edit mode code.
 +               */
 +              ob->id.recalc |= (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
 +              BKE_scene_graph_update_tagged(depsgraph, CTX_data_main(C));
 +              BKE_object_eval_transform_all(depsgraph, scene, ob);
 +              BKE_object_handle_data_update(depsgraph, scene, ob);
                ob->mode |= mode_flag;
 -              edit= PE_create_current(bmain, scene, ob);
 +
 +              edit= PE_create_current(depsgraph, scene, ob);
  
                /* mesh may have changed since last entering editmode.
                 * note, this may have run before if the edit data was just created, so could avoid this and speed up a little */
                if (edit && edit->psys)
 -                      recalc_emitter_field(ob, edit->psys);
 +                      recalc_emitter_field(depsgraph, ob, edit->psys);
  
                toggle_particle_cursor(C, 1);
                WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_PARTICLE, NULL);
                WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, NULL);
        }
  
 -      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +      // ED_workspace_object_mode_sync_from_object(wm, workspace, ob);
 +
 +      DEG_id_tag_update(&ob->id, OB_RECALC_DATA | DEG_TAG_COPY_ON_WRITE);
 +
 +      WM_msg_publish_rna_prop(mbus, &ob->id, ob, Object, mode);
 +
 +      WM_toolsystem_update_from_context_view3d(C);
  
        return OPERATOR_FINISHED;
  }
@@@ -4557,7 -4484,7 +4557,7 @@@ static int clear_edited_exec(bContext *
  
                        psys_reset(psys, PSYS_RESET_DEPSGRAPH);
                        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
 -                      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +                      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
                }
        }
        else { /* some operation might have protected hair from editing so let's clear the flag */
                psys->flag &= ~PSYS_GLOBAL_HAIR;
                psys->flag &= ~PSYS_EDITED;
                WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
 -              DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +              DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
        }
  
        return OPERATOR_FINISHED;
@@@ -4666,25 -4593,22 +4666,24 @@@ static void scale_points_to_length(PTCa
  
  static int unify_length_exec(bContext *C, wmOperator *UNUSED(op))
  {
 -      Main *bmain = CTX_data_main(C);
        Object *ob = CTX_data_active_object(C);
        Scene *scene = CTX_data_scene(C);
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
 +
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
        float average_length = calculate_average_length(edit);
 +
        if (average_length == 0.0f) {
                return OPERATOR_CANCELLED;
        }
        scale_points_to_length(edit, average_length);
  
 -      PE_update_object(bmain, scene, ob, 1);
 +      PE_update_object(depsgraph, scene, ob, 1);
        if (edit->psys) {
                WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
        }
        else {
 -              DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +              DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
                WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
        }
  
  #include "BLI_string.h"
  #include "BLI_utildefines.h"
  
 -#include "BKE_depsgraph.h"
 -#include "BKE_main.h"
+ #include "BKE_context.h"
  #include "BKE_particle.h"
  #include "BKE_pointcache.h"
- #include "BKE_context.h"
  #include "BKE_undo_system.h"
  
 +#include "DEG_depsgraph.h"
 +
  #include "ED_object.h"
  #include "ED_particle.h"
  #include "ED_physics.h"
@@@ -229,27 -229,27 +229,27 @@@ typedef struct ParticleUndoStep 
  
  static bool particle_undosys_poll(struct bContext *C)
  {
 -      Main *bmain = CTX_data_main(C);
        Scene *scene = CTX_data_scene(C);
 -      Object *ob = OBACT;
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 +      ViewLayer *view_layer = CTX_data_view_layer(C);
 +      Object *ob = OBACT(view_layer);
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
 +
        return (edit != NULL);
  }
  
  static bool particle_undosys_step_encode(struct bContext *C, UndoStep *us_p)
  {
 -      Main *bmain = CTX_data_main(C);
        ParticleUndoStep *us = (ParticleUndoStep *)us_p;
 +      ViewLayer *view_layer = CTX_data_view_layer(C);
        us->scene_ref.ptr = CTX_data_scene(C);
 -      us->object_ref.ptr = us->scene_ref.ptr->basact->object;
 -      PTCacheEdit *edit = PE_get_current(bmain, us->scene_ref.ptr, us->object_ref.ptr);
 +      us->object_ref.ptr = OBACT(view_layer);
 +      PTCacheEdit *edit = PE_get_current(us->scene_ref.ptr, us->object_ref.ptr);
        undoptcache_from_editcache(&us->data, edit);
        return true;
  }
  
  static void particle_undosys_step_decode(struct bContext *C, UndoStep *us_p, int UNUSED(dir))
  {
 -      Main *bmain = CTX_data_main(C);
        /* TODO(campbell): undo_system: use low-level API to set mode. */
        ED_object_mode_set(C, OB_MODE_PARTICLE_EDIT);
        BLI_assert(particle_undosys_poll(C));
        ParticleUndoStep *us = (ParticleUndoStep *)us_p;
        Scene *scene = us->scene_ref.ptr;
        Object *ob = us->object_ref.ptr;
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 +      PTCacheEdit *edit = PE_get_current(scene, ob);
        if (edit) {
                undoptcache_to_editcache(&us->data, edit);
 -              DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +              DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
        }
        else {
                BLI_assert(0);
  #include "BKE_context.h"
  #include "BKE_crazyspace.h"
  #include "BKE_curve.h"
 -#include "BKE_depsgraph.h"
  #include "BKE_fcurve.h"
  #include "BKE_global.h"
  #include "BKE_gpencil.h"
 +#include "BKE_layer.h"
  #include "BKE_key.h"
  #include "BKE_main.h"
  #include "BKE_mesh.h"
  #include "RNA_access.h"
  
  #include "DEG_depsgraph.h"
 +#include "DEG_depsgraph_build.h"
 +#include "DEG_depsgraph_query.h"
  
  #include "transform.h"
  #include "bmesh.h"
   */
  static void transform_around_single_fallback(TransInfo *t)
  {
 -      if ((t->total == 1) &&
 +      if ((t->data_len_all == 1) &&
            (ELEM(t->around, V3D_AROUND_CENTER_BOUNDS, V3D_AROUND_CENTER_MEAN, V3D_AROUND_ACTIVE)) &&
            (ELEM(t->mode, TFM_RESIZE, TFM_ROTATION, TFM_TRACKBALL)))
        {
@@@ -171,50 -169,45 +171,50 @@@ static int trans_data_compare_rdist(con
  
  void sort_trans_data_dist(TransInfo *t)
  {
 -      TransData *start = t->data;
 -      int i;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              TransData *start = tc->data;
 +              int i;
  
 -      for (i = 0; i < t->total && start->flag & TD_SELECTED; i++)
 -              start++;
 +              for (i = 0; i < tc->data_len && start->flag & TD_SELECTED; i++) {
 +                      start++;
 +              }
  
 -      if (i < t->total) {
 -              if (t->flag & T_PROP_CONNECTED)
 -                      qsort(start, t->total - i, sizeof(TransData), trans_data_compare_dist);
 -              else
 -                      qsort(start, t->total - i, sizeof(TransData), trans_data_compare_rdist);
 +              if (i < tc->data_len) {
 +                      if (t->flag & T_PROP_CONNECTED)
 +                              qsort(start, tc->data_len - i, sizeof(TransData), trans_data_compare_dist);
 +                      else
 +                              qsort(start, tc->data_len - i, sizeof(TransData), trans_data_compare_rdist);
 +              }
        }
  }
  
  static void sort_trans_data(TransInfo *t)
  {
 -      TransData *sel, *unsel;
 -      TransData temp;
 -      unsel = t->data;
 -      sel = t->data;
 -      sel += t->total - 1;
 -      while (sel > unsel) {
 -              while (unsel->flag & TD_SELECTED) {
 -                      unsel++;
 -                      if (unsel == sel) {
 -                              return;
 -                      }
 -              }
 -              while (!(sel->flag & TD_SELECTED)) {
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              TransData *sel, *unsel;
 +              TransData temp;
 +              unsel = tc->data;
 +              sel = tc->data;
 +              sel += tc->data_len - 1;
 +              while (sel > unsel) {
 +                      while (unsel->flag & TD_SELECTED) {
 +                              unsel++;
 +                              if (unsel == sel) {
 +                                      return;
 +                              }
 +                      }
 +                      while (!(sel->flag & TD_SELECTED)) {
 +                              sel--;
 +                              if (unsel == sel) {
 +                                      return;
 +                              }
 +                      }
 +                      temp = *unsel;
 +                      *unsel = *sel;
 +                      *sel = temp;
                        sel--;
 -                      if (unsel == sel) {
 -                              return;
 -                      }
 +                      unsel++;
                }
 -              temp = *unsel;
 -              *unsel = *sel;
 -              *sel = temp;
 -              sel--;
 -              unsel++;
        }
  }
  
   * warning; this is loops inside loop, has minor N^2 issues, but by sorting list it is OK */
  static void set_prop_dist(TransInfo *t, const bool with_dist)
  {
 -      TransData *tob;
        int a;
  
        float _proj_vec[3];
                }
        }
  
 -      for (a = 0, tob = t->data; a < t->total; a++, tob++) {
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              TransData *tob = tc->data;
 +              for (a = 0; a < tc->data_len; a++, tob++) {
  
 -              tob->rdist = 0.0f; // init, it was mallocced
 +                      tob->rdist = 0.0f; // init, it was mallocced
  
 -              if ((tob->flag & TD_SELECTED) == 0) {
 -                      TransData *td;
 -                      int i;
 -                      float dist_sq, vec[3];
 +                      if ((tob->flag & TD_SELECTED) == 0) {
 +                              TransData *td;
 +                              int i;
 +                              float dist_sq, vec[3];
  
 -                      tob->rdist = -1.0f; // signal for next loop
 +                              tob->rdist = -1.0f; // signal for next loop
  
 -                      for (i = 0, td = t->data; i < t->total; i++, td++) {
 -                              if (td->flag & TD_SELECTED) {
 -                                      if (use_island) {
 -                                              sub_v3_v3v3(vec, tob->iloc, td->iloc);
 -                                      }
 -                                      else {
 -                                              sub_v3_v3v3(vec, tob->center, td->center);
 -                                      }
 -                                      mul_m3_v3(tob->mtx, vec);
 +                              for (i = 0, td = tc->data; i < tc->data_len; i++, td++) {
 +                                      if (td->flag & TD_SELECTED) {
 +                                              if (use_island) {
 +                                                      sub_v3_v3v3(vec, tob->iloc, td->iloc);
 +                                              }
 +                                              else {
 +                                                      sub_v3_v3v3(vec, tob->center, td->center);
 +                                              }
 +                                              mul_m3_v3(tob->mtx, vec);
  
 -                                      if (proj_vec) {
 -                                              float vec_p[3];
 -                                              project_v3_v3v3(vec_p, vec, proj_vec);
 -                                              sub_v3_v3(vec, vec_p);
 -                                      }
 +                                              if (proj_vec) {
 +                                                      float vec_p[3];
 +                                                      project_v3_v3v3(vec_p, vec, proj_vec);
 +                                                      sub_v3_v3(vec, vec_p);
 +                                              }
  
 -                                      dist_sq = len_squared_v3(vec);
 -                                      if ((tob->rdist == -1.0f) || (dist_sq < SQUARE(tob->rdist))) {
 -                                              tob->rdist = sqrtf(dist_sq);
 -                                              if (use_island) {
 -                                                      copy_v3_v3(tob->center, td->center);
 -                                                      copy_m3_m3(tob->axismtx, td->axismtx);
 +                                              dist_sq = len_squared_v3(vec);
 +                                              if ((tob->rdist == -1.0f) || (dist_sq < SQUARE(tob->rdist))) {
 +                                                      tob->rdist = sqrtf(dist_sq);
 +                                                      if (use_island) {
 +                                                              copy_v3_v3(tob->center, td->center);
 +                                                              copy_m3_m3(tob->axismtx, td->axismtx);
 +                                                      }
                                                }
                                        }
 +                                      else {
 +                                              break;  /* by definition transdata has selected items in beginning */
 +                                      }
                                }
 -                              else {
 -                                      break;  /* by definition transdata has selected items in beginning */
 +                              if (with_dist) {
 +                                      tob->dist = tob->rdist;
                                }
                        }
 -                      if (with_dist) {
 -                              tob->dist = tob->rdist;
 -                      }
                }
        }
  }
  
  static void createTransTexspace(TransInfo *t)
  {
 -      Scene *scene = t->scene;
 +      ViewLayer *view_layer = t->view_layer;
        TransData *td;
        Object *ob;
        ID *id;
        short *texflag;
  
 -      ob = OBACT;
 +      ob = OBACT(view_layer);
  
        if (ob == NULL) { // Shouldn't logically happen, but still...
 -              t->total = 0;
                return;
        }
  
        id = ob->data;
        if (id == NULL || !ELEM(GS(id->name), ID_ME, ID_CU, ID_MB)) {
                BKE_report(t->reports, RPT_ERROR, "Unsupported object type for text-space transform");
 -              t->total = 0;
                return;
        }
  
        if (BKE_object_obdata_is_libdata(ob)) {
                BKE_report(t->reports, RPT_ERROR, "Linked data can't text-space transform");
 -              t->total = 0;
                return;
        }
  
 -      t->total = 1;
 -      td = t->data = MEM_callocN(sizeof(TransData), "TransTexspace");
 -      td->ext = t->ext = MEM_callocN(sizeof(TransDataExtension), "TransTexspace");
 +
 +      {
 +              BLI_assert(t->data_container_len == 1);
 +              TransDataContainer *tc = t->data_container;
 +              tc->data_len = 1;
 +              td = tc->data = MEM_callocN(sizeof(TransData), "TransTexspace");
 +              td->ext = tc->data_ext = MEM_callocN(sizeof(TransDataExtension), "TransTexspace");
 +      }
  
        td->flag = TD_SELECTED;
        copy_v3_v3(td->center, ob->obmat[3]);
        copy_v3_v3(td->ext->isize, td->ext->size);
  }
  
 +static void createTransCursor3D(TransInfo *t)
 +{
 +      TransData *td;
 +
 +      Scene *scene = t->scene;
 +      View3D *v3d = ((t->spacetype == SPACE_VIEW3D) && (t->ar->regiontype == RGN_TYPE_WINDOW)) ? t->view : NULL;
 +      View3DCursor *cursor = ED_view3d_cursor3d_get(scene, v3d);
 +
 +      if ((cursor == &scene->cursor) && ID_IS_LINKED(scene)) {
 +              BKE_report(t->reports, RPT_ERROR, "Linked data can't text-space transform");
 +              return;
 +      }
 +
 +      {
 +              BLI_assert(t->data_container_len == 1);
 +              TransDataContainer *tc = t->data_container;
 +              tc->data_len = 1;
 +              td = tc->data = MEM_callocN(sizeof(TransData), "TransTexspace");
 +              td->ext = tc->data_ext = MEM_callocN(sizeof(TransDataExtension), "TransTexspace");
 +      }
 +
 +      td->flag = TD_SELECTED;
 +      copy_v3_v3(td->center, cursor->location);
 +      td->ob = NULL;
 +
 +      unit_m3(td->mtx);
 +      quat_to_mat3(td->axismtx, cursor->rotation);
 +      normalize_m3(td->axismtx);
 +      pseudoinverse_m3_m3(td->smtx, td->mtx, PSEUDOINVERSE_EPSILON);
 +
 +      td->loc = cursor->location;
 +      copy_v3_v3(td->iloc, cursor->location);
 +
 +      td->ext->quat = cursor->rotation;
 +      copy_qt_qt(td->ext->iquat, cursor->rotation);
 +}
 +
  /* ********************* edge (for crease) ***** */
  
  static void createTransEdge(TransInfo *t)
  {
 -      BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
 -      TransData *td = NULL;
 -      BMEdge *eed;
 -      BMIter iter;
 -      float mtx[3][3], smtx[3][3];
 -      int count = 0, countsel = 0;
 -      const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 -      int cd_edge_float_offset;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
  
 -      BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
 -              if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
 -                      if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) countsel++;
 -                      if (is_prop_edit) count++;
 +              BMEditMesh *em = BKE_editmesh_from_object(tc->obedit);
 +              TransData *td = NULL;
 +              BMEdge *eed;
 +              BMIter iter;
 +              float mtx[3][3], smtx[3][3];
 +              int count = 0, countsel = 0;
 +              const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 +              int cd_edge_float_offset;
 +
 +              BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
 +                      if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
 +                              if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) countsel++;
 +                              if (is_prop_edit) count++;
 +                      }
                }
 -      }
  
 -      if (countsel == 0)
 -              return;
 +              if (countsel == 0) {
 +                      tc->data_len = 0;
 +                      continue;
 +              }
  
 -      if (is_prop_edit) {
 -              t->total = count;
 -      }
 -      else {
 -              t->total = countsel;
 -      }
 +              if (is_prop_edit) {
 +                      tc->data_len = count;
 +              }
 +              else {
 +                      tc->data_len = countsel;
 +              }
  
 -      td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransCrease");
 +              td = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransCrease");
  
 -      copy_m3_m4(mtx, t->obedit->obmat);
 -      pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
 +              copy_m3_m4(mtx, tc->obedit->obmat);
 +              pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
  
 -      /* create data we need */
 -      if (t->mode == TFM_BWEIGHT) {
 -              BM_mesh_cd_flag_ensure(em->bm, BKE_mesh_from_object(t->obedit), ME_CDFLAG_EDGE_BWEIGHT);
 -              cd_edge_float_offset = CustomData_get_offset(&em->bm->edata, CD_BWEIGHT);
 -      }
 -      else { //if (t->mode == TFM_CREASE) {
 -              BLI_assert(t->mode == TFM_CREASE);
 -              BM_mesh_cd_flag_ensure(em->bm, BKE_mesh_from_object(t->obedit), ME_CDFLAG_EDGE_CREASE);
 -              cd_edge_float_offset = CustomData_get_offset(&em->bm->edata, CD_CREASE);
 -      }
 +              /* create data we need */
 +              if (t->mode == TFM_BWEIGHT) {
 +                      BM_mesh_cd_flag_ensure(em->bm, BKE_mesh_from_object(tc->obedit), ME_CDFLAG_EDGE_BWEIGHT);
 +                      cd_edge_float_offset = CustomData_get_offset(&em->bm->edata, CD_BWEIGHT);
 +              }
 +              else { //if (t->mode == TFM_CREASE) {
 +                      BLI_assert(t->mode == TFM_CREASE);
 +                      BM_mesh_cd_flag_ensure(em->bm, BKE_mesh_from_object(tc->obedit), ME_CDFLAG_EDGE_CREASE);
 +                      cd_edge_float_offset = CustomData_get_offset(&em->bm->edata, CD_CREASE);
 +              }
  
 -      BLI_assert(cd_edge_float_offset != -1);
 +              BLI_assert(cd_edge_float_offset != -1);
  
 -      BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
 -              if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && (BM_elem_flag_test(eed, BM_ELEM_SELECT) || is_prop_edit)) {
 -                      float *fl_ptr;
 -                      /* need to set center for center calculations */
 -                      mid_v3_v3v3(td->center, eed->v1->co, eed->v2->co);
 +              BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
 +                      if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && (BM_elem_flag_test(eed, BM_ELEM_SELECT) || is_prop_edit)) {
 +                              float *fl_ptr;
 +                              /* need to set center for center calculations */
 +                              mid_v3_v3v3(td->center, eed->v1->co, eed->v2->co);
  
 -                      td->loc = NULL;
 -                      if (BM_elem_flag_test(eed, BM_ELEM_SELECT))
 -                              td->flag = TD_SELECTED;
 -                      else
 -                              td->flag = 0;
 +                              td->loc = NULL;
 +                              if (BM_elem_flag_test(eed, BM_ELEM_SELECT))
 +                                      td->flag = TD_SELECTED;
 +                              else
 +                                      td->flag = 0;
  
 -                      copy_m3_m3(td->smtx, smtx);
 -                      copy_m3_m3(td->mtx, mtx);
 +                              copy_m3_m3(td->smtx, smtx);
 +                              copy_m3_m3(td->mtx, mtx);
  
 -                      td->ext = NULL;
 +                              td->ext = NULL;
  
 -                      fl_ptr = BM_ELEM_CD_GET_VOID_P(eed, cd_edge_float_offset);
 -                      td->val  =  fl_ptr;
 -                      td->ival = *fl_ptr;
 +                              fl_ptr = BM_ELEM_CD_GET_VOID_P(eed, cd_edge_float_offset);
 +                              td->val  =  fl_ptr;
 +                              td->ival = *fl_ptr;
  
 -                      td++;
 +                              td++;
 +                      }
                }
        }
  }
@@@ -574,7 -521,7 +574,7 @@@ static short apply_targetless_ik(Objec
        return apply;
  }
  
 -static void add_pose_transdata(TransInfo *t, bPoseChannel *pchan, Object *ob, TransData *td)
 +static void add_pose_transdata(TransInfo *t, bPoseChannel *pchan, Object *ob, TransDataContainer *tc, TransData *td)
  {
        Bone *bone = pchan->bone;
        float pmat[3][3], omat[3][3];
        normalize_m3(td->axismtx);
  
        if (ELEM(t->mode, TFM_BONESIZE, TFM_BONE_ENVELOPE_DIST)) {
 -              bArmature *arm = t->poseobj->data;
 +              bArmature *arm = tc->poseobj->data;
  
                if ((t->mode == TFM_BONE_ENVELOPE_DIST) || (arm->drawtype == ARM_ENVELOPE)) {
                        td->loc = NULL;
@@@ -748,11 -695,12 +748,11 @@@ static void bone_children_clear_transfl
  
  /* sets transform flags in the bones
   * returns total number of bones with BONE_TRANSFORM */
 -int count_set_pose_transflags(int *out_mode, short around, Object *ob)
 +int count_set_pose_transflags(Object *ob, const int mode, short around, bool has_translate_rotate[2])
  {
        bArmature *arm = ob->data;
        bPoseChannel *pchan;
        Bone *bone;
 -      int mode = *out_mode;
        int total = 0;
  
        for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
                }
        }
        /* now count, and check if we have autoIK or have to switch from translate to rotate */
 -      bool has_translation = false, has_rotation = false;
 -
        for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
                bone = pchan->bone;
                if (bone->flag & BONE_TRANSFORM) {
                        total++;
  
 -                      if (mode == TFM_TRANSLATION) {
 +                      if (has_translate_rotate != NULL) {
                                if (has_targetless_ik(pchan) == NULL) {
                                        if (pchan->parent && (pchan->bone->flag & BONE_CONNECTED)) {
 -                                              if (pchan->bone->flag & BONE_HINGE_CHILD_TRANSFORM)
 -                                                      has_translation = true;
 +                                              if (pchan->bone->flag & BONE_HINGE_CHILD_TRANSFORM) {
 +                                                      has_translate_rotate[0] = true;
 +                                              }
                                        }
                                        else {
 -                                              if ((pchan->protectflag & OB_LOCK_LOC) != OB_LOCK_LOC)
 -                                                      has_translation = true;
 +                                              if ((pchan->protectflag & OB_LOCK_LOC) != OB_LOCK_LOC) {
 +                                                      has_translate_rotate[0] = true;
 +                                              }
 +                                      }
 +                                      if ((pchan->protectflag & OB_LOCK_ROT) != OB_LOCK_ROT) {
 +                                              has_translate_rotate[1] = true;
                                        }
 -                                      if ((pchan->protectflag & OB_LOCK_ROT) != OB_LOCK_ROT)
 -                                              has_rotation = true;
                                }
 -                              else
 -                                      has_translation = true;
 +                              else {
 +                                      has_translate_rotate[1] = true;
 +                              }
                        }
                }
        }
  
 -      /* if there are no translatable bones, do rotation */
 -      if (mode == TFM_TRANSLATION && !has_translation) {
 -              if (has_rotation) {
 -                      *out_mode = TFM_ROTATION;
 -              }
 -              else {
 -                      *out_mode = TFM_RESIZE;
 -              }
 -      }
 -
        return total;
  }
  
@@@ -872,25 -828,25 +872,25 @@@ void transform_autoik_update(TransInfo 
                }
        }
  
 -      /* sanity checks (don't assume t->poseobj is set, or that it is an armature) */
 -      if (ELEM(NULL, t->poseobj, t->poseobj->pose))
 -              return;
 -
        /* apply to all pose-channels */
        bool changed = false;
 -      for (pchan = t->poseobj->pose->chanbase.first; pchan; pchan = pchan->next) {
 -              changed |= pchan_autoik_adjust(pchan, *chainlen);
 -      }
  
 -#ifdef WITH_LEGACY_DEPSGRAPH
 -      if (!DEG_depsgraph_use_legacy())
 -#endif
 -      {
 -              if (changed) {
 -                      /* TODO(sergey): Consider doing partial update only. */
 -                      DAG_relations_tag_update(bmain);
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +
 +              /* sanity checks (don't assume t->poseobj is set, or that it is an armature) */
 +              if (ELEM(NULL, tc->poseobj, tc->poseobj->pose)) {
 +                      continue;
 +              }
 +
 +              for (pchan = tc->poseobj->pose->chanbase.first; pchan; pchan = pchan->next) {
 +                      changed |= pchan_autoik_adjust(pchan, *chainlen);
                }
        }
 +
 +      if (changed) {
 +              /* TODO(sergey): Consider doing partial update only. */
 +              DEG_relations_tag_update(bmain);
 +      }
  }
  
  /* frees temporal IKs */
@@@ -899,7 -855,9 +899,7 @@@ static void pose_grab_with_ik_clear(Mai
        bKinematicConstraint *data;
        bPoseChannel *pchan;
        bConstraint *con, *next;
 -#ifdef WITH_LEGACY_DEPSGRAPH
 -      bool need_dependency_update = false;
 -#endif
 +      bool relations_changed = false;
  
        for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
                /* clear all temporary lock flags */
                        if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
                                data = con->data;
                                if (data->flag & CONSTRAINT_IK_TEMP) {
 +                                      relations_changed = true;
 +
                                        /* iTaSC needs clear for removed constraints */
 -#ifdef WITH_LEGACY_DEPSGRAPH
 -                                      need_dependency_update = true;
 -#endif
                                        BIK_clear_data(ob->pose);
  
                                        BLI_remlink(&pchan->constraints, con);
                }
        }
  
 -#ifdef WITH_LEGACY_DEPSGRAPH
 -      if (!DEG_depsgraph_use_legacy() && need_dependency_update)
 -#endif
 -      {
 +      if (relations_changed) {
                /* TODO(sergey): Consider doing partial update only. */
 -              DAG_relations_tag_update(bmain);
 +              DEG_relations_tag_update(bmain);
        }
  }
  
@@@ -1086,126 -1048,89 +1086,126 @@@ static short pose_grab_with_ik(Main *bm
        /* iTaSC needs clear for new IK constraints */
        if (tot_ik) {
                BIK_clear_data(ob->pose);
 -#ifdef WITH_LEGACY_DEPSGRAPH
 -              if (!DEG_depsgraph_use_legacy())
 -#endif
 -              {
 -                      /* TODO(sergey): Consider doing partial update only. */
 -                      DAG_relations_tag_update(bmain);
 -              }
 +              /* TODO(sergey): Consider doing partial update only. */
 +              DEG_relations_tag_update(bmain);
        }
  
        return (tot_ik) ? 1 : 0;
  }
  
  
 -/* only called with pose mode active object now */
 -static void createTransPose(TransInfo *t, Object *ob)
 +/**
 + * When objects array is NULL, use 't->data_container' as is.
 + */
 +static void createTransPose(TransInfo *t, Object **objects, uint objects_len)
  {
 +      if (objects != NULL) {
 +              if (t->data_container) {
 +                      MEM_freeN(t->data_container);
 +              }
 +              t->data_container = MEM_callocN(sizeof(*t->data_container) * objects_len, __func__);
 +              t->data_container_len = objects_len;
 +              int th_index;
 +              FOREACH_TRANS_DATA_CONTAINER_INDEX (t, tc, th_index) {
 +                      tc->poseobj = objects[th_index];
 +              }
 +      }
        Main *bmain = CTX_data_main(t->context);
 -      bArmature *arm;
 -      bPoseChannel *pchan;
 -      TransData *td;
 -      TransDataExtension *tdx;
 -      short ik_on = 0;
 -      int i;
  
 -      t->total = 0;
 +      t->data_len_all = 0;
  
 -      /* check validity of state */
 -      arm = BKE_armature_from_object(ob);
 -      if ((arm == NULL) || (ob->pose == NULL)) return;
 +      bool has_translate_rotate_buf[2] = {false, false};
 +      bool *has_translate_rotate = (t->mode == TFM_TRANSLATION) ? has_translate_rotate_buf : NULL;
  
 -      if (arm->flag & ARM_RESTPOS) {
 -              if (ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE) == 0) {
 -                      BKE_report(t->reports, RPT_ERROR, "Cannot change Pose when 'Rest Position' is enabled");
 -                      return;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              Object *ob = tc->poseobj;
 +
 +              bArmature *arm;
 +              short ik_on = 0;
 +
 +              /* check validity of state */
 +              arm = BKE_armature_from_object(tc->poseobj);
 +              if ((arm == NULL) || (ob->pose == NULL)) {
 +                      continue;
 +              }
 +
 +              if (arm->flag & ARM_RESTPOS) {
 +                      if (ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE) == 0) {
 +                              BKE_report(t->reports, RPT_ERROR, "Cannot change Pose when 'Rest Position' is enabled");
 +                              return;
 +                      }
 +              }
 +
 +              /* do we need to add temporal IK chains? */
 +              if ((arm->flag & ARM_AUTO_IK) && t->mode == TFM_TRANSLATION) {
 +                      ik_on = pose_grab_with_ik(bmain, ob);
 +                      if (ik_on) t->flag |= T_AUTOIK;
                }
 +
 +              /* set flags and count total (warning, can change transform to rotate) */
 +              tc->data_len = count_set_pose_transflags(ob, t->mode, t->around, has_translate_rotate);
 +              /* len may be zero, skip next iteration. */
        }
  
 -      /* do we need to add temporal IK chains? */
 -      if ((arm->flag & ARM_AUTO_IK) && t->mode == TFM_TRANSLATION) {
 -              ik_on = pose_grab_with_ik(bmain, ob);
 -              if (ik_on) t->flag |= T_AUTOIK;
 +      /* if there are no translatable bones, do rotation */
 +      if ((t->mode == TFM_TRANSLATION) && !has_translate_rotate[0]) {
 +              if (has_translate_rotate[1]) {
 +                      t->mode = TFM_ROTATION;
 +              }
 +              else {
 +                      t->mode = TFM_RESIZE;
 +              }
        }
  
 -      /* set flags and count total (warning, can change transform to rotate) */
 -      t->total = count_set_pose_transflags(&t->mode, t->around, ob);
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              if (tc->data_len == 0) {
 +                      continue;
 +              }
 +              Object *ob = tc->poseobj;
 +              TransData *td;
 +              TransDataExtension *tdx;
 +              short ik_on = 0;
 +              int i;
  
 -      if (t->total == 0) return;
 +              tc->poseobj = ob; /* we also allow non-active objects to be transformed, in weightpaint */
  
 -      t->flag |= T_POSE;
 -      t->poseobj = ob; /* we also allow non-active objects to be transformed, in weightpaint */
 +              /* init trans data */
 +              td = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransPoseBone");
 +              tdx = tc->data_ext = MEM_callocN(tc->data_len * sizeof(TransDataExtension), "TransPoseBoneExt");
 +              for (i = 0; i < tc->data_len; i++, td++, tdx++) {
 +                      td->ext = tdx;
 +                      td->val = NULL;
 +              }
  
 -      /* disable PET, its not usable in pose mode yet [#32444] */
 -      t->flag &= ~T_PROP_EDIT_ALL;
 +              /* use pose channels to fill trans data */
 +              td = tc->data;
 +              for (bPoseChannel *pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
 +                      if (pchan->bone->flag & BONE_TRANSFORM) {
 +                              add_pose_transdata(t, pchan, ob, tc, td);
 +                              td++;
 +                      }
 +              }
  
 -      /* init trans data */
 -      td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransPoseBone");
 -      tdx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "TransPoseBoneExt");
 -      for (i = 0; i < t->total; i++, td++, tdx++) {
 -              td->ext = tdx;
 -              td->val = NULL;
 -      }
 +              if (td != (tc->data + tc->data_len)) {
 +                      BKE_report(t->reports, RPT_DEBUG, "Bone selection count error");
 +              }
  
 -      /* use pose channels to fill trans data */
 -      td = t->data;
 -      for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
 -              if (pchan->bone->flag & BONE_TRANSFORM) {
 -                      add_pose_transdata(t, pchan, ob, td);
 -                      td++;
 +              /* initialize initial auto=ik chainlen's? */
 +              if (ik_on) {
 +                      transform_autoik_update(t, 0);
                }
        }
  
 -      if (td != (t->data + t->total)) {
 -              BKE_report(t->reports, RPT_DEBUG, "Bone selection count error");
 -      }
 +      t->flag |= T_POSE;
 +      /* disable PET, its not usable in pose mode yet [#32444] */
 +      t->flag &= ~T_PROP_EDIT_ALL;
  
 -      /* initialize initial auto=ik chainlen's? */
 -      if (ik_on) transform_autoik_update(t, 0);
  }
  
 -void restoreBones(TransInfo *t)
 +void restoreBones(TransDataContainer *tc)
  {
 -      bArmature *arm = t->obedit->data;
 -      BoneInitData *bid = t->custom.type.data;
 +      bArmature *arm = tc->obedit->data;
 +      BoneInitData *bid = tc->custom.type.data;
        EditBone *ebo;
  
        while (bid->bone) {
  /* ********************* armature ************** */
  static void createTransArmatureVerts(TransInfo *t)
  {
 -      EditBone *ebo, *eboflip;
 -      bArmature *arm = t->obedit->data;
 -      ListBase *edbo = arm->edbo;
 -      TransData *td, *td_old;
 -      float mtx[3][3], smtx[3][3], bonemat[3][3];
 -      bool mirror = ((arm->flag & ARM_MIRROR_EDIT) != 0);
 -      int total_mirrored = 0, i;
 -      int oldtot;
 -      BoneInitData *bid;
 -
 -      t->total = 0;
 -      for (ebo = edbo->first; ebo; ebo = ebo->next) {
 -              oldtot = t->total;
 -
 -              if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
 -                      if (ELEM(t->mode, TFM_BONESIZE, TFM_BONE_ENVELOPE_DIST)) {
 -                              if (ebo->flag & BONE_SELECTED)
 -                                      t->total++;
 -                      }
 -                      else if (t->mode == TFM_BONE_ROLL) {
 -                              if (ebo->flag & BONE_SELECTED)
 -                                      t->total++;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              EditBone *ebo, *eboflip;
 +              bArmature *arm = tc->obedit->data;
 +              ListBase *edbo = arm->edbo;
 +              TransData *td, *td_old;
 +              float mtx[3][3], smtx[3][3], bonemat[3][3];
 +              bool mirror = ((arm->flag & ARM_MIRROR_EDIT) != 0);
 +              int total_mirrored = 0, i;
 +              int oldtot;
 +              BoneInitData *bid = NULL;
 +
 +              tc->data_len = 0;
 +              for (ebo = edbo->first; ebo; ebo = ebo->next) {
 +                      oldtot = tc->data_len;
 +
 +                      if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
 +                              if (ELEM(t->mode, TFM_BONESIZE, TFM_BONE_ENVELOPE_DIST)) {
 +                                      if (ebo->flag & BONE_SELECTED)
 +                                              tc->data_len++;
 +                              }
 +                              else if (t->mode == TFM_BONE_ROLL) {
 +                                      if (ebo->flag & BONE_SELECTED)
 +                                              tc->data_len++;
 +                              }
 +                              else {
 +                                      if (ebo->flag & BONE_TIPSEL)
 +                                              tc->data_len++;
 +                                      if (ebo->flag & BONE_ROOTSEL)
 +                                              tc->data_len++;
 +                              }
                        }
 -                      else {
 -                              if (ebo->flag & BONE_TIPSEL)
 -                                      t->total++;
 -                              if (ebo->flag & BONE_ROOTSEL)
 -                                      t->total++;
 +
 +                      if (mirror && (oldtot < tc->data_len)) {
 +                              eboflip = ED_armature_ebone_get_mirrored(arm->edbo, ebo);
 +                              if (eboflip)
 +                                      total_mirrored++;
                        }
                }
  
 -              if (mirror && (oldtot < t->total)) {
 -                      eboflip = ED_armature_ebone_get_mirrored(arm->edbo, ebo);
 -                      if (eboflip)
 -                              total_mirrored++;
 +              if (!tc->data_len) {
 +                      continue;
                }
 -      }
  
 -      if (!t->total) return;
 +              transform_around_single_fallback(t);
  
 -      transform_around_single_fallback(t);
 +              copy_m3_m4(mtx, tc->obedit->obmat);
 +              pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
  
 -      copy_m3_m4(mtx, t->obedit->obmat);
 -      pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
 +              td = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransEditBone");
  
 -      td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransEditBone");
 +              if (mirror) {
 +                      tc->custom.type.data = bid = MEM_mallocN((total_mirrored + 1) * sizeof(BoneInitData), "BoneInitData");
 +                      tc->custom.type.use_free = true;
 +              }
  
 -      if (mirror) {
 -              t->custom.type.data = bid = MEM_mallocN((total_mirrored + 1) * sizeof(BoneInitData), "BoneInitData");
 -              t->custom.type.use_free = true;
 -      }
 +              i = 0;
  
 -      i = 0;
 +              for (ebo = edbo->first; ebo; ebo = ebo->next) {
 +                      td_old = td;
 +                      ebo->oldlength = ebo->length;   // length==0.0 on extrude, used for scaling radius of bone points
  
 -      for (ebo = edbo->first; ebo; ebo = ebo->next) {
 -              td_old = td;
 -              ebo->oldlength = ebo->length;   // length==0.0 on extrude, used for scaling radius of bone points
 +                      if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
 +                              if (t->mode == TFM_BONE_ENVELOPE) {
 +                                      if (ebo->flag & BONE_ROOTSEL) {
 +                                              td->val = &ebo->rad_head;
 +                                              td->ival = *td->val;
  
 -              if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
 -                      if (t->mode == TFM_BONE_ENVELOPE) {
 -                              if (ebo->flag & BONE_ROOTSEL) {
 -                                      td->val = &ebo->rad_head;
 -                                      td->ival = *td->val;
 +                                              copy_v3_v3(td->center, ebo->head);
 +                                              td->flag = TD_SELECTED;
  
 -                                      copy_v3_v3(td->center, ebo->head);
 -                                      td->flag = TD_SELECTED;
 +                                              copy_m3_m3(td->smtx, smtx);
 +                                              copy_m3_m3(td->mtx, mtx);
  
 -                                      copy_m3_m3(td->smtx, smtx);
 -                                      copy_m3_m3(td->mtx, mtx);
 +                                              td->loc = NULL;
 +                                              td->ext = NULL;
 +                                              td->ob = tc->obedit;
  
 -                                      td->loc = NULL;
 -                                      td->ext = NULL;
 -                                      td->ob = t->obedit;
 +                                              td++;
 +                                      }
 +                                      if (ebo->flag & BONE_TIPSEL) {
 +                                              td->val = &ebo->rad_tail;
 +                                              td->ival = *td->val;
 +                                              copy_v3_v3(td->center, ebo->tail);
 +                                              td->flag = TD_SELECTED;
  
 -                                      td++;
 -                              }
 -                              if (ebo->flag & BONE_TIPSEL) {
 -                                      td->val = &ebo->rad_tail;
 -                                      td->ival = *td->val;
 -                                      copy_v3_v3(td->center, ebo->tail);
 -                                      td->flag = TD_SELECTED;
 +                                              copy_m3_m3(td->smtx, smtx);
 +                                              copy_m3_m3(td->mtx, mtx);
  
 -                                      copy_m3_m3(td->smtx, smtx);
 -                                      copy_m3_m3(td->mtx, mtx);
 +                                              td->loc = NULL;
 +                                              td->ext = NULL;
 +                                              td->ob = tc->obedit;
  
 -                                      td->loc = NULL;
 -                                      td->ext = NULL;
 -                                      td->ob = t->obedit;
 +                                              td++;
 +                                      }
  
 -                                      td++;
                                }
 +                              else if (ELEM(t->mode, TFM_BONESIZE, TFM_BONE_ENVELOPE_DIST)) {
 +                                      if (ebo->flag & BONE_SELECTED) {
 +                                              if ((t->mode == TFM_BONE_ENVELOPE_DIST) || (arm->drawtype == ARM_ENVELOPE)) {
 +                                                      td->loc = NULL;
 +                                                      td->val = &ebo->dist;
 +                                                      td->ival = ebo->dist;
 +                                              }
 +                                              else {
 +                                                      // abusive storage of scale in the loc pointer :)
 +                                                      td->loc = &ebo->xwidth;
 +                                                      copy_v3_v3(td->iloc, td->loc);
 +                                                      td->val = NULL;
 +                                              }
 +                                              copy_v3_v3(td->center, ebo->head);
 +                                              td->flag = TD_SELECTED;
  
 -                      }
 -                      else if (ELEM(t->mode, TFM_BONESIZE, TFM_BONE_ENVELOPE_DIST)) {
 -                              if (ebo->flag & BONE_SELECTED) {
 -                                      if ((t->mode == TFM_BONE_ENVELOPE_DIST) || (arm->drawtype == ARM_ENVELOPE)) {
 -                                              td->loc = NULL;
 -                                              td->val = &ebo->dist;
 -                                              td->ival = ebo->dist;
 -                                      }
 -                                      else {
 -                                              // abusive storage of scale in the loc pointer :)
 -                                              td->loc = &ebo->xwidth;
 -                                              copy_v3_v3(td->iloc, td->loc);
 -                                              td->val = NULL;
 -                                      }
 -                                      copy_v3_v3(td->center, ebo->head);
 -                                      td->flag = TD_SELECTED;
 -
 -                                      /* use local bone matrix */
 -                                      ED_armature_ebone_to_mat3(ebo, bonemat);
 -                                      mul_m3_m3m3(td->mtx, mtx, bonemat);
 -                                      invert_m3_m3(td->smtx, td->mtx);
 +                                              /* use local bone matrix */
 +                                              ED_armature_ebone_to_mat3(ebo, bonemat);
 +                                              mul_m3_m3m3(td->mtx, mtx, bonemat);
 +                                              invert_m3_m3(td->smtx, td->mtx);
  
 -                                      copy_m3_m3(td->axismtx, td->mtx);
 -                                      normalize_m3(td->axismtx);
 +                                              copy_m3_m3(td->axismtx, td->mtx);
 +                                              normalize_m3(td->axismtx);
  
 -                                      td->ext = NULL;
 -                                      td->ob = t->obedit;
 +                                              td->ext = NULL;
 +                                              td->ob = tc->obedit;
  
 -                                      td++;
 +                                              td++;
 +                                      }
                                }
 -                      }
 -                      else if (t->mode == TFM_BONE_ROLL) {
 -                              if (ebo->flag & BONE_SELECTED) {
 -                                      td->loc = NULL;
 -                                      td->val = &(ebo->roll);
 -                                      td->ival = ebo->roll;
 +                              else if (t->mode == TFM_BONE_ROLL) {
 +                                      if (ebo->flag & BONE_SELECTED) {
 +                                              td->loc = NULL;
 +                                              td->val = &(ebo->roll);
 +                                              td->ival = ebo->roll;
  
 -                                      copy_v3_v3(td->center, ebo->head);
 -                                      td->flag = TD_SELECTED;
 +                                              copy_v3_v3(td->center, ebo->head);
 +                                              td->flag = TD_SELECTED;
  
 -                                      td->ext = NULL;
 -                                      td->ob = t->obedit;
 +                                              td->ext = NULL;
 +                                              td->ob = tc->obedit;
  
 -                                      td++;
 -                              }
 -                      }
 -                      else {
 -                              if (ebo->flag & BONE_TIPSEL) {
 -                                      copy_v3_v3(td->iloc, ebo->tail);
 -
 -                                      /* Don't allow single selected tips to have a modified center,
 -                                       * causes problem with snapping (see T45974).
 -                                       * However, in rotation mode, we want to keep that 'rotate bone around root with
 -                                       * only its tip selected' behavior (see T46325). */
 -                                      if ((t->around == V3D_AROUND_LOCAL_ORIGINS) &&
 -                                          ((t->mode == TFM_ROTATION) || (ebo->flag & BONE_ROOTSEL)))
 -                                      {
 -                                              copy_v3_v3(td->center, ebo->head);
 -                                      }
 -                                      else {
 -                                              copy_v3_v3(td->center, td->iloc);
 +                                              td++;
                                        }
 +                              }
 +                              else {
 +                                      if (ebo->flag & BONE_TIPSEL) {
 +                                              copy_v3_v3(td->iloc, ebo->tail);
 +
 +                                              /* Don't allow single selected tips to have a modified center,
 +                                               * causes problem with snapping (see T45974).
 +                                               * However, in rotation mode, we want to keep that 'rotate bone around root with
 +                                               * only its tip selected' behavior (see T46325). */
 +                                              if ((t->around == V3D_AROUND_LOCAL_ORIGINS) &&
 +                                                  ((t->mode == TFM_ROTATION) || (ebo->flag & BONE_ROOTSEL)))
 +                                              {
 +                                                      copy_v3_v3(td->center, ebo->head);
 +                                              }
 +                                              else {
 +                                                      copy_v3_v3(td->center, td->iloc);
 +                                              }
  
 -                                      td->loc = ebo->tail;
 -                                      td->flag = TD_SELECTED;
 -                                      if (ebo->flag & BONE_EDITMODE_LOCKED)
 -                                              td->protectflag = OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE;
 +                                              td->loc = ebo->tail;
 +                                              td->flag = TD_SELECTED;
 +                                              if (ebo->flag & BONE_EDITMODE_LOCKED)
 +                                                      td->protectflag = OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE;
  
 -                                      copy_m3_m3(td->smtx, smtx);
 -                                      copy_m3_m3(td->mtx, mtx);
 +                                              copy_m3_m3(td->smtx, smtx);
 +                                              copy_m3_m3(td->mtx, mtx);
  
 -                                      ED_armature_ebone_to_mat3(ebo, td->axismtx);
 +                                              ED_armature_ebone_to_mat3(ebo, td->axismtx);
  
 -                                      if ((ebo->flag & BONE_ROOTSEL) == 0) {
 -                                              td->extra = ebo;
 -                                              td->ival = ebo->roll;
 -                                      }
 +                                              if ((ebo->flag & BONE_ROOTSEL) == 0) {
 +                                                      td->extra = ebo;
 +                                                      td->ival = ebo->roll;
 +                                              }
  
 -                                      td->ext = NULL;
 -                                      td->val = NULL;
 -                                      td->ob = t->obedit;
 +                                              td->ext = NULL;
 +                                              td->val = NULL;
 +                                              td->ob = tc->obedit;
  
 -                                      td++;
 -                              }
 -                              if (ebo->flag & BONE_ROOTSEL) {
 -                                      copy_v3_v3(td->iloc, ebo->head);
 -                                      copy_v3_v3(td->center, td->iloc);
 -                                      td->loc = ebo->head;
 -                                      td->flag = TD_SELECTED;
 -                                      if (ebo->flag & BONE_EDITMODE_LOCKED)
 -                                              td->protectflag = OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE;
 +                                              td++;
 +                                      }
 +                                      if (ebo->flag & BONE_ROOTSEL) {
 +                                              copy_v3_v3(td->iloc, ebo->head);
 +                                              copy_v3_v3(td->center, td->iloc);
 +                                              td->loc = ebo->head;
 +                                              td->flag = TD_SELECTED;
 +                                              if (ebo->flag & BONE_EDITMODE_LOCKED)
 +                                                      td->protectflag = OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE;
  
 -                                      copy_m3_m3(td->smtx, smtx);
 -                                      copy_m3_m3(td->mtx, mtx);
 +                                              copy_m3_m3(td->smtx, smtx);
 +                                              copy_m3_m3(td->mtx, mtx);
  
 -                                      ED_armature_ebone_to_mat3(ebo, td->axismtx);
 +                                              ED_armature_ebone_to_mat3(ebo, td->axismtx);
  
 -                                      td->extra = ebo; /* to fix roll */
 -                                      td->ival = ebo->roll;
 +                                              td->extra = ebo; /* to fix roll */
 +                                              td->ival = ebo->roll;
  
 -                                      td->ext = NULL;
 -                                      td->val = NULL;
 -                                      td->ob = t->obedit;
 +                                              td->ext = NULL;
 +                                              td->val = NULL;
 +                                              td->ob = tc->obedit;
  
 -                                      td++;
 +                                              td++;
 +                                      }
                                }
                        }
 -              }
  
 -              if (mirror && (td_old != td)) {
 -                      eboflip = ED_armature_ebone_get_mirrored(arm->edbo, ebo);
 -                      if (eboflip) {
 -                              bid[i].bone = eboflip;
 -                              bid[i].dist = eboflip->dist;
 -                              bid[i].rad_tail = eboflip->rad_tail;
 -                              bid[i].roll = eboflip->roll;
 -                              bid[i].xwidth = eboflip->xwidth;
 -                              bid[i].zwidth = eboflip->zwidth;
 -                              copy_v3_v3(bid[i].head, eboflip->head);
 -                              copy_v3_v3(bid[i].tail, eboflip->tail);
 -                              i++;
 +                      if (mirror && (td_old != td)) {
 +                              eboflip = ED_armature_ebone_get_mirrored(arm->edbo, ebo);
 +                              if (eboflip) {
 +                                      bid[i].bone = eboflip;
 +                                      bid[i].dist = eboflip->dist;
 +                                      bid[i].rad_tail = eboflip->rad_tail;
 +                                      bid[i].roll = eboflip->roll;
 +                                      bid[i].xwidth = eboflip->xwidth;
 +                                      bid[i].zwidth = eboflip->zwidth;
 +                                      copy_v3_v3(bid[i].head, eboflip->head);
 +                                      copy_v3_v3(bid[i].tail, eboflip->tail);
 +                                      i++;
 +                              }
                        }
                }
 -      }
  
 -      if (mirror) {
 -              /* trick to terminate iteration */
 -              bid[total_mirrored].bone = NULL;
 +              if (mirror) {
 +                      /* trick to terminate iteration */
 +                      bid[total_mirrored].bone = NULL;
 +              }
        }
  }
  
  
  static void createTransMBallVerts(TransInfo *t)
  {
 -      MetaBall *mb = (MetaBall *)t->obedit->data;
 -      MetaElem *ml;
 -      TransData *td;
 -      TransDataExtension *tx;
 -      float mtx[3][3], smtx[3][3];
 -      int count = 0, countsel = 0;
 -      const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 -
 -      /* count totals */
 -      for (ml = mb->editelems->first; ml; ml = ml->next) {
 -              if (ml->flag & SELECT) countsel++;
 -              if (is_prop_edit) count++;
 -      }
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              MetaBall *mb = (MetaBall *)tc->obedit->data;
 +              MetaElem *ml;
 +              TransData *td;
 +              TransDataExtension *tx;
 +              float mtx[3][3], smtx[3][3];
 +              int count = 0, countsel = 0;
 +              const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 +
 +              /* count totals */
 +              for (ml = mb->editelems->first; ml; ml = ml->next) {
 +                      if (ml->flag & SELECT) countsel++;
 +                      if (is_prop_edit) count++;
 +              }
  
 -      /* note: in prop mode we need at least 1 selected */
 -      if (countsel == 0) return;
 +              /* note: in prop mode we need at least 1 selected */
 +              if (countsel == 0) {
 +                      continue;
 +              }
  
 -      if (is_prop_edit) t->total = count;
 -      else t->total = countsel;
 +              if (is_prop_edit) tc->data_len = count;
 +              else tc->data_len = countsel;
  
 -      td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(MBall EditMode)");
 -      tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "MetaElement_TransExtension");
 +              td = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransObData(MBall EditMode)");
 +              tx = tc->data_ext = MEM_callocN(tc->data_len * sizeof(TransDataExtension), "MetaElement_TransExtension");
  
 -      copy_m3_m4(mtx, t->obedit->obmat);
 -      pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
 +              copy_m3_m4(mtx, tc->obedit->obmat);
 +              pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
  
 -      for (ml = mb->editelems->first; ml; ml = ml->next) {
 -              if (is_prop_edit || (ml->flag & SELECT)) {
 -                      td->loc = &ml->x;
 -                      copy_v3_v3(td->iloc, td->loc);
 -                      copy_v3_v3(td->center, td->loc);
 +              for (ml = mb->editelems->first; ml; ml = ml->next) {
 +                      if (is_prop_edit || (ml->flag & SELECT)) {
 +                              td->loc = &ml->x;
 +                              copy_v3_v3(td->iloc, td->loc);
 +                              copy_v3_v3(td->center, td->loc);
  
 -                      quat_to_mat3(td->axismtx, ml->quat);
 +                              quat_to_mat3(td->axismtx, ml->quat);
  
 -                      if (ml->flag & SELECT) td->flag = TD_SELECTED | TD_USEQUAT | TD_SINGLESIZE;
 -                      else td->flag = TD_USEQUAT;
 +                              if (ml->flag & SELECT) td->flag = TD_SELECTED | TD_USEQUAT | TD_SINGLESIZE;
 +                              else td->flag = TD_USEQUAT;
  
 -                      copy_m3_m3(td->smtx, smtx);
 -                      copy_m3_m3(td->mtx, mtx);
 +                              copy_m3_m3(td->smtx, smtx);
 +                              copy_m3_m3(td->mtx, mtx);
  
 -                      td->ext = tx;
 +                              td->ext = tx;
  
 -                      /* Radius of MetaElem (mass of MetaElem influence) */
 -                      if (ml->flag & MB_SCALE_RAD) {
 -                              td->val = &ml->rad;
 -                              td->ival = ml->rad;
 -                      }
 -                      else {
 -                              td->val = &ml->s;
 -                              td->ival = ml->s;
 -                      }
 +                              /* Radius of MetaElem (mass of MetaElem influence) */
 +                              if (ml->flag & MB_SCALE_RAD) {
 +                                      td->val = &ml->rad;
 +                                      td->ival = ml->rad;
 +                              }
 +                              else {
 +                                      td->val = &ml->s;
 +                                      td->ival = ml->s;
 +                              }
  
 -                      /* expx/expy/expz determine "shape" of some MetaElem types */
 -                      tx->size = &ml->expx;
 -                      tx->isize[0] = ml->expx;
 -                      tx->isize[1] = ml->expy;
 -                      tx->isize[2] = ml->expz;
 +                              /* expx/expy/expz determine "shape" of some MetaElem types */
 +                              tx->size = &ml->expx;
 +                              tx->isize[0] = ml->expx;
 +                              tx->isize[1] = ml->expy;
 +                              tx->isize[2] = ml->expz;
  
 -                      /* quat is used for rotation of MetaElem */
 -                      tx->quat = ml->quat;
 -                      copy_qt_qt(tx->iquat, ml->quat);
 +                              /* quat is used for rotation of MetaElem */
 +                              tx->quat = ml->quat;
 +                              copy_qt_qt(tx->iquat, ml->quat);
  
 -                      tx->rot = NULL;
 +                              tx->rot = NULL;
  
 -                      td++;
 -                      tx++;
 +                              td++;
 +                              tx++;
 +                      }
                }
        }
  }
@@@ -1655,277 -1572,271 +1655,277 @@@ static int bezt_select_to_transform_tri
  
  static void createTransCurveVerts(TransInfo *t)
  {
 -      Curve *cu = t->obedit->data;
 -      TransData *td = NULL;
 -      Nurb *nu;
 -      BezTriple *bezt;
 -      BPoint *bp;
 -      float mtx[3][3], smtx[3][3];
 -      int a;
 -      int count = 0, countsel = 0;
 -      const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 -      short hide_handles = (cu->drawflag & CU_HIDE_HANDLES);
 -      ListBase *nurbs;
 -
 -      /* to be sure */
 -      if (cu->editnurb == NULL) return;
  
  #define SEL_F1 (1 << 0)
  #define SEL_F2 (1 << 1)
  #define SEL_F3 (1 << 2)
  
 -      /* count total of vertices, check identical as in 2nd loop for making transdata! */
 -      nurbs = BKE_curve_editNurbs_get(cu);
 -      for (nu = nurbs->first; nu; nu = nu->next) {
 -              if (nu->type == CU_BEZIER) {
 -                      for (a = 0, bezt = nu->bezt; a < nu->pntsu; a++, bezt++) {
 -                              if (bezt->hide == 0) {
 -                                      const int bezt_tx = bezt_select_to_transform_triple_flag(bezt, hide_handles);
 -                                      if (bezt_tx & SEL_F1) { countsel++; }
 -                                      if (bezt_tx & SEL_F2) { countsel++; }
 -                                      if (bezt_tx & SEL_F3) { countsel++; }
 -                                      if (is_prop_edit) count += 3;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
  
 +              Curve *cu = tc->obedit->data;
 +              TransData *td = NULL;
 +              Nurb *nu;
 +              BezTriple *bezt;
 +              BPoint *bp;
 +              float mtx[3][3], smtx[3][3];
 +              int a;
 +              int count = 0, countsel = 0;
 +              const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 +              short hide_handles = (cu->drawflag & CU_HIDE_HANDLES);
 +              ListBase *nurbs;
 +
 +              /* to be sure */
 +              if (cu->editnurb == NULL) return;
 +
 +              /* count total of vertices, check identical as in 2nd loop for making transdata! */
 +              nurbs = BKE_curve_editNurbs_get(cu);
 +              for (nu = nurbs->first; nu; nu = nu->next) {
 +                      if (nu->type == CU_BEZIER) {
 +                              for (a = 0, bezt = nu->bezt; a < nu->pntsu; a++, bezt++) {
 +                                      if (bezt->hide == 0) {
 +                                              const int bezt_tx = bezt_select_to_transform_triple_flag(bezt, hide_handles);
 +                                              if (bezt_tx & SEL_F1) { countsel++; }
 +                                              if (bezt_tx & SEL_F2) { countsel++; }
 +                                              if (bezt_tx & SEL_F3) { countsel++; }
 +                                              if (is_prop_edit) count += 3;
 +
 +                                      }
                                }
                        }
 -              }
 -              else {
 -                      for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a > 0; a--, bp++) {
 -                              if (bp->hide == 0) {
 -                                      if (is_prop_edit) count++;
 -                                      if (bp->f1 & SELECT) countsel++;
 +                      else {
 +                              for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a > 0; a--, bp++) {
 +                                      if (bp->hide == 0) {
 +                                              if (is_prop_edit) count++;
 +                                              if (bp->f1 & SELECT) countsel++;
 +                                      }
                                }
                        }
                }
 -      }
 -      /* note: in prop mode we need at least 1 selected */
 -      if (countsel == 0) return;
 +              /* note: in prop mode we need at least 1 selected */
 +              if (countsel == 0) {
 +                      tc->data_len = 0;
 +                      continue;
 +              }
  
 -      if (is_prop_edit) t->total = count;
 -      else t->total = countsel;
 -      t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Curve EditMode)");
 +              if (is_prop_edit) tc->data_len = count;
 +              else tc->data_len = countsel;
 +              tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransObData(Curve EditMode)");
  
 -      transform_around_single_fallback(t);
 +              transform_around_single_fallback(t);
  
 -      copy_m3_m4(mtx, t->obedit->obmat);
 -      pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
 +              copy_m3_m4(mtx, tc->obedit->obmat);
 +              pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
  
 -      td = t->data;
 -      for (nu = nurbs->first; nu; nu = nu->next) {
 -              if (nu->type == CU_BEZIER) {
 -                      TransData *head, *tail;
 -                      head = tail = td;
 -                      for (a = 0, bezt = nu->bezt; a < nu->pntsu; a++, bezt++) {
 -                              if (bezt->hide == 0) {
 -                                      TransDataCurveHandleFlags *hdata = NULL;
 -                                      float axismtx[3][3];
 +              td = tc->data;
 +              for (nu = nurbs->first; nu; nu = nu->next) {
 +                      if (nu->type == CU_BEZIER) {
 +                              TransData *head, *tail;
 +                              head = tail = td;
 +                              for (a = 0, bezt = nu->bezt; a < nu->pntsu; a++, bezt++) {
 +                                      if (bezt->hide == 0) {
 +                                              TransDataCurveHandleFlags *hdata = NULL;
 +                                              float axismtx[3][3];
  
 -                                      if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 -                                              float normal[3], plane[3];
 +                                              if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 +                                                      float normal[3], plane[3];
  
 -                                              BKE_nurb_bezt_calc_normal(nu, bezt, normal);
 -                                              BKE_nurb_bezt_calc_plane(nu, bezt, plane);
 +                                                      BKE_nurb_bezt_calc_normal(nu, bezt, normal);
 +                                                      BKE_nurb_bezt_calc_plane(nu, bezt, plane);
  
 -                                              if (createSpaceNormalTangent(axismtx, normal, plane)) {
 -                                                      /* pass */
 -                                              }
 -                                              else {
 -                                                      normalize_v3(normal);
 -                                                      axis_dominant_v3_to_m3(axismtx, normal);
 -                                                      invert_m3(axismtx);
 +                                                      if (createSpaceNormalTangent(axismtx, normal, plane)) {
 +                                                              /* pass */
 +                                                      }
 +                                                      else {
 +                                                              normalize_v3(normal);
 +                                                              axis_dominant_v3_to_m3(axismtx, normal);
 +                                                              invert_m3(axismtx);
 +                                                      }
                                                }
 -                                      }
  
 -                                      /* Elements that will be transform (not always a match to selection). */
 -                                      const int bezt_tx = bezt_select_to_transform_triple_flag(bezt, hide_handles);
 +                                              /* Elements that will be transform (not always a match to selection). */
 +                                              const int bezt_tx = bezt_select_to_transform_triple_flag(bezt, hide_handles);
 +
 +                                              if (is_prop_edit || bezt_tx & SEL_F1) {
 +                                                      copy_v3_v3(td->iloc, bezt->vec[0]);
 +                                                      td->loc = bezt->vec[0];
 +                                                      copy_v3_v3(td->center, bezt->vec[(hide_handles ||
 +                                                                                        (t->around == V3D_AROUND_LOCAL_ORIGINS) ||
 +                                                                                        (bezt->f2 & SELECT)) ? 1 : 0]);
 +                                                      if (hide_handles) {
 +                                                              if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
 +                                                              else td->flag = 0;
 +                                                      }
 +                                                      else {
 +                                                              if (bezt->f1 & SELECT) td->flag = TD_SELECTED;
 +                                                              else td->flag = 0;
 +                                                      }
 +                                                      td->ext = NULL;
 +                                                      td->val = NULL;
  
 -                                      if (is_prop_edit || bezt_tx & SEL_F1) {
 -                                              copy_v3_v3(td->iloc, bezt->vec[0]);
 -                                              td->loc = bezt->vec[0];
 -                                              copy_v3_v3(td->center, bezt->vec[(hide_handles ||
 -                                                                                (t->around == V3D_AROUND_LOCAL_ORIGINS) ||
 -                                                                                (bezt->f2 & SELECT)) ? 1 : 0]);
 -                                              if (hide_handles) {
 -                                                      if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
 -                                                      else td->flag = 0;
 -                                              }
 -                                              else {
 -                                                      if (bezt->f1 & SELECT) td->flag = TD_SELECTED;
 -                                                      else td->flag = 0;
 -                                              }
 -                                              td->ext = NULL;
 -                                              td->val = NULL;
 +                                                      hdata = initTransDataCurveHandles(td, bezt);
  
 -                                              hdata = initTransDataCurveHandles(td, bezt);
 +                                                      copy_m3_m3(td->smtx, smtx);
 +                                                      copy_m3_m3(td->mtx, mtx);
 +                                                      if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 +                                                              copy_m3_m3(td->axismtx, axismtx);
 +                                                      }
  
 -                                              copy_m3_m3(td->smtx, smtx);
 -                                              copy_m3_m3(td->mtx, mtx);
 -                                              if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 -                                                      copy_m3_m3(td->axismtx, axismtx);
 +                                                      td++;
 +                                                      count++;
 +                                                      tail++;
                                                }
  
 -                                              td++;
 -                                              count++;
 -                                              tail++;
 -                                      }
 +                                              /* This is the Curve Point, the other two are handles */
 +                                              if (is_prop_edit || bezt_tx & SEL_F2) {
 +                                                      copy_v3_v3(td->iloc, bezt->vec[1]);
 +                                                      td->loc = bezt->vec[1];
 +                                                      copy_v3_v3(td->center, td->loc);
 +                                                      if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
 +                                                      else td->flag = 0;
 +                                                      td->ext = NULL;
  
 -                                      /* This is the Curve Point, the other two are handles */
 -                                      if (is_prop_edit || bezt_tx & SEL_F2) {
 -                                              copy_v3_v3(td->iloc, bezt->vec[1]);
 -                                              td->loc = bezt->vec[1];
 -                                              copy_v3_v3(td->center, td->loc);
 -                                              if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
 -                                              else td->flag = 0;
 -                                              td->ext = NULL;
 +                                                      if (t->mode == TFM_CURVE_SHRINKFATTEN) { /* || t->mode==TFM_RESIZE) {*/ /* TODO - make points scale */
 +                                                              td->val = &(bezt->radius);
 +                                                              td->ival = bezt->radius;
 +                                                      }
 +                                                      else if (t->mode == TFM_TILT) {
 +                                                              td->val = &(bezt->alfa);
 +                                                              td->ival = bezt->alfa;
 +                                                      }
 +                                                      else {
 +                                                              td->val = NULL;
 +                                                      }
  
 -                                              if (t->mode == TFM_CURVE_SHRINKFATTEN) { /* || t->mode==TFM_RESIZE) {*/ /* TODO - make points scale */
 -                                                      td->val = &(bezt->radius);
 -                                                      td->ival = bezt->radius;
 -                                              }
 -                                              else if (t->mode == TFM_TILT) {
 -                                                      td->val = &(bezt->alfa);
 -                                                      td->ival = bezt->alfa;
 -                                              }
 -                                              else {
 -                                                      td->val = NULL;
 -                                              }
 +                                                      copy_m3_m3(td->smtx, smtx);
 +                                                      copy_m3_m3(td->mtx, mtx);
 +                                                      if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 +                                                              copy_m3_m3(td->axismtx, axismtx);
 +                                                      }
  
 -                                              copy_m3_m3(td->smtx, smtx);
 -                                              copy_m3_m3(td->mtx, mtx);
 -                                              if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 -                                                      copy_m3_m3(td->axismtx, axismtx);
 +                                                      if ((bezt_tx & SEL_F1) == 0 && (bezt_tx & SEL_F3) == 0)
 +                                                              /* If the middle is selected but the sides arnt, this is needed */
 +                                                              if (hdata == NULL) { /* if the handle was not saved by the previous handle */
 +                                                                      hdata = initTransDataCurveHandles(td, bezt);
 +                                                              }
 +
 +                                                      td++;
 +                                                      count++;
 +                                                      tail++;
                                                }
 +                                              if (is_prop_edit || bezt_tx & SEL_F3) {
 +                                                      copy_v3_v3(td->iloc, bezt->vec[2]);
 +                                                      td->loc = bezt->vec[2];
 +                                                      copy_v3_v3(td->center, bezt->vec[(hide_handles ||
 +                                                                                        (t->around == V3D_AROUND_LOCAL_ORIGINS) ||
 +                                                                                        (bezt->f2 & SELECT)) ? 1 : 2]);
 +                                                      if (hide_handles) {
 +                                                              if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
 +                                                              else td->flag = 0;
 +                                                      }
 +                                                      else {
 +                                                              if (bezt->f3 & SELECT) td->flag = TD_SELECTED;
 +                                                              else td->flag = 0;
 +                                                      }
 +                                                      td->ext = NULL;
 +                                                      td->val = NULL;
  
 -                                              if ((bezt_tx & SEL_F1) == 0 && (bezt_tx & SEL_F3) == 0)
 -                                                      /* If the middle is selected but the sides arnt, this is needed */
                                                        if (hdata == NULL) { /* if the handle was not saved by the previous handle */
                                                                hdata = initTransDataCurveHandles(td, bezt);
                                                        }
  
 -                                              td++;
 -                                              count++;
 -                                              tail++;
 -                                      }
 -                                      if (is_prop_edit || bezt_tx & SEL_F3) {
 -                                              copy_v3_v3(td->iloc, bezt->vec[2]);
 -                                              td->loc = bezt->vec[2];
 -                                              copy_v3_v3(td->center, bezt->vec[(hide_handles ||
 -                                                                                (t->around == V3D_AROUND_LOCAL_ORIGINS) ||
 -                                                                                (bezt->f2 & SELECT)) ? 1 : 2]);
 -                                              if (hide_handles) {
 -                                                      if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
 -                                                      else td->flag = 0;
 -                                              }
 -                                              else {
 -                                                      if (bezt->f3 & SELECT) td->flag = TD_SELECTED;
 -                                                      else td->flag = 0;
 -                                              }
 -                                              td->ext = NULL;
 -                                              td->val = NULL;
 -
 -                                              if (hdata == NULL) { /* if the handle was not saved by the previous handle */
 -                                                      hdata = initTransDataCurveHandles(td, bezt);
 -                                              }
 +                                                      copy_m3_m3(td->smtx, smtx);
 +                                                      copy_m3_m3(td->mtx, mtx);
 +                                                      if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 +                                                              copy_m3_m3(td->axismtx, axismtx);
 +                                                      }
  
 -                                              copy_m3_m3(td->smtx, smtx);
 -                                              copy_m3_m3(td->mtx, mtx);
 -                                              if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 -                                                      copy_m3_m3(td->axismtx, axismtx);
 +                                                      td++;
 +                                                      count++;
 +                                                      tail++;
                                                }
  
 -                                              td++;
 -                                              count++;
 -                                              tail++;
 +                                              (void)hdata;  /* quiet warning */
 +                                      }
 +                                      else if (is_prop_edit && head != tail) {
 +                                              calc_distanceCurveVerts(head, tail - 1);
 +                                              head = tail;
                                        }
 -
 -                                      (void)hdata;  /* quiet warning */
                                }
 -                              else if (is_prop_edit && head != tail) {
 +                              if (is_prop_edit && head != tail)
                                        calc_distanceCurveVerts(head, tail - 1);
 -                                      head = tail;
 -                              }
 -                      }
 -                      if (is_prop_edit && head != tail)
 -                              calc_distanceCurveVerts(head, tail - 1);
  
 -                      /* TODO - in the case of tilt and radius we can also avoid allocating the initTransDataCurveHandles
 -                       * but for now just don't change handle types */
 -                      if (ELEM(t->mode, TFM_CURVE_SHRINKFATTEN, TFM_TILT, TFM_DUMMY) == 0) {
 -                              /* sets the handles based on their selection, do this after the data is copied to the TransData */
 -                              BKE_nurb_handles_test(nu, !hide_handles);
 +                              /* TODO - in the case of tilt and radius we can also avoid allocating the initTransDataCurveHandles
 +                               * but for now just don't change handle types */
 +                              if (ELEM(t->mode, TFM_CURVE_SHRINKFATTEN, TFM_TILT, TFM_DUMMY) == 0) {
 +                                      /* sets the handles based on their selection, do this after the data is copied to the TransData */
 +                                      BKE_nurb_handles_test(nu, !hide_handles);
 +                              }
                        }
 -              }
 -              else {
 -                      TransData *head, *tail;
 -                      head = tail = td;
 -                      for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a > 0; a--, bp++) {
 -                              if (bp->hide == 0) {
 -                                      if (is_prop_edit || (bp->f1 & SELECT)) {
 -                                              float axismtx[3][3];
 -
 -                                              if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 -                                                      if (nu->pntsv == 1) {
 -                                                              float normal[3], plane[3];
 -
 -                                                              BKE_nurb_bpoint_calc_normal(nu, bp, normal);
 -                                                              BKE_nurb_bpoint_calc_plane(nu, bp, plane);
 -
 -                                                              if (createSpaceNormalTangent(axismtx, normal, plane)) {
 -                                                                      /* pass */
 -                                                              }
 -                                                              else {
 -                                                                      normalize_v3(normal);
 -                                                                      axis_dominant_v3_to_m3(axismtx, normal);
 -                                                                      invert_m3(axismtx);
 +                      else {
 +                              TransData *head, *tail;
 +                              head = tail = td;
 +                              for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a > 0; a--, bp++) {
 +                                      if (bp->hide == 0) {
 +                                              if (is_prop_edit || (bp->f1 & SELECT)) {
 +                                                      float axismtx[3][3];
 +
 +                                                      if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 +                                                              if (nu->pntsv == 1) {
 +                                                                      float normal[3], plane[3];
 +
 +                                                                      BKE_nurb_bpoint_calc_normal(nu, bp, normal);
 +                                                                      BKE_nurb_bpoint_calc_plane(nu, bp, plane);
 +
 +                                                                      if (createSpaceNormalTangent(axismtx, normal, plane)) {
 +                                                                              /* pass */
 +                                                                      }
 +                                                                      else {
 +                                                                              normalize_v3(normal);
 +                                                                              axis_dominant_v3_to_m3(axismtx, normal);
 +                                                                              invert_m3(axismtx);
 +                                                                      }
                                                                }
                                                        }
 -                                              }
  
 -                                              copy_v3_v3(td->iloc, bp->vec);
 -                                              td->loc = bp->vec;
 -                                              copy_v3_v3(td->center, td->loc);
 -                                              if (bp->f1 & SELECT) td->flag = TD_SELECTED;
 -                                              else td->flag = 0;
 -                                              td->ext = NULL;
 +                                                      copy_v3_v3(td->iloc, bp->vec);
 +                                                      td->loc = bp->vec;
 +                                                      copy_v3_v3(td->center, td->loc);
 +                                                      if (bp->f1 & SELECT) td->flag = TD_SELECTED;
 +                                                      else td->flag = 0;
 +                                                      td->ext = NULL;
  
 -                                              if (t->mode == TFM_CURVE_SHRINKFATTEN || t->mode == TFM_RESIZE) {
 -                                                      td->val = &(bp->radius);
 -                                                      td->ival = bp->radius;
 -                                              }
 -                                              else {
 -                                                      td->val = &(bp->alfa);
 -                                                      td->ival = bp->alfa;
 -                                              }
 +                                                      if (t->mode == TFM_CURVE_SHRINKFATTEN || t->mode == TFM_RESIZE) {
 +                                                              td->val = &(bp->radius);
 +                                                              td->ival = bp->radius;
 +                                                      }
 +                                                      else {
 +                                                              td->val = &(bp->alfa);
 +                                                              td->ival = bp->alfa;
 +                                                      }
  
 -                                              copy_m3_m3(td->smtx, smtx);
 -                                              copy_m3_m3(td->mtx, mtx);
 -                                              if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 -                                                      if (nu->pntsv == 1) {
 -                                                              copy_m3_m3(td->axismtx, axismtx);
 +                                                      copy_m3_m3(td->smtx, smtx);
 +                                                      copy_m3_m3(td->mtx, mtx);
 +                                                      if (t->around == V3D_AROUND_LOCAL_ORIGINS) {
 +                                                              if (nu->pntsv == 1) {
 +                                                                      copy_m3_m3(td->axismtx, axismtx);
 +                                                              }
                                                        }
 -                                              }
  
 -                                              td++;
 -                                              count++;
 -                                              tail++;
 +                                                      td++;
 +                                                      count++;
 +                                                      tail++;
 +                                              }
 +                                      }
 +                                      else if (is_prop_edit && head != tail) {
 +                                              calc_distanceCurveVerts(head, tail - 1);
 +                                              head = tail;
                                        }
                                }
 -                              else if (is_prop_edit && head != tail) {
 +                              if (is_prop_edit && head != tail)
                                        calc_distanceCurveVerts(head, tail - 1);
 -                                      head = tail;
 -                              }
                        }
 -                      if (is_prop_edit && head != tail)
 -                              calc_distanceCurveVerts(head, tail - 1);
                }
        }
 -
  #undef SEL_F1
  #undef SEL_F2
  #undef SEL_F3
  
  static void createTransLatticeVerts(TransInfo *t)
  {
 -      Lattice *latt = ((Lattice *)t->obedit->data)->editlatt->latt;
 -      TransData *td = NULL;
 -      BPoint *bp;
 -      float mtx[3][3], smtx[3][3];
 -      int a;
 -      int count = 0, countsel = 0;
 -      const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
  
 -      bp = latt->def;
 -      a  = latt->pntsu * latt->pntsv * latt->pntsw;
 -      while (a--) {
 -              if (bp->hide == 0) {
 -                      if (bp->f1 & SELECT) countsel++;
 -                      if (is_prop_edit) count++;
 +              Lattice *latt = ((Lattice *)tc->obedit->data)->editlatt->latt;
 +              TransData *td = NULL;
 +              BPoint *bp;
 +              float mtx[3][3], smtx[3][3];
 +              int a;
 +              int count = 0, countsel = 0;
 +              const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 +
 +              bp = latt->def;
 +              a  = latt->pntsu * latt->pntsv * latt->pntsw;
 +              while (a--) {
 +                      if (bp->hide == 0) {
 +                              if (bp->f1 & SELECT) countsel++;
 +                              if (is_prop_edit) count++;
 +                      }
 +                      bp++;
                }
 -              bp++;
 -      }
  
 -      /* note: in prop mode we need at least 1 selected */
 -      if (countsel == 0) return;
 +              /* note: in prop mode we need at least 1 selected */
 +              if (countsel == 0) return;
  
 -      if (is_prop_edit) t->total = count;
 -      else t->total = countsel;
 -      t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Lattice EditMode)");
 +              if (is_prop_edit) tc->data_len = count;
 +              else tc->data_len = countsel;
 +              tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransObData(Lattice EditMode)");
  
 -      copy_m3_m4(mtx, t->obedit->obmat);
 -      pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
 +              copy_m3_m4(mtx, tc->obedit->obmat);
 +              pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
  
 -      td = t->data;
 -      bp = latt->def;
 -      a  = latt->pntsu * latt->pntsv * latt->pntsw;
 -      while (a--) {
 -              if (is_prop_edit || (bp->f1 & SELECT)) {
 -                      if (bp->hide == 0) {
 -                              copy_v3_v3(td->iloc, bp->vec);
 -                              td->loc = bp->vec;
 -                              copy_v3_v3(td->center, td->loc);
 -                              if (bp->f1 & SELECT) {
 -                                      td->flag = TD_SELECTED;
 -                              }
 -                              else {
 -                                      td->flag = 0;
 -                              }
 -                              copy_m3_m3(td->smtx, smtx);
 -                              copy_m3_m3(td->mtx, mtx);
 +              td = tc->data;
 +              bp = latt->def;
 +              a  = latt->pntsu * latt->pntsv * latt->pntsw;
 +              while (a--) {
 +                      if (is_prop_edit || (bp->f1 & SELECT)) {
 +                              if (bp->hide == 0) {
 +                                      copy_v3_v3(td->iloc, bp->vec);
 +                                      td->loc = bp->vec;
 +                                      copy_v3_v3(td->center, td->loc);
 +                                      if (bp->f1 & SELECT) {
 +                                              td->flag = TD_SELECTED;
 +                                      }
 +                                      else {
 +                                              td->flag = 0;
 +                                      }
 +                                      copy_m3_m3(td->smtx, smtx);
 +                                      copy_m3_m3(td->mtx, mtx);
  
 -                              td->ext = NULL;
 -                              td->val = NULL;
 +                                      td->ext = NULL;
 +                                      td->val = NULL;
  
 -                              td++;
 -                              count++;
 +                                      td++;
 +                                      count++;
 +                              }
                        }
 +                      bp++;
                }
 -              bp++;
        }
  }
  
  /* ******************* particle edit **************** */
  static void createTransParticleVerts(bContext *C, TransInfo *t)
  {
 -      TransData *td = NULL;
 -      TransDataExtension *tx;
 -      Main *bmain = CTX_data_main(C);
 -      Object *ob = CTX_data_active_object(C);
 -      ParticleEditSettings *pset = PE_settings(t->scene);
 -      PTCacheEdit *edit = PE_get_current(bmain, t->scene, ob);
 -      ParticleSystem *psys = NULL;
 -      ParticleSystemModifierData *psmd = NULL;
 -      PTCacheEditPoint *point;
 -      PTCacheEditKey *key;
 -      float mat[4][4];
 -      int i, k, transformparticle;
 -      int count = 0, hasselected = 0;
 -      const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 -
 -      if (edit == NULL || t->settings->particle.selectmode == SCE_SELECT_PATH) return;
 -
 -      psys = edit->psys;
 -
 -      if (psys)
 -              psmd = psys_get_modifier(ob, psys);
 -
 -      for (i = 0, point = edit->points; i < edit->totpoint; i++, point++) {
 -              point->flag &= ~PEP_TRANSFORM;
 -              transformparticle = 0;
 -
 -              if ((point->flag & PEP_HIDE) == 0) {
 -                      for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
 -                              if ((key->flag & PEK_HIDE) == 0) {
 -                                      if (key->flag & PEK_SELECT) {
 -                                              hasselected = 1;
 -                                              transformparticle = 1;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +
 +              TransData *td = NULL;
 +              TransDataExtension *tx;
 +              Object *ob = CTX_data_active_object(C);
 +              ParticleEditSettings *pset = PE_settings(t->scene);
 +              PTCacheEdit *edit = PE_get_current(t->scene, ob);
 +              ParticleSystem *psys = NULL;
 +              ParticleSystemModifierData *psmd = NULL;
 +              PTCacheEditPoint *point;
 +              PTCacheEditKey *key;
 +              float mat[4][4];
 +              int i, k, transformparticle;
 +              int count = 0, hasselected = 0;
 +              const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 +
 +              if (edit == NULL || t->settings->particle.selectmode == SCE_SELECT_PATH) return;
 +
 +              psys = edit->psys;
 +
 +              if (psys)
 +                      psmd = psys_get_modifier(ob, psys);
 +
 +              for (i = 0, point = edit->points; i < edit->totpoint; i++, point++) {
 +                      point->flag &= ~PEP_TRANSFORM;
 +                      transformparticle = 0;
 +
 +                      if ((point->flag & PEP_HIDE) == 0) {
 +                              for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
 +                                      if ((key->flag & PEK_HIDE) == 0) {
 +                                              if (key->flag & PEK_SELECT) {
 +                                                      hasselected = 1;
 +                                                      transformparticle = 1;
 +                                              }
 +                                              else if (is_prop_edit)
 +                                                      transformparticle = 1;
                                        }
 -                                      else if (is_prop_edit)
 -                                              transformparticle = 1;
                                }
                        }
 -              }
  
 -              if (transformparticle) {
 -                      count += point->totkey;
 -                      point->flag |= PEP_TRANSFORM;
 +                      if (transformparticle) {
 +                              count += point->totkey;
 +                              point->flag |= PEP_TRANSFORM;
 +                      }
                }
 -      }
  
 -      /* note: in prop mode we need at least 1 selected */
 -      if (hasselected == 0) return;
 +              /* note: in prop mode we need at least 1 selected */
 +              if (hasselected == 0) return;
  
 -      t->total = count;
 -      td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Particle Mode)");
 +              tc->data_len = count;
 +              td = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransObData(Particle Mode)");
  
 -      if (t->mode == TFM_BAKE_TIME)
 -              tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "Particle_TransExtension");
 -      else
 -              tx = t->ext = NULL;
 +              if (t->mode == TFM_BAKE_TIME)
 +                      tx = tc->data_ext = MEM_callocN(tc->data_len * sizeof(TransDataExtension), "Particle_TransExtension");
 +              else
 +                      tx = tc->data_ext = NULL;
  
 -      unit_m4(mat);
 +              unit_m4(mat);
  
 -      invert_m4_m4(ob->imat, ob->obmat);
 +              invert_m4_m4(ob->imat, ob->obmat);
  
 -      for (i = 0, point = edit->points; i < edit->totpoint; i++, point++) {
 -              TransData *head, *tail;
 -              head = tail = td;
 +              for (i = 0, point = edit->points; i < edit->totpoint; i++, point++) {
 +                      TransData *head, *tail;
 +                      head = tail = td;
  
 -              if (!(point->flag & PEP_TRANSFORM)) continue;
 +                      if (!(point->flag & PEP_TRANSFORM)) continue;
  
 -              if (psys && !(psys->flag & PSYS_GLOBAL_HAIR))
 -                      psys_mat_hair_to_global(ob, psmd->dm_final, psys->part->from, psys->particles + i, mat);
 +                      if (psys && !(psys->flag & PSYS_GLOBAL_HAIR))
 +                              psys_mat_hair_to_global(ob, psmd->mesh_final, psys->part->from, psys->particles + i, mat);
  
 -              for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
 -                      if (key->flag & PEK_USE_WCO) {
 -                              copy_v3_v3(key->world_co, key->co);
 -                              mul_m4_v3(mat, key->world_co);
 -                              td->loc = key->world_co;
 -                      }
 -                      else
 -                              td->loc = key->co;
 +                      for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
 +                              if (key->flag & PEK_USE_WCO) {
 +                                      copy_v3_v3(key->world_co, key->co);
 +                                      mul_m4_v3(mat, key->world_co);
 +                                      td->loc = key->world_co;
 +                              }
 +                              else
 +                                      td->loc = key->co;
  
 -                      copy_v3_v3(td->iloc, td->loc);
 -                      copy_v3_v3(td->center, td->loc);
 +                              copy_v3_v3(td->iloc, td->loc);
 +                              copy_v3_v3(td->center, td->loc);
  
 -                      if (key->flag & PEK_SELECT)
 -                              td->flag |= TD_SELECTED;
 -                      else if (!is_prop_edit)
 -                              td->flag |= TD_SKIP;
 +                              if (key->flag & PEK_SELECT)
 +                                      td->flag |= TD_SELECTED;
 +                              else if (!is_prop_edit)
 +                                      td->flag |= TD_SKIP;
  
 -                      unit_m3(td->mtx);
 -                      unit_m3(td->smtx);
 +                              unit_m3(td->mtx);
 +                              unit_m3(td->smtx);
  
 -                      /* don't allow moving roots */
 -                      if (k == 0 && pset->flag & PE_LOCK_FIRST && (!psys || !(psys->flag & PSYS_GLOBAL_HAIR)))
 -                              td->protectflag |= OB_LOCK_LOC;
 +                              /* don't allow moving roots */
 +                              if (k == 0 && pset->flag & PE_LOCK_FIRST && (!psys || !(psys->flag & PSYS_GLOBAL_HAIR)))
 +                                      td->protectflag |= OB_LOCK_LOC;
  
 -                      td->ob = ob;
 -                      td->ext = tx;
 -                      if (t->mode == TFM_BAKE_TIME) {
 -                              td->val = key->time;
 -                              td->ival = *(key->time);
 -                              /* abuse size and quat for min/max values */
 -                              td->flag |= TD_NO_EXT;
 -                              if (k == 0) tx->size = NULL;
 -                              else tx->size = (key - 1)->time;
 +                              td->ob = ob;
 +                              td->ext = tx;
 +                              if (t->mode == TFM_BAKE_TIME) {
 +                                      td->val = key->time;
 +                                      td->ival = *(key->time);
 +                                      /* abuse size and quat for min/max values */
 +                                      td->flag |= TD_NO_EXT;
 +                                      if (k == 0) tx->size = NULL;
 +                                      else tx->size = (key - 1)->time;
  
 -                              if (k == point->totkey - 1) tx->quat = NULL;
 -                              else tx->quat = (key + 1)->time;
 -                      }
 +                                      if (k == point->totkey - 1) tx->quat = NULL;
 +                                      else tx->quat = (key + 1)->time;
 +                              }
  
 -                      td++;
 -                      if (tx)
 -                              tx++;
 -                      tail++;
 +                              td++;
 +                              if (tx)
 +                                      tx++;
 +                              tail++;
 +                      }
 +                      if (is_prop_edit && head != tail)
 +                              calc_distanceCurveVerts(head, tail - 1);
                }
 -              if (is_prop_edit && head != tail)
 -                      calc_distanceCurveVerts(head, tail - 1);
        }
  }
  
  void flushTransParticles(TransInfo *t)
  {
 -      Main *bmain = CTX_data_main(t->context);
 -      Scene *scene = t->scene;
 -      Object *ob = OBACT;
 -      PTCacheEdit *edit = PE_get_current(bmain, scene, ob);
 -      ParticleSystem *psys = edit->psys;
 -      ParticleSystemModifierData *psmd = NULL;
 -      PTCacheEditPoint *point;
 -      PTCacheEditKey *key;
 -      TransData *td;
 -      float mat[4][4], imat[4][4], co[3];
 -      int i, k;
 -      const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 -
 -      if (psys)
 -              psmd = psys_get_modifier(ob, psys);
 -
 -      /* we do transform in world space, so flush world space position
 -       * back to particle local space (only for hair particles) */
 -      td = t->data;
 -      for (i = 0, point = edit->points; i < edit->totpoint; i++, point++, td++) {
 -              if (!(point->flag & PEP_TRANSFORM)) continue;
 -
 -              if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
 -                      psys_mat_hair_to_global(ob, psmd->dm_final, psys->part->from, psys->particles + i, mat);
 -                      invert_m4_m4(imat, mat);
 -
 -                      for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
 -                              copy_v3_v3(co, key->world_co);
 -                              mul_m4_v3(imat, co);
 -
 -
 -                              /* optimization for proportional edit */
 -                              if (!is_prop_edit || !compare_v3v3(key->co, co, 0.0001f)) {
 -                                      copy_v3_v3(key->co, co);
 -                                      point->flag |= PEP_EDIT_RECALC;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              Scene *scene = t->scene;
 +              ViewLayer *view_layer = t->view_layer;
 +              Object *ob = OBACT(view_layer);
 +              PTCacheEdit *edit = PE_get_current(scene, ob);
 +              ParticleSystem *psys = edit->psys;
 +              ParticleSystemModifierData *psmd = NULL;
 +              PTCacheEditPoint *point;
 +              PTCacheEditKey *key;
 +              TransData *td;
 +              float mat[4][4], imat[4][4], co[3];
 +              int i, k;
 +              const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
 +
 +              if (psys)
 +                      psmd = psys_get_modifier(ob, psys);
 +
 +              /* we do transform in world space, so flush world space position
 +               * back to particle local space (only for hair particles) */
 +              td = tc->data;
 +              for (i = 0, point = edit->points; i < edit->totpoint; i++, point++, td++) {
 +                      if (!(point->flag & PEP_TRANSFORM)) continue;
 +
 +                      if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
 +                              psys_mat_hair_to_global(ob, psmd->mesh_final, psys->part->from, psys->particles + i, mat);
 +                              invert_m4_m4(imat, mat);
 +
 +                              for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
 +                                      copy_v3_v3(co, key->world_co);
 +                                      mul_m4_v3(imat, co);
 +
 +
 +                                      /* optimization for proportional edit */
 +                                      if (!is_prop_edit || !compare_v3v3(key->co, co, 0.0001f)) {
 +                                              copy_v3_v3(key->co, co);
 +                                              point->flag |= PEP_EDIT_RECALC;
 +                                      }
                                }
                        }
 +                      else
 +                              point->flag |= PEP_EDIT_RECALC;
                }
 -              else
 -                      point->flag |= PEP_EDIT_RECALC;
 -      }
  
 -      PE_update_object(bmain, scene, OBACT, 1);
 +              PE_update_object(t->depsgraph, scene, OBACT(view_layer), 1);
 +      }
  }
  
  /* ********************* mesh ****************** */
@@@ -2574,248 -2477,243 +2573,248 @@@ static void VertsToTransData(TransInfo 
  
  static void createTransEditVerts(TransInfo *t)
  {
 -      TransData *tob = NULL;
 -      TransDataExtension *tx = NULL;
 -      BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
 -      Mesh *me = t->obedit->data;
 -      BMesh *bm = em->bm;
 -      BMVert *eve;
 -      BMIter iter;
 -      float (*mappedcos)[3] = NULL, (*quats)[4] = NULL;
 -      float mtx[3][3], smtx[3][3], (*defmats)[3][3] = NULL, (*defcos)[3] = NULL;
 -      float *dists = NULL;
 -      int a;
 -      const int prop_mode = (t->flag & T_PROP_EDIT) ? (t->flag & T_PROP_EDIT_ALL) : 0;
 -      int mirror = 0;
 -      int cd_vert_bweight_offset = -1;
 -      bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
  
 -      struct TransIslandData *island_info = NULL;
 -      int island_info_tot;
 -      int *island_vert_map = NULL;
 +              TransData *tob = NULL;
 +              TransDataExtension *tx = NULL;
 +              BMEditMesh *em = BKE_editmesh_from_object(tc->obedit);
 +              Mesh *me = tc->obedit->data;
 +              BMesh *bm = em->bm;
 +              BMVert *eve;
 +              BMIter iter;
 +              float (*mappedcos)[3] = NULL, (*quats)[4] = NULL;
 +              float mtx[3][3], smtx[3][3], (*defmats)[3][3] = NULL, (*defcos)[3] = NULL;
 +              float *dists = NULL;
 +              int a;
 +              const int prop_mode = (t->flag & T_PROP_EDIT) ? (t->flag & T_PROP_EDIT_ALL) : 0;
 +              int mirror = 0;
 +              int cd_vert_bweight_offset = -1;
 +              bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
  
 -      /* Even for translation this is needed because of island-orientation, see: T51651. */
 -      const bool is_island_center = (t->around == V3D_AROUND_LOCAL_ORIGINS);
 -      /* Original index of our connected vertex when connected distances are calculated.
 -       * Optional, allocate if needed. */
 -      int *dists_index = NULL;
 +              struct TransIslandData *island_info = NULL;
 +              int island_info_tot;
 +              int *island_vert_map = NULL;
  
 -      if (t->flag & T_MIRROR) {
 -              EDBM_verts_mirror_cache_begin(em, 0, false, (t->flag & T_PROP_EDIT) == 0, use_topology);
 -              mirror = 1;
 -      }
 +              /* Even for translation this is needed because of island-orientation, see: T51651. */
 +              const bool is_island_center = (t->around == V3D_AROUND_LOCAL_ORIGINS);
 +              /* Original index of our connected vertex when connected distances are calculated.
 +               * Optional, allocate if needed. */
 +              int *dists_index = NULL;
  
 -      /**
 -       * Quick check if we can transform.
 -       *
 -       * \note ignore modes here, even in edge/face modes, transform data is created by selected vertices.
 -       * \note in prop mode we need at least 1 selected.
 -       */
 -      if (bm->totvertsel == 0) {
 -              goto cleanup;
 -      }
 +              if (t->flag & T_MIRROR) {
 +                      /* TODO(campbell): xform: We need support for many mirror objects at once! */
 +                      if (tc->is_active) {
 +                              EDBM_verts_mirror_cache_begin(em, 0, false, (t->flag & T_PROP_EDIT) == 0, use_topology);
 +                              mirror = 1;
 +                      }
 +              }
  
 -      if (t->mode == TFM_BWEIGHT) {
 -              BM_mesh_cd_flag_ensure(bm, BKE_mesh_from_object(t->obedit), ME_CDFLAG_VERT_BWEIGHT);
 -              cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
 -      }
 +              /**
 +               * Quick check if we can transform.
 +               *
 +               * \note ignore modes here, even in edge/face modes, transform data is created by selected vertices.
 +               * \note in prop mode we need at least 1 selected.
 +               */
 +              if (bm->totvertsel == 0) {
 +                      goto cleanup;
 +              }
  
 -      if (prop_mode) {
 -              unsigned int count = 0;
 -              BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
 -                      if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
 -                              count++;
 -                      }
 +              if (t->mode == TFM_BWEIGHT) {
 +                      BM_mesh_cd_flag_ensure(bm, BKE_mesh_from_object(tc->obedit), ME_CDFLAG_VERT_BWEIGHT);
 +                      cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
                }
  
 -              t->total = count;
 +              if (prop_mode) {
 +                      unsigned int count = 0;
 +                      BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
 +                              if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
 +                                      count++;
 +                              }
 +                      }
  
 -              /* allocating scratch arrays */
 -              if (prop_mode & T_PROP_CONNECTED) {
 -                      dists = MEM_mallocN(em->bm->totvert * sizeof(float), __func__);
 -                      if (is_island_center) {
 -                              dists_index =  MEM_mallocN(em->bm->totvert * sizeof(int), __func__);
 +                      tc->data_len = count;
 +
 +                      /* allocating scratch arrays */
 +                      if (prop_mode & T_PROP_CONNECTED) {
 +                              dists = MEM_mallocN(em->bm->totvert * sizeof(float), __func__);
 +                              if (is_island_center) {
 +                                      dists_index =  MEM_mallocN(em->bm->totvert * sizeof(int), __func__);
 +                              }
                        }
                }
 -      }
 -      else {
 -              t->total = bm->totvertsel;
 -      }
 -
 -      tob = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Mesh EditMode)");
 -      if (ELEM(t->mode, TFM_SKIN_RESIZE, TFM_SHRINKFATTEN)) {
 -              /* warning, this is overkill, we only need 2 extra floats,
 -               * but this stores loads of extra stuff, for TFM_SHRINKFATTEN its even more overkill
 -               * since we may not use the 'alt' transform mode to maintain shell thickness,
 -               * but with generic transform code its hard to lazy init vars */
 -              tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension),
 -                                        "TransObData ext");
 -      }
 +              else {
 +                      tc->data_len = bm->totvertsel;
 +              }
  
 -      copy_m3_m4(mtx, t->obedit->obmat);
 -      /* we use a pseudoinverse so that when one of the axes is scaled to 0,
 -       * matrix inversion still works and we can still moving along the other */
 -      pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
 +              tob = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransObData(Mesh EditMode)");
 +              if (ELEM(t->mode, TFM_SKIN_RESIZE, TFM_SHRINKFATTEN)) {
 +                      /* warning, this is overkill, we only need 2 extra floats,
 +                       * but this stores loads of extra stuff, for TFM_SHRINKFATTEN its even more overkill
 +                       * since we may not use the 'alt' transform mode to maintain shell thickness,
 +                       * but with generic transform code its hard to lazy init vars */
 +                      tx = tc->data_ext = MEM_callocN(tc->data_len * sizeof(TransDataExtension), "TransObData ext");
 +              }
  
 -      if (prop_mode & T_PROP_CONNECTED) {
 -              editmesh_set_connectivity_distance(em->bm, mtx, dists, dists_index);
 -      }
 +              copy_m3_m4(mtx, tc->obedit->obmat);
 +              /* we use a pseudoinverse so that when one of the axes is scaled to 0,
 +               * matrix inversion still works and we can still moving along the other */
 +              pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
  
 -      if (is_island_center) {
 -              /* In this specific case, near-by vertices will need to know the island of the nearest connected vertex. */
 -              const bool calc_single_islands = (
 -                      (prop_mode & T_PROP_CONNECTED) &&
 -                      (t->around == V3D_AROUND_LOCAL_ORIGINS) &&
 -                      (em->selectmode & SCE_SELECT_VERTEX));
 +              if (prop_mode & T_PROP_CONNECTED) {
 +                      editmesh_set_connectivity_distance(em->bm, mtx, dists, dists_index);
 +              }
  
 -              island_info = editmesh_islands_info_calc(em, &island_info_tot, &island_vert_map, calc_single_islands);
 -      }
 +              if (is_island_center) {
 +                      /* In this specific case, near-by vertices will need to know the island of the nearest connected vertex. */
 +                      const bool calc_single_islands = (
 +                              (prop_mode & T_PROP_CONNECTED) &&
 +                              (t->around == V3D_AROUND_LOCAL_ORIGINS) &&
 +                              (em->selectmode & SCE_SELECT_VERTEX));
  
 -      /* detect CrazySpace [tm] */
 -      if (modifiers_getCageIndex(t->scene, t->obedit, NULL, 1) != -1) {
 -              int totleft = -1;
 -              if (modifiers_isCorrectableDeformed(t->scene, t->obedit)) {
 -                      /* check if we can use deform matrices for modifier from the
 -                       * start up to stack, they are more accurate than quats */
 -                      totleft = BKE_crazyspace_get_first_deform_matrices_editbmesh(t->scene, t->obedit, em, &defmats, &defcos);
 +                      island_info = editmesh_islands_info_calc(em, &island_info_tot, &island_vert_map, calc_single_islands);
                }
  
 -              /* if we still have more modifiers, also do crazyspace
 -               * correction with quats, relative to the coordinates after
 -               * the modifiers that support deform matrices (defcos) */
 +              /* detect CrazySpace [tm] */
 +              if (modifiers_getCageIndex(t->scene, tc->obedit, NULL, 1) != -1) {
 +                      int totleft = -1;
 +                      if (modifiers_isCorrectableDeformed(t->scene, tc->obedit)) {
 +                              /* check if we can use deform matrices for modifier from the
 +                               * start up to stack, they are more accurate than quats */
 +                              totleft = BKE_crazyspace_get_first_deform_matrices_editbmesh(t->depsgraph, t->scene, tc->obedit, em, &defmats, &defcos);
 +                      }
 +
 +                      /* if we still have more modifiers, also do crazyspace
 +                       * correction with quats, relative to the coordinates after
 +                       * the modifiers that support deform matrices (defcos) */
  
  #if 0 /* TODO, fix crazyspace+extrude so it can be enabled for general use - campbell */
 -              if ((totleft > 0) || (totleft == -1))
 +                      if ((totleft > 0) || (totleft == -1))
  #else
 -              if (totleft > 0)
 +                      if (totleft > 0)
  #endif
 -              {
 -                      mappedcos = BKE_crazyspace_get_mapped_editverts(t->scene, t->obedit);
 -                      quats = MEM_mallocN(em->bm->totvert * sizeof(*quats), "crazy quats");
 -                      BKE_crazyspace_set_quats_editmesh(em, defcos, mappedcos, quats, !prop_mode);
 -                      if (mappedcos)
 -                              MEM_freeN(mappedcos);
 -              }
 +                      {
 +                              mappedcos = BKE_crazyspace_get_mapped_editverts(t->depsgraph, t->scene, tc->obedit);
 +                              quats = MEM_mallocN(em->bm->totvert * sizeof(*quats), "crazy quats");
 +                              BKE_crazyspace_set_quats_editmesh(em, defcos, mappedcos, quats, !prop_mode);
 +                              if (mappedcos)
 +                                      MEM_freeN(mappedcos);
 +                      }
  
 -              if (defcos) {
 -                      MEM_freeN(defcos);
 +                      if (defcos) {
 +                              MEM_freeN(defcos);
 +                      }
                }
 -      }
  
 -      /* find out which half we do */
 -      if (mirror) {
 -              BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
 -                      if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && eve->co[0] != 0.0f) {
 -                              if (eve->co[0] < 0.0f) {
 -                                      t->mirror = -1;
 -                                      mirror = -1;
 +              /* find out which half we do */
 +              if (mirror) {
 +                      BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
 +                              if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && eve->co[0] != 0.0f) {
 +                                      if (eve->co[0] < 0.0f) {
 +                                              t->mirror = -1;
 +                                              mirror = -1;
 +                                      }
 +                                      break;
                                }
 -                              break;
                        }
                }
 -      }
 -
 -      BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, a) {
 -              if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
 -                      if (prop_mode || BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
 -                              struct TransIslandData *v_island = NULL;
 -                              float *bweight = (cd_vert_bweight_offset != -1) ? BM_ELEM_CD_GET_VOID_P(eve, cd_vert_bweight_offset) : NULL;
  
 -                              if (island_info) {
 -                                      const int connected_index = (dists_index && dists_index[a] != -1) ? dists_index[a] : a;
 -                                      v_island = (island_vert_map[connected_index] != -1) ?
 -                                                 &island_info[island_vert_map[connected_index]] : NULL;
 -                              }
 +              BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, a) {
 +                      if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
 +                              if (prop_mode || BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
 +                                      struct TransIslandData *v_island = NULL;
 +                                      float *bweight = (cd_vert_bweight_offset != -1) ? BM_ELEM_CD_GET_VOID_P(eve, cd_vert_bweight_offset) : NULL;
 +
 +                                      if (island_info) {
 +                                              const int connected_index = (dists_index && dists_index[a] != -1) ? dists_index[a] : a;
 +                                              v_island = (island_vert_map[connected_index] != -1) ?
 +                                                         &island_info[island_vert_map[connected_index]] : NULL;
 +                                      }
  
  
 -                              VertsToTransData(t, tob, tx, em, eve, bweight, v_island);
 -                              if (tx)
 -                                      tx++;
 +                                      VertsToTransData(t, tob, tx, em, eve, bweight, v_island);
 +                                      if (tx)
 +                                              tx++;
  
 -                              /* selected */
 -                              if (BM_elem_flag_test(eve, BM_ELEM_SELECT))
 -                                      tob->flag |= TD_SELECTED;
 +                                      /* selected */
 +                                      if (BM_elem_flag_test(eve, BM_ELEM_SELECT))
 +                                              tob->flag |= TD_SELECTED;
  
 -                              if (prop_mode) {
 -                                      if (prop_mode & T_PROP_CONNECTED) {
 -                                              tob->dist = dists[a];
 -                                      }
 -                                      else {
 -                                              tob->flag |= TD_NOTCONNECTED;
 -                                              tob->dist = FLT_MAX;
 +                                      if (prop_mode) {
 +                                              if (prop_mode & T_PROP_CONNECTED) {
 +                                                      tob->dist = dists[a];
 +                                              }
 +                                              else {
 +                                                      tob->flag |= TD_NOTCONNECTED;
 +                                                      tob->dist = FLT_MAX;
 +                                              }
                                        }
 -                              }
  
 -                              /* CrazySpace */
 -                              if (defmats || (quats && BM_elem_flag_test(eve, BM_ELEM_TAG))) {
 -                                      float mat[3][3], qmat[3][3], imat[3][3];
 +                                      /* CrazySpace */
 +                                      if (defmats || (quats && BM_elem_flag_test(eve, BM_ELEM_TAG))) {
 +                                              float mat[3][3], qmat[3][3], imat[3][3];
  
 -                                      /* use both or either quat and defmat correction */
 -                                      if (quats && BM_elem_flag_test(eve, BM_ELEM_TAG)) {
 -                                              quat_to_mat3(qmat, quats[BM_elem_index_get(eve)]);
 +                                              /* use both or either quat and defmat correction */
 +                                              if (quats && BM_elem_flag_test(eve, BM_ELEM_TAG)) {
 +                                                      quat_to_mat3(qmat, quats[BM_elem_index_get(eve)]);
  
 -                                              if (defmats)
 -                                                      mul_m3_series(mat, defmats[a], qmat, mtx);
 +                                                      if (defmats)
 +                                                              mul_m3_series(mat, defmats[a], qmat, mtx);
 +                                                      else
 +                                                              mul_m3_m3m3(mat, mtx, qmat);
 +                                              }
                                                else
 -                                                      mul_m3_m3m3(mat, mtx, qmat);
 -                                      }
 -                                      else
 -                                              mul_m3_m3m3(mat, mtx, defmats[a]);
 +                                                      mul_m3_m3m3(mat, mtx, defmats[a]);
  
 -                                      invert_m3_m3(imat, mat);
 +                                              invert_m3_m3(imat, mat);
  
 -                                      copy_m3_m3(tob->smtx, imat);
 -                                      copy_m3_m3(tob->mtx, mat);
 -                              }
 -                              else {
 -                                      copy_m3_m3(tob->smtx, smtx);
 -                                      copy_m3_m3(tob->mtx, mtx);
 -                              }
 +                                              copy_m3_m3(tob->smtx, imat);
 +                                              copy_m3_m3(tob->mtx, mat);
 +                                      }
 +                                      else {
 +                                              copy_m3_m3(tob->smtx, smtx);
 +                                              copy_m3_m3(tob->mtx, mtx);
 +                                      }
  
 -                              /* Mirror? */
 -                              if ((mirror > 0 && tob->iloc[0] > 0.0f) || (mirror < 0 && tob->iloc[0] < 0.0f)) {
 -                                      BMVert *vmir = EDBM_verts_mirror_get(em, eve); //t->obedit, em, eve, tob->iloc, a);
 -                                      if (vmir && vmir != eve) {
 -                                              tob->extra = vmir;
 +                                      /* Mirror? */
 +                                      if ((mirror > 0 && tob->iloc[0] > 0.0f) || (mirror < 0 && tob->iloc[0] < 0.0f)) {
 +                                              BMVert *vmir = EDBM_verts_mirror_get(em, eve); //t->obedit, em, eve, tob->iloc, a);
 +                                              if (vmir && vmir != eve) {
 +                                                      tob->extra = vmir;
 +                                              }
                                        }
 +                                      tob++;
                                }
 -                              tob++;
                        }
                }
 -      }
  
 -      if (island_info) {
 -              MEM_freeN(island_info);
 -              MEM_freeN(island_vert_map);
 -      }
 +              if (island_info) {
 +                      MEM_freeN(island_info);
 +                      MEM_freeN(island_vert_map);
 +              }
  
 -      if (mirror != 0) {
 -              tob = t->data;
 -              for (a = 0; a < t->total; a++, tob++) {
 -                      if (ABS(tob->loc[0]) <= 0.00001f) {
 -                              tob->flag |= TD_MIRROR_EDGE;
 +              if (mirror != 0) {
 +                      tob = tc->data;
 +                      for (a = 0; a < tc->data_len; a++, tob++) {
 +                              if (ABS(tob->loc[0]) <= 0.00001f) {
 +                                      tob->flag |= TD_MIRROR_EDGE;
 +                              }
                        }
                }
 -      }
  
  cleanup:
 -      /* crazy space free */
 -      if (quats)
 -              MEM_freeN(quats);
 -      if (defmats)
 -              MEM_freeN(defmats);
 -      if (dists)
 -              MEM_freeN(dists);
 -      if (dists_index)
 -              MEM_freeN(dists_index);
 -
 -      if (t->flag & T_MIRROR) {
 -              EDBM_verts_mirror_cache_end(em);
 +              /* crazy space free */
 +              if (quats)
 +                      MEM_freeN(quats);
 +              if (defmats)
 +                      MEM_freeN(defmats);
 +              if (dists)
 +                      MEM_freeN(dists);
 +              if (dists_index)
 +                      MEM_freeN(dists_index);
 +
 +              if (t->flag & T_MIRROR) {
 +                      EDBM_verts_mirror_cache_end(em);
 +              }
        }
  }
  
  void flushTransNodes(TransInfo *t)
  {
        const float dpi_fac = UI_DPI_FAC;
 -      int a;
 -      TransData *td;
 -      TransData2D *td2d;
  
 -      applyGridAbsolute(t);
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              int a;
 +              TransData *td;
 +              TransData2D *td2d;
  
 -      /* flush to 2d vector from internally used 3d vector */
 -      for (a = 0, td = t->data, td2d = t->data2d; a < t->total; a++, td++, td2d++) {
 -              bNode *node = td->extra;
 -              float locx, locy;
 +              applyGridAbsolute(t);
 +
 +              /* flush to 2d vector from internally used 3d vector */
 +              for (a = 0, td = tc->data, td2d = tc->data_2d; a < tc->data_len; a++, td++, td2d++) {
 +                      bNode *node = td->extra;
 +                      float locx, locy;
  
 -              /* weirdo - but the node system is a mix of free 2d elements and dpi sensitive UI */
 +                      /* weirdo - but the node system is a mix of free 2d elements and dpi sensitive UI */
  #ifdef USE_NODE_CENTER
 -              locx = (td2d->loc[0] - (BLI_rctf_size_x(&node->totr)) * +0.5f) / dpi_fac;
 -              locy = (td2d->loc[1] - (BLI_rctf_size_y(&node->totr)) * -0.5f) / dpi_fac;
 +                      locx = (td2d->loc[0] - (BLI_rctf_size_x(&node->totr)) * +0.5f) / dpi_fac;
 +                      locy = (td2d->loc[1] - (BLI_rctf_size_y(&node->totr)) * -0.5f) / dpi_fac;
  #else
 -              locx = td2d->loc[0] / dpi_fac;
 -              locy = td2d->loc[1] / dpi_fac;
 +                      locx = td2d->loc[0] / dpi_fac;
 +                      locy = td2d->loc[1] / dpi_fac;
  #endif
  
 -              /* account for parents (nested nodes) */
 -              if (node->parent) {
 -                      nodeFromView(node->parent, locx, locy, &node->locx, &node->locy);
 -              }
 -              else {
 -                      node->locx = locx;
 -                      node->locy = locy;
 +                      /* account for parents (nested nodes) */
 +                      if (node->parent) {
 +                              nodeFromView(node->parent, locx, locy, &node->locx, &node->locy);
 +                      }
 +                      else {
 +                              node->locx = locx;
 +                              node->locy = locy;
 +                      }
                }
 -      }
  
 -      /* handle intersection with noodles */
 -      if (t->total == 1) {
 -              ED_node_link_intersect_test(t->sa, 1);
 +              /* handle intersection with noodles */
 +              if (tc->data_len == 1) {
 +                      ED_node_link_intersect_test(t->sa, 1);
 +              }
        }
  }
  
@@@ -2888,14 -2783,13 +2887,14 @@@ BLI_INLINE void trans_update_seq(Scene 
  void flushTransSeq(TransInfo *t)
  {
        ListBase *seqbasep = BKE_sequencer_editing_get(t->scene, false)->seqbasep; /* Editing null check already done */
 +
        int a, new_frame;
        TransData *td = NULL;
        TransData2D *td2d = NULL;
        TransDataSeq *tdsq = NULL;
        Sequence *seq;
  
 -
 +      TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
  
        /* prevent updating the same seq twice
         * if the transdata order is changed this will mess up
        int old_start_prev = 0, sel_flag_prev = 0;
  
        /* flush to 2d vector from internally used 3d vector */
 -      for (a = 0, td = t->data, td2d = t->data2d; a < t->total; a++, td++, td2d++) {
 +      for (a = 0, td = tc->data, td2d = tc->data_2d; a < tc->data_len; a++, td++, td2d++) {
                int old_start;
                tdsq = (TransDataSeq *)td->extra;
                seq = tdsq->seq;
                }
  
                /* update effects inside meta's */
 -              for (a = 0, seq_prev = NULL, td = t->data, td2d = t->data2d;
 -                   a < t->total;
 +              for (a = 0, seq_prev = NULL, td = tc->data, td2d = tc->data_2d;
 +                   a < tc->data_len;
                     a++, td++, td2d++, seq_prev = seq)
                {
                        tdsq = (TransDataSeq *)td->extra;
        /* need to do the overlap check in a new loop otherwise adjacent strips
         * will not be updated and we'll get false positives */
        seq_prev = NULL;
 -      for (a = 0, td = t->data, td2d = t->data2d; a < t->total; a++, td++, td2d++) {
 +      for (a = 0, td = tc->data, td2d = tc->data_2d; a < tc->data_len; a++, td++, td2d++) {
  
                tdsq = (TransDataSeq *)td->extra;
                seq = tdsq->seq;
@@@ -3055,167 -2949,159 +3054,167 @@@ static void createTransUVs(bContext *C
        Image *ima = CTX_data_edit_image(C);
        Scene *scene = t->scene;
        ToolSettings *ts = CTX_data_tool_settings(C);
 -      TransData *td = NULL;
 -      TransData2D *td2d = NULL;
 -      BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
 -      BMFace *efa;
 -      BMIter iter, liter;
 -      UvElementMap *elementmap = NULL;
 -      BLI_bitmap *island_enabled = NULL;
 -      struct { float co[2]; int co_num; } *island_center = NULL;
 -      int count = 0, countsel = 0, count_rejected = 0;
 +
        const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
        const bool is_prop_connected = (t->flag & T_PROP_CONNECTED) != 0;
        const bool is_island_center = (t->around == V3D_AROUND_LOCAL_ORIGINS);
 -      const int cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV);
 -      const int cd_poly_tex_offset = CustomData_get_offset(&em->bm->pdata, CD_MTEXPOLY);
  
 -      if (!ED_space_image_show_uvedit(sima, t->obedit))
 -              return;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
  
 -      /* count */
 -      if (is_prop_connected || is_island_center) {
 -              /* create element map with island information */
 -              const bool use_facesel = (ts->uv_flag & UV_SYNC_SELECTION) == 0;
 -              elementmap = BM_uv_element_map_create(em->bm, use_facesel, false, true);
 -              if (elementmap == NULL) {
 -                      return;
 -              }
 +              TransData *td = NULL;
 +              TransData2D *td2d = NULL;
 +              BMEditMesh *em = BKE_editmesh_from_object(tc->obedit);
 +              BMFace *efa;
 +              BMIter iter, liter;
 +              UvElementMap *elementmap = NULL;
 +              BLI_bitmap *island_enabled = NULL;
 +              struct { float co[2]; int co_num; } *island_center = NULL;
 +              int count = 0, countsel = 0, count_rejected = 0;
 +              const int cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV);
  
 -              if (is_prop_connected) {
 -                      island_enabled = BLI_BITMAP_NEW(elementmap->totalIslands, "TransIslandData(UV Editing)");
 +              if (!ED_space_image_show_uvedit(sima, tc->obedit)) {
 +                      continue;
                }
  
 -              if (is_island_center) {
 -                      island_center = MEM_callocN(sizeof(*island_center) * elementmap->totalIslands, __func__);
 -              }
 -      }
 +              /* count */
 +              if (is_prop_connected || is_island_center) {
 +                      /* create element map with island information */
 +                      const bool use_facesel = (ts->uv_flag & UV_SYNC_SELECTION) == 0;
 +                      elementmap = BM_uv_element_map_create(em->bm, use_facesel, false, true);
 +                      if (elementmap == NULL) {
 +                              return;
 +                      }
  
 -      BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
 -              MTexPoly *tf = BM_ELEM_CD_GET_VOID_P(efa, cd_poly_tex_offset);
 -              BMLoop *l;
 +                      if (is_prop_connected) {
 +                              island_enabled = BLI_BITMAP_NEW(elementmap->totalIslands, "TransIslandData(UV Editing)");
 +                      }
  
 -              if (!uvedit_face_visible_test(scene, ima, efa, tf)) {
 -                      BM_elem_flag_disable(efa, BM_ELEM_TAG);
 -                      continue;
 +                      if (is_island_center) {
 +                              island_center = MEM_callocN(sizeof(*island_center) * elementmap->totalIslands, __func__);
 +                      }
                }
  
 -              BM_elem_flag_enable(efa, BM_ELEM_TAG);
 -              BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
 -                      if (uvedit_uv_select_test(scene, l, cd_loop_uv_offset)) {
 -                              countsel++;
 +              BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
 +                      BMLoop *l;
  
 -                              if (is_prop_connected || island_center) {
 -                                      UvElement *element = BM_uv_element_get(elementmap, efa, l);
 +                      if (!uvedit_face_visible_test(scene, tc->obedit, ima, efa)) {
 +                              BM_elem_flag_disable(efa, BM_ELEM_TAG);
 +                              continue;
 +                      }
  
 -                                      if (is_prop_connected) {
 -                                              BLI_BITMAP_ENABLE(island_enabled, element->island);
 -                                      }
 +                      BM_elem_flag_enable(efa, BM_ELEM_TAG);
 +                      BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
 +                              if (uvedit_uv_select_test(scene, l, cd_loop_uv_offset)) {
 +                                      countsel++;
 +
 +                                      if (is_prop_connected || island_center) {
 +                                              UvElement *element = BM_uv_element_get(elementmap, efa, l);
  
 -                                      if (is_island_center) {
 -                                              if (element->flag == false) {
 -                                                      MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
 -                                                      add_v2_v2(island_center[element->island].co, luv->uv);
 -                                                      island_center[element->island].co_num++;
 -                                                      element->flag = true;
 +                                              if (is_prop_connected) {
 +                                                      BLI_BITMAP_ENABLE(island_enabled, element->island);
 +                                              }
 +
 +                                              if (is_island_center) {
 +                                                      if (element->flag == false) {
 +                                                              MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
 +                                                              add_v2_v2(island_center[element->island].co, luv->uv);
 +                                                              island_center[element->island].co_num++;
 +                                                              element->flag = true;
 +                                                      }
                                                }
                                        }
                                }
 -                      }
  
 -                      if (is_prop_edit) {
 -                              count++;
 +                              if (is_prop_edit) {
 +                                      count++;
 +                              }
                        }
                }
 -      }
  
 -      /* note: in prop mode we need at least 1 selected */
 -      if (countsel == 0) {
 -              goto finally;
 -      }
 +              /* note: in prop mode we need at least 1 selected */
 +              if (countsel == 0) {
 +                      goto finally;
 +              }
  
 -      if (is_island_center) {
 -              int i;
 +              if (is_island_center) {
 +                      int i;
  
 -              for (i = 0; i < elementmap->totalIslands; i++) {
 -                      mul_v2_fl(island_center[i].co, 1.0f / island_center[i].co_num);
 -                      mul_v2_v2(island_center[i].co, t->aspect);
 +                      for (i = 0; i < elementmap->totalIslands; i++) {
 +                              mul_v2_fl(island_center[i].co, 1.0f / island_center[i].co_num);
 +                              mul_v2_v2(island_center[i].co, t->aspect);
 +                      }
                }
 -      }
  
 -      t->total = (is_prop_edit) ? count : countsel;
 -      t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(UV Editing)");
 -      /* for each 2d uv coord a 3d vector is allocated, so that they can be
 -       * treated just as if they were 3d verts */
 -      t->data2d = MEM_callocN(t->total * sizeof(TransData2D), "TransObData2D(UV Editing)");
 +              tc->data_len = (is_prop_edit) ? count : countsel;
 +              tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransObData(UV Editing)");
 +              /* for each 2d uv coord a 3d vector is allocated, so that they can be
 +               * treated just as if they were 3d verts */
 +              tc->data_2d = MEM_callocN(tc->data_len * sizeof(TransData2D), "TransObData2D(UV Editing)");
  
 -      if (sima->flag & SI_CLIP_UV)
 -              t->flag |= T_CLIP_UV;
 +              if (sima->flag & SI_CLIP_UV)
 +                      t->flag |= T_CLIP_UV;
  
 -      td = t->data;
 -      td2d = t->data2d;
 +              td = tc->data;
 +              td2d = tc->data_2d;
  
 -      BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
 -              BMLoop *l;
 +              BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
 +                      BMLoop *l;
  
 -              if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
 -                      continue;
 +                      if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
 +                              continue;
  
 -              BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
 -                      const bool selected = uvedit_uv_select_test(scene, l, cd_loop_uv_offset);
 -                      MLoopUV *luv;
 -                      const float *center = NULL;
 +                      BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
 +                              const bool selected = uvedit_uv_select_test(scene, l, cd_loop_uv_offset);
 +                              MLoopUV *luv;
 +                              const float *center = NULL;
  
 -                      if (!is_prop_edit && !selected)
 -                              continue;
 +                              if (!is_prop_edit && !selected)
 +                                      continue;
  
 -                      if (is_prop_connected || is_island_center) {
 -                              UvElement *element = BM_uv_element_get(elementmap, efa, l);
 +                              if (is_prop_connected || is_island_center) {
 +                                      UvElement *element = BM_uv_element_get(elementmap, efa, l);
  
 -                              if (is_prop_connected) {
 -                                      if (!BLI_BITMAP_TEST(island_enabled, element->island)) {
 -                                              count_rejected++;
 -                                              continue;
 +                                      if (is_prop_connected) {
 +                                              if (!BLI_BITMAP_TEST(island_enabled, element->island)) {
 +                                                      count_rejected++;
 +                                                      continue;
 +                                              }
                                        }
 -                              }
  
 -                              if (is_island_center) {
 -                                      center = island_center[element->island].co;
 +                                      if (is_island_center) {
 +                                              center = island_center[element->island].co;
 +                                      }
                                }
 -                      }
  
 -                      BM_elem_flag_enable(l, BM_ELEM_TAG);
 -                      luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
 -                      UVsToTransData(t->aspect, td++, td2d++, luv->uv, center, selected);
 +                              BM_elem_flag_enable(l, BM_ELEM_TAG);
 +                              luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
 +                              UVsToTransData(t->aspect, td++, td2d++, luv->uv, center, selected);
 +                      }
                }
 -      }
 -
 -      if (is_prop_connected) {
 -              t->total -= count_rejected;
 -      }
  
 -      if (sima->flag & SI_LIVE_UNWRAP)
 -              ED_uvedit_live_unwrap_begin(t->scene, t->obedit);
 +              if (is_prop_connected) {
 +                      tc->data_len -= count_rejected;
 +              }
  
 +              if (sima->flag & SI_LIVE_UNWRAP) {
 +                      /* TODO(campbell): xform: Only active object currently!
 +                       * it uses a static variable. */
 +                      if (tc->is_active) {
 +                              ED_uvedit_live_unwrap_begin(t->scene, tc->obedit);
 +                      }
 +              }
  
  finally:
 -      if (is_prop_connected || is_island_center) {
 -              BM_uv_element_map_free(elementmap);
 +              if (is_prop_connected || is_island_center) {
 +                      BM_uv_element_map_free(elementmap);
  
 -              if (is_prop_connected) {
 -                      MEM_freeN(island_enabled);
 -              }
 +                      if (is_prop_connected) {
 +                              MEM_freeN(island_enabled);
 +                      }
  
 -              if (island_center) {
 -                      MEM_freeN(island_center);
 +                      if (island_center) {
 +                              MEM_freeN(island_center);
 +                      }
                }
        }
  }
  void flushTransUVs(TransInfo *t)
  {
        SpaceImage *sima = t->sa->spacedata.first;
 -      TransData2D *td;
 -      int a;
 -      float aspect_inv[2], size[2];
        const bool use_pixel_snap = ((sima->flag & SI_PIXELSNAP) && (t->state != TRANS_CANCEL));
  
 -      aspect_inv[0] = 1.0f / t->aspect[0];
 -      aspect_inv[1] = 1.0f / t->aspect[1];
 -
 -      if (use_pixel_snap) {
 -              int size_i[2];
 -              ED_space_image_get_size(sima, &size_i[0], &size_i[1]);
 -              size[0] = size_i[0];
 -              size[1] = size_i[1];
 -      }
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              TransData2D *td;
 +              int a;
 +              float aspect_inv[2], size[2];
  
 -      /* flush to 2d vector from internally used 3d vector */
 -      for (a = 0, td = t->data2d; a < t->total; a++, td++) {
 -              td->loc2d[0] = td->loc[0] * aspect_inv[0];
 -              td->loc2d[1] = td->loc[1] * aspect_inv[1];
 +              aspect_inv[0] = 1.0f / t->aspect[0];
 +              aspect_inv[1] = 1.0f / t->aspect[1];
  
                if (use_pixel_snap) {
 -                      td->loc2d[0] = roundf(td->loc2d[0] * size[0]) / size[0];
 -                      td->loc2d[1] = roundf(td->loc2d[1] * size[1]) / size[1];
 +                      int size_i[2];
 +                      ED_space_image_get_size(sima, &size_i[0], &size_i[1]);
 +                      size[0] = size_i[0];
 +                      size[1] = size_i[1];
 +              }
 +
 +              /* flush to 2d vector from internally used 3d vector */
 +              for (a = 0, td = tc->data_2d; a < tc->data_len; a++, td++) {
 +                      td->loc2d[0] = td->loc[0] * aspect_inv[0];
 +                      td->loc2d[1] = td->loc[1] * aspect_inv[1];
 +
 +                      if (use_pixel_snap) {
 +                              td->loc2d[0] = roundf(td->loc2d[0] * size[0]) / size[0];
 +                              td->loc2d[1] = roundf(td->loc2d[1] * size[1]) / size[1];
 +                      }
                }
        }
  }
  
  bool clipUVTransform(TransInfo *t, float vec[2], const bool resize)
  {
 -      TransData *td;
 -      int a;
        bool clipx = true, clipy = true;
        float min[2], max[2];
  
        max[0] = t->aspect[0];
        max[1] = t->aspect[1];
  
 -      for (a = 0, td = t->data; a < t->total; a++, td++) {
 -              minmax_v2v2_v2(min, max, td->loc);
 -      }
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
  
 -      if (resize) {
 -              if (min[0] < 0.0f && t->center[0] > 0.0f && t->center[0] < t->aspect[0] * 0.5f)
 -                      vec[0] *= t->center[0] / (t->center[0] - min[0]);
 -              else if (max[0] > t->aspect[0] && t->center[0] < t->aspect[0])
 -                      vec[0] *= (t->center[0] - t->aspect[0]) / (t->center[0] - max[0]);
 -              else
 -                      clipx = 0;
 +              TransData *td;
 +              int a;
  
 -              if (min[1] < 0.0f && t->center[1] > 0.0f && t->center[1] < t->aspect[1] * 0.5f)
 -                      vec[1] *= t->center[1] / (t->center[1] - min[1]);
 -              else if (max[1] > t->aspect[1] && t->center[1] < t->aspect[1])
 -                      vec[1] *= (t->center[1] - t->aspect[1]) / (t->center[1] - max[1]);
 -              else
 -                      clipy = 0;
 -      }
 -      else {
 -              if (min[0] < 0.0f)
 -                      vec[0] -= min[0];
 -              else if (max[0] > t->aspect[0])
 -                      vec[0] -= max[0] - t->aspect[0];
 -              else
 -                      clipx = 0;
 +              for (a = 0, td = tc->data; a < tc->data_len; a++, td++) {
 +                      minmax_v2v2_v2(min, max, td->loc);
 +              }
  
 -              if (min[1] < 0.0f)
 -                      vec[1] -= min[1];
 -              else if (max[1] > t->aspect[1])
 -                      vec[1] -= max[1] - t->aspect[1];
 -              else
 -                      clipy = 0;
 +              if (resize) {
 +                      if (min[0] < 0.0f && t->center_global[0] > 0.0f && t->center_global[0] < t->aspect[0] * 0.5f)
 +                              vec[0] *= t->center_global[0] / (t->center_global[0] - min[0]);
 +                      else if (max[0] > t->aspect[0] && t->center_global[0] < t->aspect[0])
 +                              vec[0] *= (t->center_global[0] - t->aspect[0]) / (t->center_global[0] - max[0]);
 +                      else
 +                              clipx = 0;
 +
 +                      if (min[1] < 0.0f && t->center_global[1] > 0.0f && t->center_global[1] < t->aspect[1] * 0.5f)
 +                              vec[1] *= t->center_global[1] / (t->center_global[1] - min[1]);
 +                      else if (max[1] > t->aspect[1] && t->center_global[1] < t->aspect[1])
 +                              vec[1] *= (t->center_global[1] - t->aspect[1]) / (t->center_global[1] - max[1]);
 +                      else
 +                              clipy = 0;
 +              }
 +              else {
 +                      if (min[0] < 0.0f)
 +                              vec[0] -= min[0];
 +                      else if (max[0] > t->aspect[0])
 +                              vec[0] -= max[0] - t->aspect[0];
 +                      else
 +                              clipx = 0;
 +
 +                      if (min[1] < 0.0f)
 +                              vec[1] -= min[1];
 +                      else if (max[1] > t->aspect[1])
 +                              vec[1] -= max[1] - t->aspect[1];
 +                      else
 +                              clipy = 0;
 +              }
        }
  
        return (clipx || clipy);
  
  void clipUVData(TransInfo *t)
  {
 -      TransData *td = NULL;
 -      int a;
 -
 -      for (a = 0, td = t->data; a < t->total; a++, td++) {
 -              if (td->flag & TD_NOACTION)
 -                      break;
 +      FOREACH_TRANS_DATA_CONTAINER (t, tc) {
 +              TransData *td = tc->data;
 +              for (int a = 0; a < tc->data_len; a++, td++) {
 +                      if (td->flag & TD_NOACTION)
 +                              break;
  
 -              if ((td->flag & TD_SKIP) || (!td->loc))
 -                      continue;
 +                      if ((td->flag & TD_SKIP) || (!td->loc))
 +                              continue;
  
 -              td->loc[0] = min_ff(max_ff(0.0f, td->loc[0]), t->aspect[0]);
 -              td->loc[1] = min_ff(max_ff(0.0f, td->loc[1]), t->aspect[1]);
 +                      td->loc[0] = min_ff(max_ff(0.0f, td->loc[0]), t->aspect[0]);
 +                      td->loc[1] = min_ff(max_ff(0.0f, td->loc[1]), t->aspect[1]);
 +              }
        }
  }
  
@@@ -3354,8 -3233,6 +3353,8 @@@ static void createTransNlaData(bContex
  
        int count = 0;
  
 +      TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
 +
        /* determine what type of data we are operating on */
        if (ANIM_animdata_get_context(C, &ac) == 0)
                return;
        }
  
        /* allocate memory for data */
 -      t->total = count;
 +      tc->data_len = count;
  
 -      t->data = MEM_callocN(t->total * sizeof(TransData), "TransData(NLA Editor)");
 -      td = t->data;
 -      t->custom.type.data = tdn = MEM_callocN(t->total * sizeof(TransDataNla), "TransDataNla (NLA Editor)");
 -      t->custom.type.use_free = true;
 +      tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransData(NLA Editor)");
 +      td = tc->data;
 +      tc->custom.type.data = tdn = MEM_callocN(tc->data_len * sizeof(TransDataNla), "TransDataNla (NLA Editor)");
 +      tc->custom.type.use_free = true;
  
        /* loop 2: build transdata array */
        for (ale = anim_data.first; ale; ale = ale->next) {
@@@ -3950,11 -3827,11 +3949,11 @@@ typedef struct tGPFtransdata 
  /* This function helps flush transdata written to tempdata into the gp-frames  */
  void flushTransIntFrameActionData(TransInfo *t)
  {
 -      tGPFtransdata *tfd = t->custom.type.data;
 -      int i;
 +      TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
 +      tGPFtransdata *tfd = tc->custom.type.data;
  
        /* flush data! */
 -      for (i = 0; i < t->total; i++, tfd++) {
 +      for (int i = 0; i < tc->data_len; i++, tfd++) {
                *(tfd->sdata) = round_fl_to_int(tfd->val);
        }
  }
@@@ -4111,19 -3988,17 +4110,19 @@@ static void createTransActionData(bCont
                return;
        }
  
 +      TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
 +
        /* allocate memory for data */
 -      t->total = count;
 +      tc->data_len = count;
  
 -      t->data = MEM_callocN(t->total * sizeof(TransData), "TransData(Action Editor)");
 -      t->data2d = MEM_callocN(t->total * sizeof(TransData2D), "transdata2d");
 -      td = t->data;
 -      td2d = t->data2d;
 +      tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransData(Action Editor)");
 +      tc->data_2d = MEM_callocN(tc->data_len * sizeof(TransData2D), "transdata2d");
 +      td = tc->data;
 +      td2d = tc->data_2d;
  
        if (ELEM(ac.datatype, ANIMCONT_GPENCIL, ANIMCONT_MASK)) {
 -              t->custom.type.data = tfd = MEM_callocN(sizeof(tGPFtransdata) * count, "tGPFtransdata");
 -              t->custom.type.use_free = true;
 +              tc->custom.type.data = tfd = MEM_callocN(sizeof(tGPFtransdata) * count, "tGPFtransdata");
 +              tc->custom.type.use_free = true;
        }
  
        /* loop 2: build transdata array */
  
        /* calculate distances for proportional editing */
        if (is_prop_edit) {
 -              td = t->data;
 +              td = tc->data;
  
                for (ale = anim_data.first; ale; ale = ale->next) {
                        AnimData *adt;
@@@ -4511,20 -4386,18 +4510,20 @@@ static void createTransGraphEditData(bC
                return;
        }
  
 +      TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
 +
        /* allocate memory for data */
 -      t->total = count;
 +      tc->data_len = count;
  
 -      t->data = MEM_callocN(t->total * sizeof(TransData), "TransData (Graph Editor)");
 +      tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransData (Graph Editor)");
        /* for each 2d vert a 3d vector is allocated, so that they can be treated just as if they were 3d verts */
 -      t->data2d = MEM_callocN(t->total * sizeof(TransData2D), "TransData2D (Graph Editor)");
 -      t->custom.type.data = MEM_callocN(t->total * sizeof(TransDataGraph), "TransDataGraph");
 -      t->custom.type.use_free = true;
 +      tc->data_2d = MEM_callocN(tc->data_len * sizeof(TransData2D), "TransData2D (Graph Editor)");
 +      tc->custom.type.data = MEM_callocN(tc->data_len * sizeof(TransDataGraph), "TransDataGraph");
 +      tc->custom.type.use_free = true;
  
 -      td = t->data;
 -      td2d = t->data2d;
 -      tdg = t->custom.type.data;
 +      td = tc->data;
 +      td2d = tc->data_2d;
 +      tdg = tc->custom.type.data;
  
        /* precompute space-conversion matrices for dealing with non-uniform scaling of Graph Editor */
        unit_m3(mtx);
  
        if (is_prop_edit) {
                /* loop 2: build transdata arrays */
 -              td = t->data;
 +              td = tc->data;
  
                for (ale = anim_data.first; ale; ale = ale->next) {
                        AnimData *adt = ANIM_nla_mapping_get(&ac, ale);
@@@ -4808,13 -4681,11 +4807,13 @@@ static void beztmap_to_data(TransInfo *
        int i, j;
        char *adjusted;
  
 +      TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
 +
        /* dynamically allocate an array of chars to mark whether an TransData's
         * pointers have been fixed already, so that we don't override ones that are
         * already done
         */
 -      adjusted = MEM_callocN(t->total, "beztmap_adjusted_map");
 +      adjusted = MEM_callocN(tc->data_len, "beztmap_adjusted_map");
  
        /* for each beztmap item, find if it is used anywhere */
        bezm = bezms;
                /* loop through transdata, testing if we have a hit
                 * for the handles (vec[0]/vec[2]), we must also check if they need to be swapped...
                 */
 -              td2d = t->data2d;
 -              td = t->data;
 -              for (j = 0; j < t->total; j++, td2d++, td++) {
 +              td2d = tc->data_2d;
 +              td = tc->data;
 +              for (j = 0; j < tc->data_len; j++, td2d++, td++) {
                        /* skip item if already marked */
                        if (adjusted[j] != 0) continue;
  
@@@ -4927,11 -4798,9 +4926,11 @@@ void flushTransGraphData(TransInfo *t
        double secf = FPS;
        int a;
  
 +      TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
 +
        /* flush to 2d vector from internally used 3d vector */
 -      for (a = 0, td = t->data, td2d = t->data2d, tdg = t->custom.type.data;
 -           a < t->total;
 +      for (a = 0, td = tc->data, td2d = tc->data_2d, tdg = tc->custom.type.data;
 +           a < tc->data_len;
             a++, td++, td2d++, tdg++)
        {
                AnimData *adt = (AnimData *)td->extra; /* pointers to relevant AnimData blocks are stored in the td->extra pointers */
@@@ -5293,15 -5162,13 +5292,15 @@@ static void SeqTransDataBounds(TransInf
  }
  
  
 -static void freeSeqData(TransInfo *t, TransCustomData *custom_data)
 +static void freeSeqData(TransInfo *t, TransDataContainer *tc, TransCustomData *custom_data)
  {
        Editing *ed = BKE_sequencer_editing_get(t->scene, false);
  
        if (ed != NULL) {
 +
 +
                ListBase *seqbasep = ed->seqbasep;
 -              TransData *td = t->data;
 +              TransData *td = tc->data;
                int a;
  
                /* prevent updating the same seq twice
                        {
                                int overlap = 0;
  
 -                              for (a = 0, seq_prev = NULL; a < t->total; a++, td++, seq_prev = seq) {
 +                              for (a = 0, seq_prev = NULL; a < tc->data_len; a++, td++, seq_prev = seq) {
                                        seq = ((TransDataSeq *)td->extra)->seq;
                                        if ((seq != seq_prev) && (seq->depth == 0) && (seq->flag & SEQ_OVERLAP)) {
                                                overlap = 1;
                                        for (seq = seqbasep->first; seq; seq = seq->next)
                                                seq->tmp = NULL;
  
 -                                      td = t->data;
 -                                      for (a = 0, seq_prev = NULL; a < t->total; a++, td++, seq_prev = seq) {
 +                                      td = tc->data;
 +                                      for (a = 0, seq_prev = NULL; a < tc->data_len; a++, td++, seq_prev = seq) {
                                                seq = ((TransDataSeq *)td->extra)->seq;
                                                if ((seq != seq_prev)) {
                                                        /* check effects strips, we cant change their time */
  
                                        if (t->flag & T_ALT_TRANSFORM) {
                                                int minframe = MAXFRAME;
 -                                              td = t->data;
 -                                              for (a = 0, seq_prev = NULL; a < t->total; a++, td++, seq_prev = seq) {