Add conformance tests from Blender docs and generate ContextDict TypedDict

Add conformance test files for bpy.ops, bpy.types.Object, bpy.types.Context,
bpy.msgbus, and bpy.data from official Blender documentation for both 5.0 and
5.1. Generate a ContextDict TypedDict from Context properties so that
Context.copy() returns precisely typed values instead of dict[str, object],
fixing type errors when unpacking into temp_override().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-02 11:40:51 +02:00
co-authored by Claude Opus 4.6
parent 004b6b11bc
commit 5b3253b81a
15 changed files with 307 additions and 2 deletions
+26
View File
@@ -0,0 +1,26 @@
import bpy
# Print all objects.
for obj in bpy.data.objects:
print(obj.name)
# Print all scene names in a list.
print(bpy.data.scenes.keys())
# Remove mesh Cube.
if "Cube" in bpy.data.meshes:
mesh = bpy.data.meshes["Cube"]
print("removing mesh", mesh)
bpy.data.meshes.remove(mesh)
# Write images into a file next to the blend.
import os
with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as fs:
for image in bpy.data.images:
fs.write(
"{:s} {:d} x {:d}\n".format(image.filepath, image.size[0], image.size[1])
)
@@ -0,0 +1,26 @@
import bpy
# Any Python object can act as the subscription's owner.
owner = object()
assert bpy.context.object is not None
subscribe_to = bpy.context.object.location
def msgbus_callback(*args: int):
# This will print:
# Something changed! (1, 2, 3)
print("Something changed!", args)
bpy.msgbus.subscribe_rna(
key=subscribe_to,
owner=owner,
args=(1, 2, 3),
notify=msgbus_callback,
)
assert bpy.context.object is not None
subscribe_to = bpy.context.object.path_resolve("name", False)
subscribe_to = (bpy.types.Object, "location")
@@ -0,0 +1,14 @@
# Collection add popup.
import bpy
from bpy import context
bpy.ops.object.collection_instance_add("INVOKE_DEFAULT")
# Maximize 3d view in all windows.
for window in context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == "VIEW_3D":
with context.temp_override(window=window, area=area):
bpy.ops.screen.screen_full_area()
break
@@ -0,0 +1,9 @@
# Calling an operator.
import bpy
bpy.ops.mesh.subdivide(number_cuts=3, smoothness=0.5)
# Check poll() to avoid exception.
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode="EDIT")
@@ -0,0 +1,8 @@
# Remove all objects in scene rather than the selected ones.
import bpy
from bpy import context
context_override = context.copy()
context_override["selected_objects"] = list(context.scene.objects)
with context.temp_override(**context_override):
bpy.ops.object.delete()
+36
View File
@@ -0,0 +1,36 @@
import bpy
from bpy import context
# Example inserting keyframe for the hovered property.
active_property = bpy.context.property
if active_property:
datablock, data_path, index = active_property
datablock.keyframe_insert(data_path=data_path, index=index, frame=1)
# Reload the current file and select all.
bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)
window = context.window_manager.windows[0]
with context.temp_override(window=window):
bpy.ops.mesh.primitive_uv_sphere_add()
# The context override is needed so it's possible to set edit-mode.
bpy.ops.object.mode_set(mode="EDIT")
win_active = context.window
win_other = None
for win_iter in context.window_manager.windows:
if win_iter != win_active:
win_other = win_iter
break
# Add cube in the other window.
with context.temp_override(window=win_other):
bpy.ops.mesh.primitive_cube_add()
my_objects = [context.scene.camera]
with context.temp_override(selected_objects=my_objects) as override:
override.logging_set(
True, # Enable logging.
hide_missing=True, # Don't show failed attempts.
)
bpy.ops.object.delete()
+20
View File
@@ -0,0 +1,20 @@
import bpy
view_layer = bpy.context.view_layer
# Create new light data-block.
light_data = bpy.data.lights.new(name="New Light", type="POINT")
# Create new object with our light data-block.
light_object = bpy.data.objects.new(name="New Light", object_data=light_data)
# Link light object to the active collection of current view layer,
# so that it'll appear in the current scene.
view_layer.active_layer_collection.collection.objects.link(light_object)
# Place light to a specified location.
light_object.location = (5.0, 5.0, 5.0)
# And finally select it and make it active.
light_object.select_set(True)
view_layer.objects.active = light_object