0

I am trying to use Python to mirror the UVs of my mesh. The UVs get unwrapped just fine but when I run the mirror command it is just a normal bpy.ops.transform command and the object gets mirrored and the UVs do not change. I looked a bit online and it seems that I need to access the UVs through a couple loops, however I was unable to get them to mirror with that approach.

Does anyone know how to mirror the UVs in Python?

Mirror command from the scripting Info window:

bpy.ops.transform.mirror(orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)

Mirror command from the interface: enter image description here

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
  • bpy.ops commands take bpy.context into account, they are more suitable for the users interaction through gui. Best to access the UVs through vertex loops. – Jaroslav Jerryno Novotny Mar 30 '21 at 03:42
  • While accessing the UVs through a loop with the below code I am not sure how to make it mirror the UVs. obj = context.active_object me = obj.data bm = bmesh.from_edit_mesh(me)

    uv_layer = bm.loops.layers.uv.verify()

    adjust uv coordinates

    for face in bm.faces: for loop in face.loops: loop_uv = loop[uv_layer]

    bmesh.update_edit_mesh(me)

    – Clinton Walsh Mar 30 '21 at 03:51
  • Using bmesh: https://blender.stackexchange.com/a/19727/7777, not using bmesh: https://blender.stackexchange.com/a/30679/7777 – Jaroslav Jerryno Novotny Mar 30 '21 at 04:24
  • Thank you for the help Jaroslav. I ended up going with the code posted in the answer below. – Clinton Walsh Mar 31 '21 at 03:02

1 Answers1

1

Thank you for the help. Instead of working with the UVs in the loops (which is probably the proper way of doing things) I used the below code. It changes the context to allow bpy.ops.transform.mirror to work.

original_area = bpy.context.area.type
bpy.context.view_layer.objects.active = unreal_mesh

bpy.context.area.type = 'IMAGE_EDITOR' bpy.ops.object.mode_set(mode='EDIT', toggle=False)

bpy.ops.mesh.reveal() bpy.ops.mesh.select_all(action='SELECT') #+ select the uvs bpy.ops.uv.select_all(action='SELECT')

bpy.ops.transform.mirror(orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)

#+ return to the original mode where the script was run bpy.context.area.type = original_area bpy.ops.object.mode_set(mode='OBJECT', toggle=False)