[Python] Black screen instead of rendering 3D model

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have been trying to use kivy to render a 3D model for an interactive app in a website but I am yet to achieve this. At first it was crashing and I have done a few changes to solve those issues up to this point, namely:

    • Adding a missing default.glsl file which I got from github
    • Adding an "empty.png" file when it crashed because this file was absent
    • I changed a portion of the objloader.py from this:

    if not material.map:
    material.map = Image(folder + '/empty.png').texture
    material.texture_ratio = 0.0
    mesh = Mesh(geometry, material)
    return mesh


    to this:

    if not material.map:
    empty_path = os.path.join(folder, 'empty.png')
    if os.path.exists(empty_path):
    try:
    img = Image(empty_path)
    material.map = img.texture
    except Exception as e:
    Logger.warning(f"WaveObject: Could not load texture '{empty_path}': {e}")
    material.map = None
    else:
    Logger.warning(f"WaveObject: 'empty.png' not found at '{empty_path}'")
    material.map = None
    material.texture_ratio = 0.0
    mesh = Mesh(geometry, material)
    return mesh


    Now it no longer crashes but I get a black screen and only the mouse feature of right-clicking to place a red dot and left clicking to remove it works.

    The following is my main file (It has some debugging code from when I was trying to get to the bottom of the issue).

    import os
    os.environ['KIVY_IMAGE'] = 'pil'

    from kivy.core.image import Image
    from kivy.app import App
    from kivy.core.window import Window
    from kivy.uix.widget import Widget
    from kivy3 import Scene, Renderer
    from kivy3.loaders import OBJLoader
    from kivy3 import PerspectiveCamera

    from kivy.graphics.context_instructions import Scale

    class MainWidget(Widget):
    def __init__(self, **kwargs):
    super().__init__(**kwargs)

    # Create a scene and load the 3D model
    self.scene = Scene()
    loader = OBJLoader()
    obj = loader.load('C:/Users/chris/OneDrive/Documents/St.lucia_Cultural_Heritage_Project/assets/St_Lucia_Map_Final_2.obj')
    self.scene.add(obj)

    # Optional debugging: print model info
    print("OBJ model loaded:", obj)
    if hasattr(obj, 'pos'):
    print("Model position:", obj.pos)
    if hasattr(obj, 'scale'):
    print("Model scale:", obj.scale)
    if hasattr(obj, 'geometry'):
    print(f"Geometry has {len(obj.geometry.vertices)} vertices")

    obj.pos = (0,0,0)
    self.scene.add(obj)

    # Set up the camera
    self.camera = PerspectiveCamera(75, 1, 1, 1000)
    self.camera.pos = (0, 0, -10) # Adjust position as necessary
    self.camera.aspect = self.width/float(self.height) if self.height != 0 else 1

    print("Camera position:", self.camera.pos)
    print("Camera aspect ratio:", self.camera.aspect)
    # Set up the renderer
    self.renderer = Renderer()
    self.renderer.camera = self.camera
    self.renderer.scene = self.scene

    self.renderer.size = Window.size
    print("Renderer size:", self.renderer.size)

    self.add_widget(self.renderer)

    # Print renderer and scene info
    print("Renderer size:", self.renderer.size)
    print("Scene children count:", len(self.scene.objects) if hasattr(self.scene, 'objects') else 'n/a')

    # Ensure the renderer resizes with the window
    Window.bind(on_resize=self.on_window_resize)
    print("Initial setup complete")

    def on_window_resize(self, window, width, height):
    self.renderer.size = (width, height)
    if height != 0:
    self.camera.aspect = width / float(height)
    else:
    self.camera.aspect = 1
    print(f"Window resized: size=({width}, {height}), camera aspect= {self.camera.aspect}")


    class StLucia3DApp(App):
    def build(self):
    return MainWidget()

    if __name__ == '__main__':
    StLucia3DApp().run()`


    Any assistance would be greatly appreciated.

    I was expecting it to display a 3D model which I had made. I was hoping to then make it interactive where the user clicks on a spot of the map and it would zoom into that area and display information and other multimedia.

    I was hoping that I'd then be able to integrate it into a website made with Django.

    Continue reading...

Compartilhe esta Página