-1
$\begingroup$

I wonder if there is a way in script (Python) to select all the objects that does not have any image attached to their UV?

That is: all my objects do have UVs, but only some of them have some actual images assigned to their UV. What I am asking is the way automatically selecting only those object without any image assigned to their UV, as I want to reset UV of such objects afterwards (and I do not want to reset UV for those that actually already have some image assigned to their UV, of course).

Although I am Blender Python script newbie I already know how to select all the objects with Python, I also already know how to parse thru all of them, I just do not know how to select only such ones I described above.

My Python code for selecting all the object and parsing them is this:

for ob in bpy.context.selected_objects:
    bpy.context.view_layer.objects.active = ob

I would say there will be some IF after the FOR loop but have no clue what exactly (just guess), right?

Can anyone show me the functional version of this simple code so that it would select only the objects having no image assigned to their UV, please?

$\endgroup$
5
  • 1
    $\begingroup$ Since all materials in blender are node based, you would have to iterate through the node tree of each slot per object and then find all texture nodes which are (un)linked, which isn't that easy as you might guess, I'd suggest start here: Control Cycles material nodes and material properties in Python. BTW: The second line in your "selection code" sets each object to "active" which isn't really needed for this case. $\endgroup$
    – brockmann
    Commented Nov 4, 2020 at 9:22
  • $\begingroup$ Yea, right, and therefore you downvoted my newbie question, right? Unbelievable, anyway that is just a small portion of the code: it has sense for the code later in the script which is not there as it has no reason to be there, if I mistakenly or unnecesarily added a line that does not need to be there, what direct relevance it has to the merit of my question??? Stop being a virtual bully - if you have no practical answer, please, spare me of your "pseudo-wise statement" in the future...thanx $\endgroup$
    – qraqatit
    Commented Nov 4, 2020 at 11:32
  • $\begingroup$ I'm not the downvoter, see: i.sstatic.net/MdBNG.png $\endgroup$
    – brockmann
    Commented Nov 4, 2020 at 11:49
  • $\begingroup$ I don't think the question absolutely deserves the downvote, but I also hesitate to upvote it since the terminology is all wrong. You do not attach an image to UV. Image could be attached to a material and UV coordinates can be used to map it to the 3d surface. $\endgroup$ Commented Nov 4, 2020 at 15:37
  • $\begingroup$ and I absolutely do not understand how many times I have to tell you I AM NEWBIE IN PYTHON/BLENDER, so yes, it is somewhat expected that my terminology may be wrong, but adult person as me would also expect that you experienced folks around here would be also adult enough to understand that and get over it as I am sure all of you understand the merit of my question - this is really highly ridiculous, I feel like you do not care for the question and instead searching for ways/reason to put a person down...shame on you! $\endgroup$
    – qraqatit
    Commented Nov 4, 2020 at 15:42

1 Answer 1

3
$\begingroup$

So I finally make it on my own (I made it by combinig and editing several separate posts into one functional code)!

In this case (example) I go thru all the objects in the scene and select all those that do have uvmaps but don't have material with image (which in LEGO model are bricks that do have decorations on them, which was my aim with all of this as I need these to be unselected from the bunch).

All comment lines in the code are mine, so if it has some sort of not exact wording it is because english is not my native language, or simply my Blender/Python terminology is not right but I guess they are still descriptive enough for other newbies like me who has to endure virtual bullying from some moderators here (instead of normal factual answer like this code of mine here that actually do help!), so I hope it would help them a bit:

import bpy

# LOOP THRU ALL OBJECTS IN THE SCENE
for ob in bpy.context.scene.objects:
    
    # IF THIS OBJECT IS A MESH (THAT IS: NO LIGHT, NO CAMERA...)
    if ob.type=='MESH':
    
        # IF THIS OBJECT HAS UVMAP
        if ob.data.uv_layers:
        
            # IF THIS OBJECT HAS ANY MATERIAL
            if ob.active_material:
        
                # LOOP THRU ALL ITS MATERIALS
                for n in ob.active_material.node_tree.nodes:
            
                    # IF THIS MATERIAL CONTAINS IMAGE
                    if n.type=='TEX_IMAGE':
                
                        # DESELECT THIS OBJECT
                        ob.select_set(False)
                
                    # IF THIS MATERIAL CONTAINS NO IMAGE
                    else:
                    
                        # SELECT THIS OBJECT
                        ob.select_set(True)
                
            # IF THIS OBJECT HAS NO MATERIAL
            else:
            
                # SELECT THIS OBJECT
                ob.select_set(True)
            
        # IF THIS OBJECT DOESN'T HAVE UVMAP
        else:
                    
            # SELECT THIS OBJECT
            ob.select_set(True)
        
    # IF THIS OBJECT IS NOT MESH
    else:
                    
        # DESELECT THIS OBJECT
        ob.select_set(False)

EDIT: code slightly updated with suggestions from @lemon and @MartynasŽiemys - thank you for the help, guys!

$\endgroup$
8
  • $\begingroup$ Should test if ob is a mesh. ob.type == 'MESH' $\endgroup$
    – lemon
    Commented Nov 4, 2020 at 15:00
  • $\begingroup$ might be, but in my case all the object in the scene are meshes, but I can add it too, thank you! $\endgroup$
    – qraqatit
    Commented Nov 4, 2020 at 15:01
  • $\begingroup$ btw, reasons for DV are sometimes a bit obscure. Don't matter about that. $\endgroup$
    – lemon
    Commented Nov 4, 2020 at 15:03
  • $\begingroup$ code updated - thank you + I do not know what is DV $\endgroup$
    – qraqatit
    Commented Nov 4, 2020 at 15:07
  • 2
    $\begingroup$ All objects in the scene would be bpy.context.scene.objects and not bpy.data.objects. Blender's file may have multiple scenes and bpy.data.objects would contain all objects in the file. $\endgroup$ Commented Nov 4, 2020 at 15:23

You must log in to answer this question.

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