This tutorial shows how to write a Python script. Upon executing the script, a new mesh object is added to the current scene. The mesh follows a sine wave curve.
sine-wave.py
.
Sine Wave
As shown in the 3D view, the Python script creates a mesh in the shape of a sine wave. The script:
import math import Blender from Blender import NMesh x = -1 * math.pi mesh = NMesh.GetRaw() vNew = NMesh.Vert( x, math.sin( x ), 0 ) mesh.verts.append( vNew ) while x < math.pi: x += 0.1 vOld = vNew vNew = NMesh.Vert( x, math.sin( x ), 0 ) mesh.verts.append( vNew ) mesh.addEdge( vOld, vNew ) NMesh.PutRaw( mesh, "SineWave", 1 ) Blender.Redraw()
Here is what the lines mean:
import math
import blender
from Blender import NMesh
Indicates that the math and blender packages will be used. These packages provide the functionality of Python's Math library and Blender's Python library. The last line permits using the terseNMesh
instead of the verboseBlender.NMesh
.x = -1 * math.pi
Although Blender uses three dimensions, a sine wave typically has a two dimensional representation. This line sets the lower limit for the sine wave's starting value. In this case, the sine wave will be modelled from negative pi to positive pi.mesh = NMesh.GetRaw()
Create a new mesh object. This object begins with no vertices; the remainder of the code calculates the position for those vertices and then assigns them to the mesh.vNew = NMesh.Vert( x, math.sin( x ), 0 )
Before entering the loop, the first vertex is created. Since the vertices must be connected to form an edge, the script tracks two vertices: (1) the vertex that was just added; and (2) the vertex that will be added next.mesh.verts.append( vNew )
Takes the first vertex and makes it part of the mesh.while x < math.pi:
The loop to insert vertices into the mesh object begins here. The loop will continue until the value forx
exceeds pi.x += 0.1
The next vertex should be 0.1 units away from the current vertex. Ultimately, this controls the number of vertices that become part of the mesh. A smaller number means more vertices will be added because it would take longer forx
to count beyond the value of pi.vOld = vNew
Tracks the vertex that was previously included in the mesh. Eventually the old vertex and the new vertex will be used to create an edge.vNew = NMesh.Vert( x, math.sin( x ), 0 )
Create a new vertex based on the value ofx
. The value of y is the mathematical result ofsin( x )
. A sine wave generally does not fly off in the Z axis direction, so the Z component remains zero.mesh.verts.append( vNew )
Slam the newly created vertex onto the end of the mesh.mesh.addEdge( vOld, vNew )
Create an edge between the old vertex and the new vertex. The next time the loop occurs, the new vertex will become the old vertex.NMesh.PutRaw( mesh, "SineWave", 1 )
This is the magic line to insert the newly created mesh object into the current scene.Blender.Redraw()
Politely ask Blender to redraw the current scene. Without this line the mesh might not appear. Your milage may vary.
THE END - Download
Here are links related to Blender with Python: