I am trying to get the furthest/closest vertices on both the X and Y axis for a mesh object. However, Blender returns empty lists for the coordinates for the same object. I initially thought the mesh has not been loaded into blender but then I realized that although the object has been loaded, I still might get an empty list. I may note that the problem occurs for random meshes and not only for an specific mesh. Below I post the relevant codes:
Here's how I load a mesh:
obj_file = os.path.join(model_path)
bpy.ops.import_scene.obj(filepath=obj_file)
###############################################################
## obj file comes as a bunch of different meshes, unify them ##
###############################################################
obs = []
for ob in bpy.context.scene.objects:
# whatever objects you want to join...
if ob.type == 'MESH':
obs.append(ob)
if len(obs) > 1:
# Join the meshes
ctx = bpy.context.copy()
ctx['active_object'] = obs[0]
ctx['selected_objects'] = obs
# we need the scene bases as well for joining
ctx['selected_editable_bases'] = [bpy.context.scene.object_bases[ob.name] for ob in obs]
bpy.ops.object.join(ctx)
for ob in bpy.context.selectable_objects:
#if ob.name.startswith('simplified_model'):
if ob.type == 'MESH':
obj = ob # this is for scoping reasons
obj.select = True
bpy.context.scene.objects.active = obj
if dissolve != None:
if dissolve:
# This is to be used for simplifying mesh models
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.dissolve_limited()
bpy.ops.object.mode_set(mode='OBJECT')
obj.collision.use = True
And then setting up the scene:
bpy.context.scene.render.resolution_x = 448
bpy.context.scene.render.resolution_y = 448
bpy.context.scene.render.resolution_percentage = 100
s = 1.25
bpy.context.scene.camera.location.x = s
bpy.context.scene.camera.location.y = s
bpy.context.scene.camera.location.z = s
empty = bpy.data.objects.new('empty', None)
bpy.context.scene.objects.link(empty)
bpy.context.scene.update()
camera_follow_constraint = bpy.context.scene.camera.constraints.new(type='TRACK_TO')
camera_follow_constraint.target = obj #empty
camera_follow_constraint.track_axis = 'TRACK_NEGATIVE_Z'
camera_follow_constraint.up_axis = 'UP_Y'
And finally here's how I get the coordinates. Sometimes, the xs or ys lists are returned empty and I haven't been able to find out why:
mat = obj.matrix_world
coords = [mat * vert.co for vert in obj.data.vertices]
xs = [co.x for co in coords]
ys = [co.y for co in coords]
I am using Python 3. I would appreciate if someone can give me some help on this.
Here are two links for two meshes for which I experienced the problem. I may note again that the problem does not only occur for these two models. I have thousands of meshes and each time it occurs for a bunch of them:
Thanks
obj
contains no data. It is not clear from your code whatobj
actually is. It looks like it is the last ofbpy.context.selectable_objects:
of typemesh
. Are you sure this object exists and actually have vertices? Print out some information about this object. $\endgroup$obj.name
to check that you have the correct object $\endgroup$