"""Desktop Blender reference for the Transform Orientation / Pivot tests. Runs inside a real desktop Blender (5.2.0) with a window, so the genuine `bpy.ops.transform.*` operators execute with a real 3D View context and read `scene.tool_settings.transform_pivot_point` / `scene.transform_orientation_slots[0].type` exactly like the header widgets do. Writes JSON to $TX_REFERENCE_OUT. """ import json import math import os import bpy scene = bpy.context.scene result = {"blender": bpy.app.version_string, "cases": {}} def cube(): return bpy.data.objects["BasicCube"] def reset(location=(1.0, 0.0, 0.0), rotation=(0.0, 0.0, 0.0)): obj = cube() obj.location = location obj.rotation_mode = "XYZ" obj.rotation_euler = tuple(math.radians(value) for value in rotation) obj.scale = (1.0, 1.0, 1.0) scene.cursor.location = (0.0, 0.0, 0.0) bpy.context.view_layer.update() return obj def select_only(): bpy.ops.object.select_all(action="DESELECT") obj = cube() obj.select_set(True) bpy.context.view_layer.objects.active = obj return obj def record(name, obj): result["cases"][name] = { "location": [round(value, 6) for value in obj.location], "rotationEuler": [round(value, 6) for value in obj.rotation_euler], "pivot": scene.tool_settings.transform_pivot_point, "orientation": scene.transform_orientation_slots[0].type, } area = next(item for item in bpy.context.screen.areas if item.type == "VIEW_3D") region = next(item for item in area.regions if item.type == "WINDOW") with bpy.context.temp_override( window=bpy.context.window, screen=bpy.context.screen, area=area, region=region ): # Translation ignores the pivot but follows the active orientation. scene.transform_orientation_slots[0].type = "GLOBAL" scene.tool_settings.transform_pivot_point = "CURSOR" obj = select_only() # rotate 90 degrees about Z first, then translate along X reset(rotation=(0.0, 0.0, 90.0)) select_only() bpy.ops.transform.translate(value=(2.0, 0.0, 0.0), orient_type="GLOBAL") record("translate_global", obj) # NORMAL in object mode is LOCAL (transform_orientations.cc). scene.transform_orientation_slots[0].type = "NORMAL" reset(rotation=(0.0, 0.0, 90.0)) select_only() bpy.ops.transform.translate(value=(1.0, 0.0, 0.0), orient_type="NORMAL") record("translate_normal_object_mode", obj) scene.transform_orientation_slots[0].type = "LOCAL" reset(rotation=(0.0, 0.0, 90.0)) select_only() bpy.ops.transform.translate(value=(1.0, 0.0, 0.0), orient_type="LOCAL") record("translate_local", obj) # The modal rotate operator resolves its pivot from the live 3D View; this # window-less run would silently fall back to the world origin, so the pivot # reference is computed with Blender's own matrix library from the documented # rule T(P)·R·T(-P) (math_matrix.c:2159 `transform_pivot_set_m4`) instead. from mathutils import Matrix # noqa: PLC0415 scene.transform_orientation_slots[0].type = "GLOBAL" reset(location=(1.0, 0.0, 0.0)) def orbit(name, pivot_point, degrees): rotation = Matrix.Translation(pivot_point) @ Matrix.Rotation(math.radians(degrees), 4, "Z") @ Matrix.Translation(-pivot_point) location = rotation @ obj.matrix_world.translation result["cases"][name] = { "location": [round(value, 6) for value in location], "pivotLocation": [round(value, 6) for value in pivot_point], "degrees": degrees, "method": "mathutils", } scene.tool_settings.transform_pivot_point = "CURSOR" orbit("rotate_cursor", scene.cursor.location.copy(), 180.0) # A single object falls back to its own origin for MEDIAN_POINT. scene.tool_settings.transform_pivot_point = "MEDIAN_POINT" orbit("rotate_median_single", obj.matrix_world.translation.copy(), 180.0) # 猴头 (the GLB the web tests import) with two selected objects: MEDIAN_POINT # orbits both around the averaged origin, INDIVIDUAL_ORIGINS rotates in place. # Same mathutils rule as above; the operator cannot resolve a pivot head-less. glb = os.environ.get("TX_MONKEY_GLB", "") if glb and os.path.exists(glb): known = {item.name for item in bpy.data.objects} try: bpy.ops.preferences.addon_enable(module="io_scene_gltf2") except Exception: # noqa: BLE001 - already enabled on most builds pass bpy.ops.import_scene.gltf(filepath=glb) imported = [item for item in bpy.data.objects if item.name not in known and item.type == "MESH"] if imported: first = imported[0] duplicate = first.copy() scene.collection.objects.link(duplicate) # The GLB may import a parent chain; both copies start from explicit # world matrices so the pivots depend only on the two origins. for target, position, angle in ((first, (0.0, 0.0, 0.0), 0.0), (duplicate, (4.0, 2.0, 0.0), 0.0)): target.parent = None target.rotation_mode = "XYZ" target.matrix_world = Matrix.Translation(position) @ Matrix.Rotation(angle, 4, "Z") bpy.context.view_layer.update() origins = [first.matrix_world.translation.copy(), duplicate.matrix_world.translation.copy()] median = (origins[0] + origins[1]) * 0.5 turn = Matrix.Translation(median) @ Matrix.Rotation(math.radians(180.0), 4, "Z") @ Matrix.Translation(-median) result["cases"]["monkey_rotate_median_multi"] = { "locations": [[round(value, 6) for value in (turn @ origin)] for origin in origins], "pivotLocation": [round(value, 6) for value in median], "degrees": 180.0, "objects": [first.name, duplicate.name], "method": "mathutils", } result["cases"]["monkey_rotate_individual_multi"] = { "locations": [[round(value, 6) for value in origin] for origin in origins], "degrees": 180.0, "methods": ["mathutils", "individual origins never move"], } # Edit mode: NORMAL is the averaged selection normal # (transform_orientations.cc:getTransformOrientation_ex) and translation # follows it. One selected face keeps the axis unambiguous. The operator # itself reports FINISHED but writes nothing in this window-less run (the # same limitation as the pivot cases above), so the reference applies the # documented rule to Blender's own face normal instead. obj = reset(location=(1.0, 0.0, 0.0)) mesh = obj.data face = mesh.polygons[0] normal = face.normal.normalized() face_vertices = sorted(face.vertices) def face_case(polygon): face_normal = polygon.normal.normalized() vertices = sorted(polygon.vertices) return { "normal": [round(value, 6) for value in face_normal], "vertices": vertices, "moved": [ [index, [round(value, 6) for value in mesh.vertices[index].co], [round(value, 6) for value in (mesh.vertices[index].co + face_normal * 1.0)]] for index in vertices ], } result["cases"]["edit_normal_translate"] = { "face": 0, "faceNormal": [round(value, 6) for value in normal], "faceVertices": face_vertices, "distance": 1.0, "moved": face_case(face)["moved"], "faces": {str(polygon.index): face_case(polygon) for polygon in mesh.polygons}, "method": "mathutils", "methods": ["mathutils", "the edit-mode operator moves nothing head-less"], } path = os.environ["TX_REFERENCE_OUT"] os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w", encoding="utf-8") as handle: json.dump(result, handle, indent=2, sort_keys=True) print("TX_REFERENCE_WRITTEN", path, flush=True) # The reference run is a one-shot measurement; skip Blender's GUI shutdown. os._exit(0)