4
$\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$
4
$\begingroup$

You need to override the context. AFAIK setting area.type does not work.

Baking cloth is very similar to baking fluid or smoke.
This question has already been asked for smoke and fluid, so I will quote one of the answers here:

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 the api docs.

So for a smoke simulation:

import bpy

for scene in bpy.data.scenes:
    for object in scene.objects:
        for modifier in object.modifiers:
            if modifier.type == 'SMOKE':
                if modifier.smoke_type == 'DOMAIN':
                    override = {'scene': scene, 'active_object': object, 'point_cache': modifier.domain_settings.point_cache}
                    bpy.ops.ptcache.bake(override, bake=True)
                    break

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

Using the simple script below which is adapted from the script in the above quote (which is in turn adapted from brecht's answer about baking fluid), you can bake the cloth cache for every object in the file which has a cloth modifier:

import bpy

for scene in bpy.data.scenes:
    for object in scene.objects:
        for modifier in object.modifiers:
            if modifier.type == 'CLOTH':
                override = {'scene': scene, 'active_object': object, 'point_cache': modifier.point_cache}
                bpy.ops.ptcache.bake(override, bake=True)
                break 
| improve this answer | |
$\endgroup$
  • $\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 '14 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 '14 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 '14 at 7:41

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.