- Offscreen worker backend publishes the camera probe, so the G/R/S modal's pointer basis, transform gizmo and wireframe work on both renderers. - Edit-mode INDIVIDUAL_ORIGINS pivots every connected island on its own centre, committed as one grouped transaction (T(P)·R·S·T(-P) in object space). - Gizmo hits carry the pivot index; orthographic views size the axes from the visible world height instead of the 45° fov fallback. - View-menu navigation no longer switches workspace when a viewport is already on screen (the engine workspace transaction dropped the editor out of Edit). - Modal commits queue behind the shared command tail, so a trailing editEditorContext cannot consume the user's first Ctrl+Z. - Floating sidebar rotation field confirmed in degrees; headed cases for the offscreen path, per-island pivots and the sidebar unit, plus receipts/docs. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
228 lines
11 KiB
TypeScript
228 lines
11 KiB
TypeScript
import { expect, type Page } from '@playwright/test';
|
|
import { nativeRequest, waitNativeInputReady } from './native-modeling-workflow';
|
|
import { pythonReply } from './wasm-console-workflow';
|
|
|
|
/**
|
|
* Readback that tolerates a concurrent display-level change. The App keeps the
|
|
* native Display Axes flag in sync with the active object (`viewportObjectAxis`),
|
|
* so the python transaction can legitimately observe `changed:true` while still
|
|
* returning correct state. `queryNativeJSON` keeps its stricter `changed:false`
|
|
* contract for tests that prove readbacks are side-effect free.
|
|
*/
|
|
async function queryViewportJSON(page: Page, source: string) {
|
|
const response = await nativeRequest(page, { type: 'jsonCommand', cmd: 'pythonExec', params: { source: `exec(${JSON.stringify(source)}, {'__name__': '__wbv2_viewport_readback__'})`, filename: 'ViewportNavigationReadback.py', console: false } });
|
|
const reply = await pythonReply(page);
|
|
expect(reply).not.toBeNull();
|
|
expect(reply!.requestId).toBe(response.requestId);
|
|
expect(reply!.state, reply!.output).toBe('DONE');
|
|
expect(Number.isSafeInteger(reply!.revision)).toBe(true);
|
|
return { revision: reply!.revision, value: JSON.parse(reply!.output) };
|
|
}
|
|
|
|
export interface NativeView3D {
|
|
viewDistance: number;
|
|
viewLocation: [number, number, number];
|
|
viewRotation: [number, number, number, number];
|
|
perspective: string;
|
|
region: [number, number, number, number];
|
|
window: [number, number];
|
|
}
|
|
|
|
const finite = (value: unknown, label: string): number => {
|
|
expect(typeof value, label).toBe('number');
|
|
expect(Number.isFinite(value as number), label).toBe(true);
|
|
return value as number;
|
|
};
|
|
|
|
const sum = (values: readonly number[]): number => values.reduce((total, value) => total + Math.abs(value), 0);
|
|
|
|
/** Region 3D state of the native VIEW_3D area (zoom / pan / orbit readback). */
|
|
export async function readNativeView3D(page: Page): Promise<NativeView3D> {
|
|
const source = [
|
|
"import bpy, json\narea = bpy.context.area",
|
|
"assert area.type == 'VIEW_3D', area.type",
|
|
"view = area.spaces.active.region_3d",
|
|
"region = next(r for r in area.regions if r.type == 'WINDOW')",
|
|
"print(json.dumps({'viewDistance': view.view_distance, 'viewLocation': list(view.view_location),",
|
|
" 'viewRotation': list(view.view_rotation), 'perspective': view.view_perspective,",
|
|
" 'region': [region.x, region.y, region.width, region.height],",
|
|
" 'window': [bpy.context.window.width, bpy.context.window.height]}))",
|
|
].join('\n');
|
|
const { value } = await queryViewportJSON(page, source);
|
|
expect(value !== null && typeof value === 'object').toBe(true);
|
|
const view: NativeView3D = {
|
|
viewDistance: finite(value.viewDistance, 'viewDistance'),
|
|
viewLocation: value.viewLocation,
|
|
viewRotation: value.viewRotation,
|
|
perspective: value.perspective,
|
|
region: value.region,
|
|
window: value.window,
|
|
};
|
|
expect(view.viewDistance).toBeGreaterThan(0);
|
|
for (const vector of [view.viewLocation, view.viewRotation]) {
|
|
expect(Array.isArray(vector)).toBe(true);
|
|
expect(vector).toHaveLength(vector.length === 4 ? 4 : 3);
|
|
vector.forEach((item: unknown, index: number) => finite(item, `vector[${index}]`));
|
|
}
|
|
for (const box of [view.region, view.window]) {
|
|
expect(Array.isArray(box)).toBe(true);
|
|
expect(box).toHaveLength(box.length === 4 ? 4 : 2);
|
|
box.forEach((item: unknown, index: number) => finite(item, `box[${index}]`));
|
|
}
|
|
return view;
|
|
}
|
|
|
|
/** Total movement of the orbit quaternion, sign independent. */
|
|
export const rotationDelta = (before: readonly number[], after: readonly number[]): number =>
|
|
sum([0, 1, 2, 3].map((index) => (after[index] ?? 0) - (before[index] ?? 0)));
|
|
|
|
export const locationDelta = (before: readonly number[], after: readonly number[]): number =>
|
|
sum([0, 1, 2].map((index) => after[index] - before[index]));
|
|
|
|
/**
|
|
* Engine-level selection (the same state the native Outliner writes). Selecting
|
|
* through the React Outliner requires hiding the native canvas, which leaves a
|
|
* pending WM UI handler that keeps viewport input from becoming ready; this
|
|
* helper drives the authoritative Main selection directly instead.
|
|
*/
|
|
export async function selectNativeObject(page: Page, name: string) {
|
|
const source = [
|
|
'import bpy, json',
|
|
`o = bpy.data.objects[${JSON.stringify(name)}]`,
|
|
"for other in bpy.context.view_layer.objects:\n other.select_set(False)",
|
|
"o.select_set(True)\nbpy.context.view_layer.objects.active = o",
|
|
"print(json.dumps({'active': bpy.context.view_layer.objects.active.name if bpy.context.view_layer.objects.active else None,",
|
|
" 'selected': sorted(x.name for x in bpy.context.scene.objects if x.select_get())}))",
|
|
].join('\n');
|
|
const { value } = await queryViewportJSON(page, source);
|
|
expect(value.active).toBe(name);
|
|
return value as { active: string | null; selected: string[] };
|
|
}
|
|
|
|
/** Clear the native selection so a later click has to select for real. */
|
|
export async function deselectNativeObjects(page: Page) {
|
|
const source = [
|
|
'import bpy, json',
|
|
"bpy.ops.object.select_all(action='DESELECT')",
|
|
"print(json.dumps({'selected': sorted(o.name for o in bpy.context.scene.objects if o.select_get())}))",
|
|
].join('\n');
|
|
const { value } = await queryViewportJSON(page, source);
|
|
expect(value.selected).toEqual([]);
|
|
}
|
|
|
|
/** Selected objects and the active object of the native scene. */
|
|
export async function hideNativeObjects(page: Page, keep: readonly string[] = []) {
|
|
/* Views draw through the draw manager, which skips `hide_viewport` objects, so
|
|
* this removes occluders from a pick without touching selection or undo. */
|
|
const source = [
|
|
'import bpy, json',
|
|
`keep = set(${JSON.stringify(keep)})`,
|
|
'hidden = []',
|
|
'for o in bpy.context.scene.objects:',
|
|
' if o.name not in keep and not o.hide_viewport:',
|
|
' o.hide_viewport = True; hidden.append(o.name)',
|
|
"print(json.dumps({'hidden': hidden}))",
|
|
].join('\n');
|
|
const { value } = await queryViewportJSON(page, source);
|
|
return value.hidden as string[];
|
|
}
|
|
|
|
export async function readNativeSelection(page: Page): Promise<{ selected: string[]; active: string | null }> {
|
|
const source = [
|
|
"import bpy, json\nactive = bpy.context.view_layer.objects.active",
|
|
"print(json.dumps({'selected': sorted(o.name for o in bpy.context.scene.objects if o.select_get()),",
|
|
" 'active': active.name if active else None}))",
|
|
].join('\n');
|
|
const { value } = await queryViewportJSON(page, source);
|
|
expect(value !== null && typeof value === 'object').toBe(true);
|
|
expect(Array.isArray(value.selected)).toBe(true);
|
|
expect(value.selected.every((item: unknown) => typeof item === 'string')).toBe(true);
|
|
expect(value.active === null || typeof value.active === 'string').toBe(true);
|
|
return { selected: value.selected, active: value.active };
|
|
}
|
|
|
|
/** Object transform plus the native Display Axes flag (`Object.show_axis`). */
|
|
export async function readNativeObjectState(page: Page, name: string) {
|
|
const source = [
|
|
`import bpy, json\no = bpy.data.objects[${JSON.stringify(name)}]`,
|
|
"print(json.dumps({'location': list(o.location), 'rotation': list(o.rotation_euler),",
|
|
" 'scale': list(o.scale), 'showAxis': bool(o.show_axis)}))",
|
|
].join('\n');
|
|
const { value } = await queryViewportJSON(page, source);
|
|
expect(value !== null && typeof value === 'object').toBe(true);
|
|
for (const key of ['location', 'rotation', 'scale']) {
|
|
expect(Array.isArray(value[key])).toBe(true);
|
|
expect(value[key]).toHaveLength(3);
|
|
value[key].forEach((item: unknown, index: number) => finite(item, `${key}[${index}]`));
|
|
}
|
|
expect(typeof value.showAxis).toBe('boolean');
|
|
return { location: value.location as [number, number, number], rotation: value.rotation as [number, number, number], scale: value.scale as [number, number, number], showAxis: value.showAxis as boolean };
|
|
}
|
|
|
|
/**
|
|
* Screen point of an object's origin, expressed in CSS pixels of the native
|
|
* desktop canvas.
|
|
*/
|
|
export async function projectNativeObjectOrigin(page: Page, name: string) {
|
|
const source = [
|
|
`import bpy, json\nfrom bpy_extras.view3d_utils import location_3d_to_region_2d\no = bpy.data.objects[${JSON.stringify(name)}]`,
|
|
"area = bpy.context.area\nassert area.type == 'VIEW_3D'",
|
|
"region = next(r for r in area.regions if r.type == 'WINDOW')",
|
|
"view = area.spaces.active.region_3d",
|
|
"point = location_3d_to_region_2d(region, view, o.matrix_world.translation)",
|
|
"assert point is not None, 'Object origin is outside the view'",
|
|
"print(json.dumps({'x': region.x + point.x, 'y': region.y + point.y,",
|
|
" 'window': [bpy.context.window.width, bpy.context.window.height]}))",
|
|
].join('\n');
|
|
const { value } = await queryViewportJSON(page, source);
|
|
expect(value !== null && typeof value === 'object').toBe(true);
|
|
finite(value.x, 'x');
|
|
finite(value.y, 'y');
|
|
expect(Array.isArray(value.window)).toBe(true);
|
|
expect(value.window).toHaveLength(2);
|
|
const bounds = await page.locator('canvas[aria-label="Blender desktop"]').boundingBox();
|
|
expect(bounds).not.toBeNull();
|
|
return {
|
|
native: value as { x: number; y: number; window: [number, number] },
|
|
point: { x: bounds!.x + (value.x as number) * bounds!.width / value.window[0], y: bounds!.y + (1 - (value.y as number) / value.window[1]) * bounds!.height },
|
|
};
|
|
}
|
|
|
|
/** A point inside the native VIEW_3D main region, as fractions of its rect. */
|
|
export async function nativeViewportPoint(page: Page, fx = 0.5, fy = 0.5) {
|
|
const area = await page.evaluate(() => (window as any).__nativeWorkflowAreas?.find((area: any) => area.editor === 'VIEW_3D' && area.regions.some((region: any) => region.kind === 'MAIN' && region.visible)));
|
|
expect(area).toBeDefined();
|
|
const { x, y, width, height } = area.rect as { x: number; y: number; width: number; height: number };
|
|
const bounds = await page.locator('canvas[aria-label="Blender desktop"]').boundingBox();
|
|
expect(bounds).not.toBeNull();
|
|
return { x: bounds!.x + (x + width * fx) * bounds!.width, y: bounds!.y + (1 - y - height * fy) * bounds!.height };
|
|
}
|
|
|
|
/** Centre of the native VIEW_3D region in page coordinates. */
|
|
export const nativeViewportCenter = (page: Page) => nativeViewportPoint(page, 0.5, 0.5);
|
|
|
|
/** Press-move-release one gesture in the viewport, holding `modifier` if given. */
|
|
export async function dragViewport(page: Page, point: { x: number; y: number }, options: { dx: number; dy: number; button?: 'middle' | 'left' | 'right'; modifier?: 'Shift' | 'Control' }) {
|
|
const button = options.button ?? 'middle';
|
|
if (options.modifier) await page.keyboard.down(options.modifier);
|
|
try {
|
|
await page.mouse.move(point.x, point.y);
|
|
await page.mouse.down({ button });
|
|
for (let step = 1; step <= 8; step++) await page.mouse.move(point.x + (options.dx * step) / 8, point.y + (options.dy * step) / 8);
|
|
await page.mouse.up({ button });
|
|
/* The release is flushed by the next animation frame, not by mouse.up. Wait
|
|
* for the canvas to re-publish readiness, otherwise the command that follows
|
|
* a drag can reach Main first and be refused with NATIVE_GESTURE_ACTIVE. */
|
|
await waitNativeInputReady(page, point);
|
|
}
|
|
finally {
|
|
if (options.modifier) await page.keyboard.up(options.modifier);
|
|
}
|
|
}
|
|
|
|
/** Wheel ticks with the pointer parked over the viewport. */
|
|
export async function wheelViewport(page: Page, point: { x: number; y: number }, deltaY: number) {
|
|
await page.mouse.move(point.x, point.y);
|
|
await page.mouse.wheel(0, deltaY);
|
|
}
|