2
$\begingroup$

Sorry if this has been asked many times but I couldnt find the answer that I was looking for.

Coming from Max I used to have scripts that would place the pivot to the left, right, top or bottom of an object by the push of a button. Now im trying to create something similar in blender python but I cant for the life of me figure out how it works.

I've searched online but the answers are all pretty confusing and most of them are written for versions earlier than 2.8.

However I found a script that should place the pivot on the bottom of an object AND doesn't throw an error. BUT actually it doesn't do anything at all either. So i'm wondering why this script won't work for me?

import bpy
context = bpy.context

for obj in context.selected_objects:
    mx = obj.matrix_world
    minz = min((mx @ v.co)[2] for v in obj.data.vertices)
    mx.translation.z -= minz

The pivot remains in the center of the object.

$\endgroup$
5
  • 1
    $\begingroup$ What do you mean by 'doesn't do anything at all'? It doesn't work, but it should move the object up... Does it not do that? How are you using it? $\endgroup$ Apr 21 '20 at 6:40
  • $\begingroup$ @batFINGER, you marked this as a duplicate of a post that has * instead of @ in the code and will not work with newer Python in 2.8x, so it does not fully answer the question. The OP mentioned specifically "I've searched online but the answers are all pretty confusing and most of them are written for versions earlier than 2.8." I think clearing the confusion here would be in order. $\endgroup$ Apr 21 '20 at 8:39
  • $\begingroup$ Disagree this time, don't see any issue for the OP. Also, you always can suggest an edit @MartynasŽiemys $\endgroup$
    – brockmann
    Apr 21 '20 at 8:51
  • $\begingroup$ Well, alright then. I will try to add another answer to the old question later then. $\endgroup$ Apr 21 '20 at 8:54
  • $\begingroup$ @MartynasŽiemys Yes, you are right, I saw it now. The object indeed moves up, but the pivot remains in the center. That is what i want to move to the bottom of the object. So again, this script is not correct. $\endgroup$ Apr 22 '20 at 0:39
1
$\begingroup$

If you need to move the origin to the bottom, you would need to move it down by the distance it is higher than the lowest point, so you could do:

import bpy
from mathutils import Vector

o = bpy.context.object
d = o.data
m = o.matrix_world

lowest = min((m @ v.co)[2] for v in d.vertices)
difference = Vector((0,0,o.location.z - lowest))
local_difference = difference @ m
for v in d.vertices:
    v.co += local_difference
m.translation -= difference
$\endgroup$
2
  • $\begingroup$ Sorry, I really don't have time at the moment, I will try to put it into an operator that loops through all selected and make a pie menu for setting origin to top, bottom and sides as well and share it here, when I have a minute, since I kind of need it for myself as well. $\endgroup$ Apr 21 '20 at 8:48
  • $\begingroup$ Thank you, I tried this script but my object disappears... $\endgroup$ Apr 22 '20 at 0:24

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