Drawing objects#
This section explains how to render and interact with objects in HelloTriangle.
Which objects can be rendered?#
You can render both Mesh and Coords objects.
For meshes, all supported element types can be rendered (see Element types).
Objects are drawn using the draw() function:
draw(mymesh, name="mymesh", color="red")
draw(mycoords, name="mycoords", color="black")
Note
If you do not specify a name, a random one will be assigned automatically.
The name will appear in the Visualization Settings panel of the GUI.
Your script may contain multiple draw() statements — all corresponding objects will appear in the 3D viewer.
Choosing colors#
You can specify object colors in several ways:
Hexadecimal (string):
"#990000"Named colors (string):
"skyblue"(any of the 140 X11 color names)RGB values (string):
"rgb(255, 0, 0)"or"rgb(100%, 0%, 0%)"HSL values (string):
"hsl(0, 100%, 50%)"
Rendering scalar fields#
HelloTriangle meshes can contain fields (per-node or per-element) imported from files or added programmatically. These can be visualized directly in the 3D viewer.
Example: adding a nodal field and rendering it
rect = shapes.rectangle(div=1, eltype='quad4')
rect.add_field([0.0, 0.5, 1.0, 2.0], type="node", name="temp")
draw(rect, field='temp')
Field-based coloring is automatically applied when field is specified in
draw().
The types of fields apart from nodal (vertex) fields are:
Element fields (per-element/face data): Use
type="elem"when adding the field.Nodal element fields (per-element-per-node data): Use
type="elemn"when adding the field.
When rendering, HelloTriangle will automatically select the appropriate representation
Advanced styling with the style parameter#
For more control over mesh appearance, use the style parameter with a dictionary.
This allows you to set root-level properties that apply to all representations (faces, edges, points),
as well as representation-specific overrides.
Basic style dict example:
# Root-level properties apply to all representations
draw(mymesh, name="mesh", style={
"color": "#ff0000",
"opacity": 0.8
})
Representation-specific styling:
For 2D (surface) and 3D (volumetric) meshes, you can separately control faces, edges (lines), and vertices (points):
# Different colors for faces, edges, and points
draw(mymesh, name="mesh", style={
"faces": {
"color": "#00ff00",
"opacity": 0.9,
"metalness": 0.5,
"roughness": 0.5
},
"lines": {
"color": "#000000",
"width": 2.0,
"opacity": 1.0
},
"points": {
"color": "#ff0000",
"size": 3.0,
"opacity": 1.0
}
})
Available style properties:
Root level (applies to all representations):
color: Color string (any format above)opacity: Float from 0.0 (transparent) to 1.0 (opaque)
Faces (
facesdict):color: Face coloropacity: Face transparencymetalness: Float from 0.0 (non-metallic) to 1.0 (metallic)roughness: Float from 0.0 (smooth) to 1.0 (rough)visible: Boolean to show/hide facestransparencyMode: String, one of"default","simple", or"dithered"colorSource:"solid"or"field"
Lines/Edges (
linesdict):color: Line coloropacity: Line transparencywidth: Line width (float)visible: Boolean to show/hide edgestransparencyMode: String,"default"or"simple"colorSource:"solid"or"field"
Points/Vertices (
pointsdict):color: Point coloropacity: Point transparencysize: Point size (float)visible: Boolean to show/hide pointstransparencyMode: String,"default"or"simple"colorSource:"solid"or"field"
Note
Transparency modes:
"default": Standard transparency with depth writing (best for faces)"simple": Disables depth writing when transparent (best for edges/points)"dithered": Alpha hashing for order-independent transparency (faces only)
Combining root and representation styles:
Root-level properties serve as defaults. Representation-specific values override them:
# Root color applies to all, but faces get a different opacity
draw(mymesh, name="mesh", style={
"color": "#2BB786", # Applied to faces, lines, and points
"faces": {
"opacity": 0.7 # Only faces are semi-transparent
}
})
GUI options#
After running a script with one or more draw() calls, all rendered objects will appear in the Visualization Settings panel on the right-hand side of the screen.
From this panel you can:
Interactively change object colors
Adjust transparency
For 2D (surface) and 3D (volumetric) meshes, separately control the appearance of faces, edges, and vertices
Copy the current style as Python code to paste into your script
Note
When you re-run a script, any manual changes made in the visualization settings panel will be reset.
To preserve your styling, copy the style from the visualization panel and add it to your script using the style parameter.
Scene-level settings#
Use set_background() and set_camera() to configure scene-level properties.
These settings apply to the entire 3D viewer, not individual objects.
# Set background color
set_background("#f0f0f0")
# Set camera type and position
set_camera({"type": "orthographic"})
set_camera({"type": "perspective", "position": [10, 5, 20], "target": [0, 0, 0]})
# Draw your meshes
draw(mymesh, name="mesh", color="red")
set_background(color)
Sets the scene background color. Accepts any CSS color value:
set_background("white")
set_background("#1a1a2e")
set_background("lightblue")
set_camera(config)
Sets the camera type and optional position. Pass a dictionary with any of these keys:
"type":"perspective"(default) or"orthographic""position":[x, y, z]— world-space camera position"target":[x, y, z]— look-at point (default[0, 0, 0])"up":[x, y, z]— up vector (default[0, 1, 0])"fov": field of view in degrees (perspective only)"zoom": zoom multiplier (orthographic only)
set_camera({"type": "perspective", "fov": 60})
set_camera({"type": "orthographic", "zoom": 1.5})
Note
set_background() and set_camera() are typically placed at the beginning of a script.
Settings defined in code take precedence over any manual adjustments made in the visualization panel.
When a setting is removed from the script on rerun, any previously saved manual adjustment for that
setting is restored.
The older set_scene() function (e.g. set_scene(background="#f0f0f0", camera={"type": "orthographic"}))
is still supported for backwards compatibility, but the dedicated functions are preferred.