1
$\begingroup$

I've been trying to make a small python script that among other things project unwraps UVs using a specific axis and projection type and it fails to project the UVs at the 3D View's manipulated camera angle.

Every time I have tried to use bpy.ops.view3d.view_axis to manipulate the 3D View's camera and get a UV unwrap at a specific angle all I get is the angle from where the camera was before the script executed, not the camera changes I made in the script

I have tried different ways to substitute contexts and other weird things to see if there's a bug but no dice.

Here's what I have so far, any thoughts would be greatly appreciated:


import bpy
context = bpy.context
object = context.active_object


prev_region = bpy.context.area.type
bpy.context.area.type = 'VIEW_3D'

prev_perspective = bpy.context.space_data.region_3d.view_perspective

if bpy.context.space_data.region_3d.view_perspective == 'PERSP':
    bpy.ops.view3d.view_persportho()


# https://blender.stackexchange.com/questions/15118/how-do-i-override-context-for-bpy-ops-mesh-loopcut
win      = bpy.context.window
scr      = win.screen
areas3d  = [bpy.context.area]
region   = [region for region in areas3d[0].regions if region.type == 'WINDOW']

override = {'window':win,
            'screen':scr,
            'area'  :areas3d[0],
            'region':region[0],
            'scene' :bpy.context.scene,
            'space' :areas3d[0].spaces[0],
            }
 
bpy.ops.wm.toolbar(override)

print(override['window'])
print(override['area'].type)
print(override['region'].type)

bpy.ops.object.mode_set(override, mode='EDIT')
bpy.ops.mesh.select_all(override, action='SELECT')

bpy.ops.mesh.uv_texture_add(override)
object.data.uv_layers[0].name = 'Color Spread'
object.data.uv_layers[0].active_render = True
object.data.uv_layers[0].active = True


# The Problem
#-----------------------------------
# Changing the view_axis won't change the view when using
# bpy.ops.uv.project_from_view() even though the 3D view
# has been changed.

bpy.ops.view3d.view_selected(override)
bpy.ops.view3d.view_axis(override, type='TOP')




# Failures so far
#-----------------------------------
# Setting view axis multiple times
# Setting projection and view axis multiple times
# AGGRESSIVELY SETTING OVERRIDES FOR EVERYTHING
# Using an Override with as many details as possible
# Only having one 3D View Active
    
    
bpy.ops.uv.project_from_view(override, correct_aspect=True, 
    clip_to_bounds=True, scale_to_bounds=True)
```
$\endgroup$

1 Answer 1

4
$\begingroup$

Run region_3d.update() before project_from_view().

Made few changes to run script without changing window type to 3D View

import bpy

def getArea(type):
    for screen in bpy.context.workspace.screens:
        for area in screen.areas:
            if area.type == type:
                return area
            
override = {'area': getArea('VIEW_3D')}


import bpy
context = bpy.context
object = context.active_object


prev_region = bpy.context.area.type

for ns3d in getArea('VIEW_3D').spaces:
    if ns3d.type == "VIEW_3D":
        break

# https://blender.stackexchange.com/questions/15118/how-do-i-override-context-for-bpy-ops-mesh-loopcut
win      = bpy.context.window
scr      = win.screen
areas3d  = [getArea('VIEW_3D')]
region   = [region for region in areas3d[0].regions if region.type == 'WINDOW']

override = {'window':win,
            'screen':scr,
            'area'  :getArea('VIEW_3D'),
            'region':region[0],
            'scene' :bpy.context.scene,
            'space' :getArea('VIEW_3D').spaces[0],
            }
            
prev_perspective = ns3d.region_3d.view_perspective

if ns3d.region_3d.view_perspective == 'PERSP':
    bpy.ops.view3d.view_persportho(override)


print(override['window'])
print(override['area'].type)
print(override['region'].type)

bpy.ops.object.mode_set(override, mode='EDIT')
bpy.ops.mesh.select_all(override, action='SELECT')

bpy.ops.mesh.uv_texture_add(override)
object.data.uv_layers[0].name = 'Color Spread'
object.data.uv_layers[0].active_render = True
object.data.uv_layers[0].active = True


# The Problem
#-----------------------------------
# Changing the view_axis won't change the view when using
# bpy.ops.uv.project_from_view() even though the 3D view
# has been changed.

bpy.ops.view3d.view_selected(override)
bpy.ops.view3d.view_axis(override, type='TOP')

# Update region:
ns3d.region_3d.update()

# Failures so far
#-----------------------------------
# Setting view axis multiple times
# Setting projection and view axis multiple times
# AGGRESSIVELY SETTING OVERRIDES FOR EVERYTHING
# Using an Override with as many details as possible
# Only having one 3D View Active
    
    
bpy.ops.uv.project_from_view(override, correct_aspect=True, 
    clip_to_bounds=True, scale_to_bounds=True)
$\endgroup$
1
  • 1
    $\begingroup$ Thanks so much for responding, this fixed it! $\endgroup$
    – Takanu
    Mar 18, 2022 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .