Render output file with camera name

Hi,

Is it possible to add camera name to render file output with e.g. some kind of variable?

Best regs.

Sure. If you want it stamped on the render, in the Render panel under Metadata check “Stamp Output”, and make sure Camera is also enabled below.

Oh, sorry, I’ve written “file output”, it should be “file name” like render_camera01.jpg

As far as I know, this feature isn’t built into blender. I could use this as well; I am often making small changes to multiple file outputs in order to keep track of variations in what I’m rendering (different cameras, e.g.). It would be awesome if this could be streamlined. But this sounds to me like a question for the Python gurus. (I looked around for an addon that did this, and couldn’t find anything, btw).

Agreed, this would be very useful functionality. I did find a script at stackexchange for adding prefixes to filenames:

It has a function for appending a timestamp to the filename. It’s pretty elaborate because it adds a new Save operator to the File menu. I imagine the script could be easily modified to use a camera name variable; sorry, I don’t know how.

Adapting that script to file output is beyond me.

I think it would be a great addition to the Output Module in the Node Editor…

I figured out how to use this script to add the active camera name as prefix to the blendfile name. Basically it registers as an add-on, and you specifiy the prefix with Python code in the addon’s preferences. It worked for me in 2.76 in Windows.

  1. Copy the script text (reproduced below) and save with a name like “Fileprefix.py”.
  2. In Blender, open File->User Preferences->Add-ons
  3. Click “Install from FIle…” at the bottom. Navigate to the saved script and open it.
    This will install the add-on “Save File Prefix” to your Blender configuration.
  4. Find the add-on in the System section of User Preferences. Click the check box to activate it, then click the triangle to open the add-on information. In the Preferences field at the bottom, type bpy.context.scene.camera.name and press Enter.
  5. Close User Preferences and continue blending as usual.
  6. When ready to save your file, go to the File menu. Use the top entry “Save Prefixed Blendfile”.
    The file will be saved with the active camera object’s name prepended to the existing file name. If there’s no file name yet, the file will just have the camera’s name.
    Note: The code hijacks the Ctrl-S keystroke so be careful, if you instinctively use Ctrl-S to save and this addon is active, you can end up with filenames like CameraCameraCamera.blend. To save without appending the prefix, you use the original File menu->Save operator.

Apparently you can use all kinds of python variables or code in the Preference field, but just finding this camera name variable stretched my knowledge of the API!

This code was originally posted by user Sambler at the blender stackexchange.

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": "Runs user specified python code",
    "wiki_url": "https://github.com/sambler/addonsByMe/blob/master/file_prefix.py",
    "tracker_url": "https://github.com/sambler/addonsByMe/issues",
    "category": "System",
}

import bpy
import os
import time, datetime

class PrefixSavePreferences(bpy.types.AddonPreferences):
    bl_idname = __name__

    prefix = bpy.props.StringProperty(name="Prefix calculation",
                    description="Python string that calculates the file prefix.",
                    default="timestamp().strftime('%Y_%m_%d_%H_%M_%S') + '_'")

    def draw(self, context):
        layout = self.layout
        col = layout.column()

        row = col.row()
        row.prop(self,"prefix")

def timestamp():
    # convienience function that is available to the user in their calculations
    return datetime.datetime.fromtimestamp(time.time())

def fn_prefix(context):
    user_preferences = context.user_preferences
    addon_prefs = user_preferences.addons[__name__].preferences
    return eval(addon_prefs.prefix)

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(context) + bpy.path.basename(bpy.data.filepath)
        outpath = os.path.dirname(bpy.path.abspath(bpy.data.filepath))
        print(os.path.join(outpath, outname))
        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()

Thanks CD38. I think Eneen wants the camera name added to the image output, however. Still, kudos for figuring out what you did!

Oh my bad, of course that’s what Eneen wanted. As a Python newb I was so excited when I figured this out I forgot the original question!

I imagine one could write a script for their OS that extracts the camera name from the metadata and renames the image file. But it would be nice to have Blender do this automatically.

Edit: Here’s code I found at stackexchangethat sets the Base Path of any existing File Output nodes to the blendfile name. Maybe it would be useful in conjunction with the above add-on?

I haven’t tested this code or the whole process yet.

import bpy
import os

# Get file name:
filename = bpy.path.basename(bpy.context.blend_data.filepath)

# Remove .blend extension:
filename = os.path.splitext(filename)[0]

# set the base path for all file output nodes to filename:
for scene in bpy.data.scenes:
    for node in scene.node_tree.nodes:
        if node.type == 'OUTPUT_FILE':
            node.base_path = "//" + filename

Edit2: Close but no cigar. This script changes the path name (folder) but not the render file name itself. Also make sure there are File Output nodes in your scene or the script will throw an error.

Hi all,

As addressed in this other thread I have found an addon called loom: https://github.com/p2or/blender-loom that can help with variables such as camera names, scene names, and more within the output file name.

Hope it helps.