Create material palette
This script creates leather materials with different colors.
Note
Please put the textures in the same directory as the script.
1from p3dsdk import *
2import os
3
4
5def resource_path(filename: str) -> str:
6 """Get a path relative to the script path"""
7 return os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
8
9
10def import_texture(group: TextureGroup, filename: str, name: str) -> Texture:
11 with open(resource_path(filename), 'rb') as f: # Read the file as binary
12 texture = group.import_from_encoded_data(f.read())
13 texture.name = name
14 return texture
15
16
17with CurrentP3DSession(fallback_port=33900) as p3d:
18 # Create a texture group to put our textures in
19 texture_group = p3d.data.create_texture_group("Leather textures")
20 p3d.data.active_texture_group = texture_group # Optional, only to select the group in P3D
21
22 # Import textures
23 color_map = import_texture(texture_group, "Leather025_2K_Color.jpg", "Leather_Color")
24 normal_map = import_texture(texture_group, "Leather025_2K_Normal.jpg", "Leather_Normal")
25 roughness_map = import_texture(texture_group, "Leather025_2K_Roughness.jpg", "Leather_Roughness")
26
27 # Create a material group to put our materials in
28 material_group = p3d.data.create_material_group("Leathers")
29 p3d.data.active_material_group = material_group # Optional, only to select the group in P3D
30
31 leather_colors = [
32 ("Orange", (0.67, 0.25, 0.09)),
33 ("Black", (0.09, 0.09, 0.09)),
34 ("Blue", (0.00, 0.30, 0.39)),
35 ]
36
37 for color_name, color in leather_colors:
38 material = material_group.create_standard_material("Leather %s" % color_name)
39
40 # Diffuse properties
41 material.diffuse_color = color
42 material.diffuse_intensity = 0.7
43 material.diffuse_map = color_map
44 material.diffuse_map_width = 0.1 # 10 cm
45 material.diffuse_map_height = 0.1 # 10 cm
46
47 # Specular properties
48 material.reflection_intensity = 0.3
49 material.refraction_index = 1.55
50 material.roughness_map = roughness_map
51 material.roughness_map_width = 0.1 # 10 cm
52 material.roughness_map_height = 0.1 # 10 cm
53 material.roughness = 13
54
55 # Bump properties
56 material.bump_map = normal_map
57 material.bump_mode = BumpMode.NORMAL
58 material.bump_map_width = 0.1 # 10 cm
59 material.bump_map_height = 0.1 # 10 cm
60 material.bump_diffuse = True
61 material.bump_diffuse_depth = -1
62 material.bump_reflection = True
63 material.bump_reflection_depth = -1
Textures