Searching tags using Python

Is there a way to collect all shotgun assets for a project where at least one take contains the string being searching?

For example we have a bunch of Assets that are tagged with keywords like the following…

[ 
  { 'code': 'Blue_Particle_Motion_Background_1080',
    'id': 23,
    'tags': [ { 'id': 212, 'name': 'fire', 'type': 'Tag'},
              { 'id': 277, 'name': 'pyro', 'type': 'Tag'}],
    'type': 'CustomNonProjectEntity08'
  },
  { 'code': 'Crazy_Balls',
    'id': 24,
    'tags': [ { 'id': 212, 'name': 'fire', 'type': 'Tag'},
              { 'id': 277, 'name': 'pyro', 'type': 'Tag'},
              { 'id': 209, 'name': 'smoke', 'type': 'Tag'}],
    'type': 'CustomNonProjectEntity08'
  },
  { 'code': 'Formation_Particles__Videvo',
    'id': 25,
    'tags': [ { 'id': 212, 'name': 'fire', 'type': 'Tag'},
              { 'id': 277, 'name': 'pyro', 'type': 'Tag'}],
    'type': 'CustomNonProjectEntity08'
  },
  { 'code': 'Particle_Random_49',
    'id': 26,
    'tags': [ { 'id': 212, 'name': 'fire', 'type': 'Tag'},
              { 'id': 277, 'name': 'pyro', 'type': 'Tag'}],
    'type': 'CustomNonProjectEntity08'
  }
]

I want to make a query where if i use the string ‘mok’ it returns all items where any tag contains the string ‘mok’ in it. In this case it would return 1 item, since it contains the tag ‘smoke’.
Is this possible?

2 Likes

Hey Joker,

Happy Friday! You’ll want to make use of the name_contains filter operator like this:

assets = sg.find('Asset', [['tags', 'name_contains', 'mok']])

You can see a full list of the different operators at your disposal here: https://developer.shotgunsoftware.com/python-api/reference.html#operators-and-arguments

Note that string comparison operators are less efficient than more strict comparisons like is , so the tighter you can scope your filters, the faster they will be. We have a great doc on how best to optimize your queries be they in the web UI or API here:

3 Likes

@brandon.foster , I’m really struggling to get this to work in python. I’ve posted a sample on another thread.