Tutorial - How-To import Virtual Lego Models in Panda3d (LDraw to Gltf)

I thought with my recent discoveries I would go ahead and write a tutorial for how to get lego models into Panda3d.



  1. LeoCad models are natively stored in the LDraw file format, so make a model in leoCad and simply save the file with your preferred name. It should have an .ldr file extension. Note, if you are using another lego cad system, export to ldraw file format.

  2. Make sure the blender add-on for importing LDraw is installed. Do this by going to Edit → Preferences → Add-ons → Install and selecting the zip file downloaded from github.

  3. Import the ldraw file in blender with File → Import → LDraw. Important when you import into blender you MUST tell the importer where your Ldraw library files are. Simply copy/paste the full path to the downloaded Ldraw Library root directory. (I personally downscale the fidelity of the geometry during this step. I like the look and performance of low poly. I also group studs as their own objects, this allows me to remove unseen studs very easily. These settings are in the importer).

  4. Switch to the scripting tab at the top of blender. Paste this code into the console and hit enter. What this does is it takes the ShaderNodeGroup that has a nested Principled BSDF shader in it and grabs its color. It this deletes the node group and replaces it with a flattened Shader node and connects it to the Material Output node. This is required for the gltf export to be in the right format for the panda3d gltf importer.

import bpy

# Run through all materials of the current blend file
for mat in bpy.data.materials:
    if mat.node_tree:
            output = mat.node_tree.nodes["Material Output"]
            group = mat.node_tree.nodes["Group"]
            group2 = mat.node_tree.nodes["Group.001"]
            r,g,b,a = group.inputs['Color'].default_value
            bsdf = mat.node_tree.nodes.new(type="ShaderNodeBsdfPrincipled")
            bsdf.inputs["Base Color"].default_value = (r,g,b,a)
            mat.node_tree.links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])

  1. Make sure your model is oriented on the +y axis. This will mean its “facing forward” in panda!
  2. Select object you want to export and export to GLTF with File → Export → GLTF 2.0 (you can do either glb/gltf, doesnt matter).
  3. Make sure the gltf tools are installed for panda.
python -m pip install -U panda3d-gltf

  1. Import the gltf as usual with the loader and put it in the scene graph.
# Your import code may look different depending on where you need it.
self.model = base.loader.loadModel("models/myLdrExportedTo.glb")
  1. Profit!

Model in LeoCad:

Model in Blender:

Model in Panda!

Let me know if you need anymore assistance and happy hacking.