Hi, i would like to know how to write a Python script to create batchfile from within blender. I would like it to save the current scene with the viewlayer that is active appended to the filename and create a .bat file with the write path to the scene and the framerange.
Or maybe there is already one that starts a instance without ui that renders, so i can close blender and have it still render. I want to save memory.
Well… for all the seetings done in your blend file and for the whole animation it’s simply : blender -b file.blend -a
You may have a look at the Docs-Blender Manual: Advanced → Commandline → Render …
Thanks, but I don’t know if you understand me right. I want to set the viewlayer and settings in the open blendfile and execute a script that saves the scene as a new scene,with a new name containing the viewlayername and creating a batchfile that renders that scene. Then i want to change the viewlayer and settings and do it again until i got all the layers i want. I can’t just enable all viewlayers, because i want to change e.g. the renderengine from cycles to evee for one layer. And i want the open scene keeping its original name.
Yes i do not understand why you arent just saving the blend file under a new name and also why the viewlayer has to be saved (it simply part of the scene; you can’t add another one)… or are you speaking of multilpe scenes ?? ( Also the batch render is always using the camera…)
You may have to elaborate this to get a more suited answer.
I just hate to have to save the scene by hand and then copy the filepath and paste it into the batchfile, I just don’t like it. I thought i should be relatively easy to do that with python. Just get the filename, viewlayername and print the batchcommand and do all the saving. I just wish it was not so tedious. Viewlayer is the layer that is rendered, isn’t it ? So if i enable it and disable the others, just that layer is rendered. Since i am changing between evee and cycles and i can’t do a override on the viewlayer i have to render it in different scenes. I know i could do it with scenes, but i am also using turbo tools i start to get confused with all the settings. So i would prefer to do it one by one.
In fact i was mixing something up about viewlayers, you can have several. But only the actual seems to be used when hitting F12 ( render interactively ) and i’m not sure about the batch call… Properly the default layer if not specifically selected via compositing and the Render Layers node.
So you might start to get the actual blend file name (and path) and the viewlayer name by…
import bpy
import os
import platform
print()
# get the full file path and name
filepath = bpy.context.blend_data.filepath
print(filepath)
# get the "pure" dir path
dirpath = os.path.dirname(filepath)
print(dirpath)
filename = bpy.path.basename(filepath)
# remove the extension .blend
filename = os.path.splitext(filename)[0]
print(filename)
viewlayername = bpy.context.window.view_layer.name
# using unix file path separator !!
newpath = dirpath + '/' + filename + '_' + viewlayername
# converting to os separator
newpath = bpy.path.native_pathsep(newpath)
print(newpath)
blendfile = bpy.path.ensure_ext( newpath, '.blend' )
#save as new file (disabled for now)
## bpy.ops.wm.save_as_mainfile(blendfile)
runningsystem = platform.system()
if runningsystem == 'Linux':
ext = '.sh'
executable = 'blender'
header = '#!/usr/bin/env bash'
elif runningsystem == 'Windows':
ext = '.bat'
executable = 'blender.exe'
header = ''
elif runningsystem == 'Darwin':
ext = '.sh'
executable = 'blender'
else:
ext = '.whatareyou'
executable = 'What'
header = ''
batchfile = bpy.path.ensure_ext( newpath, ext )
print(batchfile)
# add needed commandline options as needed
## THIS WRITES A FILE
f = open( batchfile , 'w')
f.write( executable + ' -b ' + blendfile + ' -a')
f.close()
OMG ! Thank you ! I was hoping, but didn’t expect someone to write it. Eager to try it
Well… it’s only a prototype… you may have to play with the options generated for the bathe file.
And i didn’t make the name of the view layer “file name save”… so if you use some weird characters that’s might be a problem…
actually there is that for doing so…
docs.blender api bpy.path → bpy.path.clean_name
so maybe add after:
newpath = bpy.path.native_pathsep(newpath)
newpath = bpy.path.clean_name(newpath)
Ok will have a look at it. I just tried it. Exactly what i need. Thanks again. Love the community on blenderartist !