Skip to content

Instantly share code, notes, and snippets.

@tcrowson
Last active November 13, 2018 12:20
Show Gist options
  • Save tcrowson/db966b57398e62ef6819 to your computer and use it in GitHub Desktop.
Save tcrowson/db966b57398e62ef6819 to your computer and use it in GitHub Desktop.
Increment the version number of the current Modo scene, respecting existing digit padding.
#python
# tc_SaveIncremental.py
# Tim Crowson, 9/29/2014
# Saves a new version of your current scene. Requires the scene to have been saved previously.
# - Searches for an existing version that follows the pattern 'sceneName_vXXX.lxo'
# - Any number of digits can be used for the version number: the script will respect the existing number padding.
# - If no version is identified (e.g. "sceneName.lxo") the script will append '_v001' to end of the scene name.
# - You can configure the padding for this First Version by setting the FIRST_VERSION_PADDING variable
import os
import re
import lx
import lxu.select
__version = 1.0
# If no version is identified, this script will save the scene as 'v001'
# The digit padding for this number can be customized here. Default is 3.
FIRST_VERSION_PADDING = 3
# Get the current scene path
scenePath = lxu.select.SceneSelection().current().Filename()
# If the scene has never been saved, inform the user...
if scenePath is None:
lx.out('tc_SaveIncremental.py: Cannot increment scene version. Your scene has never been saved!')
lx.eval('dialog.setup info')
lx.eval('dialog.title {Save Incremental (tc_SaveIncremental.py)}')
lx.eval('dialog.msg {Cannot increment scene version. Your scene has never been saved!}')
lx.eval('dialog.result ok')
lx.eval('dialog.open')
else:
# Get the directory name and file name
dirName = os.path.dirname(scenePath)
fileName = os.path.basename(scenePath)
# Search the filename for a version.
# We'll do this via Regex, looking for a string that matches '_vXXX.lxo,'
# where "XXX" represents any number of digits.
match = re.search("_v\\d+\.lxo", fileName)
# If a version is found, increment it and set a new version, taking care to
# respect the padding of the original.
if match:
currentVersion = re.search("\d+", match.group(0)).group(0)
padding = len(currentVersion)
newVersionInt = int(currentVersion) + 1
# If the new version has more digits than the padding,
# set the padding to be the length of the new version.
# This way we can increment from 99 to 100, etc.
if len(str(newVersionInt)) > padding:
padding = len(str(newVersionInt))
newVersion = str(newVersionInt).zfill(padding)
newFileName = fileName.replace(currentVersion, newVersion)
# If no match is found, set this as "v001"
else:
sceneName = fileName.split(".")[0]
newVersion = str(1).zfill(FIRST_VERSION_PADDING)
newFileName = "%s_v%s.lxo" %(sceneName, newVersion)
# Define the new file path for the 'Save As' command
saveAsFile = os.path.join(dirName, newFileName)
# Get a list of existing versions in the same directory
existingVersions = [file for file in os.listdir(dirName)]
# If our new version doesn't already exist, save it
if not newFileName in existingVersions:
lx.eval("scene.saveAs {%s}" %saveAsFile)
lx.out("tc_SaveIncremental.py: Saved %s" %saveAsFile)
# If the version already exists, ask the user whether or not to overwrite
else:
try:
lx.eval('dialog.setup yesNo')
lx.eval('dialog.title {Save Incremental (tc_SaveIncremental.py)}')
lx.eval('dialog.msg {Version %s already exists! Overwrite it?}'%newVersion)
lx.eval('dialog.open')
lx.eval('dialog.result ?')
lx.eval("scene.saveAs {%s}" %saveAsFile)
lx.out("tc_SaveIncremental.py: Saved %s" %saveAsFile)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment