def fix_uris(fn: str, gltf: GLTF): dn = os.path.dirname(fn) resource: GLTFResource uri2new = {} for i, resource in list(enumerate(gltf.resources)): # logger.info(resource=resource.uri) if resource.data is None: f = os.path.join(dn, resource.uri) resource.data = read_bytes_from_file(f) md5 = get_md5(resource.data) o = os.path.basename(resource.uri) newuri = f"{md5}-{o}" resource2 = GLBResource(resource.data) resource2._uri = newuri gltf.resources.append(resource2) resource3 = gltf.convert_to_file_resource(resource2, filename=newuri) gltf.resources.append(resource3) gltf.resources.remove(resource) uri2new[resource.uri] = newuri if gltf.model: image: Image if gltf.model.images: for image in gltf.model.images: if image.uri in uri2new: image.uri = uri2new[image.uri] buffer: Buffer if gltf.model.buffers: for buffer in gltf.model.buffers: if buffer.uri in uri2new: buffer.uri = uri2new[buffer.uri]
def make_texture(gltf: GLTF, fn: str) -> int: data = read_bytes_from_file(fn) md5 = get_md5(data) bn = os.path.basename(fn) noext, ext = os.path.splitext(bn) if fn not in gltf.fn2texture_index: uri = f"textures/{noext}-{md5}{ext}" rf = FileResource(uri, data=data) gltf.resources.append(rf) image = Image(uri=uri) image_index = add_image(gltf, image) texture = Texture(sampler=0, source=image_index) texture_index = add_texture(gltf, texture) gltf.fn2texture_index[fn] = texture_index return gltf.fn2texture_index[fn]
def draw_svg(self, drawing, g): T = 0.562 / 0.585 T = 1 S = 1.0 rect = drawing.rect(insert=(-S / 2, -S / 2), size=(S, S), fill="#0a0", stroke="none") g.add(rect) if self.fn: texture = read_bytes_from_file(self.fn) if b"git-lfs" in texture: msg = f"The file {self.fn} is a Git LFS pointer. Repo not checked out correctly." raise Exception(msg) ID = f"texture-{self.kind}" for img in drawing.defs.elements: if img.attribs.get("id", None) == ID: break else: href = data_encoded_for_src(texture, mime_from_fn(self.fn)) img = drawing.image( href=href, size=(T, T), insert=(-T / 2, -T / 2), # style=" ", style="transform: rotate(0deg) scaleX(-1) rotate(-90deg) ", ) img.attribs["class"] = "tile-textures" img.attribs["id"] = ID drawing.defs.add(img) use = Use(f"#{ID}") g.add(use) # # if draw_directions_lanes: # if self.kind != 'floor': # start = (-0.5, -0.25) # end = (+0, -0.25) # line = drawing.line(start=start, end=end, stroke='blue', stroke_width='0.01') # g.add(line) draw_axes(drawing, g) draw_children(drawing, self, g)
def export_sign(gltf: GLTF, name, ob: Sign): _ = get_resource_path("sign_generic/main.gltf") tname = ob.get_name_texture() logger.info(f"t = {type(ob)} tname = {tname}") fn_texture = get_texture_file(tname)[0] uri = os.path.join("textures/signs", os.path.basename(fn_texture)) data = read_bytes_from_file(fn_texture) def transform(g: GLTF) -> GLTF: rf = FileResource(uri, data=data) g.resources.append(rf) g.model.images[0] = Image(name=tname, uri=uri) return g index = embed_external(gltf, _, key=tname, transform=transform) # M = SE3_roty(np.pi/5) M = SE3_rotz(-np.pi / 2) n2 = Node(matrix=gm(M), children=[index]) return add_node(gltf, n2)
def resource_from_file(fn: FilePath) -> FileResource: data = read_bytes_from_file(fn) bn = os.path.basename(fn) return FileResource(bn, data=data)