Introduction
This tutorial will showcase a practical example of crafting a simple Python script using MeshLib SDK - an open-source 3D geometry library. With MeshLib SDK, you can perform advanced operations using C++ or Python.
Once the MeshLib installation is complete, we'll generate two basic objects. Then, execute a Boolean operation and save the outcome to disk.
Required installations
Python. MeshLib supports Python versions 3.8.0-3.12.0 on Windows and 3.8.0-3.11.0 on MacOS.
Code Editor. For example, Visual Studio Code, but any Python editor will suffice.
Example
Now, you can open your preferred code editor.
MeshLib conveniently installs via pip, and the following command accomplishes this:
py -3 -m pip install meshlib
This command downloads and installs the library. Once the download is complete, you're all set and ready to proceed.
Let's initiate our example by creating a new file in the code editor. For illustration, we'll use MeshInspector software to visualize some steps. First, we need to import the MeshLib library:
import meshlib.mrmeshpy as mr
Next, create a variable 'parameters' for a sphere with default values, setting the unit radius:
sparams = mr.SphereParams()
Now, generate a mesh sphere with the configured parameters:
smallSphere = mr.makeSphere(sparams)
Adjust the number of vertices in the sphere to 400:
sparams.numMeshVertices = 400
Set the sphere's radius to two:
sparams.radius = 2
Then, create a larger sphere using the same parameters:
bigSphere = mr.makeSphere(sparams)
Construct a variable for the shift vector that is initially set to zero:
transVector = mr.Vector3f()
Specify a Z-axis shift of two units:
transVector.z = 2
Build a transformation matrix with the provided vector:
diffXf = mr.AffineXf3f.translation(transVector)
Apply the transformation to the small sphere, shifting it along the Z-axis:
smallSphere.transform(diffXf)
Subtract the small sphere from the large after shifting:
diff = mr.boolean(bigSphere, smallSphere, mr.BooleanOperation.DifferenceAB)
Save the resulting mesh to a file, specifying the saving path, e.g.:
mr.saveMesh(diff.mesh, mr.Path("C:\\Users\\Administrator\\Documents\\Demo\\diffSpheres.stl"))
Now, you can open the resulting file using the 3D viewing and editing software MeshInspector in the web browser or download it for free.
โ