-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path02.random_scene.py
157 lines (129 loc) · 4.46 KB
/
02.random_scene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 02.random_scene.py
#
# This shows how to generate a randomized scene using a couple built-in mesh
# types and some randomized materials.
import nvisii
from random import *
import colorsys
opt = lambda: None
opt.nb_objs = 10000
opt.spp = 16
opt.width = 1920
opt.height = 1080
opt.out = '02_random_scene.png'
# nvisii uses sets of components to represent a scene.
# We can increase the max component limit here if necessary.
# In this case, we'll need 16 meshes, a material for each object,
# and finally a transform for each object as well as one more for the camera.
nvisii.initialize(
headless = True,
verbose = True,
lazy_updates = True,
max_entities = opt.nb_objs + 1,
max_transforms = opt.nb_objs + 1,
max_materials = opt.nb_objs,
max_meshes = 16
# these are also available
# max_lights, max_textures, & max_cameras
)
# Turn on the denoiser
nvisii.enable_denoiser()
# Create a camera
camera = nvisii.entity.create(
name = "camera",
transform = nvisii.transform.create("camera"),
camera = nvisii.camera.create(
name = "camera",
aspect = float(opt.width)/float(opt.height)
)
)
camera.get_transform().look_at(at = (0,0,0), up = (1,0,0), eye = (0,0,5))
nvisii.set_camera_entity(camera)
# Lets create a random scene.
# First lets pre-load some mesh components.
nvisii.mesh.create_sphere('m_0')
nvisii.mesh.create_torus_knot('m_1')
nvisii.mesh.create_teapotahedron('m_2')
nvisii.mesh.create_box('m_3')
nvisii.mesh.create_capped_cone('m_4')
nvisii.mesh.create_capped_cylinder('m_5')
nvisii.mesh.create_capsule('m_6')
nvisii.mesh.create_cylinder('m_7')
nvisii.mesh.create_disk('m_8')
nvisii.mesh.create_dodecahedron('m_9')
nvisii.mesh.create_icosahedron('m_10')
nvisii.mesh.create_icosphere('m_11')
nvisii.mesh.create_rounded_box('m_12')
nvisii.mesh.create_spring('m_13')
nvisii.mesh.create_torus('m_14')
nvisii.mesh.create_tube('m_15')
def add_random_obj(name = "name"):
# this function adds a random object that uses one of the pre-loaded mesh
# components, assigning a random pose and random material to that object.
obj = nvisii.entity.create(
name = name,
transform = nvisii.transform.create(name),
material = nvisii.material.create(name)
)
mesh_id = randint(0,15)
# set the mesh. (Note that meshes can be shared, saving memory)
mesh = nvisii.mesh.get(f'm_{mesh_id}')
obj.set_mesh(mesh)
obj.get_transform().set_position((
uniform(-5,5),
uniform(-5,5),
uniform(-1,3)
))
obj.get_transform().set_rotation((
uniform(0,1), # X
uniform(0,1), # Y
uniform(0,1), # Z
uniform(0,1) # W
))
s = uniform(0.05,0.15)
obj.get_transform().set_scale((
s,s,s
))
rgb = colorsys.hsv_to_rgb(
uniform(0,1),
uniform(0.7,1),
uniform(0.7,1)
)
obj.get_material().set_base_color(rgb)
mat = obj.get_material()
# Some logic to generate "natural" random materials
material_type = randint(0,2)
# Glossy / Matte Plastic
if material_type == 0:
if randint(0,2): mat.set_roughness(uniform(.9, 1))
else : mat.set_roughness(uniform(.0,.1))
# Metallic
if material_type == 1:
mat.set_metallic(uniform(0.9,1))
if randint(0,2): mat.set_roughness(uniform(.9, 1))
else : mat.set_roughness(uniform(.0,.1))
# Glass
if material_type == 2:
mat.set_transmission(uniform(0.9,1))
# controls outside roughness
if randint(0,2): mat.set_roughness(uniform(.9, 1))
else : mat.set_roughness(uniform(.0,.1))
# controls inside roughness
if randint(0,2): mat.set_transmission_roughness(uniform(.9, 1))
else : mat.set_transmission_roughness(uniform(.0,.1))
mat.set_sheen(uniform(0,1)) # <- soft velvet like reflection near edges
mat.set_clearcoat(uniform(0,1)) # <- Extra, white, shiny layer. Good for car paint.
if randint(0,1): mat.set_anisotropic(uniform(0.9,1)) # elongates highlights
# (lots of other material parameters are listed in the docs)
# Now, use the above function to make a bunch of random objects
for i in range(opt.nb_objs):
add_random_obj(str(i))
print("\rcreating random object", i, end="")
print(" - done!")
nvisii.render_to_file(
width = opt.width,
height = opt.height,
samples_per_pixel = opt.spp,
file_path = opt.out
)
nvisii.deinitialize()