4
$\begingroup$

Problem: I have a bunch of .blend files that I want to make the same changes to as quickly as possible.

Solution: Run a python script on each file from the command line?

Here's the python script I'm currently using:

import bpy

#show all layers
bpy.context.scene.layers = ((True,)*20)

#delete the camera, sun and ground
for obj in bpy.data.objects:
    obj.select = False
    if obj.name == "sun":
        obj.select = True
    elif obj.name == "ground":
        obj.select = True
    elif obj.name == "camera":
        obj.select = True
    bpy.ops.object.delete(use_global = False)

#adjust object locations
for ob in bpy.data.objects:
    ob.location[0] = ob.location[0] / 10
    ob.location[1] = ob.location[1] / 10
    ob.location[2] = ob.location[2] / 10

#save the file and quit
bpy.ops.wm.save_mainfile()

And here's the .bat file:

cd C:\Program Files\Blender Foundation\Blender

blender -b "C:\[path]\file_01.blend" --python "C:\[path]\adjust.py"
blender -b "C:\[path]\file_02.blend" --python "C:\[path]\adjust.py"

The python script works inside Blender, but when I run the .bat file (in admin mode) it just closes. What am I doing wrong?

Bonus: Is there a way to simplify the .bat script so that it just runs on every .blend file in the folder without them being typed out?

$\endgroup$
  • $\begingroup$ You can check if there is any error by running it from direct commandline, I think, but I will try to run it from my computer also. $\endgroup$ – Alperen Üretmen Jun 17 '17 at 11:46
  • $\begingroup$ By the way is script working in Blender when you try to run it manually? $\endgroup$ – Alperen Üretmen Jun 17 '17 at 11:54
  • $\begingroup$ Originally, no it wasn't working (I was thinking of a different script when I posted this). I've corrected it now (and updated my post), so the script definitely runs in Blender from the text editor but the problem of running it from the command line persists. $\endgroup$ – BlenderBro Jun 17 '17 at 13:05
  • $\begingroup$ Try using forward slashes in your path. Backslashes have different meanings in different environments, so are often a cause of trouble. You also don't need to specify the full path, relative paths are fine too. $\endgroup$ – dr. Sybren Jun 17 '17 at 14:08
  • $\begingroup$ No change when using forward slashes. $\endgroup$ – BlenderBro Jun 17 '17 at 15:23
2
$\begingroup$

There is nothing wrong with either script. The problem was the use of an ampersand (&) in the file path. Don't use special characters in your folder/file names. Newbie mistake on my part.

$\endgroup$

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.