3
$\begingroup$

I'm trying to UV unwrap an object in order to overlay a texture, using Python scripting. The mesh is generated on the fly, is flat, and the top vertices are selected. Unwrapping should happen in the normal coordinate system (x horizontally, y vertically), and scaled to the unwrappping area.

I'm setting the view (numpad 7) and using project_from_view, which using the interface gives the correct result: correct result with project_from_view

However, doing the same from a Python script does not work: it's projected from the initial view (skewed), and the view isn't updated when set from the python script (although I do see the interface being updated immediately after the UI comes up after the script):

enter image description here

def get_context(_type='VIEW_3D'):
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == _type:
                for space in area.spaces:
                    if space.type == _type:
                        for region in area.regions:
                            if region.type == 'WINDOW':
                                context = {'window': window, 'screen': screen, 'area': area,
                                           'region': region, 'edit_object': bpy.context.edit_object,
                                           'scene': bpy.context.scene, 'blend_data': bpy.context.blend_data}
                                return context

c = get_context()
bpy.ops.view3d.viewnumpad(context, type='TOP')
bpy.ops.view3d.view_persportho(context)
bpy.ops.uv.project_from_view(context, camera_bounds=False, orthographic=False,
                             correct_aspect=True, clip_to_bounds=True, scale_to_bounds=True)

Running from command line with: blender -noaudio --factory-startup --python script.py

Any ideas on how to update the view from the command line? Trying out various ideas elsewhere (adding bpy.ops.view3d.smoothview(context), bpy.ops.screen.screen_full_area(context), setting region_3d.view_matrix explicitly) did not work so far either.

I'm aware of the non-ideal workflow of using UI elements in scripts and would love to use a more trivial unwrapping feature, but it seems to be missing, as all other unwrap tools try to be too 'smart':

wrong results

$\endgroup$
  • $\begingroup$ Maybe what you want and what I would done is to "unwrap" by writing values directly into UV coordinates with python. If your mesh is flat that should be as easy as just scaling the world coordinates into <0,1> range. There are many answers here on this site how to access UV coords with python. $\endgroup$ – Jaroslav Jerryno Novotny Nov 13 '15 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.