import {z} from 'zod' export type DirectionalAccessoryAxes = {forward?: number | undefined, up?: number | undefined, out?: number | undefined}; export type DirectionalAccessoryItem = {item: string, name?: string | undefined, placement?: Record | undefined, kind?: ('blade' | 'gun' | 'staff' | 'bow' | 'shield' | 'helmet' | 'hat' | 'back' | 'other') | undefined, point?: ('right_hand' | 'left_hand' | 'head' | 'back' | 'hips') | undefined, bone?: string | undefined, size?: number | undefined, move?: DirectionalAccessoryAxes | undefined, turn?: DirectionalAccessoryAxes | undefined, flip?: boolean | undefined, grip?: boolean | undefined}; export type DirectionalAccessoriesPayload = {model: string, items: Array, save_on_model?: boolean | undefined, keep_saved?: boolean | undefined}; export type CreateApiAssetsSpriteDirectionalAccessoriesArgs = {body: DirectionalAccessoriesPayload}; export type CreateApiAssetsSpriteDirectionalAccessoriesOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const directionalAccessoryPlacement = z.object({bone: z.string().optional(), point: z.string().optional(), kind: z.string().optional(), flipped: z.boolean().optional(), grip: z.boolean().optional(), base_scale: z.number().optional(), position: z.array(z.number()).optional(), quaternion: z.array(z.number()).optional(), scale: z.array(z.number()).optional()}); export const directionalAccessoriesResult = z.object({model_url: z.string().optional(), saved: z.boolean().optional(), items: z.array(z.object({name: z.string().optional(), placement: directionalAccessoryPlacement.optional()})).optional(), kept: z.array(z.object({name: z.string().optional(), bone: z.string().optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Put accessories on a rigged 3D model for Directional Sprites: up to 8 items, each at the `placement` fitDirectionalAccessory returned (or, without one, at the automatic fit plus the same size/move/turn adjustments). Returns `model_url`, one GLB of the model holding them all (kept 7 days): pass it as `model` to createDirectionalSprites, createDirectionalStills or previewDirectionalSprites. When `model` is the URL of one of your 3D models (as listGenerations or create3DModel returned it), the items are also saved on that model (`saved: true`; `save_on_model: false` skips it), so the Ludo web app's Directional Sprites shows them on it. Items the model already had saved stay on it and in the GLB (`kept`), except those held by a bone a new item takes; `keep_saved: false` replaces them all. Free (no credits) and synchronous. Requires an API key (user scope). * * POST /assets/sprite/directional-accessories */ export const createApiAssetsSpriteDirectionalAccessories = async (args: CreateApiAssetsSpriteDirectionalAccessoriesArgs, options: CreateApiAssetsSpriteDirectionalAccessoriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/assets/sprite/directional-accessories${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(directionalAccessoriesResult, await res.json()) };