k3d.factory.volume(volume, color_map=None, opacity_function=None, color_range=[], samples=512.0, alpha_coef=50.0, gradient_step=0.005, shadow='off', interpolation=True, shadow_delay=500, shadow_res=128, focal_length=0.0, focal_plane=100.0, ray_samples_count=16, mask=[], mask_opacities=[], name=None, group=None, custom_data=None, compression_level=0, **kwargs)[source]#

Create a Volume drawable for 3D volumetric data.

By default, the volume are a grid inscribed in the -0.5 < x, y, z < 0.5 cube regardless of the passed voxel array shape ,like aspect ratio.

Different grid size, shape and rotation can be obtained using kwargs

  • volume(..., bounds=[0, 300, 0, 400, 0, 500])

  • volume(..., scaling=[scale_x, scale_y, scale_z])

  • volume (ndarray) – 3D array of float.

  • color_map (list, optional) – List of float quadruplets (attribute value, R, G, B) sorted by attribute value, by default None. The first quadruplet should have value 0.0, the last 1.0; R, G, B are RGB color components in the range 0.0 to 1.0.

  • opacity_function (list, optional) – float tuples (attribute value, opacity) sorted by attribute value, by default []. The first tuples should have value 0.0, the last 1.0; opacity is in the range 0.0 to 1.0.

  • color_range (list, optional) – [min_value, max_value] pair determining the levels of color attribute mapped to 0 and 1 in the colormap, by default [].

  • samples (float, optional) – Number of iteration per 1 unit of space, by default 512.0.

  • alpha_coef (float, optional) – Alpha multiplier, by default 50.0.

  • gradient_step (float, optional) – Gradient light step, by default 0.005.

  • shadow ({'off', 'on_demand', 'dynamic'}, optional) – Type of shadow on volume, by default “off”.

  • interpolation (bool, optional) – Interpolate volume raycasting data, by default True.

  • shadow_delay (int, optional) – Minimum number of miliseconds between shadow map updates, by default 500.

  • shadow_res (int, optional) – Resolution of shadow map, by default 128.

  • focal_length (float, optional) – Focal length of depth of field renderer, by default 0.0.

  • focal_plane (float, optional) – Focal plane of depth of field renderer, by default 100.0.

  • ray_samples_count (int, optional) – Number of rays for depth of field rendering, by default 16.

  • mask (array_like.) – 3D array of int in range (0, 255).

  • mask_opacities (array_like.) – List of opacity values for mask.

  • name (str, optional) – Object name, by default None.

  • group (str, optional) – Name of a group, by default None.

  • custom_data (dict) – A object with custom data attached to object.

  • compression_level (int, optional) – Level of data compression [-1, 9], by default 0.

  • **kwargs – For other keyword-only arguments, see process_transform_arguments.


Volume Drawable.

Return type


See also


Render mhd volumetric data#

heart.mhd heart.zraw

import k3d
import numpy as np
import SimpleITK as sitk

im_sitk = sitk.ReadImage('heart.mhd')
img = sitk.GetArrayFromImage(im_sitk)

plt_volume = k3d.volume(img.astype(np.float32))

plot = k3d.plot()
plot += plt_volume