2
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ Tips on how to use the blender documentation? What do you mean? $\endgroup$
    – TheLabCat
    Apr 20, 2021 at 2:50
  • $\begingroup$ 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, $\endgroup$
    – Ebi
    Apr 20, 2021 at 3:45
  • $\begingroup$ 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. $\endgroup$
    – TheLabCat
    Apr 20, 2021 at 3:45
  • $\begingroup$ I might need to get a better mic... 😬 $\endgroup$
    – Ebi
    Apr 20, 2021 at 6:56

1 Answer 1

6
$\begingroup$

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)
$\endgroup$
2
  • $\begingroup$ thanks a lot for the detailed answer, that works like a charm. And the list option is a great addition i didn't think of 😃 $\endgroup$
    – Ebi
    Apr 20, 2021 at 6:54
  • $\begingroup$ Done, @Ratt ;). $\endgroup$
    – Ebi
    Apr 20, 2021 at 9:26

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.