5
$\begingroup$

I want to use python to move the UVs of the selected objects. I am having trouble with moving the UVs. When I run my script I moves the objects instead, not the UVs. All object have exactly 1 UV map.

My approach the problem:

for mesh in selection:

  • for UVs in mesh

    • select all UVs

      • move UVs

Here is my code.

import bpy

scene = bpy.context.scene
for count, ob in enumerate(bpy.context.selected_objects):
    if ob.type == 'MESH':
        scene.objects.active = ob
        bpy.ops.object.mode_set(mode='EDIT', toggle=False)

        bpy.ops.mesh.reveal()
        bpy.ops.mesh.select_all(action='SELECT')

        # execute any editmode tool
        move_x = count % 10
        move_y = count // 10
        bpy.ops.transform.translate(value=(move_x, move_y, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.263331)


        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
$\endgroup$
0

1 Answer 1

5
$\begingroup$

It's all about context, at no point are you in the image editor. The same bpy.ops.transform.translate() is used in the 3d view and the image editor. Basically, you could just save the current area, switch to the editor, perform transforms and switch back. Granted, there are much more elegant ways to do this but for a 3 line hack, it works quite well.

import bpy

#+ grab the current area
original_area = bpy.context.area.type

view_layer = bpy.context.view_layer
for count, ob in enumerate(bpy.context.selected_objects):
    if ob.type == 'MESH':
        view_layer.objects.active = ob
        #+ switch to the UV editor to perform transforms etc
        bpy.context.area.ui_type = 'UV'
        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.translate(value=(0.125, 0, 0), constraint_axis=(True, False, 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$
7
  • $\begingroup$ So the command for transform in the UV view and 3d view is the same? Is there a unique command for moving in the UV? $\endgroup$
    – Vader
    Commented Mar 5, 2014 at 17:36
  • $\begingroup$ @Vader No and it wouldn't work, you need to be in that space by some means when doing any operations in it. $\endgroup$
    – iKlsR
    Commented Mar 5, 2014 at 17:39
  • $\begingroup$ This does not work, the UVs. Stay in place $\endgroup$
    – Vader
    Commented Mar 5, 2014 at 19:24
  • $\begingroup$ @Vader Are you using the exact same code in the example? It works on my end. Unwrap a mesh or several, go to object mode and run the script. $\endgroup$
    – iKlsR
    Commented Mar 5, 2014 at 19:25
  • $\begingroup$ Instead of using the value 0.125 I am using count. $\endgroup$
    – Vader
    Commented Mar 5, 2014 at 19:31

You must log in to answer this question.

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