# How to Unwrap project from view with script?

I was trying to make a importer/exporter and place a UV mapping operation in the middle. my only issue is that when I try to a unwrap_project_from_view it gives me null results.

what is the correct way to unwrap_project_from_view a mesh in from a top view with ortho??

My current progress:

import bpy

# Variables
obj_file = "D:\\HUD move left\\OBJs\\HUD Move_Left Grey_000172.obj"
dae_file = "D:\\HUD move left\\DAEs\\HUD Move_Left Grey_000172.dae"
hud_object = 'HUD Move_Left Grey_000172'

# Import OBJ
bpy.ops.import_scene.obj(filepath=obj_file)

# Set active Object
object = bpy.data.objects[hud_object]
bpy.context.scene.objects.active = object

# Search for 3D Area on layout
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
space_data = area.spaces.active

# Change to Edit mode
bpy.ops.object.mode_set(mode = 'EDIT')

# Unwrap (!!! NOT WORKING YET !!!)
if bpy.ops.uv.project_from_view.poll():
bpy.ops.uv.project_from_view(orthographic=True, scale_to_bounds=True)

# Change to Object mode
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')

break

# Export DAE

# Delete Mesh in Scene
bpy.data.objects[hud_object].select = True
bpy.ops.object.delete()

• any updates on this? – gonephishing Mar 27 '18 at 7:43

I bumped into the same issue and finally solved it! you just need to have a 3d viewport opened with your desired view and run this script:

import bpy

for obj in bpy.context.selected_objects:
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action= 'SELECT')
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
override = {'area': area, 'region': region, 'edit_object': bpy.context.edit_object}
bpy.ops.uv.project_from_view(override , camera_bounds=False, correct_aspect=True, scale_to_bounds=True)
bpy.ops.object.mode_set(mode = 'OBJECT')


Hope I helped you!

Cheers!

• This is bad in many ways but mostly because you are using entirely view-dependent operator without ensuring the view is set to orthogonal top down view. In fact this script will execute UV projecting for every 3D view you have open. The proper way would be to eliminate the view-dependent operator and instead assign UVs based on vertex coordinates, discarding Z coordinate which is basically what ortho top down project from view does. – kheetor May 8 '18 at 23:05