5
$\begingroup$

I'm having trouble using bpy.ops.ptcache.bake().

I get the error message bpy.ops.ptcache.bake.poll() failed, context is incorrect no matter what I set the context to. I tried using bpy.context.area.type="PROPERTIES" but I still get the same error.

How can I set the context in order to bake a cloth simulation in a script?

$\endgroup$
1

1 Answer 1

5
$\begingroup$

You need to override the context for the operator to work. similar to this answer on baking fluid or smoke, you can bake the cloth cache for every object in the file which has a cloth modifier like this:

import bpy

for scene in bpy.data.scenes:
    for object in scene.objects:
        for modifier in object.modifiers:
            if modifier.type == 'CLOTH':
                with bpy.context.temp_override(scene=scene,
                                               active_object=object,
                                               point_cache=modifier.point_cache):
                    bpy.ops.ptcache.bake(bake=True)
$\endgroup$
5
  • $\begingroup$ Thanks. I missed it for some reason. What should I replace domain_settings.point_cache with? since a cloth has no domain (I took out that last If, btw, for the same reason. $\endgroup$
    – Shef
    Jan 14, 2014 at 4:41
  • $\begingroup$ Ok, so I tried using only modifier.point_cache, but now it starts baking, then gives me the same error as before, only before it it says things like PyContext 'window' not found or PyContext 'region' not found. Any tips? $\endgroup$
    – Shef
    Jan 14, 2014 at 4:47
  • $\begingroup$ @Shef It works for me. AFAIK those PyContext messages are just warnings, including the context override for those is not actually necessary in this case. $\endgroup$
    – gandalf3
    Jan 14, 2014 at 7:41
  • $\begingroup$ ptcache.bake(override is no longer supported in v3.4. Can someone update this answer? thx $\endgroup$
    – james_t
    Feb 19, 2023 at 18:38
  • 1
    $\begingroup$ @james_t I've updated my answer, thanks for the heads up! $\endgroup$
    – gandalf3
    Feb 22, 2023 at 2:09

You must log in to answer this question.

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