2

I am trying to automate the process of removing all UV layers except for one named 'baked' on an active object. looking online I can only see answers using data.uv_textures, something that changed in Blender's API.

I am very new to scripting and don't really understand how to read and use the Blender Documentation

I would like to go from enter image description here

to this enter image description here

Also, Any tips of how to use the blender documentation will be greatly appreciated. thanks.

Ebi
  • 871
  • 6
  • 16
  • Tips on how to use the blender documentation? What do you mean? – TheLabCat Apr 20 '21 at 02:50
  • Just trying to work with it and it feels very confusing, again, I don't really know coding so it might be a dumb request. MeshUVLoopLayer or UVLoopLayer for instance, – Ebi Apr 20 '21 at 03:45
  • Well, I already know Python and I was getting confused by the Blender API, but it’s not really all that hard. Mostly just finding out where stuff is. However, if you have no coding experience at all, “consider purchasing a better microphone.” Try SoloLearn for free introductory Python courses. – TheLabCat Apr 20 '21 at 03:45
  • I might need to get a better mic... – Ebi Apr 20 '21 at 06:56

1 Answers1

6

Based on @batFINGER answer

The following allows for passing either a single string of a uv_map name to keep or a list of names to keep.

Since the function rem_uvs takes a parameter reference of the object a check is made to ensure the object is a mesh type (which may have uv_maps). Attempts to iterate through objects that do not include data.uv_layers would cause AttributeError and the script would fail.

In order to avoid errors caused when modifying a loop control all items that are intended to be removed are 1st added to their own list. That list is then used to remove the uv_maps from the object.

Be sure to only use one of the 2 function calls at a time or include the single name in the list of names.

import bpy

def rem_uvs(uvs_to_keep, ob): uvs_to_rem = [] if type(uvs_to_keep) == 'str': uvs_to_keep = list(uvs_to_keep) if ob.type != 'MESH': return for uv in ob.data.uv_layers: if uv.name in uvs_to_keep: continue uvs_to_rem.append(uv) while uvs_to_rem: ob.data.uv_layers.remove(uvs_to_rem.pop())

providing single name to keep

rem_uvs("baked", bpy.context.object)

providing list of names to keep

names = [ "AO", "Combined", "Normal", ] #rem_uvs(names, bpy.context.object)

Ratt
  • 2,126
  • 1
  • 10
  • 17