4
$\begingroup$

I am trying to create an animation programmatically (never using the interface) with Blender. I can take my script and run it in Blender via the "scripts" tab just fine. It creates new scenes, adds cameras, object, lights, etc. I can render the results of the script via the GUI or via command line. But when I run it from the command line, it doesn't actually do anything!

I am using a "blank" .blend file with one initial scene in it with a title card.

Here is the .bat file script (Windows), it uses a python script to create the animation file:

cd\program files\blender foundation\blender
blender --background  C:\pathtofile\ANIMATION.blend --python CREATE_ANIMATION_FILE.py

It gives me:

found bundled python: C:\Program Files\Blender Foundation\Blender\2.78\python
read blend: C:\pathtofile\ANIMATION.blend 

Blender quit

It runs the script just fine but doesn't change the ANIMATION.blend file at all. Again, if I run the script from the scripts window via the in Blender GUI, it works fine. Anyone come across this?

I found some info here:

11) Next step is to build a userpref.blend with Auto Run Python Scripts enabled. NOTE: This is essential because of a bug in blender. Starting blender with -y does not work. Neither does starting blender with a python script setting the autorun flag to ON. It even does not work to compile blender yourself with the WITH_PYTHON_SECURITY flag disabled (OFF). Blender will still not execute the python script. The only way to enable this auto execute is via the GUI. Since we do not have a GUI (or want to install one), we have to build a userpref.blend on a different machine.

$\endgroup$
7
  • 3
    $\begingroup$ by any chance, does your script not save the animation.blend file after it changes it? $\endgroup$
    – David
    Oct 11, 2016 at 22:51
  • $\begingroup$ I had no idea you had to do that. Yikes! I'll look in the API docs for that (As Larry David would say ... they are pretty, pretty, pretty sparse) $\endgroup$
    – J Rowoldt
    Oct 11, 2016 at 23:28
  • $\begingroup$ The api doesn't do anything differently than if you were using the UI yourself, you are just replicating steps with code, so if you want changes to save in the file, then, as you would do normally via the UI and as David says, you will have to save. $\endgroup$ Oct 12, 2016 at 0:21
  • 1
    $\begingroup$ I respectfully disagree that the API doesn't do anything differently if you did it on the UI. Direct access to the data blocks, for one. Also many, many commands that you execute in the UI (by mousing over or looking at the script log) do not work at all in the API. I've been working with it for around 9 months now and it's a hodgepodge of working perfectly and not working. Lots of Nonetype errors. Lots of attributes not found. :) $\endgroup$
    – J Rowoldt
    Oct 12, 2016 at 0:39
  • $\begingroup$ That wasn't quite my point. You are still replicating steps the UI takes, regardless of whether it takes more steps in code to do it. Replicating the order in which you do things in the UI is often given as a starting point to people new to the api as it teaches them to think about the order in which operations need to be done in code. That is what solves the problem in your question. $\endgroup$ Oct 12, 2016 at 1:52

1 Answer 1

2
$\begingroup$

David above solved it. When manipulating a .blend file with a python script, that script must contain the following line at the end of the run in order to save the file:

bpy.ops.wm.save_as_mainfile(filepath=savepath)

Obviously insert your filepath/filename in the "filepath" variable. Thanks!

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .