Having trouble with view3d_utils.location_3d_to_region_2d

I’m having an issue with a script I wrote where from certain angles, the script fails. This is the line of code that sometimes just returns NoneType:

view3d_utils.location_3d_to_region_2d(viewport, bpy.context.space_data.region_3d, selVerts[0].co).y

Does anybody know why this would sometimes return nothing? I’ve attached the script in its entirety in case that helps. The idea is that you can align a selection to the farthest left/right/up/down vertex in that selection, relative to the view. It’s kind of ugly, and has a crappy method of determining view angles, so apologies for the icky mess :stuck_out_tongue:

pat_smartAlign.py (5.2 KB)

I’m sure you’ve solved this by now (just spotted this in a search that I was running) but if not, the issue is normally when you’re trying to run location_3d_to_region_2d on geometry that’s behind the viewport. Let me know if you still need help with this and I’ll try to work out a fix for your code.