pypbr.material.MaterialBase
- class pypbr.material.MaterialBase(albedo: Image | ndarray | FloatTensor | None = None, albedo_is_srgb: bool = True, normal: Image | ndarray | FloatTensor | None = None, roughness: Image | ndarray | FloatTensor | None = None, device: device = device(type='cpu'), **kwargs)[source]
Bases:
object
Base Class for PBR Materials.
This class provides common functionality for PBR materials, allowing for dynamic addition of texture maps and common operations such as resizing, cropping, and transforming texture maps.
- __init__(albedo: Image | ndarray | FloatTensor | None = None, albedo_is_srgb: bool = True, normal: Image | ndarray | FloatTensor | None = None, roughness: Image | ndarray | FloatTensor | None = None, device: device = device(type='cpu'), **kwargs)[source]
Initialize the Material with optional texture maps.
- Parameters:
albedo (Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]]) – The albedo map.
albedo_is_srgb (bool) – Flag indicating if albedo is in sRGB space. Defaults to True.
normal (Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]]) – The normal map.
roughness (Optional[Union[Image.Image, np.ndarray, torch.FloatTensor]]) – The roughness map.
device (torch.device) – The device to store the texture maps. Defaults to CPU.
**kwargs – Additional texture maps.
Methods
__init__
([albedo, albedo_is_srgb, normal, ...])Initialize the Material with optional texture maps.
adjust_normal_strength
(strength_factor)Adjust the strength of the normal map.
apply_transform
(transform)Apply a transformation to all texture maps.
compute_height_from_normal
([scale])Compute the height map from the normal map using Poisson reconstruction.
compute_normal_from_height
([scale])Compute the normal map from the height map.
crop
(top, left, height, width)Crop all texture maps to the specified region.
Flip all texture maps horizontally.
Flip all texture maps vertically.
Invert the Y component of the normal map.
resize
(size[, antialias])Resize all texture maps to the specified size.
roll
(shift)Roll all texture maps along the specified shift dimensions.
rotate
(angle[, expand, padding_mode])Rotate all texture maps by a given angle.
save_to_folder
(folder_path)Save the material maps to a folder.
tile
(num_tiles)Tile all texture maps by repeating them.
to
(device)Moves all tensors in the material to the specified device.
Convert the albedo map to linear space if it's in sRGB.
to_numpy
()Convert all texture maps to NumPy arrays.
to_pil
([maps_mode])Convert all texture maps to PIL Images.
to_srgb
()Convert the albedo map to sRGB space if it's in linear space.
Attributes
Get the albedo map in linear space.
Get the normal map in RGB space.
Get the size of the texture maps.
- adjust_normal_strength(strength_factor: float)[source]
Adjust the strength of the normal map.
- Parameters:
strength_factor (float) – The factor to adjust the strength of the normal map.
- Returns:
Returns self for method chaining.
- Return type:
- apply_transform(transform)[source]
Apply a transformation to all texture maps.
- Parameters:
transform – A function that takes a tensor and returns a tensor.
- Returns:
Returns self for method chaining.
- Return type:
- compute_height_from_normal(scale: float = 1.0)[source]
Compute the height map from the normal map using Poisson reconstruction.
- Parameters:
scale (float) – Scaling factor for the gradients.
- Returns:
Returns self for method chaining.
- Return type:
- compute_normal_from_height(scale: float = 1.0)[source]
Compute the normal map from the height map.
- Parameters:
scale (float) – The scaling factor for the height map gradients. Controls the strength of the normals.
- Returns:
Returns self for method chaining.
- Return type:
- crop(top: int, left: int, height: int, width: int)[source]
Crop all texture maps to the specified region.
- Parameters:
top – The top pixel coordinate.
left – The left pixel coordinate.
height – The height of the crop.
width – The width of the crop.
- Returns:
Returns self for method chaining.
- Return type:
- flip_horizontal()[source]
Flip all texture maps horizontally. When flipping normal maps, the X component is inverted.
- Returns:
Returns self for method chaining.
- Return type:
- flip_vertical()[source]
Flip all texture maps vertically. When flipping the normal map, the Y component is inverted.
- Returns:
Returns self for method chaining.
- Return type:
- invert_normal()[source]
Invert the Y component of the normal map.
- Returns:
Returns self for method chaining.
- Return type:
- property linear_albedo
Get the albedo map in linear space.
- Returns:
The albedo map in linear space.
- Return type:
torch.FloatTensor
- property normal_rgb
Get the normal map in RGB space.
- Returns:
The normal map in RGB space.
- Return type:
torch.FloatTensor
- resize(size: int | Tuple[int, int], antialias: bool = True)[source]
Resize all texture maps to the specified size.
- Parameters:
size – The desired output size.
antialias – Whether to apply antialiasing.
- Returns:
Returns self for method chaining.
- Return type:
- roll(shift: Tuple[int, int])[source]
Roll all texture maps along the specified shift dimensions.
- Parameters:
shift – The shift values for each dimension.
- Returns:
Returns self for method chaining.
- Return type:
- rotate(angle: float, expand: bool = False, padding_mode: str = 'constant')[source]
Rotate all texture maps by a given angle.
- Parameters:
angle (float) – The rotation angle in degrees.
expand (bool) – Whether to expand the output image to hold the entire rotated image.
padding_mode (str) – Padding mode. Options are ‘constant’ or ‘circular’.
- Returns:
Returns self for method chaining.
- Return type:
- save_to_folder(folder_path: str)[source]
Save the material maps to a folder.
- Parameters:
folder_path – The path to the folder where maps will be saved.
- property size: Tuple[int, int] | None
Get the size of the texture maps.
- Returns:
A tuple (height, width) representing the size of the texture maps. If multiple maps are present, returns the size of the first non-None map. Returns None if no maps are available.
- Return type:
Optional[Tuple[int, int]]
- tile(num_tiles: int)[source]
Tile all texture maps by repeating them.
- Parameters:
num_tiles – Number of times to tile the textures.
- Returns:
Returns self for method chaining.
- Return type:
- to(device: device)[source]
Moves all tensors in the material to the specified device.
- Parameters:
device (torch.device) – The target device.
- Returns:
Returns self for method chaining.
- Return type:
- to_linear()[source]
Convert the albedo map to linear space if it’s in sRGB.
- Returns:
Returns self for method chaining.
- Return type:
- to_numpy()[source]
Convert all texture maps to NumPy arrays.
- Returns:
A dictionary containing NumPy arrays of the texture maps.
- Return type:
dict
- to_pil(maps_mode: Dict[str, str] = None)[source]
Convert all texture maps to PIL Images.
- Parameters:
maps_mode (Dict[str, str]) – Optional dictionary specifying modes for each map type.
- Returns:
A dictionary containing PIL Images of the texture maps.
- Return type:
dict
- to_srgb()[source]
Convert the albedo map to sRGB space if it’s in linear space.
- Returns:
Returns self for method chaining.
- Return type: