Exemple #1
0
    def load_dae(self, path, postprocess=None):
        logger.info("Loading model:" + path + "...")

        if postprocess:
            self.scene = pyassimp.load(path, postprocess)
        else:
            self.scene = pyassimp.load(path)
        logger.info("Done.")

        scene = self.scene
        #log some statistics
        logger.info("  meshes: %d" % len(scene.meshes))
        logger.info("  total faces: %d" %
                    sum([len(mesh.faces) for mesh in scene.meshes]))
        logger.info("  materials: %d" % len(scene.materials))
        self.bb_min, self.bb_max = get_bounding_box(self.scene)
        logger.info("  bounding box:" + str(self.bb_min) + " - " +
                    str(self.bb_max))

        self.scene_center = [(a + b) / 2.
                             for a, b in zip(self.bb_min, self.bb_max)]

        for index, mesh in enumerate(scene.meshes):
            self.prepare_gl_buffers(mesh)

        # Finally release the model
        pyassimp.release(scene)
    def load_model(self, path, postprocess = aiProcessPreset_TargetRealtime_MaxQuality):
        logger.info("Loading model:" + path + "...")

        if postprocess:
            self.scene = pyassimp.load(path, postprocess)
        else:
            self.scene = pyassimp.load(path)
        logger.info("Done.")

        scene = self.scene
        #log some statistics
        logger.info("  meshes: %d" % len(scene.meshes))
        logger.info("  total faces: %d" % sum([len(mesh.faces) for mesh in scene.meshes]))
        logger.info("  materials: %d" % len(scene.materials))
        self.bb_min, self.bb_max = get_bounding_box(self.scene)
        logger.info("  bounding box:" + str(self.bb_min) + " - " + str(self.bb_max))

        self.scene_center = [(a + b) / 2. for a, b in zip(self.bb_min, self.bb_max)]

        for index, mesh in enumerate(scene.meshes):
            self.prepare_gl_buffers(mesh)

        # Finally release the model
        pyassimp.release(scene)

        logger.info("Ready for 3D rendering!")
Exemple #3
0
def main(filename=None):

    scene = pyassimp.load(filename)
    
    #the model we load
    print("MODEL:" + filename)
    print
    
    #write some statistics
    print("SCENE:")
    print("  meshes:" + str(len(scene.meshes)))
    print("  materials:" + str(len(scene.materials)))
    print("  textures:" + str(len(scene.textures)))
    print
    
    print("NODES:")
    recur_node(scene.rootnode)

    print
    print("MESHES:")
    for index, mesh in enumerate(scene.meshes):
        print("  MESH" + str(index+1))
        print("    material id:" + str(mesh.materialindex+1))
        print("    vertices:" + str(len(mesh.vertices)))
        print("    first 3 verts:\n" + str(mesh.vertices[:3]))
        if mesh.normals.any():
                print("    first 3 normals:\n" + str(mesh.normals[:3]))
        else:
                print("    no normals")
        print("    colors:" + str(len(mesh.colors)))
        tcs = mesh.texturecoords
        if tcs:
            for index, tc in enumerate(tcs):
                print("    texture-coords "+ str(index) + ":" + str(len(tcs[index])) + "first3:" + str(tcs[index][:3]))

        else:
            print("    no texture coordinates")
        print("    uv-component-count:" + str(len(mesh.numuvcomponents)))
        print("    faces:" + str(len(mesh.faces)) + " -> first:\n" + str(mesh.faces[:3]))
        print("    bones:" + str(len(mesh.bones)) + " -> first:" + str([str(b) for b in mesh.bones[:3]]))
        print

    print("MATERIALS:")
    for index, material in enumerate(scene.materials):
        print("  MATERIAL (id:" + str(index+1) + ")")
        for key, value in material.properties.items():
            print("    %s: %s" % (key, value))
    print
    
    print("TEXTURES:")
    for index, texture in enumerate(scene.textures):
        print("  TEXTURE" + str(index+1))
        print("    width:" + str(texture.width))
        print("    height:" + str(texture.height))
        print("    hint:" + str(texture.achformathint))
        print("    data (size):" + str(len(texture.data)))
   
    # Finally release the model
    pyassimp.release(scene)
