I have to select the exact half of a mesh in Blender 2.79 thorugh a Python script. I tried obd = bpy.data.objects[actor_name].dimensions for a selected object but these coordinates seem not to be the right ones. If i use the half of each value like bpy.ops.view3d.select_border(override, gesture_mode=3, xmin=0, xmax=obd[0]/2, ymin=0, ymax=obd[1]/2, extend=True) its far away from selecting the exact half of a mesh.

Is there any mistake in the usage or is there another option to get the size of a mesh in Blender?

  • $\begingroup$ You will find the coordinates required for box select operator are 2d region coordinates as opposed to components of 3d coordinates, which have nothing to do with object dimensions. The bottom left hand corner of the 3d view region is always (0, 0), the top RH corner (context.region.width, context.region.height) $\endgroup$ – batFINGER Jan 10 at 14:11
  • $\begingroup$ I think defining 'center' would also be a good idea. Center can be the center of mass, average of coordinates of all vertices, it could be the center of a bounding box of the object, then it would need to be defined as well - what orientation should be considered for the bounding box, because it needs some axis - is it local object space, global space?.. Center can be many things... $\endgroup$ – Martin Z Jan 10 at 14:17

Selecting the local left half

Which half? top, bottom, front, back, left or right... or other..

Here is a test script, run in edit mode, selects the local left half of edit object including those on centre plane.

A plane can be defined by a point in space plane_co, and its normal plane_no.

The mathutils.geometry.distance_point_to_plane(point, plane_co, plane_no) returns how far a point is from a plane along its normal. Will be negative if on other side to which normal is facing.

The plane coordinate is the centre of the bounding box, the normal is one of the 3 axes made from a corner.

For this example the x axis of bounding box is chosen, to give left - right selection. Choose y for front-back, and z for top-bottom.

import bpy
import bmesh
from mathutils.geometry import distance_point_to_plane
from mathutils import Vector

context = bpy.context

def bbox(ob):
    return (Vector(b) for b in ob.bound_box)

def bbox_center(ob):
    return sum(bbox(ob), Vector()) / 8

def bbox_axes(ob):
    bb = list(bbox(ob))
    return tuple(bb[i] - bb[0] for i in (4, 3, 1))

ob = context.edit_object
o = bbox_center(ob)
x, y, z = bbox_axes(ob) 

print(o, x, y, z)
# select left right

me = ob.data
bm = bmesh.from_edit_mesh(me)
for v in bm.verts:
    v.select = distance_point_to_plane(v.co, o, x) >= 0


enter image description here Test run on Suze

  • $\begingroup$ Well, the complete half (doesn't matter if left or right first, I need both) including top and bottom of the mesh. But it works perfectly! Thanks! $\endgroup$ – SnoopyBrown Jan 10 at 14:15

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.