Skip to content

Instantly share code, notes, and snippets.

@sambler
Created October 24, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sambler/605f40fed72af01d5b97 to your computer and use it in GitHub Desktop.
Save sambler/605f40fed72af01d5b97 to your computer and use it in GitHub Desktop.
Blender addon to add a prefix to the blend file when saving.
# script made in response to
# http://blender.stackexchange.com/q/40436/935
bl_info = {
"name": "Save File Prefix",
"author": "sambler",
"version": (1,0),
"blender": (2, 71, 0),
"location": "File->Save Prefixed Blendfile",
"description": "Add a prefix to the filename before saving.",
"warning": "beta",
"category": "System",
}
import bpy
import os
# adjust this function to return the prefix you want
def fn_prefix():
return 'xx12345xx_'
class PrefixFileSave(bpy.types.Operator):
"""Set a filename prefix before saving the file"""
bl_idname = "wm.save_prefix"
bl_label = "Save Prefixed Blendfile"
def execute(self, context):
outname = fn_prefix() + bpy.path.basename(bpy.data.filepath)
outpath = os.path.dirname(bpy.path.abspath(bpy.data.filepath))
return bpy.ops.wm.save_mainfile(filepath=os.path.join(outpath, outname), check_existing=True)
def menu_save_prefix(self, context):
self.layout.operator(PrefixFileSave.bl_idname, text=PrefixFileSave.bl_label, icon="FILE_TICK")
def register():
bpy.utils.register_module(__name__)
# add the menuitem to the top of the file menu
bpy.types.INFO_MT_file.prepend(menu_save_prefix)
wm = bpy.context.window_manager
win_keymaps = wm.keyconfigs.user.keymaps.get('Window')
if win_keymaps:
# disable standard save file keymaps
for kmi in win_keymaps.keymap_items:
if kmi.idname == 'wm.save_mainfile':
kmi.active = False
# add a keymap for our save operator
kmi = win_keymaps.keymap_items.new(PrefixFileSave.bl_idname, 'S', 'PRESS', ctrl=True)
def unregister():
wm = bpy.context.window_manager
win_keymaps = wm.keyconfigs.user.keymaps.get('Window')
if win_keymaps:
for kmi in win_keymaps.keymap_items:
# re-enable standard save file
if kmi.idname == 'wm.save_mainfile':
kmi.active = True
if kmi.idname == PrefixFileSave.bl_idname:
win_keymaps.keymap_items.remove(kmi)
bpy.types.INFO_MT_file.remove(menu_save_prefix)
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment