2
$\begingroup$

How can I select the object using a sequence of letters in the name . I know there are operators endswith and starswith, they allocate objects to spell the end or the beginning . But how can I select an object if at the beginning and at the end of the characters are the same, but differ only in the middle of the name. For example, the operator selects the object name in the end:

for ob in bpy.context.visible_objects:
      if ob.type == 'MESH' and ob.name.endswith("_P678GHNV"):
          obs.add(ob)
          ob.select = True

for example, my naming:

_Н480_2_ТХ_VALVE1_Linde_Clea
_Н480_3_ТХ_VALVE2_Linde_Clea

And I need to select the objects that include the VALVE in the name

$\endgroup$
3
  • $\begingroup$ please show a couple of examples of your naming convention. $\endgroup$ Commented Jan 6, 2016 at 9:42
  • $\begingroup$ but in this case exactly $\endgroup$ Commented Jan 6, 2016 at 10:10
  • $\begingroup$ I think whatever way you look at this, it is more a general Python string processing question rather than directly Blender Python related. There will be numerous ways to do it, depending on your object naming convention. $\endgroup$ Commented Jan 6, 2016 at 10:15

2 Answers 2

2
$\begingroup$

You could also use regular expressions which allow you flexible pattern matching:

import re
import bpy

pattern = re.compile("PREFIX.*POSTFIX") # edit this string

for ob in bpy.context.visible_objects:
    if ob.type != 'MESH':
        continue

    match = pattern.match(ob.name)
    if match == None:
        print("no match for %s"  % ob.name)
    else:
        print("matched %s" % match.group())
        ob.select = True
$\endgroup$
2
$\begingroup$

Let's not overlook that you can test if a name contains a certain sequence of letters.

for ob in bpy.context.visible_objects:
      if ob.type == 'MESH' and ('VALVE' in ob.name):
          obs.add(ob)
          ob.select = True
$\endgroup$
1
  • $\begingroup$ for example, my naming: _Н480_2_ТХ_VALVE1_Linde_Clea, _Н480_3_ТХ_VALVE2_Linde_Clea And I need to select the objects that include the VALVE in the name $\endgroup$ Commented Jan 6, 2016 at 9:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.