Tools/Python modules to design and work with folderstructures and naming conventions?

Hi there,

Before thinking about eventually rollong my own. Anyone knows any python modules and/or tools to design and work with folder structures and naming conventions?

I am thinking along the lines of having a simple visual editor that allows laying folder structures using rules/regexes etc. and possibly a python module to enforce it/generate proper paths and filenames.

Cheers,
Thorsten

pathlib is a object oriented way to work with paths and perform filesystem operations, and it comes with python3.x and can easily be installed with pip in python 2.x.
https://docs.python.org/3/library/pathlib.html

Also if you are using maya pymel ships with a very similar class as well.
http://download.autodesk.com/global/docs/maya2014/zh_cn/PyMel/generated/pymel.util.path.html#pymel.util.path

Hey there,

thanks but i was looking more for tools to define restrictions (e.g. //$server/$scene/$shot/$task and alike) and make it easy to construct valid paths accordingly.

Cheers,
Thorsten

That sounds pretty workflow-specific. I’m not aware of any extensions that would fit that description. I imagine you’ll need to roll your own stuff there.

I’ve been down that road before, it’s rough terrain… :slight_smile:

To the extent you can define your project needs clearly, you can do variable substitution inside a new method to create well -formed paths.


class PathString (str):
    def __new__(cls, *args, **kwargs):
        pathified = os.path.normpath(os.path.expandvars)
        return pathified 

test = PathString('${TMP}/${USERNAME}/my/folder')
# C:\\Users\\Steve\\AppData\\Local\\Temp\\Steve\\my\\folder

You can impose your rules in the new method (i usually enforce always-right-slashes, for example). If you have too many variables to manage with environment vars, use a string template:


class TemplatePathString(str):
    def __new__(k, *args, **kwargs):
        pathified = os.path.normpath(os.path.expandvars(args[0]))
        return Template(pathified).substitute(kwargs)

test2 = TString("c:\\ul\\${fred}\\folder", fred = 'barney')
# 'c:\\ul\\barney\\folder'

You could just enforce a given structure by providing the templat inside of new:


class ServerPath(str):
    def __new__ (k, *args, **kwargs):
        server_path = "\\\\${server}\\${scene}\\${shot}\\" + args[0]
        pathified = os.path.normpath(os.path.expandvars(server_path))
        return Template(pathified).substitute(kwargs)

ServerPath("myFile.xyz", server = 'network', scene = '01', shot = '05')
 # \\\
etwork\\01\\05\\myFile.xyz

In this particular case you could use a dictionary with the server/scene/shot info to clean up the typing too:


scene_info = {}
scene_info['server'] = 'network'
scene_info['scene']  = 'example'
scene_info['shot'] = 25
ServerPath ("myfilel.xyz", **scene)
# \\\
etwork\\example\\25\\myfilel.xyz

With a little work you could move the logic out of new into an overridable function and then use inheritance to build a whole family of structured path classes. That’s what I do for my project structures.

Since these guys all derive from string, other code will just treat them as strings. You could, however, add some instance methods to them so you could validate the path or create a new directory.