How to execute python inside blend file from command line

I’ll explain the numbering of what I did.

  1. I created a text file called lowRes inside the blend file as shown in the attached image.
    image
  2. And I noticed that when I click Run Script in Blender run with GUI, the resolution of all scenes is changed to 128x72.
  3. Then undo by pressing Ctrl + Z, set it to 1920x1080 resolution, and save the file.
  4. And I did the Command Line Rendering using the code below.
blender -b "my.blend" ^
-P lowRes.py ^
-S "scene 01" ^
-o "//output\##" -F PNG -f 3 ^
-S "scene 02" ^
-o "//output\##" -F PNG -f 5

good.
03.png and 05.png were generated as expected.
But the resolution is 1920x1080, not 128x72.
The python code doesn’t seem to be running.
What am I doing wrong?

If you use -P Blender expects a .py file.

To run a text inside the blend file I think you would use:
--python-text lowRes

https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html

Amazing! That works for me!
--python-text was listed in the official manual but I haven’t looked into it closely.
I have one last thing I want to know.
As far as I know, if you don’t have a textblock or external python file, you can run python code with --python-expr .
But I failed to execute lowRes code using --python-expr .
I am using Notepad. How can I write code that works correctly?

If you want an external editor you could try notepad++ but the text editor in Blender is usually enough for small and even larger stuff.
Note that there are three buttons in the text editor right of the open file thingy which add Line Numbers, Word Wrapping and Context highlighting. Perhaps you have turned them off.

Oh, sorry. I will write the question more clearly.

  1. I prepared a blend file without a python text data block.
  2. I lacked some python basics, so I wrote the stupid code below in Notepad.
blender -b "my.blend" ^
--python-expr ^
"import bpy ^
for scene in bpy.data.scenes : ^
    scene.render.resolution_x = 128 ^
    scene.render.resolution_y = 72"^
-S "scene 01" ^
-o "//output\##" -F PNG -f 3 ^
-S "scene 02" ^
-o "//output\##" -F PNG -f 5
  1. This code does not work.

After these attempts I was curious about the correct way to write multiple lines of python code for --python-expr.

I have never --python-expr but I think you need semicolons after every command.

I understood that -P, --python-text is recommended over --python-expr.
But I want to understand how to use multiple lines in --python-expr.
I wrote the code below and it failed.

blender -b "my.blend" ^
--python-expr "import bpy; for scene in bpy.data.scenes :; scene.render.resolution_x=128; scene.render.resolution_y=72" ^
-f 1

CMD outputs the following error:


What am I doing wrong?