Ivan Viti
Ivan Viti

Reputation: 21

Loading a .gltf file in panda3d with python, but colors aren't showing up like in blender

I have a .gltf file which I am trying to view using panda3d. I am able to load and see the mesh but not the colors associated with it. If I load it into blender, I can see both the mesh and the associated colors if I set up the color attribute and link it to base color.

this is what I have so far:

from panda3d.core import AmbientLight, DirectionalLight, Vec3, Vec4
from direct.showbase.ShowBase import ShowBase
import simplepbr

class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        simplepbr.init()

        self.environ = self.loader.loadModel("models/output_file.gltf")
        self.environ.reparentTo(self.render)
        self.environ.setPos(0, 0, 0)

        # Create a new directional light and set its direction
        dlight = DirectionalLight('dlight')
        dlight.setColor(Vec4(1, 1, 1, 1))
        dlight.setDirection(Vec3(-1, -1, -1))

        # Create a NodePath for the light, attach it to the render graph,
        # and call set_light to cause it to cast light on everything in the scene
        dlnp = self.render.attach_new_node(dlight)
        self.render.set_light(dlnp)

        # Add an ambient light
        alight = AmbientLight('alight')
        alight.setColor(Vec4(0.2, 0.2, 0.2, 1))
        alnp = self.render.attach_new_node(alight)
        self.render.set_light(alnp)

app = MyApp()
app.run()

these are the files (gltf and bin) : https://drive.google.com/file/d/1037G2ytddx3a_gAuG96g0AqR57dXVRgc/view?usp=sharing https://drive.google.com/file/d/13zYEAymYSSbnPSkLQQzEm_phqHh3AAeu/view?usp=sharing

I also tried showing the model directly without simplebpr, but I get the same thing. I have also tried all sorts of different conversions (egg,obj,ply,etc...) but they all just give me the grey model.

Upvotes: 0

Views: 355

Answers (1)

Ivan Viti
Ivan Viti

Reputation: 21

I solved the problem by myself by going directly from trimesh to egg:

def create_egg(self, prompt):
    latents = sample_latents(
        batch_size=self.batch_size,
        model=self.model,
        diffusion=self.diffusion,
        guidance_scale=self.guidance_scale,
        model_kwargs=dict(texts=[prompt] * self.batch_size),
        progress=True,
        clip_denoised=True,
        use_fp16=self.use_fp16,
        use_karras=self.use_karras,
        karras_steps=self.karras_steps,
        sigma_min=self.sigma_min,
        sigma_max=self.sigma_max,
        s_churn=self.s_churn,
    )

    trimesh_object = []
    for i, latent in enumerate(latents):
        t = decode_latent_mesh(self.xm, latent).tri_mesh()
        trimesh_object = t

    egg_data = EggData()

    vertex_pool = EggVertexPool('vertex_pool')
    egg_data.addChild(vertex_pool)

    group = EggGroup('group')
    egg_data.addChild(group)

    verts = trimesh_object.verts  # replace with your vertices
    faces = trimesh_object.faces  # replace with your faces

    for face in faces:
        egg_poly = EggPolygon()

        for i, vertex_index in enumerate(face):
            vertex = EggVertex()
            vertex_pos = verts[vertex_index]
            vertex.set_pos(LPoint3d(*vertex_pos))

            # If the trimesh object has color data per vertex
            if trimesh_object.has_vertex_colors():
                # Accessing the color data
                r = trimesh_object.vertex_channels['R'][vertex_index]
                g = trimesh_object.vertex_channels['G'][vertex_index]
                b = trimesh_object.vertex_channels['B'][vertex_index]
                # Setting the vertex color
                vertex.set_color(LColor(r, g, b, 1))  # Assuming RGB values are already in the 0-1 range

            vertex = vertex_pool.addVertex(vertex)
            egg_poly.addVertex(vertex)

        group.addChild(egg_poly)

    output_filename = f'models/{prompt}.egg'
    egg_data.writeEgg(Filename(output_filename))

    return output_filename

Upvotes: 0

Related Questions