Exemple #4
0
    def load(self, filename, ctx=None, world=DEFAULT_WORLD, root=None, only_meshes=False):
        """Loads a Collada (or any Assimp compatible model) file in the world.

        The kinematic chains are added to the world's geometric state.
        The meshes are added to the meshes repository.

        A new 'load' event is also added in the world timeline.

        :param string path: the path (relative or absolute) to the Collada resource
        :param Context ctx: an existing underworlds context. If not provided, a
                            new one is created (named 'model loader')
        :param string world: the target world for the creation of nodes
        :param Node root: if given, the loaded nodes will be parented to this
                          node instead of the scene's root.
        :param bool only_meshes: if true, no node is created. Only the
        meshes are pushed to the server.
        :returns: the list of loaded underworlds nodes.
        """

        if not only_meshes and world is None:
            raise RuntimeError("Can not create nodes if the world is None")

        close_ctx_at_end = False

        # Create a context if needed:
        if ctx is None:
            close_ctx_at_end = True
            ctx = underworlds.Context("model loader")

        logger.info("Loading model <%s> with ASSIMP..." % filename)
        model = pyassimp.load(filename, aiProcessPreset_TargetRealtime_MaxQuality)
        logger.info("...done")

        if not only_meshes:
            if not root:
                logger.info("Merging the root nodes")
                self.node_map[model.rootnode.name] = (model.rootnode, ctx.worlds[world].scene.rootnode)
        else:
            self.node_map[model.rootnode.name] = (model.rootnode, Node())

        logger.info("Nodes found:")
        self.recur_node(model.rootnode, model)

        logger.info("%d nodes in the model" % len(self.node_map))
        logger.info("Loading the nodes...")
        for n, pair in list(self.node_map.items()):
            self.fill_node_details(*pair,
                                    assimp_model = model,
                                    custom_root=root)
        logger.info("...done")


        # Send first the meshes to make sure they are available on the server
        # when needed by clients.  (but only if they do not already exist on
        # the server)
        logger.info("Sending meshes to the server...")
        count_sent = 0
        count_notsent = 0
        for id, mesh in list(self.meshes.items()):
            if ctx.has_mesh(id):
                logger.debug("Not resending mesh <%s>: already available on the server" % id)
                count_notsent += 1
            else:
                logger.debug("Sending mesh <%s>" % id)
                ctx.push_mesh(mesh)
                count_sent += 1

        logger.info("Sent %d meshes (%d were already available on the server)" % (count_sent, count_notsent))

        if not only_meshes:
            nodes = ctx.worlds[world].scene.nodes

            # Send the nodes to the server (only the nodes)
            logger.info("Sending the nodes to the server...")
            for name, pair in list(self.node_map.items()):
                nodes.update(pair[1])


        pyassimp.release(model)



        if close_ctx_at_end:
            ctx.close()

        return [nodes[1] for _,nodes in self.node_map.items()]
