جامعة الفارابي االهلية
قسم هندسة الحاسوب
Designing a Simple 3D Virtual World Using Python:
First stage
اسم المشرف .ذو الفقار حازم
This report is done by
عمر ماجد عباس
دنيا ضياء صربي
رسل حليم مالك
حسني حممد عباس
Introduction
Creating 3D models with code can seem daunting to those without a
programming background.
However, Python makes the process incredibly approachable, even for
beginners. With just a few key libraries, you can start scripting detailed
3D models in no time.
This guide will walk you through everything you need to know, from
choosing the right Python 3D modeling tools to step-by-step tutorials for
building your first models. You'll be rendering intricate 3D graphics
before you know it!
Introduction to Python for 3D Modeling
Python is an incredibly versatile programming language that can be used
for 3D modeling and computer-aided design (CAD) applications. With
Python's extensive libraries and tools for 3D graphics and visualization, it
provides a flexible and accessible way to create and manipulate 3D
models.
Some of the key benefits of using Python for 3D modeling include:
❖ Automation - Python makes it easy to automate repetitive
modeling tasks, allowing you to work more efficiently. Common
uses include batch processing models, parametric design, and
creating customized workflows.
❖ Rapid Prototyping - Python is great for quickly testing modeling
concepts before investing time in more complex CAD software.
Models can be visualized and shared easily.
Designing a Simple 3D Virtual World Using Python: Page 2
❖ Customization - Python gives you fine-grained control to
customize modeling behaviors and build domain-specific tools
tailored to your needs.
❖ Interoperability - Python can read and write many standard 3D
file formats like STL, STEP, and IGES for integration across
modeling pipelines.
Some popular Python libraries used for 3D modeling and CAD work
include:
❖ CadQuery - High-level API for creating parametric CAD models.
Great for programmers.
❖ FreeCAD - General purpose CAD package with Python scripting
interface. More user-friendly.
❖ MeshPy - Tools for triangle mesh generation and 3D tetrahedral
meshing.
❖ PyVista - 3D plotting and mesh analysis via VTK.
Now let's explore key techniques for applying Python to 3D modeling
workflows.
Exploring the Best Python 3D Library for Your Needs
When selecting a Python 3D modeling library, first consider your
application area and specific use cases. Key criteria include:
❖ Ease of Use - Is a simple, clean API available or is deeper
programming knowledge expected?
❖ Feature Set - Evaluate available modeling operations, file formats,
and visualization tools.
Designing a Simple 3D Virtual World Using Python: Page 3
❖ Performance - Speed and scalability needs depend on model
complexity.
❖ Interoperability - Assess integration options with external CAD
software if needed.
For example, CadQuery provides a high-level, easy-to-use API tailored
for programmers to build parametric CAD models using Python scripting.
It uses the OpenCASCADE geometry kernel and focuses specifically on
CAD workflows.
In contrast, FreeCAD offers a more expansive general purpose CAD
feature set through its Python API, including mesh operations,
simulations, and documentation tools beyond just modeling. But it has a
steeper learning curve.
So CadQuery may be preferable for rapid scripted CAD, while FreeCAD
is better for advanced applications needing broader CAD capabilities.
Automating 3D Modeling Tasks with Python
Python is extremely useful for automating repetitive and time-consuming
modeling steps. Common examples include:
❖ Batch Processing Models - Automatically translate, rotate, scale
or otherwise modify models in bulk.
❖ Parametric CAD - Programmatically construct models based on
key parameters and relationships for quick design iterations.
❖ Custom Workflows - Script pipeline tools connecting modeling,
analysis, and optimization tasks.
Designing a Simple 3D Virtual World Using Python: Page 4
For instance, you could use Python to batch export multiple STEP files as
STLs while standardizing materials and optimizing meshes for 3D
printing. Or develop parametric CAD systems where models update on-
the-fly based on logic relationships between dimensions and constraints.
Python's flexibility makes it straightforward to eliminate tedious manual
operations in your modeling pipeline.
Running Python Code for 3D Modeling
To start scripting 3D models with Python, first install a Python
distribution like Anaconda and your CAD library of choice. Many
libraries have getting started tutorials, like CadQuery or FreeCAD.
Most Python CAD scripts follow this general workflow:
1. Import the library - import cadquery, import FreeCAD, etc.
2. Build modeling workspace - Instantiate document, create base
shapes.
3. Execute modeling operations - Extrude, fillet, add holes via
library functions.
4. Export model - Write model to file format like STEP, STL, etc.
For example, here is some CadQuery code to model a simple I-beam:
Designing a Simple 3D Virtual World Using Python: Page 5
Review library documentation to explore available modeling
functionality. Execute scripts in IDEs like Jupyter notebooks or directly
from the command line.
Viewing 3D Models with Python 3D Model Viewer Tools
When developing 3D models, it's crucial to visually inspect the output
geometry. Some Python options for model viewing include:
❖ CadQuery & FreeCAD Viewers - Built-in graphical views.
❖ MeshPy, Trimesh, PyVista - Mesh analysis and point cloud
visualization.
❖ Matplotlib, Plotly - General purpose Python plotting for basic
3D.
❖ IPython Interactive Widgets - Enable 3D manipulation in
Jupyter notebooks.
For example, to view an STL model in CadQuery:
Designing a Simple 3D Virtual World Using Python: Page 6
The viewer supports pan, zoom, rotate, with configurable display
properties. Additional controls are available in premium versions.
Integrated Python model viewing makes it easy to visually debug
geometry scripting workflows.
Python is an extremely versatile programming language that can be used
for 3D modeling and rendering in a variety of ways. Here are some of the
most popular options:
Using Python 3D Modeling Libraries
There are several Python libraries available specifically for generating
and manipulating 3D models, such as:
❖ PyMesh - Focuses on mesh generation and processing. Useful for
tasks like cleaning up 3D scans.
❖ Open3D - Provides data structures and algorithms for 3D data
processing. Supports 3D model reconstruction.
❖ Trimesh - Designed for loading and storing triangular meshes.
Simplifies working with STL files and point clouds.
These libraries have functions to help construct 3D models
programmatically using Python code. For example, you can script
complex parametric models, generate 3D meshes from scratch, or process
scanned 3D data.
Designing a Simple 3D Virtual World Using Python: Page 7
Integrating Python with 3D Modeling Software
Many 3D modeling and CAD packages have Python APIs or plugins
available, allowing Python to automate and extend their functionality:
❖ FreeCAD - Open source CAD software that can be fully scripted
in Python. The Python API allows creating custom tools, macros,
and workflows.
❖ Blender - Includes a Python API for writing scripts and addons
that create models, set up scenes, add materials and textures, etc.
❖ CADquery - Module for generating parametric CAD models.
Integrates well with FreeCAD.
This makes Python a very capable tool for advanced modeling, rendering,
and automation tasks.
Viewing and Converting 3D Models
There are Python libraries focused on loading, viewing, and converting
various 3D file formats:
❖ Trimesh can render a variety of 3D model files in an OpenGL
window for quick visualization
❖ numpy-stl provides utilities for working with STL files
❖ PyVista can create plots and renderings from VTK datasets
In summary, Python has rich capabilities when it comes to working with
3D data, whether it's for procedural modeling, integrating with existing
software, automation, or just loading and converting file types. The
multitude of specialized Python libraries make practically any 3D task
possible.
Designing a Simple 3D Virtual World Using Python: Page 8
References
1. McReynolds, T., & Blythe, D. (2005). Advanced Graphics
Programming Using OpenGL. Elsevier.
2. Shreiner, D., Sellers, G., Kessenich, J., & Licea-Kane, B. (2013).
OpenGL Programming Guide: The Official Guide to Learning
OpenGL, Version 4.3. Addison-Wesley.
3. Phillips, D., & Dwyer, J. (2019). Python Programming for
Beginners: An Introduction to the Python Language. Independently
Published.
4. Van Rossum, G., & Drake, F. L. (2011). The Python Language
Reference Manual. Network Theory Ltd.
5. Lavelle, J. (2015). Learning Python Design Patterns. Packt
Publishing.
Designing a Simple 3D Virtual World Using Python: Page 9