6

I have a linux vps that has some spare resources left from main activity. I can not get the GUI working, but I was thinking if there is a way to use it for baking with python script.

I see that there is an operator:

bpy.ops.ptcache.bake(bake=True) 

But what is the right context for it?

Also if anyone has ideas to get the GUI working under Ubuntu VPS, that would be much appreciated.

gandalf3
  • 157,169
  • 58
  • 601
  • 1,133
user2801402
  • 143
  • 2
  • 5

1 Answers1

8

Baking smoke is very similar to baking fluid, so I will quote brecht's answer:

It's a bit more complicated than it could be because fluid simulating baking is only exposed as an operator. That means it expects an active object in the context, which is usually provided by the user interface that is not available in background mode.

However, it is possible to override context manually, see this answer as well as the documentation.

For a smoke simulation, this might look like:

import bpy

for scene in bpy.data.scenes: for obj in scene.objects: for modifier in obj.modifiers: if modifier.type == 'FLUID' and modifier.fluid_type == 'DOMAIN': if modifier.domain_settings.domain_type == 'GAS': with bpy.context.temp_override(scene=scene, active_object=obj): # bpy.ops.fluid.free_all() # if you'd like to free existing bakes first bpy.ops.fluid.bake_data()

updated for blender 3.4+. for pre-mantaflow version (<2.82) see this revision

This will bake the selected cache for all domain objects in the file.

You can then run this python script in blender with

blender --background /path/to/file.blend --python /path/to/script.py

Or the short way:

blender -b /path/to/file.blend -P /path/to/script.py
gandalf3
  • 157,169
  • 58
  • 601
  • 1,133
  • Thanks a bunch, it works. I tried to fiddle with Brecht's script too, but got the override part wrong. – user2801402 Nov 19 '13 at 05:03
  • At least on recent blenders, it seems that you need to explicitly specify an external cache for blender to work with, otherwise headless baking won’t work (although it would create a blendcache_* folder automatically if run with a gui, I think). I might have misinterpreted something here though. – Jonas Schäfer Jul 14 '14 at 18:47
  • 1
    You may add bpy.ops.ptcache.free_bake(override) just before the bake. I came into a case where the bake wouldn't do anything (.blend copied from local computer without the cache) – Romuald Brunet May 11 '19 at 10:54
  • unfortunately, override has been deprocated by v3.4. Can someone update this answer? – james_t Feb 19 '23 at 18:16
  • 1
    @james_t I've updated my answer, thanks for the heads up! – gandalf3 Feb 22 '23 at 02:02
  • 1
    oh wow, thanks for the update. This ex-java programmer still pulls his hair out with bpy !!! – james_t Feb 22 '23 at 16:55