# Writing 3D Models out to Disk¶

Panda has two native file formats for models.

Egg files (with the extension .egg) are written in an ASCII human readable format. The egg format is designed to be easy to read and modify if necessary, and easy to write a convert into from another third-party format. Also, the egg format is intended to be backward-compatible from all future versions of Panda3D, so if you have an egg file that Panda can load now, it should always be able to load that file. (Well, we can’t really make guarantees, but this is what we shoot for.) See Parsing and Generating Egg Files for more information about the egg format.

## BAM Files¶

Because of the way the egg syntax is designed, an egg file might be very large, sometimes many times larger than the file it was converted from. It can also sometimes take several seconds for Panda to load a large egg file.

Bam files (with the extension .bam), on the other hand, are binary files that are closely tied to a particular version of Panda3D. The bam format is designed to be as similar as possible to the actual Panda data structures, so that a bam file is relatively small and can be loaded very quickly. However, you should not consider the bam file format to be a good long-term storage format for your models, since a future version of Panda3D may not be able to load bam files from older versions.

You can always convert egg files to bam files using the program egg2bam. For many simple models, it is also possible to convert back again with the program bam2egg, but you should not rely on this, since it does not convert advanced features like animation; and some structure of the original egg file may be lost in the conversion.

You can load files of these formats, as well as any other supported format, using the loader.loadModel interface. Any file types other than .bam or .egg will be automatically converted at runtime, exactly as if you had run the appropriate command-line conversion tool first.

## The Bam Interface¶

The easiest way to save geometry is to use to call writeBamFile(filename) from the NodePath that contains your geometry.

myPanda=loader.loadModel("panda")

#do some fancy calculations on the normals, or texture coordinates that you dont
#want to do at runtime

#Save your new custom Panda
myPanda.writeBamFile("customPanda.bam")


## The Egg Interface¶

One easy way to create .egg file for geometry that has already been made is to create a .bam file and use bam2egg. However, you will often want to use the egg interface to create geometry in the first place; this is usually the easiest way to create geometry in Panda3D.

The complete documentation for using the egg interfaces has yet to be written, but the egg library is really quite simple to use. The basic idea is that you create an EggData, and an EggVertexPool to hold your vertices; and then you can create a series of EggVertex and EggPolygon objects. If you want to create some structure in your egg file, you can create one or more EggGroups to separate the polygons into different groups. Here is an example:

from panda3d.core import Point3D, deg2Rad, NodePath, Filename, CSZupRight
from panda3d.egg import EggPolygon, EggVertexPool, EggData, EggVertex, loadEggData, EggCoordinateSystem
import math

...

def makeWedge(angleDegrees = 360, numSteps = 16):

z_up = EggCoordinateSystem()
z_up.setValue(CSZupRight)

data = EggData()

vp = EggVertexPool('fan')

poly = EggPolygon()

v = EggVertex()
v.setPos(Point3D(0, 0, 0))

for i in range(numSteps + 1):
a = angleRadians * i / numSteps
y = math.sin(a)
x = math.cos(a)

v = EggVertex()
v.setPos(Point3D(x, 0, y))

return data
...

# Creating egg data
data = makeWedge()

# To write the egg file to disk, use this:
data.writeEgg(Filename("wedge.egg"))

# To load the egg file and render it immediately, use this:
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.23281961679458618, "perplexity": 2379.144526700717}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-50/segments/1606141723602.80/warc/CC-MAIN-20201203062440-20201203092440-00332.warc.gz"}