3
$\begingroup$

I would like to know if there is a easy way to get the location of the current selection? (The one used by translate/rotate/scale, and depending on selected elements and the center options like median/active element/bbox/3d cursor.)

Or to put it differently is there a way to get the 4x4 matrix of the manipulator? So I can grab the current orientation and position in one go.

I'm aware of the set_cursor_to_selected() operator, which allows me to grab this location via the 3d_cursor location. but as I'm running it several times per second this locks away the 3d cursor and spams the info log. And it feels hacky and clunky...

So if there is a more elegant way than to recreate the blender behaviour, I would be very interested..

$\endgroup$
1
$\begingroup$

Yes, you can get it without moving the cursor.

    import bpy

o = bpy.context.object
vcos = [ o.matrix_world * v.co for v in o.data.vertices ]
findCenter = lambda l: ( max(l) + min(l) ) / 2

x,y,z  = [ [ v[i] for v in vcos ] for i in range(3) ]
center = [ findCenter(axis) for axis in [x,y,z] ]

print( center )

(That was not made by me)

Will print the location of the active object.

Note, in the image it shows [5.960464477539063e-08, -1.1920928955078125e-07, 3.3469696044921875] instead of [0, 0, 3.3469696044921875] but it's a super tiny difference of at most 0.00000011920928955078125 Example

$\endgroup$
  • $\begingroup$ Thanks, but this is not really what I had in mind, as this will result in replicating all the pivot modes... which I try to avoid. The code itself looks handy for implementing it :) $\endgroup$ – kio Aug 1 '17 at 13:51
  • $\begingroup$ Glad I could help. $\endgroup$ – Wrenth Davis Aug 1 '17 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.