Tank.template_from_path() can't return multiple templates?

Just encountered some toolkit behavior that seems a bit short-sighted in its implementation:

tk.template_from_path(path)

due to some ambiguity in our templates it produces this error:

2 templates are matching the path ‘/proj/fsp/sequences/fs1/fb/fb0170/plates/publish/bg/0001/4324x2280/fs1_fb0170_bg_v0001.%d.exr’.

The overlapping templates are:

<Sgtk TemplatePath standalone_shot_publish: sequences/[{sg_shot_installment}/]{sg_shot_sequence}/{sg_shot}/{sg_step}/publish/{task_name}/{version}[/{width}x{height}]/{sg_shot_basename_prefix}{sg_shot}{task_name}[_{name}]_v{version}[.{seq_frame}].{ext}>

{‘sg_shot’: ‘fb0170’, ‘seq_frame’: ‘%d’, ‘ext’: ‘exr’, ‘sg_shot_installment’: ‘fs1’, ‘sg_shot_sequence’: ‘fb’, ‘width’: 4324, ‘sg_shot_basename_prefix’: ‘fs1’, ‘version’: 1, ‘task_name’: ‘bg’, ‘sg_step’: ‘plates’, ‘height’: 2280}

<Sgtk TemplatePath plate_shot_publish: sequences/[{sg_shot_installment}/]{sg_shot_sequence}/{sg_shot}/{sg_step}/publish/{name}/{version}[/{width}x{height}]/{Installment}{sg_shot}{name}_v{version}[.{seq_frame}].{ext}>

{‘sg_shot’: ‘fb0170’, ‘name’: ‘bg’, ‘Installment’: ‘fs1’, ‘sg_shot_installment’: ‘fs1’, ‘sg_shot_sequence’: ‘fb’, ‘width’: 4324, ‘ext’: ‘exr’, ‘version’: 1, ‘seq_frame’: ‘%d’, ‘sg_step’: ‘plates’, ‘height’: 2280}

It appears the method refuses to accommodate the ambiguity and return multiple templates. Instead I’m reduced to wrapping this call in a try/except and parsing the error message for a list of candidate templates. Surely there’s a better way that doesn’t require me rewriting tk.template_from_path()?

1 Like

Hey Nico!

I started writing a long response here, speculating about the why behind this choice, and thinking out loud that, considering how much of our existing Toolkit code would have to change if template_from_path() changed, it might actually be safer to have a second method that returned multiple templates instead of failing, in addition to template_from_path()–a templates_from_path() method perhaps.

And then I looked at our documentation, and it appears it already exists: https://developer.shotgunsoftware.com/tk-core/core.html?highlight=template_from_path#sgtk.Sgtk.templates_from_path

Give it a go; hopefully it’s exactly what you’re looking for :slight_smile:

3 Likes

Ooomph. Thats embarrassing. I’ll go sit in the corner now and have a good think on what I’ve done. :flushed:

Thanks for doing my legwork for me @tannaz!

2 Likes

no worries at all! i learned something, too!

1 Like