# Running Script and rendering from command line with external script

I'm trying to run a script like so from the command line

    blender -b oop-blender-demo.blend -P model.py


followed by

    blender -b oop-blender-demo.blend -a


to render the file.

I've noticed that the second command renders the file oop-blender-demo thats within my blender gui, which has a different script in it than the one i'm trying to run with model.py. It's almost like nothing is saving. I get no errors running the first command so I would think that's the final script that would be used to render the file

While posting this question I looked at this post

Does running a python script via Blender command line not actually do anything?

but when I try to input my blend file as the filepath it says "cannot write to this file". I can't find any information on this error. Is there a way to save this script/link it to the .blend file?

Or alternatively if you understand how to use the method bpy.ops.wm.save_as_mainfile(filepath=???) described in the question I linked, what should I put in the filepath, parameter?

You are running blender twice, the first time it opens the file and runs your script then it closes the file and quits. The second time it opens the file and renders the animation. Without saving the blend file in the first run, the render step will start with the same data as before the script was run.

Blender processes command line arguments in order given, that is you can do it in one step by running the script and then rendering.

blender -b oop-blender-demo.blend -P model.py -a


Another option is to have the script do the rendering by adding

bpy.ops.render.render(animation=True)


Using save_as_mainfile() you would want to give it a filepath parameter. The save as operator saves a copy of the current blend file using the filepath parameter, without it you can't be sure where the copy is saved. If it did work, you will want to open the saved copy to render rather then the same original file. Normal system security settings are in effect, so the inability to save a file may come from the system preventing you from writing to the current directory or path .

Instead of saving a copy of the file you may want to use bpy.ops.wm.save_mainfile() which does a normal save of the current file rather than saving a copy.