1
$\begingroup$

I am in need of a way to remove a UV Map datablock of the same name from all selected objects in my scene. I have accomplished this...

import bpy

for obj in bpy.context.selected_objects:
    uvt = obj.data.uv_textures
    uvt.remove(uvt['UVMap'])

BUT I am looking to improve the functionality of my code to filter through all the selected objects, check to see which objects have the unwanted UV Map datablock, and remove the UVMap from only the matching objects.

For example: Plane.001 has "UVMap" & "Map2", and Plane.002 has only "Map2". Right now, if I have both objects selected, I get an error saying that it couldn't find bpy_prop_collection[key] "UVMap" and fails to execute the script. This must be because Plane.002 doesn't have a "UVMap" datablock...

Here's my current code:

import bpy

for obj in bpy.context.selected_objects:
    uvt = obj.data.uv_textures
    if uvt != "UVMap":
        obj.select = False

    uvt.remove(uvt['UVMap'])

How can I have blender deselect Plane.002 and then run the script only on objects that actually have "UVMap" datablock?

$\endgroup$
0
$\begingroup$

obj.data.uv_textures is a list of uvmaps. Each item in the list has a name property that you can test -

import bpy

for obj in bpy.context.selected_objects:
    uvt = obj.data.uv_textures
    for m in uvt:
        if m.name == "UVMap":
            obj.data.uv_textures.remove(m)
$\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.