def load_model_from_gltf_file(filename): data = gltf.GLTF2().load(filename) data.convert_buffers(gltf.BufferFormat.BINARYBLOB) #print(data.nodes[0]) model_data = dict() meshes = list() skeleton = None for node in data.nodes: if node.mesh is not None: for p in data.meshes[node.mesh].primitives: mesh = extract_mesh(data, p) if "vertices" not in mesh: continue if p.material is not None: mesh["material"] = extract_material(data, p.material) meshes.append(mesh) if node.skin is not None: skeleton = extract_skeleton(data, node.skin) print(skeleton) #skeleton = None animations = extract_animations(data) print("found", len(animations), "animations") model_data["mesh_list"] = meshes model_data["skeleton"] = skeleton model_data["animations"] = animations return model_data
def main(): filename_base = 'D:/Google Drive/UBC Postdoc/Full Skeletal Rig/SkinCap Rig/SkeletalRig_v2_0' new_fname = filename_base + '_animated.gltf' filename = filename_base + '.gltf' gltfDict = mocap2gltf(mvnDict) curr_gltf = pygltf.GLTF2().load(filename) new_gltf = addGltfAnimation(curr_gltf, gltfDict) new_gltf.save(new_fname) return gltfDict, new_gltf
def save(self, filename): gltf = pygltflib.GLTF2( scene=0, scenes=[pygltflib.Scene(nodes=[self.root_node])], nodes=self.nodes, materials=self.materials, meshes=self.meshes, buffers=[pygltflib.Buffer(byteLength=len(self.buffer))], bufferViews=self.bufferViews, accessors=self.accessors) gltf.set_binary_blob(self.buffer) gltf.convert_buffers(pygltflib.BufferFormat.DATAURI) gltf.save_json(filename)
def __init__(self, vfs: VfsDatabase, export_path, filename, lod=0, save_to_one_dir=False, flat_file_layout=False, include_skeleton=False, texture_format=None): self.vfs = vfs self.filename = filename self.lod = lod self.save_to_one_dir = save_to_one_dir self.flat_file_layout = flat_file_layout self.include_skeleton = include_skeleton if texture_format is None: self.texture_format = 'dds' else: self.texture_format = texture_format self.gltf = pyg.GLTF2() self.gltf.asset.version = "2.0" self.gltf.asset.generator = "DECA extractor" self.gltf.extensionsUsed.append('KHR_materials_pbrSpecularGlossiness') self.d_stack = [] self.d_scene = DecaGltfScene(self, name='Scene0') if self.save_to_one_dir: self.export_path = os.path.join( export_path, filename + '.dir', 'model-lod_{}.gltf'.format(self.lod)) self.resource_prefix_abs = os.path.join(export_path, filename + '.dir') self.resource_prefix_uri = '' else: self.export_path = os.path.join( export_path, filename + '-lod_{}.gltf'.format(self.lod)) self.resource_prefix_abs = export_path self.resource_prefix_uri = '' dirs = os.path.dirname(filename) while len(dirs) > 0: self.resource_prefix_uri += '../' dirs = os.path.dirname(dirs) os.makedirs(os.path.dirname(self.export_path), exist_ok=True) self.db = Deca3dDatabase(self.vfs, self.resource_prefix_abs, self.resource_prefix_uri, self.flat_file_layout, self.texture_format)
import ctypes from io import BytesIO from typing import Dict, List, Callable import numpy as np import pygltflib from OpenGL import GL from PIL import Image as PIL_Image from tremor.graphics import shaders from tremor.graphics.mesh import Mesh from tremor.graphics.surfaces import MaterialTexture, TextureUnit, Material from tremor.util import configuration GLTF = pygltflib.GLTF2() def glb_object(filepath) -> pygltflib.GLTF2: f = None try: f = open(filepath) except FileNotFoundError: raise Exception('The specified file ' + filepath + ' could not be found') return GLTF.load_binary(filepath) class UnboundBuffer: """ I guess here's the ones we care about: * GL_ARRAY_BUFFER: the most common one. For anything that uses glVertexAttribPointer GL_TEXTURE_BUFFER: for textures?