Switch materials on keypress¶
material-switch.py¶
1# © 2024 Autodesk, Inc. All rights reserved.
2
3print("Executing material switch script!")
4
5newScene()
6loadGeometry("$VRED_EXAMPLES/geo/teddy.osb")
7updateScene()
8
9nose = findNode("Nose")
10
11noseMat = nose.getMaterial()
12white = findMaterial("fur_white")
13brown = findMaterial("fur_brown")
14black = findMaterial("plastic_black_glossy")
15
16# define key 1 to switch to original material
17key1 = vrKey(Key_1)
18key1.connect("nose.setMaterial(noseMat)")
19print("Press 1 to set material to original material")
20
21# define key 2 to switch to white
22key2 = vrKey(Key_2)
23key2.connect("nose.setMaterial(white)")
24print("Press 2 to set material to white")
25
26# define key 3 to switch to brown
27key3 = vrKey(Key_3)
28key3.connect("nose.setMaterial(brown)")
29print("Press F3 to set material to brown")
30
31# define key 4 to switch to black
32key4 = vrKey(Key_4)
33key4.connect("nose.setMaterial(black)")
34print("Press 4 to set material to black")
35
36# define material list
37materials = []
38materials.append(noseMat)
39materials.append(white)
40materials.append(brown)
41materials.append(black)
42
43print(len(materials))
44
45index = 0
46
47# define function to switch to next/previous material
48# parameter s is the increment
49def setMaterials(s):
50 global index
51
52 index += s
53 if index < 0:
54 index = 0
55 if index >= len(materials):
56 index = len(materials) - 1
57 nose.setMaterial(materials[index])
58
59
60# define key + to switch to next material
61keyPlus = vrKey(Key_Plus)
62keyPlus.connect(setMaterials, 1)
63print("Press + to switch to next material")
64
65# define key - to switch to previous material
66keyMinus = vrKey(Key_Minus)
67keyMinus.connect(setMaterials, -1)
68print("Press - to switch to previous material")
69