# Modifying existing geometry data¶

If you want to load a model and operate on its vertices, you can walk through the vertices as shown in the previous section, but you should substitute modifyGeom(), modifyVertexData(), and modifyPrimitive() for getGeom(), getVertexData(), and getPrimitive(), respectively. These calls ensure that, in case the data happens to be shared between multiple different GeomNodes, you will get your own unique copy to modify, without inadvertently affecting other nodes.

If you want to modify the vertex data, you have two choices. The simplest option is to create a new GeomVertexData and fill it up with your new vertex data (as described in Creating and filling a GeomVertexData), and then assigning this data to the geom with the call geom.setVertexData(). You must ensure that you add enough vertices to the new GeomVertexData to satisfy the GeomPrimitives that reference it.

Your second choice is to modify the vertex data in-place, by operating on the existing vertices. You can do this with a GeomVertexWriter. For instance, if you want to copy the (X, Y) position of each vertex to its (U, V) texture coordinate, you could do something like this:

GeomVertexWriter texcoord(vdata, "texcoord");

while (!vertex.is_at_end()) {
LVector3 v = vertex.get_data3();
texcoord.set_data2(v[0], v[1]);
}


Important

Important! When you are simultaneously reading from and writing to the same GeomVertexData object, you should create all of the GeomVertexWriters you need before you create any GeomVertexReader. This is because of Panda’s internal reference-counting mechanism; creating a GeomVertexWriter may automatically (and transparently) force a copy of the data in the GeomVertexData, which could invalidate any GeomVertexReaders you have already created.

Writing to a column with a GeomVertexWriter does require that the GeomVertexData’s format already has the appropriate columns to handle the data you are writing (in the above example, for instance, the format must already have a ‘texcoord’ column, or the above code will fail). Furthermore, the columns must have the appropriate format. For instance, if you wanted to upgrade a model’s texture coordinates from 2-D texture coordinates to 3-D texture coordinates, simply calling texcoord.setData3(u, v, w) wouldn’t change the fact that the existing texcoord column is a 2-component format; you would just be trying to stuff a 3-component value into a 2-component column.

If you want to add a new column to a GeomVertexData, or modify the format of an existing column, you will have to create a new GeomVertexFormat that includes the new column (see Defining your own GeomVertexFormat), and then change the format on the GeomVertexData via vdata.setFormat(format). This call will internally adjust all of the data to match the new format. (Because of this internal adjustment, it is important to do this before you create the first GeomVertexWriter or GeomVertexReader.)
{"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.1683368682861328, "perplexity": 1210.7752990721965}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "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-24/segments/1590347413624.48/warc/CC-MAIN-20200531182830-20200531212830-00389.warc.gz"}