1
$\begingroup$

Is there a way to make blender only use lowercase letters when saving a file? And if not, is there a way around it? For instance executing a script to change the file name to lowercase before actually saving the file in a directory?

For some context, I work with multiple blender artist, and even tho we have a naming convention it does happen that some of us slip back in their old habits of capitalizing names, which creates some problems with GIT that I would like to prevent.

PS. this is specifically about the name that's used to save the .blend file

Update: I believe I found a way to change the file name but only after it's saved, so now I'm looking for a way to pause blender to change the name.

    def execute(self, context):


        bpy.ops.wm.save_as_mainfile('INVOKE_DEFAULT')

        outname = bpy.path.basename(bpy.data.filepath)
        outpath = os.path.dirname(bpy.path.abspath(bpy.data.filepath))
        filename = bpy.path.basename(bpy.context.blend_data.filepath)


        filename = filename.lower()
        print(filename)
        if not bpy.app.handlers.save_post:
            print("not finished")
            return {'PASS_THROUGH'}
            if bpy.ops.file.execute():
                print("yes")

        else:
            print("finished")
        print('done')


#       wait = bpy.app.handlers.save_post
        print(filename)
        print("test111")
        bpy.ops.wm.save_mainfile(filepath=os.path.join(outpath, outname),
        check_existing=True)

        print(filename)
        self.SaveAsRotationFix()

        return {'FINISHED'}

This is what I'm currently working with, (probably not a very efficient way of doing this.) My thought process is to be able to save the file with capitalization and after the file is created, run another save action to change the name to a lower case version. This fixes the issue I have when saving without a pre-existing file.

Edit: TLDR: if I save a file as Cube.blend I want it to change it to cube.blend

$\endgroup$
5
  • $\begingroup$ Sounds like this should rather be done in the git configuration. If core.ignorecase is set to true it shouldn't really matter what case is used locally. $\endgroup$ – Robert Gützkow Jan 20 '20 at 14:22
  • $\begingroup$ We tried that, and Git does work with capital letters and lowercase letters, we want a clean look in the project folders though, and consistency between all our machines with names $\endgroup$ – henry de lange Jan 20 '20 at 14:32
  • $\begingroup$ Do you want to do this because blender saves the next manually saved files as .blend1 or .blend2? $\endgroup$ – Nate_Sycro27 Jan 20 '20 at 15:02
  • $\begingroup$ Yes, we disabled the .blend1 and .blend2, functionality because we are working with GIT to back up our work, but even so, it would still work fine if you just wanted to change the capitalization of the name. $\endgroup$ – henry de lange Jan 20 '20 at 15:51
  • $\begingroup$ I managed to find a different script for adding a prefix to a .blend name, so I'm trying to figure out if I can "convert" it to change it to lowercase instead. blender.stackexchange.com/questions/40436/… $\endgroup$ – henry de lange Jan 20 '20 at 15:57
2
$\begingroup$

Override the Save Mainfile operator

Similarly to Add prefix to filename on "Save Blender File" which on a quick parse could be given the standard CtrlS shortcut.

Another method is to override the save operator and simply pass the converted filepath to the save as operator. Proof of concept code below. Simply prints the filepath to be "rejigged" and saved. Notice how it becomess the operator in the menu and called by shortcut.

Below is the export operator from the templates rejigged to override "wm.save_mainfile"

import bpy

from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty
from bpy.types import Operator


class ExportSomeData(Operator, ExportHelper):
    """This appears in the tooltip of the operator and in the generated docs"""
    bl_idname = "wm.save_mainfile"  # 
    bl_label = "Save..."

    # ExportHelper mixin class uses this
    filename_ext = ".blend"

    filter_glob: StringProperty(
        default="*.blend",
        options={'HIDDEN'},
        maxlen=255, 
    )



    def execute(self, context):
        print(f"Call save as, rejig {self.filepath}")
        #bpy.ops.wm.save_as_mainfile(filepath=self.filepath)
        return {'FINISHED'}




def register():
    bpy.utils.register_class(ExportSomeData)



def unregister():
    bpy.utils.unregister_class(ExportSomeData)



if __name__ == "__main__":
    register()

This ofcourse won't help if a user chooses save as instead and uses an uppercase filename.

$\endgroup$
3
  • $\begingroup$ When converting this into an addon, what's a reliable way of re-registering the old operator without hassle? $\endgroup$ – brockmann Jun 26 '20 at 13:15
  • $\begingroup$ Good question, would make a good question.. If there is a python class can reimport and re-register, and generally can get registered class from bpy.types. For builtins am unsure eg this one bpy.types.WM_OT_save_mainfile Would prob recommend installing this one as a startup module based on Q. $\endgroup$ – batFINGER Jun 26 '20 at 13:34
  • $\begingroup$ Right, done: blender.stackexchange.com/questions/184245/… Feel free to edit... $\endgroup$ – brockmann Jun 26 '20 at 15:19
1
$\begingroup$
import bpy
import os


def my_save_pre(context):
    print("Before Save")
    filename = bpy.path.basename(bpy.context.blend_data.filepath)
    outpath = os.path.dirname(bpy.path.abspath(bpy.data.filepath))
    if filename is not "":
        new_name = os.path.join(outpath, filename.lower())
        return bpy.ops.wm.save_mainfile(filepath=new_name)
    else:
        raise Exception("No File Saved")
        
        
#bpy.app.handlers.save_pre.clear()


def register():
    bpy.app.handlers.save_pre.append(my_save_pre)
    
def unregister():
    bpy.app.handlers.save_pre.remove(my_save_pre)

    
if __name__ == "__main__":
    bpy.app.handlers.save_pre.clear()
    register()

Let know if this dont work out.

$\endgroup$
1
0
$\begingroup$

I have a script I use for cleaning up large library and converting the names to lower, but this will check to see if your file to be changed has a key string in it...such as a file extension. It is not 'quite' what you asked for but if you need to do some clean up then this might work. You can if you want run this from blenders python.

import os
import sys



##        this r is important, just copy your folder path into here and keep it between the quotes
targetfolder = r'C:\Users\bigmeanie\Videos'


## add in file extensions to change here. 
file_types_to_change = [ '.jpg' , ' .png' , '.blend', '.obj', '.TGA' ]

renamefolders= False


################### don't touch under here, well what am I
######### talking about, I like to watch the world burn, touch all you want

for i in file_types_to_change:
    FTC = i.lower()
    #print(FTC)

FTC = file_types_to_change

TD = targetfolder


def renamelower(path, name):
    lowername = os.path.join(path , name.lower())
    uppername = os.path.join(path , name)
    print(uppername)
    os.rename( uppername , lowername)





def filescan():
    File_Index = []

    for path, subfolders, filenames in os.walk(TD):
        for name in filenames:
            if name.islower() == False:
                if any(s in name.lower() for s in FTC):
                    renamelower(path, name)





filescan()

print( "")
print("end of list")
print( "")
$\endgroup$
1
  • $\begingroup$ Thank you for your answer, but I want to run this check when I save a file. $\endgroup$ – henry de lange Jan 21 '20 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.