Set origin to Matrix_world bottom center of Lego bricks's bound box
guys. I get the answer about the question. Thanks for the help from Martynas Žiemys
this is the code:
def origin_to_bottom(ob):
if ob.mode != 'OBJECT':
    bpy.ops.object.mode_set(mode='OBJECT', toggle=True)

d = ob.data
m = ob.matrix_world

bounds_center = (sum((m @ Vector(b) for b in ob.bound_box), Vector()))/8
difference = m.translation - bounds_center
local_difference = difference @ m
for v in d.vertices:
    v.co += local_difference
    m.translation -= difference                   

difference = Vector((0,0,0))
bound = min((m @ v.co).z for v in d.vertices)
difference.z = m.translation.z - bound

local_difference = difference @ m
for v in d.vertices:
    v.co += local_difference
    m.translation -= difference