# Using subset TemplateKey in Workfiles

**URL:** <https://community.shotgridsoftware.com/t/using-subset-templatekey-in-workfiles/7856>\
**Category:** Pipeline Integrations\
**Tags:** fptr-tk, templates\
**Created:** [March 24, 2020, 7:30pm UTC](https://community.shotgridsoftware.com/t/using-subset-templatekey-in-workfiles/7856 "2020-03-24T19:30:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kporangehat](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/kporangehat/32/313_2.png) [@kporangehat](https://community.shotgridsoftware.com/u/kporangehat)\
**Post date:** [March 24, 2020, 7:30pm UTC](https://community.shotgridsoftware.com/t/using-subset-templatekey-in-workfiles/7856/1 "2020-03-24T19:30:06Z")

</div>

We have a client that would like to insert the artist initials in the filename for all workfiles and publishes. I know we can create a TemplateKey that looks roughly something like this:

```auto
    user_initials:
        shotgun_entity_type: HumanUser
        shotgun_field_name: name
        type: str
        # Spaces are replaced with `-` by TK
        subset: '([A-Z])[a-z]*-([A-Z])[a-z]*'

```

And there’s similar [docs about using `subset` here](https://developer.shotgunsoftware.com/tk-core/core.html#sgtk.StringKey.subset).

And a template like so:

```auto
    photoshop_asset_work:
        definition: '@asset_root/work/photoshop/{Asset}_{Step}_{user_initials}_v{version}.psd'

```

For `tk-multi-workfiles2`, this works fine when saving files. But when reading them back to open files, this will fall over because the `{user_initials}` in the filename is now two characters (eg `KP`) and doesn’t match the template key definition anymore - thus generating a template validation error.

The workaround I’m currently using (and have used in similar circumstances with other studios), is relying on storing this information in a separate field in Shotgun as `HumanUser.sg_initials` and defining the key with:

```auto
    user_initials:
        shotgun_entity_type: HumanUser
        shotgun_field_name: sg_initials
        type: str
        filter_by: "^[A-Za-z]{2}$"

```

This works fine for both opening and saving files. Is there a more elegant way to not have to store and maintain this extra data in Shotgun as a separate data point? Feels like there _should_ be a way… so maybe I’m missing something?

Any ideas or suggestions from others who may be doing something similar are welcome!

cheers,  
kp

---

<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:** [March 24, 2020, 11:53pm UTC](https://community.shotgridsoftware.com/t/using-subset-templatekey-in-workfiles/7856/2 "2020-03-24T23:53:35Z")

</div>

Hey KP –

I’m going to test this out and see if I get what you’re getting, but first:

- Are you saying it doesn’t work the second time around because it’s now running that regex against “KP” instead of “Kevin-Porterfield”, so it doesn’t match?
- If so, I wonder if you can just tweak your regex so it works in both cases. What if you did this:

```auto
        subset: '([A-Z])[a-z\-]*([A-Z])[a-z]*'

```

The only difference from what you have is that the middle dash becomes optional along with all the lowercase letters, so it _should_ match both `KP` and `Kevin-Porterfield`. Still a hack, but one with a much smaller footprint.

---

<div class="post-metadata">

**Author:** ![kporangehat](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/kporangehat/32/313_2.png) [@kporangehat](https://community.shotgridsoftware.com/u/kporangehat)\
**Post date:** [March 26, 2020, 8:25pm UTC](https://community.shotgridsoftware.com/t/using-subset-templatekey-in-workfiles/7856/3 "2020-03-26T20:25:36Z")

</div>

Thanks Tannaz!

This would probably simplify things. And actually I could update it to something even a little more flexible which will take care of some possible anomalies like not capitalizing the first letter of names and other garbage:

```auto
        subset: '([A-Za-z])\w*\W*([A-Za-z]).*'

```

However, the subset functionality falls victim to a bug in `tk-multi-workfiles2` which I have previously submitted a PR for here [https://github.com/shotgunsoftware/tk-multi-workfiles2/pull/89](https://github.com/shotgunsoftware/tk-multi-workfiles2/pull/89). (@philip.scadding saw my initial PR for this a few months back I think. I had to resubmit it because we inadvertently blew away the initial branch we submitted it from).

When the app is constructing the File Keys to use for matching work and publish files to determine the next version number, it uses the raw value of the template key (“Kevin Porterfield”) instead of the processed value (“KP”). This manifests itself by not incrementing the next available version number correctly which can cause data loss.

So for now I’ll use the regex but will use it with our forked version of the app until the PR gets reviewed and hopefully merged.

Thanks!
