Add-ons and more

Tiny Blender Addon: Snap and transform

Enhancements available now: check the description

I was doing some arch-viz the other day and I was placing a lot of objects in a large scene. The objects where placeholders that I created on the spot and more often than not I needed to move the origin of the new mesh object to a selected vertex for easy positioning, scaling, rotating etc.

This is of course simple enough: select Snap cursor to selected in edit mode, switch to object mode and then select Transform → origin to 3d cursor.
But it is also a lot of actions for a simple operation, especially if you doing this a hundred times in a scene...

Another common scenario that I encounter is that I want to position the origin at the lowest point of a mesh. This is a little bit more involved as far as the code is concerned, a small explanation below for those who are interested in doing this on large meshes in a fast way.

Anyway, here is a small add-on: Edit mode origin tools. It does nothing fancy, it will just create two new menu entries in edit mode:
Mesh → Snap → Origin to selected,
Mesh → Snap → Origin to lowest vertex (along z-axis)
and save you some time :-)

Code availability

Download it from my GitHub repository (right-click on the first link and select save as ...) and in Blender go to File → User preferences → Add-ons → install from file ... (don't forget to enable it after installation. Note that the downloaded file is called snapandtransform.py while the add-on will appear as Edit mode transform tools)

Finding the location of the lowest vertex (fast)

If we want to get all the vertex coordinates fast, we got to switch to object mode first (line 2), get the number of vertices present (line 4) and the allocate an empty numpy array to hold all coordinates (line 7). Then we can use the foreach_get() method to get all coords (the co attribute of the verts array) in one go (line 8). It will be a flattened array so we have to reshape it to an array of 3-vectors (line 9).
 def execute(self, context):
  bpy.ops.object.editmode_toggle()
  me = context.active_object.data
  count = len(me.vertices)
  if count > 0:  # degenerate mesh, but better safe than sorry
   shape = (count, 3)
   verts = np.empty(count*3, dtype=np.float32)
   me.vertices.foreach_get('co', verts)
   verts.shape = shape
   verts2 = np.ones((count,4))
   verts2[:,:3] = verts
   M = np.array(context.active_object.matrix_world,
       dtype=np.float32)
   verts = (M @ verts2.T).T[:,:3]
   min_co = verts[np.argsort(verts[:,2])[0]]
   context.scene.cursor_location = min_co
   bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
  bpy.ops.object.editmode_toggle()
  return {'FINISHED'}
Now all the coordinates will be in object space so we will want to convert all of them to world space. For this we need to multiply each of them with the matrix_world of the object. This is necessary because due to rotations the lowest vertex in object space need not be the lowest vertex in world space!

The world matrix is a 4x4 matrix (one that holds not only scale and rotation but translation as well) so we need to extend all our coordinate vectors with a fourth coordinate of 1 (lines 10,11). We also convert the matrix_world to a numpy array (line 12).

Line 14 is then where all the magic happens: we multiply our numpy world matrix M with our array of extend coordinates using the new @ operator. (new since Python 3.5 and especially added to allow numpy code to be better readable). The double transpose is to allow matrix multiplication of a 4x4 matrix with a list of 4-vectors and transform the result back again. The fourth coordinate of the result is dropped by the [:,:3] slice index.

Now that we have converted all coordinates to world space, all we have to do is the find the index of the coordinate with the lowest z-coordinate with argsort() and assign this to the position of the 3d-cursor before calling the origin-set() operator.

5 comments:

  1. Hi Michel, thks for the correction and adding this new feature but I have this traceback with numpy>multiarray
    I use blender 2,78c and 2,79 numpy.
    but in numpy>core, not multiarray py file, just this:
    multiarray.cp35-win_amd64.pyd

    and addon_utils.py can't find it normally!
    [code]
    Warning! Legacy WGL is unable to select between OpenGL versions.Traceback (most recent call last):
    File "C:\Program Files\Blender Foundation\Blender\2.78\scripts\modules\addon_utils.py", line 330, in enable
    mod = __import__(module_name)
    File "C:\Users\lenovo\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\addons\mesh_snapandtransform.py", line 23, in
    import numpy as np
    File "C:\Users\lenovo\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\modules\numpy\__init__.py", line 180, in
    from . import add_newdocs
    File "C:\Users\lenovo\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\modules\numpy\add_newdocs.py", line 13, in
    from numpy.lib import add_newdoc
    File "C:\Users\lenovo\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\modules\numpy\lib\__init__.py", line 8, in
    from .type_check import *
    File "C:\Users\lenovo\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\modules\numpy\lib\type_check.py", line 11, in
    import numpy.core.numeric as _nx
    File "C:\Users\lenovo\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\modules\numpy\core\__init__.py", line 14, in
    from . import multiarray
    ImportError: cannot import name 'multiarray'
    [\code]

    ReplyDelete
  2. I am not 100% certain but it looks like your scipts directory is messed up: numpy and all its libs and .py files normally are inside Blenders' python folder, for example /home/michel/blender-2.78a/2.78/python/lib/python3.5/site-packages/numpy/

    You could try a fresh installation (of the new release candidate for example) and then NOT copy existing confgurations when you first start it (otherwise it will copy your scripts directory as well)

    ReplyDelete
  3. Hi Michel, this is such an excellent addon! Simple but a fantastic time-saver. Thanks for sharing. Konrad Welz

    ReplyDelete
  4. This is an irreplaceable addon! To measure, to align, it is used for each movement. Congratulations!

    ReplyDelete
  5. Please when will the blender version 2.8 be available?

    ReplyDelete