1
$\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$
  • $\begingroup$ please show a couple of examples of your naming convention. $\endgroup$ – zeffii Jan 6 '16 at 9:42
  • $\begingroup$ but in this case exactly $\endgroup$ – Lev Jan 6 '16 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$ – zeffii Jan 6 '16 at 10:15
1
$\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$
1
$\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$
  • $\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$ – Lev Jan 6 '16 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.