Hi @rthompson,
I’m not sure what the problem was with the regexes that you tried, but I can show you one that worked for me:
'[0-9]{0,3}_?[A-Z]{3}_?[0-9]{4}'
First, to talk through the regex:
-
[0-9]{0,3}
: 0-3 digit characters. (I’m sure you could replace the [0-9]
with \d
.) Allowing 0
of these makes it optional by nature.
-
_?
: optional underscore
-
[A-Z]{3}
: exactly 3 uppercase letters
-
_?
: second optional underscore
-
[0-9]{4}
: exactly 4 digit characters
How I tested it:
- I created the following template key in
templates.yml
, using the above regex:
Shot:
type: str
filter_by: '[0-9]{0,3}_?[A-Z]{3}_?[0-9]{4}'
- I created Shots called
VDC331
and 167_VAL_1560
on my test project.
- I used
tank shell
to get an interactive Python session with an existing Toolkit API handle called tk
.
>>> template_obj = tk.templates['maya_shot_work']
# test 1: 167_VAL_1560
>>> fields = {'Shot': '167_VAL_1560', 'Step': 'comp', 'name': 'test', 'version': 3, 'Sequence': 'sq200'}
>>> template_obj.apply_fields(fields)
'/sgtk/projects/goats/sequences/sq200/167_VAL_1560/comp/work/maya/test.v003.ma'
# test 2: VDC3310
>>> fields = {'Shot': 'VDC3310', 'Step': 'comp', 'name': 'test', 'version': 3, 'Sequence': 'sq200'}
>>> template_obj.apply_fields(fields)
'/sgtk/projects/goats/sequences/sq200/VDC3310/comp/work/maya/test.v003.ma'
# test 3: s009, which should *not* match the filter regex:
>>> fields = {'Shot': 's009', 'Step': 'comp', 'name': 'test', 'version': 3, 'Sequence': 'sq200'}
>>> template_obj.apply_fields(fields)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/sgtk/software/shotgun/goats/install/core/python/tank/template.py", line 260, in apply_fields
return self._apply_fields(fields, platform=platform)
File "/sgtk/software/shotgun/goats/install/core/python/tank/template.py", line 604, in _apply_fields
fields, ignore_types, platform, skip_defaults=skip_defaults
File "/sgtk/software/shotgun/goats/install/core/python/tank/template.py", line 317, in _apply_fields
value, ignore_type=ignore_type
File "/sgtk/software/shotgun/goats/install/core/python/tank/templatekey.py", line 229, in str_from_value
raise TankError(self._last_error)
TankError: <Sgtk StringKey Shot> Illegal value 's009' does not fit filter_by '[0-9]{0,3}_?[A-Z]{3}_?[0-9]{4}'
So, using the ?
operator for optional characters does indeed work – it correctly matched the two Shots we expected it to match, and errored on the one that didn’t. I’m not sure what the specific issue was with the regexes that you tested, but maybe start with mine and then make modifications to make it match your exact use case?
Let me know if you’re still stuck, and we can keep digging.