Python question. Creating a dictionary of classes within bpy.context

Regarding bpy.context I have:


>>> dir(bpy.context)
['__doc__', '__module__', '__slots__', 'active_base', 'active_bone', 'active_gpencil_frame', 'active_gpencil_layer', 'active_object', 'active_operator', 'active_pose_bone', 'area', 'bl_rna', 'blend_data', 'copy', 'edit_object', 'editable_bones', 'editable_gpencil_layers', 'editable_gpencil_strokes', 'gpencil_data', 'gpencil_data_owner', 'image_paint_object', 'mode', 'object', 'particle_edit_object', 'region', 'region_data', 'rna_type', 'scene', 'screen', 'sculpt_object', 'selectable_bases', 'selectable_objects', 'selected_bases', 'selected_bones', 'selected_editable_bases', 'selected_editable_bones', 'selected_editable_objects', 'selected_editable_sequences', 'selected_objects', 'selected_pose_bones', 'selected_sequences', 'sequences', 'space_data', 'tool_settings', 'user_preferences', 'vertex_paint_object', 'visible_bases', 'visible_bones', 'visible_gpencil_layers', 'visible_objects', 'visible_pose_bones', 'weight_paint_object', 'window', 'window_manager']

But I would like to get something like this:


override = {'blend_data': context.blend_data, 'region': context.region, 'active_object': context.active_object, 'edit_object': context.edit_object, 'gpencil_data': context.gpencil_data, 'scene': context.scene, 'window': context.window, 'screen': context.screen, 'area': context.area, ...}

It is a dictionary with all bpy.context items.
Does anyone know how?

One way is something like this:


override = {}
for ele in dir(bpy.context):
  override[ele] = eval('bpy.context.' + ele)

This might not be one of the accepted uses of ‘eval’ but it will get the job done for what you have asked.

I highly suspect that there are better ways to access the members in the module, but I have never needed that so I’m not sure what the tricks are.

I have to get used to using the “eval”. good idea @kastoria :slight_smile:
I also found another solution:

override = bpy.context.copy()

also works

copy() is preferable, and it’s easy to modify afterwards (remove elements, change values of existing keys)