Question: Select_pattern () error with Blender 3.1.0?

Hello all,

I am a game design student and I’m currently learning how to script in Blender. I am writing a script that is supposed to filter objects based on name.
While doing research, I found out about select_pattern ().

When trying it out, it get an error I don’t fully understand. Since I am a beginner concerning scripting in Blender and programming in general, I lack the experience and knowledge to understand this error.

This is the error:

And this is the line of code that seems to be the problem:

bpy.ops.object.select_pattern(pattern = ‘*cover *’)

I started working on this in Blender 3.1.0 and now updated to Blender 3.1.2, it did not make a difference unfortunately.
I do not only get this error when I run the script, but also when I try it out via Blender’s own menu (Object mode → Select → Select Pattern)
Can someone help me understand what the problem is?

Thank you in advance!
Kind regards, Tanya

I think this might be outdated, I can’t find it in the most recent docs, and Blender python was completely re-done with the 3.0 release. Have you tried this in 2.8?

Indeed, it seems to work in 2.8.0. Does this mean that the reason for the error is that it is outdated and it cannot be used in newer versions of blender?
For my situation it would be best to use at least 3.0.0, so it’s quite an inconvenience in this case. Do you know if there are alternatives to select_pattern that work in the newer versions?

thank you for your help!

1 Like

Actually I did just find it in the 3.0 docs: https://docs.blender.org/api/3.0/bpy.ops.object.html#bpy.ops.object.select_pattern and it looks like you’re using it correctly :thinking: maybe try this- instead of bpy.ops.object.select_pattern(pattern = ‘*cover *’), try bpy.ops.object.select_pattern(‘*cover *’)

Oh and make sure you’re not using smart quotes, so the final line of code would be: bpy.ops.object.select_pattern('*cover *')

Unfortunately, this raised another error, now trying in version 3.1.2. again.
This is the error:

What I wrote:

bpy.ops.object.select_pattern(‘* cube*’)
i also tried " "

Hmmm ok… this new error makes me think the context for the operator is wrong, it’s expecting a string that defines an INVOKE or EXEC, which sounds like it doesn’t know how to execute the operator in the given context :thinking: it’s odd, though, usually with that error it just tells you straight up “no context defined for operator”. Any chance you could DM me your full code? I’m not sure how to interpret this error without a bigger picture look

Well, this is basically all code there is :smiley:

I just want to try out this function so this is the line I tried with just a cube. So next to the import bpy and the select pattern line, there is nothing

Ok I think I’ve got it- the problem seems to be the asterisks in your pattern keyword. This code works fine with no errors:

import bpy
bpy.ops.object.select_pattern(pattern="cube")

EDIT: It’s not the asterisks, as this code works too:

import bpy
bpy.ops.object.select_pattern(pattern="cube*")

I think it’s just some hyper-specific formatting thing, try copying and pasting that second one and see if it works for you

Hello! After some days of trying around I found a ‘solution’. When trying the select pattern function on itself, just like it is mentioned in the documentation, it ended up working fine.
I had tried to integrate the function into a UI panel, which was the point where the error was thrown. After some trying around it turned out to be a problem that I had named my class ‘SelectPattern’. Apparently this was a problem, because as soon as I had renamed the class and reinstalled Blender then, it worked fine. Unfortunately, it was necessary to reinstall Blender and delete all temp files for the error to go away. So in the end, it was a simple naming issue :smiley:

1 Like