Index · Object → RefCounted → MeshDataTool

MeshDataTool

Helper tool to access and edit Mesh data.

Class Description

MeshDataTool provides access to individual vertices in a Mesh. It allows users to read and edit vertex data of meshes. It also creates an array of faces and edges.

To use MeshDataTool, load a mesh with create_from_surface(). When you are finished editing the data commit the data to a mesh with commit_to_surface().

Below is an example of how MeshDataTool may be used.

var mesh = ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, BoxMesh.new().get_mesh_arrays())
var mdt = MeshDataTool.new()
mdt.create_from_surface(mesh, 0)
for i in range(mdt.get_vertex_count()):
	var vertex = mdt.get_vertex(i)
	# In this example we extend the mesh by one unit, which results in separated faces as it is flat shaded.
	vertex += mdt.get_vertex_normal(i)
	# Save your change.
	mdt.set_vertex(i, vertex)
mesh.clear_surfaces()
mdt.commit_to_surface(mesh)
var mi = MeshInstance.new()
mi.mesh = mesh
add_child(mi)

See also ArrayMesh, ImmediateMesh and SurfaceTool for procedural geometry generation.

Note: Godot uses clockwise winding order for front faces of triangle primitive modes.

Methods

func clear() -> void

Clears all data currently in MeshDataTool.

func commit_to_surface(mesh: ArrayMesh, compression_flags: int = 0) -> Error

Adds a new surface to specified Mesh with edited data.

func create_from_surface(mesh: ArrayMesh, surface: int) -> Error

Uses specified surface of given Mesh to populate data for MeshDataTool.

Requires Mesh with primitive type Mesh.PRIMITIVE_TRIANGLES.

func get_edge_count() -> int

Returns the number of edges in this Mesh.

func get_edge_faces(idx: int) -> PackedInt32Array

Returns array of faces that touch given edge.

func get_edge_meta(idx: int) -> Variant

Returns meta information assigned to given edge.

func get_edge_vertex(idx: int, vertex: int) -> int

Returns the index of the specified vertex connected to the edge at index idx.

vertex can only be 0 or 1, as edges are composed of two vertices.

func get_face_count() -> int

Returns the number of faces in this Mesh.

func get_face_edge(idx: int, edge: int) -> int

Returns the edge associated with the face at index idx.

edge argument must be either 0, 1, or 2 because a face only has three edges.

func get_face_meta(idx: int) -> Variant

Returns the metadata associated with the given face.

func get_face_normal(idx: int) -> Vector3

Calculates and returns the face normal of the given face.

func get_face_vertex(idx: int, vertex: int) -> int

Returns the specified vertex index of the given face.

vertex must be either 0, 1, or 2 because faces contain three vertices.

var index = mesh_data_tool.get_face_vertex(0, 1) # Gets the index of the second vertex of the first face.
var position = mesh_data_tool.get_vertex(index)
var normal = mesh_data_tool.get_vertex_normal(index)
func get_format() -> int

Returns the Mesh's format as a combination of the Mesh.ArrayFormat flags. For example, a mesh containing both vertices and normals would return a format of 3 because Mesh.ARRAY_FORMAT_VERTEX is 1 and Mesh.ARRAY_FORMAT_NORMAL is 2.

func get_material() -> Material

Returns the material assigned to the Mesh.

func get_vertex(idx: int) -> Vector3

Returns the position of the given vertex.

func get_vertex_bones(idx: int) -> PackedInt32Array

Returns the bones of the given vertex.

func get_vertex_color(idx: int) -> Color

Returns the color of the given vertex.

func get_vertex_count() -> int

Returns the total number of vertices in Mesh.

func get_vertex_edges(idx: int) -> PackedInt32Array

Returns an array of edges that share the given vertex.

func get_vertex_faces(idx: int) -> PackedInt32Array

Returns an array of faces that share the given vertex.

func get_vertex_meta(idx: int) -> Variant

Returns the metadata associated with the given vertex.

func get_vertex_normal(idx: int) -> Vector3

Returns the normal of the given vertex.

func get_vertex_tangent(idx: int) -> Plane

Returns the tangent of the given vertex.

func get_vertex_uv(idx: int) -> Vector2

Returns the UV of the given vertex.

func get_vertex_uv2(idx: int) -> Vector2

Returns the UV2 of the given vertex.

func get_vertex_weights(idx: int) -> PackedFloat32Array

Returns bone weights of the given vertex.

func set_edge_meta(idx: int, meta: Variant) -> void

Sets the metadata of the given edge.

func set_face_meta(idx: int, meta: Variant) -> void

Sets the metadata of the given face.

func set_material(material: Material) -> void

Sets the material to be used by newly-constructed Mesh.

func set_vertex(idx: int, vertex: Vector3) -> void

Sets the position of the given vertex.

func set_vertex_bones(idx: int, bones: PackedInt32Array) -> void

Sets the bones of the given vertex.

func set_vertex_color(idx: int, color: Color) -> void

Sets the color of the given vertex.

func set_vertex_meta(idx: int, meta: Variant) -> void

Sets the metadata associated with the given vertex.

func set_vertex_normal(idx: int, normal: Vector3) -> void

Sets the normal of the given vertex.

func set_vertex_tangent(idx: int, tangent: Plane) -> void

Sets the tangent of the given vertex.

Note: Even though tangent is a Plane, it does not directly represent the tangent plane. Its Plane.x, Plane.y, and Plane.z represent the tangent vector and Plane.d should be either -1 or 1. See also Mesh.ARRAY_TANGENT.

func set_vertex_uv(idx: int, uv: Vector2) -> void

Sets the UV of the given vertex.

func set_vertex_uv2(idx: int, uv2: Vector2) -> void

Sets the UV2 of the given vertex.

func set_vertex_weights(idx: int, weights: PackedFloat32Array) -> void

Sets the bone weights of the given vertex.

Inherited from RefCounted (4)
func init_ref() -> bool
func reference() -> bool
func unreference() -> bool
Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from Object (2)