Python command to bake hair physics

I got this from right clicking the bake button

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

I paste that into the text and press play I Get

NameError: name ‘bpy’ is not defined

??? So I added impoty bpy to the beginning

I get context is incorrect

Im looking for a python command that will bake the hair physics on the selected object and particle system?

Thank you

Hello, you need to setup an override with an active object and the point cloud. eg

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)

See https://blender.stackexchange.com/a/6252/86891 and https://blender.stackexchange.com/questions/248274/a-comprehensive-list-of-operator-overrides

Its not workig. I guess I just dont know enough about python. I see that is says cloth for the modifier type and I was asking about hair??

What Im looking for is just a way to essentially hit the “bake” button on the selected object and particles system. As if I were clicking it with the mouse but a python command.