Exemple #5
0
    def load(self, filename, ctx=None, world=DEFAULT_WORLD, root=None, only_meshes=False, scale = 1.0):
        """Loads a Collada (or any Assimp compatible model) file in the world.

        The kinematic chains are added to the world's geometric state.
        The meshes are added to the meshes repository.

        A new 'load' event is also added in the world timeline.

        :param string path: the path (relative or absolute) to the Collada resource
        :param Context ctx: an existing underworlds context. If not provided, a
                            new one is created (named 'model loader')
        :param string world: the target world for the creation of nodes
        :param Node root: if given, the loaded nodes will be parented to this
                          node instead of the scene's root.
        :param bool only_meshes: if true, no node is created. Only the
        meshes are pushed to the server.
        :param float scale : scale of the model
        :returns: the list of loaded underworlds nodes.
        """

        self.meshes = {}
        self.node_map = {}

        if not only_meshes and world is None:
            raise RuntimeError("Can not create nodes if the world is None")

        close_ctx_at_end = False

        # Create a context if needed:
        if ctx is None:
            close_ctx_at_end = True
            ctx = underworlds.Context("model loader")

        logger.info("Loading model <%s> with ASSIMP..." % filename)
        model = pyassimp.load(filename, aiProcessPreset_TargetRealtime_MaxQuality)
        logger.info("...done")

        if not only_meshes:
            if not root:
                logger.info("Merging the root nodes")
                self.node_map[model.rootnode.name] = (model.rootnode, ctx.worlds[world].scene.rootnode)
        else:
            self.node_map[model.rootnode.name] = (model.rootnode, Entity())

        logger.info("Nodes found:")
        self.recur_node(model.rootnode, model)

        logger.info("%d nodes in the model" % len(self.node_map))
        logger.info("Loading the nodes...")
        for n, pair in list(self.node_map.items()):
            self.fill_node_details(*pair,
                                    assimp_model = model,
                                    custom_root=root, scale=scale)
        logger.info("...done")


        # Send first the meshes to make sure they are available on the server
        # when needed by clients.  (but only if they do not already exist on
        # the server)
        logger.info("Sending meshes to the server...")
        count_sent = 0
        count_notsent = 0
        for id, mesh in list(self.meshes.items()):
            if ctx.has_mesh(id):
                logger.debug("Not resending mesh <%s>: already available on the server" % id)
                count_notsent += 1
            else:
                logger.debug("Sending mesh <%s>" % id)
                ctx.push_mesh(mesh)
                count_sent += 1

        logger.info("Sent %d meshes (%d were already available on the server)" % (count_sent, count_notsent))

        if not only_meshes:
            nodes = ctx.worlds[world].scene.nodes

            # Send the nodes to the server (only the nodes)
            logger.info("Sending the nodes to the server...")
            for name, pair in list(self.node_map.items()):
                nodes.update(pair[1])


        pyassimp.release(model)



        if close_ctx_at_end:
            ctx.close()

        return [nodes[1] for _,nodes in self.node_map.items()]
Exemple #6
0
def main(filename=None):

    scene = pyassimp.load(filename)

    #the model we load
    print("MODEL:" + filename)
    print

    #write some statistics
    print("SCENE:")
    print("  meshes:" + str(len(scene.meshes)))
    print("  materials:" + str(len(scene.materials)))
    print("  textures:" + str(len(scene.textures)))
    print

    print("NODES:")
    recur_node(scene.rootnode)

    print
    print("MESHES:")
    for index, mesh in enumerate(scene.meshes):
        print("  MESH" + str(index + 1))
        print("    material id:" + str(mesh.materialindex + 1))
        print("    vertices:" + str(len(mesh.vertices)))
        print("    first 3 verts:\n" + str(mesh.vertices[:3]))
        if mesh.normals.any():
            print("    first 3 normals:\n" + str(mesh.normals[:3]))
        else:
            print("    no normals")
        print("    colors:" + str(len(mesh.colors)))
        tcs = mesh.texturecoords
        if tcs:
            for index, tc in enumerate(tcs):
                print("    texture-coords " + str(index) + ":" +
                      str(len(tcs[index])) + "first3:" + str(tcs[index][:3]))

        else:
            print("    no texture coordinates")
        print("    uv-component-count:" + str(len(mesh.numuvcomponents)))
        print("    faces:" + str(len(mesh.faces)) + " -> first:\n" +
              str(mesh.faces[:3]))
        print("    bones:" + str(len(mesh.bones)) + " -> first:" +
              str([str(b) for b in mesh.bones[:3]]))
        print

    print("MATERIALS:")
    for index, material in enumerate(scene.materials):
        print("  MATERIAL (id:" + str(index + 1) + ")")
        for key, value in material.properties.items():
            print("    %s: %s" % (key, value))
    print

    print("TEXTURES:")
    for index, texture in enumerate(scene.textures):
        print("  TEXTURE" + str(index + 1))
        print("    width:" + str(texture.width))
        print("    height:" + str(texture.height))
        print("    hint:" + str(texture.achformathint))
        print("    data (size):" + str(len(texture.data)))

    # Finally release the model
    pyassimp.release(scene)
