How do I select a random face?

Since it’s always been a challenge to work with Python in Blender, I decided to ask ChatGPT for help :slight_smile:
All I am trying to do right now is just to enter Edit mode and select a random face. Seems like trivial task, but either impossible for ChatGPT to solve or I am asking something wrong. What I get is this:

import bpy
import random

Get the active object (assuming it’s a mesh)

obj = bpy.context.active_object

Ensure we are in edit mode with face selection

bpy.ops.object.mode_set(mode=‘EDIT’)
bpy.ops.mesh.select_mode(type=‘FACE’)

Get the mesh data

mesh = obj.data

Deselect all faces first

bpy.ops.mesh.select_all(action=‘DESELECT’)

Select a random face

if mesh.polygons:
random_face_index = random.randint(0, len(mesh.polygons) - 1)
mesh.polygons[random_face_index].select = True

And this code does nothing (it enters Edit mode, but doesn’t select a random face). Why?

As you can clearly see, ChatGPT(especially the free version) is really terrible at coding outside the most basic and most popular stuff in it’s training dataset. It’s still really not the best way to learn with it. You could ask it questions about Python in general and probably get good answers and use it that way, but learning the regular old-fashioned way is still a very good idea. Start with Python, maybe look for some more resources for learning it like w3schools or video tutorials on YouTube and learn the basics at least, preferably more. You will find that Python is one relatively very easy language to learn and it has amazing resources online. There is this really nice situation with Python - you can easily find really good material to help you learn it online.

Once you are done with it, progress to Blender’s Python API. the documentation is amazing, you have plenty of websites to help you with it as well like Blender Stack Exchange, these Blenderartists.org forums as well. There are also examples in Blender’s editor and then you have one of the greatest most amazing thing for it you could ever dream of - virtually all the add-ons you can get are just written in Python using Blender’s Python API and they have to be plain text for them to work so you can study them.

import bpy
import random

C = bpy.context
d = C.object.data

# you can select and deselect faces from object mode
if C.mode != 'OBJECT':
    bpy.ops.object.mode_set(mode='OBJECT')

# deselect all
verts, faces, edges = d.vertices[:], d.polygons[:], d.edges[:]
for x in faces + edges + verts:                   
    x.select = False               

#select random face
face = random.choice(faces)
face.select = True

# I have no idea if that's the case, but I assume you 
# want to be in edit mode to see the selected face
if C.mode != 'EDIT':
    bpy.ops.object.mode_set(mode='EDIT')

You could as well use bmesh. I think you should find answers to that on Blender Stack Exchange if you serach the web for it.

3 Likes

Thanks! Sadly, I don’t have a lot of free time to learn yet another thing :frowning: I was hoping to utilize this new tech, but I guess they handicapped it beyond belief. I used ChatGPT when they just rolled it out and it did wonders. It helped me to materialized a few automation scripts based on my step by step input.

So, is ChatGPT’s code I posted just a bunch of random code that has nothing to do with Blender’s API ?

No it’s not random, just wrong, because you need to select and deselect elements that way from object mode. It doesn’t update from edit mode and then back. If you had a reason to stay in edit mode, like for example you are writing an add-on and want it to be professional and more efficient and avoid updates happening when switching modes as well as using operators that are really meant for the user and not so much for the scripts, you would use bmesh.