0
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ – Jaroslav Jerryno Novotny Mar 30 at 3:42
  • $\begingroup$ 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) $\endgroup$ – Clinton Walsh Mar 30 at 3:51
  • $\begingroup$ Using bmesh: blender.stackexchange.com/a/19727/7777, not using bmesh: blender.stackexchange.com/a/30679/7777 $\endgroup$ – Jaroslav Jerryno Novotny Mar 30 at 4:24
  • $\begingroup$ Thank you for the help Jaroslav. I ended up going with the code posted in the answer below. $\endgroup$ – Clinton Walsh Mar 31 at 3:02
0
$\begingroup$

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)
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.