# Refactoring templates

**URL:** <https://community.shotgridsoftware.com/t/refactoring-templates/3462>\
**Category:** Pipeline Integrations\
**Created:** [October 22, 2019, 10:13am UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462 "2019-10-22T10:13:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmoshev](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/mmoshev/32/476_2.png) [@mmoshev](https://community.shotgridsoftware.com/u/mmoshev)\
**Post date:** [October 22, 2019, 10:13am UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/1 "2019-10-22T10:13:59Z")

</div>

We are in a situation where we have over 200 templates. We do not need all of them, but even if I cleaned up, they would still be too many. And the bigger problem is with the ones we do use.

There are several dimensions along which templates are separated:

- work vs publish - their directory hierarchy is different, work files are usually in user sandboxes, etc.
- asset vs shot - again different hierarchy, and often assets are not user-specific.
- dcc app - esp. tag/output/node field - different dcc tk apps have hardcoded different field names for the tag. I thought field aliases were the solution to this, but could not get it to work.
- file type and roots - for instance we have separate roots for renders, caches. Scene and render templates are quite different in other ways.

We are getting a combinatorial explosion of these, and managing them becomes increasingly difficult.

We _have_ abstracted some pieces away into their own templates that get referenced (e.g. “cache\_base\_name”), but it is not satisfactory at this point.  
Of course, one answer would be to simplify our naming conventions, but some decisions are very practical, such as having different roots.

Anyone reached template nirvana and willing to share?

---

<div class="post-metadata">

**Author:** ![tannaz](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/tannaz/32/32_2.png) [@tannaz](https://community.shotgridsoftware.com/u/tannaz)\
**Post date:** [October 22, 2019, 1:57pm UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/2 "2019-10-22T13:57:22Z")

</div>

Hey Mois – moved this one over to the Pipeline Integrations category so we can get more Toolkit eyes on it.

One thing you might want to consider is using separate files to organize your templates via _includes_ – doesn’t limit the quantity of templates you have, but at least makes the files shorter and hopefully easier to look at.

Here’s a simple example using includes in `templates.yml`:

templates.yml (not including all of it here, just the important bits)

```auto
include: "./test.yml"

paths:

    sequence_root: sequences/{Sequence}

    foo_bar_in_master: "@sequence_root/@foo_bar" 

```

And the included `test.yml` file:

```auto
keys:
    Foo:
        type: str
    Bar:
        type: str

paths:
    foo_bar: test_path/{Foo}/{Bar}

strings:
    foo_bar_name: "string_{Foo}_{Bar}"

```

And when I run this in a shell, I get:

```auto
>>> tk.templates["foo_bar"]
<Sgtk TemplatePath foo_bar: test_path/{Foo}/{Bar}>
>>> tk.templates["foo_bar_name"]
<Sgtk TemplateString foo_bar_name: string_{Foo}_{Bar}>
>>> tk.templates["foo_bar_in_master"]
<Sgtk TemplatePath foo_bar_in_master: sequences/{Sequence}/test_path/{Foo}/{Bar}>

```

Beyond that, I’m spitballing, but maybe it’d be helpful to employ some sort of bidirectional conversion between YAML and CSV, so you can view/edit your data in Google Sheets or Excel?

And, beyond _that_, 🤞 that you get some good ideas from the community. Good luck!

---

<div class="post-metadata">

**Author:** ![nico.vandenbosch](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/nico.vandenbosch/32/4949_2.png) [@nico.vandenbosch](https://community.shotgridsoftware.com/u/nico.vandenbosch)\
**Post date:** [October 23, 2019, 12:14am UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/3 "2019-10-23T00:14:12Z")

</div>

A word of advice on what probably _not_ to do. Our templates are fairly consistent across DCCs and shotgun contexts so we found ourselves repeating the same work and publish templates over and over again varying only the relevant key. We thought it’d be a good idea to consolidate all of these into a series of “generic” templates which relied _heavily_ on optional keys to describe the context (the rationale being that contexts are largely mutually exclusive, so you wouldn’t have a key like “Asset” and “Shot” defined in one context):

> generic\_publish:  
> definition: ‘[assets/{sg\_asset\_category}/{sg\_asset}/][sequences/{sg\_sequence}/][sequences/{sg\_shot\_sequence}/{sg\_shot}/]{sg\_step}/publish/{task\_name}/{version}[/{width}x{height}][/{name}]/{Project}[_{sg\_asset}][_{sg\_shot}]_{task\_name}[_{name}]\_v{version}[.{seq\_frame}].{ext}’  
> root\_name: ‘primary’

This worked well for a while until we started spotting significant increases in startup time in toolkit. Note that the above example contains 9 optional keys, which tk-core internal expands to a list of every conceivable permutation of template definition for a total of 512 (2^9). Iterating through hundreds of templates in python is still manageable and accounted for mere seconds of additional overhead. It wasn’t until we introduced new contexts and increased the number of optional keys to 14 (for a grand total of 2^14=16,384 permutations) that startup went from seconds to minutes.

Long story short, we’ve rolled back to a lengthy list of individually managed templates. Would likewise love to know if there’s any other solutions that have been implemented with greater success.

---

<div class="post-metadata">

**Author:** ![mmoshev](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/mmoshev/32/476_2.png) [@mmoshev](https://community.shotgridsoftware.com/u/mmoshev)\
**Post date:** [October 23, 2019, 7:13am UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/4 "2019-10-23T07:13:46Z")

</div>

Thank you both,

@tannaz this would actually help, we could divide templates by dcc or by purpose. Readability is indeed a concern.  
Re: the conversion idea, it might work to have an additional layer (e.g. python) which allows you to manage templates at a higher level, while preserving them more or less as they are now.

@nico.vandenbosch ooh, having a lot of optional keys does sound difficult to manage, too. Funny that it impacted performance as well.

One thing I would like to see is tk dcc apps provide a configuration option for what the tag field name should be. This won’t be difficult to pull off, and would reduce the number of templates by a factor of {NUMBEROFDCCS}.

---

<div class="post-metadata">

**Author:** ![tannaz](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/tannaz/32/32_2.png) [@tannaz](https://community.shotgridsoftware.com/u/tannaz)\
**Post date:** [October 23, 2019, 7:51am UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/5 "2019-10-23T07:51:59Z")

</div>

Mois, can you say more about

> [@mmoshev](#):
>
> One thing I would like to see is tk dcc apps provide a configuration option for what the tag field name should be.

Maybe give an example of what you have in mind? I’m not sure what ‘tag field’ you’re referring to.

---

<div class="post-metadata">

**Author:** ![mmoshev](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/mmoshev/32/476_2.png) [@mmoshev](https://community.shotgridsoftware.com/u/mmoshev)\
**Post date:** [October 23, 2019, 9:15am UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/6 "2019-10-23T09:15:22Z")

</div>

Right, I did not qualify it. Usually you can output more than one artifact from a scene.  
For renders, you have a `{name}` field for the scene name. You have one extra token for the artifact:

- `tk-nuke-writenode` uses `nuke.output`
- `tk-houdini-alembicnode` uses `houdini.node`
- in max we use our own name, so that could be changed easily.

So the name is something like `{project}_{sequence}_{shot}_{name}_{render_tag}.exr`  
but we need a separate template for each dcc.

Is this something that field aliases can help with? I couldn’t wrap my head around them.

---

<div class="post-metadata">

**Author:** ![tannaz](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/tannaz/32/32_2.png) [@tannaz](https://community.shotgridsoftware.com/u/tannaz)\
**Post date:** [October 23, 2019, 9:29am UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/7 "2019-10-23T09:29:31Z")

</div>

Ah, that’s interesting. I’m not sure it’s a direction we want to go in, but I’ll share it with the product team and leave it to them to decide.

As far as the `alias` setting on keys, you’re not the first to be confused by it! Unfortunately it’s not helpful here: it’s more for the case where an app is _expecting_ a field called, for example `version`, but you need to define `version` differently for different situations, so you have a naming clash. In that case, you can define two keys, say, `nuke_version` and `maya_version`, but alias them both to `version` so that the app that’s _reading_ the template gets the key it’s expecting. For the record, [here is the documentation for the `alias` setting](https://support.shotgunsoftware.com/hc/en-us/articles/219039868-Integrations-File-System-Reference#Example%20-%20Two%20fields%20both%20named%20version%20via%20an%20alias).

---

<div class="post-metadata">

**Author:** ![nico.vandenbosch](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/nico.vandenbosch/32/4949_2.png) [@nico.vandenbosch](https://community.shotgridsoftware.com/u/nico.vandenbosch)\
**Post date:** [October 23, 2019, 9:45pm UTC](https://community.shotgridsoftware.com/t/refactoring-templates/3462/8 "2019-10-23T21:45:05Z")

</div>

Agreed, @mmoshev the one-liner looks pretty heinous but considering that it was designed to not only replace multiple template definitions as well as entire entity context settings blocks in tk-multi-app.yml files per app the reduction seemed like a reasonable trade-off in complexity.
