0
$\begingroup$

I have many objects in the scene as they have been duplicated. pad, pad.001, pad.002 etc etc. I require a python code to select all of them by name.. name starting with 'pad' Thanks in advance.

$\endgroup$
1
$\begingroup$

The following snippet iterates through the objects in the scene and checks if their name matches a prefix. If it does it's added to the list.

objs = [obj for obj in bpy.context.scene.objects if obj.name.startswith("pad")]

If you want to select them, as in selecting them in the scene, then you can do the following for 2.7x:

for obj in bpy.context.scene.objects:
    if obj.name.startswith("pad"):
        obj.select=True

For 2.8:

for obj in bpy.context.scene.objects:
    if obj.name.startswith("pad"):
        obj.select_set(True)

You could also use:

bpy.ops.object.select_pattern(pattern="pad*")
$\endgroup$
  • $\begingroup$ Traceback (most recent call last): File "<blender_console>", line 3, in <module> AttributeError: 'Object' object has no attribute 'select' $\endgroup$ – Michael Teiniker Jul 13 '19 at 11:02
  • $\begingroup$ Is this for 2.8, I've tried the script. it will not select the objects. $\endgroup$ – Michael Teiniker Jul 13 '19 at 11:08
  • $\begingroup$ @MichaelTeiniker updated the answer for 2.8. $\endgroup$ – Robert Gützkow Jul 13 '19 at 11:13
  • $\begingroup$ works, how would I make the active objects selected? I have tried this, no luck. activeObject = bpy.context.active_object $\endgroup$ – Michael Teiniker Jul 13 '19 at 11:52
  • $\begingroup$ @MichaelTeiniker I'm not sure what you mean. Active objects are already selected. $\endgroup$ – Robert Gützkow Jul 13 '19 at 11:55

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.