# Abstract\_paths\_from\_template and optional fields?

**URL:** <https://community.shotgridsoftware.com/t/abstract-paths-from-template-and-optional-fields/17130>\
**Category:** Pipeline Integrations\
**Tags:** templates, toolkit\
**Created:** [April 6, 2023, 7:28am UTC](https://community.shotgridsoftware.com/t/abstract-paths-from-template-and-optional-fields/17130 "2023-04-06T07:28:37Z")\
**Posts on this page:** 3\
**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:** [April 6, 2023, 7:28am UTC](https://community.shotgridsoftware.com/t/abstract-paths-from-template-and-optional-fields/17130/1 "2023-04-06T07:28:37Z")

</div>

I noticed that using `tk.abstract_paths_from_template` with optional fields, paths that do not contain the optional fields are not returned. I guess this is because one glob pattern is used for all. Trying to understand the code for this.  
Example:

```auto
paths = ["BOO_059_320_bg01", "BOO_059_420"]
template_def: "{Sequence}_{Shot}_{Scene}[_{Element}]"
tk.abstract_paths_from_template(tk.templates["template_def"], {"Sequence": "BOO"})

```

In a case like this, _only_ the paths that have the optional field are detected.

---

<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:** [April 6, 2023, 8:47am UTC](https://community.shotgridsoftware.com/t/abstract-paths-from-template-and-optional-fields/17130/2 "2023-04-06T08:47:13Z")

</div>

Found that calling `template.parent` returns a template for the directory, but breaks optional keys by including them unconditionally.

---

<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:** [April 6, 2023, 7:51pm UTC](https://community.shotgridsoftware.com/t/abstract-paths-from-template-and-optional-fields/17130/3 "2023-04-06T19:51:00Z")

</div>

Digging into this more. Template.parent for a path is `os.path.dirname(template.definition)`  
However `template.definition` has no knowledge of optional fields - they are shown as normal fields. There is `template._repr_def` which contains the original definition, and it works with `os.path.dirname`! So I will fork some of the functions for the time being.

I will put together a PR on GitHub when I have a better understanding of the issue, comments especially from SG peeps welcome.  
It happens right here, when we use `template.parent`:

> <https://github.com/shotgunsoftware/tk-core/blob/master/python/tank/api.py#L682>
