"""Photo-scanned models for the kitchen's objects in the Lit renderer: the YCB Object and Model Set (CC BY 4.0, http://ycbbenchmarks.com), its 16k-face Google scans, each one GLB with its texture (as JPEG), and the list of which object body each stands in for (the "objects" of dressing.json). usage: python tools/make_objects.py (needs trimesh, numpy, pillow) Lit draws each model on its body, fitted to the body's collision shapes (src/render/view.js); the physics and the grasps stay with the shapes. Toon and Pixel keep the shapes. The colours match the objects' labels (the red can, the blue can). """ import io import json import os import struct import tarfile import tempfile import urllib.request import numpy as np import trimesh from PIL import Image ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") OUT = os.path.join(ROOT, "assets", "environments", "kitchen", "dressing") URL = "http://ycb-benchmarks.s3-website-us-east-1.amazonaws.com/data/google/{}_google_16k.tgz" # object body -> (YCB model, yaw in degrees before fitting, texture size, roughness, metalness) OBJECTS = { "apple": ("013_apple", 0, 512, 0.45, 0.0), "orange": ("017_orange", 0, 512, 0.6, 0.0), "lemon": ("014_lemon", 0, 512, 0.5, 0.0), "banana": ("011_banana", 90, 512, 0.55, 0.0), # its length along the capsule (the body's x) "can": ("005_tomato_soup_can", 0, 1024, 0.35, 0.3), # the red can (scene.xml's material; a cola in the tasks) "can_2": ("002_master_chef_can", 0, 1024, 0.35, 0.3), # the blue can "mug": ("025_mug", 0, 1024, 0.25, 0.0), "bowl": ("024_bowl", 0, 512, 0.3, 0.0), "plate": ("029_plate", 0, 512, 0.3, 0.0), "bottle": ("006_mustard_bottle", 0, 1024, 0.35, 0.0), } def convert(cache, body, oid, yaw, size, roughness, metalness): tgz = os.path.join(cache, f"{oid}.tgz") if not os.path.exists(tgz): urllib.request.urlretrieve(URL.format(oid), tgz) with tarfile.open(tgz) as t: t.extractall(cache, filter="data") mesh = trimesh.load(os.path.join(cache, oid, "google_16k", "textured.obj"), force="mesh", process=False) v = np.asarray(mesh.vertices, np.float64) turn = yaw if body == "mug": # its handle toward +x, where the shape's handle is: the handle is the thin side, away from the mean lo, hi = v.min(0), v.max(0) if v[:, 0].mean() > (lo[0] + hi[0]) / 2: turn += 180 a = np.radians(turn) rz = np.array([[np.cos(a), -np.sin(a), 0], [np.sin(a), np.cos(a), 0], [0, 0, 1]]) pos = (v @ rz.T).astype(np.float32) nrm = (np.asarray(mesh.vertex_normals, np.float64) @ rz.T).astype(np.float32) uv = np.asarray(mesh.visual.uv, np.float32).copy() uv[:, 1] = 1 - uv[:, 1] idx = np.asarray(mesh.faces, np.uint32).reshape(-1) img = mesh.visual.material.image.convert("RGB").resize((size, size), Image.LANCZOS) jpg = io.BytesIO() img.save(jpg, "JPEG", quality=88, optimize=True) # one buffer: indices, positions, normals, uvs, the image parts, views, accessors = [], [], [] data = bytearray() def view(blob, target=None): while len(data) % 4: data.append(0) views.append({"buffer": 0, "byteOffset": len(data), "byteLength": len(blob), **({"target": target} if target else {})}) data.extend(blob) return len(views) - 1 def accessor(arr, kind, comp, target, minmax=False): a = {"bufferView": view(arr.tobytes(), target), "componentType": comp, "count": int(len(arr) if kind != "SCALAR" else arr.size), "type": kind} if minmax: a["min"], a["max"] = arr.min(0).tolist(), arr.max(0).tolist() accessors.append(a) return len(accessors) - 1 i_idx = accessor(idx, "SCALAR", 5125, 34963) i_pos = accessor(pos, "VEC3", 5126, 34962, True) i_nrm = accessor(nrm, "VEC3", 5126, 34962) i_uv = accessor(uv, "VEC2", 5126, 34962) i_img = view(jpg.getvalue()) gltf = { "asset": {"version": "2.0", "generator": "tools/make_objects.py"}, "scene": 0, "scenes": [{"nodes": [0]}], "nodes": [{"mesh": 0, "name": oid}], "meshes": [{"name": oid, "primitives": [{"attributes": {"POSITION": i_pos, "NORMAL": i_nrm, "TEXCOORD_0": i_uv}, "indices": i_idx, "material": 0}]}], "materials": [{"name": oid, "pbrMetallicRoughness": {"baseColorTexture": {"index": 0}, "metallicFactor": metalness, "roughnessFactor": roughness}}], "textures": [{"source": 0, "sampler": 0}], "images": [{"bufferView": i_img, "mimeType": "image/jpeg"}], "samplers": [{"magFilter": 9729, "minFilter": 9987, "wrapS": 33071, "wrapT": 33071}], "accessors": accessors, "bufferViews": views, "buffers": [{"byteLength": len(data)}], } text = json.dumps(gltf, separators=(",", ":")).encode() text += b" " * ((4 - len(text) % 4) % 4) while len(data) % 4: data.append(0) name = f"ycb_{oid[4:]}" path = os.path.join(OUT, f"{name}.glb") with open(path, "wb") as f: f.write(struct.pack("