Select all object with the name of the selected object

Hello, I’m trying to learn Blender python (let’s say no more beginner guy in coding ever existed).
I’m trying to make some kind of premade selection sets (patterns).

The actual select pattern is not too handy, I would like to implement it this way:

  1. I select an object
  2. Hit a button what selects all the objects with the same name (like I select ‘appleV01 C01_.001’, after hitting the button it selects ‘apple V01 C01_.001’, ‘apple V01 C01_.002’ and so on without using the 'Select pattern" menu.

What I need is to know how I could put to a python code the name of the object with simple selecting it.

Pattern selection works like this:

bpy.ops.object.select_pattern(pattern=“apple_”)

I would like to substitute the *apple" with something like “getselectedactiveobjectname without numbering”
Sorry for the ridiculous example, I just decided today to make a script for that.

I would use something to make the difference between name and numbering (like the ‘_’ in the example)

Thanks

If you know about using naming patterns you’re not a total programming n00b :D. This wouldn’t be all that difficult from the sounds of it, you would just have a script that trimmed off all the characters up to (and including) the last dot/period for the name of the current selected object (the “active” object), have it store that part as the search pattern, and then go through the rest of the objects in the scene trimming off the same stuff and seeing if they match the pattern after being trimmed.

pseudo code

On_Button_Press
Store_Active_Object_Name
Trim_Off_Everything_From_Last_Char_To_Last_Period_Of_Stored_Name
Save_Trimmed_Name_As_Pattern
Get_List_Of_Objects_In_Scene
From_First_Object_in_List_Until_LastGet_Object_Name
Trim_Off_Everything_From_End_To_Last_Period
If_Trimmed_Name_Matches_Pattern
[INDENT=2]Select_Object[/INDENT]

I don’t know how to code the button part, but to get the current selected/active object name I think the code is:
bpy.context.object.name

Then to search through all the objects in the scene, you could use something like:


for obj in bpy.context.scene.objects:
    tmp_name = obj.name
    # code to trim off last bit of tmp_name... (can't remember how to do this off hand)
    if trimmed_name == pattern:
        obj.select = True

nBurn:

Thank you very much.
You exactly wrote it down with the ‘pseudo code’ how I should think in coding and what missing from my mindset as a beginner (I used to draw flowcharts, but from user (GUI) perspective, which is different.

I was able to make some steps forward yesterday (making the menu, calling the pattern selection module, but without the concrete pattern).
I’m not sure in the next few days I will have time to go further (job), but I will test your example this week and I will share my experiences.

Thanks again.