bpy.ops.uv.cylinder_project does not use 3D view

Hi!

I’m trying to run bpy.ops.uv.cylinder_project from Python, however the results are different from running this in the GUI; namely the Python function doesn’t seem to be using the 3D view direction. This makes it impossible to properly use this function, as the projection is dependent on the 3D view direction. Is this a bug, or am I missing something?

Steps to reproduce on Blender 2.78a (Windows 64):

  • Create a new cylinder
  • Switch to EDIT mode
  • Press NUMPAD 1 to change the 3D view to a FRONT view.
  • Press “u”, select “Cylinder Projection”. Select “View on Equator” and “Scale to Bounds”. View the UV/Image Editor and verify that the projection is correct (should fully fill the UV space).
  • In the python console, enter bpy.ops.uv.cylinder_project(direction=‘VIEW_ON_EQUATOR’, scale_to_bounds=True). View the UV/Image Editor and verify that the projection is completely different from step 4 (these should give the same results).
  • Rotate the 3D view and repeat steps 4 & 5. In step 4, you will note that the projection changes with the view angle, as expected. However, in step 5 the projection never changes, regardless of the 3D view.

Thanks for any help!
Tim

I filed a bug report for this issue, which was closed as “invalid” but had the helpful response from Bastien Montagne: “Active 3D view is part of the Context data that operators use to get the info they need. If you want to execute an operator using 3DView context data from somewhere else, you’ll have to override its context.” In case someone in the future encounters this same issue, below is a method to set the context and correctly use this operator:


for area in bpy.context.screen.areas:
    if area.type == "VIEW_3D":
        break
for region in area.regions:
    if region.type == "WINDOW":
        break
space = area.spaces[0]
context = bpy.context.copy()
context['area'] = area
context['region'] = region
context['space_data'] = space

bpy.ops.uv.cylinder_project(context, direction='VIEW_ON_EQUATOR', scale_to_bounds=True)