def main(filename=None):
    filename = filename or DEFAULT_MODEL
    
    print "Reading model", filename
    scene = pyassimp.load(filename)
    
    #the model we load
    print "MODEL:", filename
    print
    
    #write some statistics
    print "SCENE:"
    print "  meshes:", len(scene.meshes)
    print "  materials:", len(scene.materials)
    print "  textures:", len(scene.textures)
    print
    
    print "NODES:"
    recur_node(scene.rootnode)

    print
    print "MESHES:"
    for index, mesh in enumerate(scene.meshes):
        print "  MESH", index+1
        print "    material id:", mesh.materialindex+1
        print "    vertices:", len(mesh.vertices)
        print "    first 3 verts:", mesh.vertices[:3]
        if len(mesh.normals) > 0:
                print "    first 3 normals:", mesh.normals[:3]
        else:
                print "    no normals"
        print "    colors:", len(mesh.colors)
        tc = mesh.texturecoords
        if len(tc) >= 4:
            print "    texture-coords 1:", len(tc[0]), "first3:", tc[0][:3]
            print "    texture-coords 2:", len(tc[1]), "first3:", tc[1][:3]
            print "    texture-coords 3:", len(tc[2]), "first3:", tc[2][:3]
            print "    texture-coords 4:", len(tc[3]), "first3:", tc[3][:3]
        elif len(tc) == 0:
            print "    no texture coordinates"
        else:
            print "    tc is an unexpected number of elements (expect 4, got", len(tc), ")"
        print "    uv-component-count:", len(mesh.numuvcomponents)
        print "    faces:", len(mesh.faces), "first:", [f for f in mesh.faces[:3]]
        print "    bones:", len(mesh.bones), "first:", [str(b) for b in mesh.bones[:3]]
        print

    print "MATERIALS:"
    for index, material in enumerate(scene.materials):
        print("  MATERIAL (id:" + str(index+1) + ")")
        for key, value in material.properties.items():
            print "    %s: %s" % (key, value)
    print
    
    print "TEXTURES:"
    for index, texture in enumerate(scene.textures):
        print "  TEXTURE", index+1
        print "    width:", texture.width
        print "    height:", texture.height
        print "    hint:", texture.achformathint
        print "    data (size):", len(texture.data)
   
    # Finally release the model
    pyassimp.release(scene)

    print "Finished parsing the model."
Exemple #8
0
def main(filename=None):
    filename = filename or DEFAULT_MODEL

    print "Reading model", filename
    scene = pyassimp.load(filename)

    #the model we load
    print "MODEL:", filename
    print

    #write some statistics
    print "SCENE:"
    print "  meshes:", len(scene.meshes)
    print "  materials:", len(scene.materials)
    print "  textures:", len(scene.textures)
    print

    print "NODES:"
    recur_node(scene.rootnode)

    print
    print "MESHES:"
    for index, mesh in enumerate(scene.meshes):
        print "  MESH", index + 1
        print "    material id:", mesh.materialindex + 1
        print "    vertices:", len(mesh.vertices)
        print "    first 3 verts:", mesh.vertices[:3]
        if len(mesh.normals) > 0:
            print "    first 3 normals:", mesh.normals[:3]
        else:
            print "    no normals"
        print "    colors:", len(mesh.colors)
        tc = mesh.texturecoords
        if len(tc) >= 4:
            print "    texture-coords 1:", len(tc[0]), "first3:", tc[0][:3]
            print "    texture-coords 2:", len(tc[1]), "first3:", tc[1][:3]
            print "    texture-coords 3:", len(tc[2]), "first3:", tc[2][:3]
            print "    texture-coords 4:", len(tc[3]), "first3:", tc[3][:3]
        elif len(tc) == 0:
            print "    no texture coordinates"
        else:
            print "    tc is an unexpected number of elements (expect 4, got", len(
                tc), ")"
        print "    uv-component-count:", len(mesh.numuvcomponents)
        print "    faces:", len(
            mesh.faces), "first:", [f for f in mesh.faces[:3]]
        print "    bones:", len(
            mesh.bones), "first:", [str(b) for b in mesh.bones[:3]]
        print

    print "MATERIALS:"
    for index, material in enumerate(scene.materials):
        print("  MATERIAL (id:" + str(index + 1) + ")")
        for key, value in material.properties.items():
            print "    %s: %s" % (key, value)
    print

    print "TEXTURES:"
    for index, texture in enumerate(scene.textures):
        print "  TEXTURE", index + 1
        print "    width:", texture.width
        print "    height:", texture.height
        print "    hint:", texture.achformathint
        print "    data (size):", len(texture.data)

    # Finally release the model
    pyassimp.release(scene)

    print "Finished parsing the model."