Spaces:
Running
on
Zero
Running
on
Zero
File size: 15,466 Bytes
b6a9b6d |
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# Copyright (C) 2023, Princeton University.
# This source code is licensed under the BSD 3-Clause license found in the LICENSE file in the root directory of this source tree.
# Authors: Yiming Zuo
import bpy
import numpy as np
from numpy.random import choice, randint, uniform
import infinigen
import infinigen.core.util.blender as butil
from infinigen.assets.material_assignments import AssetList
from infinigen.assets.objects.table_decorations.utils import (
nodegroup_lofting,
nodegroup_star_profile,
)
from infinigen.core import surface
from infinigen.core.nodes import node_utils
from infinigen.core.nodes.node_wrangler import Nodes, NodeWrangler
from infinigen.core.placement.factory import AssetFactory
from infinigen.core.util.math import FixedSeed
class VaseFactory(AssetFactory):
def __init__(self, factory_seed, coarse=False, dimensions=None):
super(VaseFactory, self).__init__(factory_seed, coarse=coarse)
if dimensions is None:
z = uniform(0.17, 0.5)
x = z * uniform(0.3, 0.6)
dimensions = (x, x, z)
self.dimensions = dimensions
self.get_params_dict()
with FixedSeed(factory_seed):
self.params = self.sample_parameters(dimensions)
self.material_params, self.scratch, self.edge_wear = (
self.get_material_params()
)
self.params.update(self.material_params)
def get_params_dict(self):
# list all the parameters (key:name, value: [type, range]) used in this generator
self.params_dict = {
"dimension_x": ["continuous", (0.05, 0.4)],
"dimension_z": ["continuous", (0.2, 0.8)],
"neck_scale": ["continuous", (0.15, 0.8)],
"profile_inner_radius": ["continuous", (0.8, 1.2)],
"profile_star_points": ["discrete", (2,3,4,5,6,7,8,9,10,16,18,20,22,24,26,28,30)],
"top_scale": ["continuous", (0.6, 1.4)],
"neck_mid_position": ["continuous", (0.5, 1.5)],
"neck_position": ["continuous", (-0.2, 0.2)],
"shoulder_position": ["continuous", (0.1, 0.8)],
"shoulder_thickness": ["continuous", (0.1, 0.3)],
"foot_scale": ["continuous", (0.2, 0.8)],
"foot_height": ["continuous", (0.01, 0.1)],
}
def get_material_params(self):
material_assignments = AssetList["VaseFactory"]()
params = {
"Material": material_assignments["surface"].assign_material(),
}
wrapped_params = {
k: surface.shaderfunc_to_material(v) for k, v in params.items()
}
scratch_prob, edge_wear_prob = material_assignments["wear_tear_prob"]
scratch, edge_wear = material_assignments["wear_tear"]
is_scratch = uniform() < scratch_prob
is_edge_wear = uniform() < edge_wear_prob
if not is_scratch:
scratch = None
if not is_edge_wear:
edge_wear = None
return wrapped_params, scratch, edge_wear
@staticmethod
def sample_parameters(dimensions):
# all in meters
if dimensions is None:
z = uniform(0.25, 0.40)
x = uniform(0.2, 0.4) * z
dimensions = (x, x, z)
x, y, z = dimensions
U_resolution = 64
V_resolution = 64
neck_scale = uniform(0.2, 0.8)
parameters = {
"Profile Inner Radius": choice([1.0, uniform(0.8, 1.0)]),
"Profile Star Points": randint(16, U_resolution // 2 + 1),
"U_resolution": U_resolution,
"V_resolution": V_resolution,
"Height": z,
"Diameter": x,
"Top Scale": neck_scale * uniform(0.8, 1.2),
"Neck Mid Position": uniform(0.7, 0.95),
"Neck Position": 0.5 * neck_scale + 0.5 + uniform(-0.05, 0.05),
"Neck Scale": neck_scale,
"Shoulder Position": uniform(0.3, 0.7),
"Shoulder Thickness": uniform(0.1, 0.25),
"Foot Scale": uniform(0.4, 0.6),
"Foot Height": uniform(0.01, 0.1),
}
return parameters
def fix_unused_params(self, params):
return params
def update_params(self, params):
x, y, z = params["dimension_x"], params["dimension_x"], params["dimension_z"]
U_resolution = 64
V_resolution = 64
neck_scale = params["neck_scale"]
parameters = {
"Profile Inner Radius": np.clip(params["profile_inner_radius"], 0.8, 1.0),
"Profile Star Points": params["profile_star_points"],
"U_resolution": U_resolution,
"V_resolution": V_resolution,
"Height": z,
"Diameter": x,
"Top Scale": neck_scale * params["top_scale"],
"Neck Mid Position": params["neck_mid_position"],
"Neck Position": 0.5 * neck_scale + 0.5 + params["neck_position"],
"Neck Scale": neck_scale,
"Shoulder Position": params["shoulder_position"],
"Shoulder Thickness": params["shoulder_thickness"],
"Foot Scale": params["foot_scale"],
"Foot Height": params["foot_height"],
}
self.params.update(parameters)
self.material_params, self.scratch, self.edge_wear = (
self.get_material_params()
)
self.params.update(self.material_params)
def create_asset(self, **params):
bpy.ops.mesh.primitive_plane_add(
size=2,
enter_editmode=False,
align="WORLD",
location=(0, 0, 0),
scale=(1, 1, 1),
)
obj = bpy.context.active_object
surface.add_geomod(obj, geometry_vases, apply=True, input_kwargs=self.params)
butil.modify_mesh(obj, "SOLIDIFY", apply=True, thickness=0.002)
butil.modify_mesh(obj, "SUBSURF", apply=True, levels=2, render_levels=2)
return obj
def finalize_assets(self, assets):
if self.scratch:
self.scratch.apply(assets)
if self.edge_wear:
self.edge_wear.apply(assets)
@node_utils.to_nodegroup(
"nodegroup_vase_profile", singleton=False, type="GeometryNodeTree"
)
def nodegroup_vase_profile(nw: NodeWrangler):
# Code generated using version 2.6.4 of the node_transpiler
group_input = nw.new_node(
Nodes.GroupInput,
expose_input=[
("NodeSocketGeometry", "Profile Curve", None),
("NodeSocketFloat", "Height", 0.0000),
("NodeSocketFloat", "Diameter", 0.0000),
("NodeSocketFloat", "Top Scale", 0.0000),
("NodeSocketFloat", "Neck Mid Position", 0.0000),
("NodeSocketFloat", "Neck Position", 0.5000),
("NodeSocketFloat", "Neck Scale", 0.0000),
("NodeSocketFloat", "Shoulder Position", 0.0000),
("NodeSocketFloat", "Shoulder Thickness", 0.0000),
("NodeSocketFloat", "Foot Scale", 0.0000),
("NodeSocketFloat", "Foot Height", 0.0000),
],
)
combine_xyz_1 = nw.new_node(
Nodes.CombineXYZ, input_kwargs={"Z": group_input.outputs["Height"]}
)
multiply = nw.new_node(
Nodes.Math,
input_kwargs={
0: group_input.outputs["Top Scale"],
1: group_input.outputs["Diameter"],
},
attrs={"operation": "MULTIPLY"},
)
neck_top = nw.new_node(
Nodes.Transform,
input_kwargs={
"Geometry": group_input.outputs["Profile Curve"],
"Translation": combine_xyz_1,
"Scale": multiply,
},
)
multiply_1 = nw.new_node(
Nodes.Math,
input_kwargs={
0: group_input.outputs["Height"],
1: group_input.outputs["Neck Position"],
},
attrs={"operation": "MULTIPLY"},
)
combine_xyz = nw.new_node(Nodes.CombineXYZ, input_kwargs={"Z": multiply_1})
multiply_2 = nw.new_node(
Nodes.Math,
input_kwargs={
0: group_input.outputs["Diameter"],
1: group_input.outputs["Neck Scale"],
},
attrs={"operation": "MULTIPLY"},
)
neck = nw.new_node(
Nodes.Transform,
input_kwargs={
"Geometry": group_input.outputs["Profile Curve"],
"Translation": combine_xyz,
"Scale": multiply_2,
},
)
subtract = nw.new_node(
Nodes.Math,
input_kwargs={0: 1.0000, 1: group_input.outputs["Neck Position"]},
attrs={"use_clamp": True, "operation": "SUBTRACT"},
)
multiply_add = nw.new_node(
Nodes.Math,
input_kwargs={
0: subtract,
1: group_input.outputs["Neck Mid Position"],
2: group_input.outputs["Neck Position"],
},
attrs={"operation": "MULTIPLY_ADD"},
)
multiply_3 = nw.new_node(
Nodes.Math,
input_kwargs={0: multiply_add, 1: group_input.outputs["Height"]},
attrs={"operation": "MULTIPLY"},
)
combine_xyz_2 = nw.new_node(Nodes.CombineXYZ, input_kwargs={"Z": multiply_3})
add = nw.new_node(
Nodes.Math,
input_kwargs={
0: group_input.outputs["Neck Scale"],
1: group_input.outputs["Top Scale"],
},
)
divide = nw.new_node(
Nodes.Math, input_kwargs={0: add, 1: 2.0000}, attrs={"operation": "DIVIDE"}
)
multiply_4 = nw.new_node(
Nodes.Math,
input_kwargs={0: group_input.outputs["Diameter"], 1: divide},
attrs={"operation": "MULTIPLY"},
)
neck_middle = nw.new_node(
Nodes.Transform,
input_kwargs={
"Geometry": group_input.outputs["Profile Curve"],
"Translation": combine_xyz_2,
"Scale": multiply_4,
},
)
neck_geometry = nw.new_node(
Nodes.JoinGeometry, input_kwargs={"Geometry": [neck, neck_middle, neck_top]}
)
map_range = nw.new_node(
Nodes.MapRange,
input_kwargs={
"Value": group_input.outputs["Shoulder Position"],
3: group_input.outputs["Foot Height"],
4: group_input.outputs["Neck Position"],
},
)
subtract_1 = nw.new_node(
Nodes.Math,
input_kwargs={
0: group_input.outputs["Neck Position"],
1: group_input.outputs["Foot Height"],
},
attrs={"operation": "SUBTRACT"},
)
multiply_5 = nw.new_node(
Nodes.Math,
input_kwargs={0: subtract_1, 1: group_input.outputs["Shoulder Thickness"]},
attrs={"operation": "MULTIPLY"},
)
add_1 = nw.new_node(
Nodes.Math, input_kwargs={0: map_range.outputs["Result"], 1: multiply_5}
)
minimum = nw.new_node(
Nodes.Math,
input_kwargs={0: add_1, 1: group_input.outputs["Neck Position"]},
attrs={"operation": "MINIMUM"},
)
multiply_6 = nw.new_node(
Nodes.Math,
input_kwargs={0: minimum, 1: group_input.outputs["Height"]},
attrs={"operation": "MULTIPLY"},
)
combine_xyz_3 = nw.new_node(Nodes.CombineXYZ, input_kwargs={"Z": multiply_6})
body_top = nw.new_node(
Nodes.Transform,
input_kwargs={
"Geometry": group_input.outputs["Profile Curve"],
"Translation": combine_xyz_3,
"Scale": group_input.outputs["Diameter"],
},
)
subtract_2 = nw.new_node(
Nodes.Math,
input_kwargs={0: map_range.outputs["Result"], 1: multiply_5},
attrs={"operation": "SUBTRACT"},
)
maximum = nw.new_node(
Nodes.Math,
input_kwargs={0: subtract_2, 1: group_input.outputs["Foot Height"]},
attrs={"operation": "MAXIMUM"},
)
multiply_7 = nw.new_node(
Nodes.Math,
input_kwargs={0: maximum, 1: group_input.outputs["Height"]},
attrs={"operation": "MULTIPLY"},
)
combine_xyz_5 = nw.new_node(Nodes.CombineXYZ, input_kwargs={"Z": multiply_7})
body_bottom = nw.new_node(
Nodes.Transform,
input_kwargs={
"Geometry": group_input.outputs["Profile Curve"],
"Translation": combine_xyz_5,
"Scale": group_input.outputs["Diameter"],
},
)
body_geometry = nw.new_node(
Nodes.JoinGeometry, input_kwargs={"Geometry": [body_bottom, body_top]}
)
multiply_8 = nw.new_node(
Nodes.Math,
input_kwargs={
0: group_input.outputs["Foot Height"],
1: group_input.outputs["Height"],
},
attrs={"operation": "MULTIPLY"},
)
combine_xyz_4 = nw.new_node(Nodes.CombineXYZ, input_kwargs={"Z": multiply_8})
multiply_9 = nw.new_node(
Nodes.Math,
input_kwargs={
0: group_input.outputs["Diameter"],
1: group_input.outputs["Foot Scale"],
},
attrs={"operation": "MULTIPLY"},
)
foot_top = nw.new_node(
Nodes.Transform,
input_kwargs={
"Geometry": group_input,
"Translation": combine_xyz_4,
"Scale": multiply_9,
},
)
foot_bottom = nw.new_node(
Nodes.Transform, input_kwargs={"Geometry": group_input, "Scale": multiply_9}
)
foot_geometry = nw.new_node(
Nodes.JoinGeometry, input_kwargs={"Geometry": [foot_bottom, foot_top]}
)
join_geometry_2 = nw.new_node(
Nodes.JoinGeometry,
input_kwargs={"Geometry": [foot_geometry, body_geometry, neck_geometry]},
)
group_output = nw.new_node(
Nodes.GroupOutput,
input_kwargs={"Geometry": join_geometry_2},
attrs={"is_active_output": True},
)
def geometry_vases(nw: NodeWrangler, **kwargs):
# Code generated using version 2.6.4 of the node_transpiler
starprofile = nw.new_node(
nodegroup_star_profile().name,
input_kwargs={
"Resolution": kwargs["U_resolution"],
"Points": kwargs["Profile Star Points"],
"Inner Radius": kwargs["Profile Inner Radius"],
},
)
vaseprofile = nw.new_node(
nodegroup_vase_profile().name,
input_kwargs={
"Profile Curve": starprofile.outputs["Curve"],
"Height": kwargs["Height"],
"Diameter": kwargs["Diameter"],
"Top Scale": kwargs["Top Scale"],
"Neck Mid Position": kwargs["Neck Mid Position"],
"Neck Position": kwargs["Neck Position"],
"Neck Scale": kwargs["Neck Scale"],
"Shoulder Position": kwargs["Shoulder Position"],
"Shoulder Thickness": kwargs["Shoulder Thickness"],
"Foot Scale": kwargs["Foot Scale"],
"Foot Height": kwargs["Foot Height"],
},
)
lofting = nw.new_node(
nodegroup_lofting().name,
input_kwargs={
"Profile Curves": vaseprofile,
"U Resolution": 64,
"V Resolution": 64,
},
)
delete_geometry = nw.new_node(
Nodes.DeleteGeometry,
input_kwargs={
"Geometry": lofting.outputs["Geometry"],
"Selection": lofting.outputs["Top"],
},
)
set_material = nw.new_node(
Nodes.SetMaterial,
input_kwargs={"Geometry": delete_geometry, "Material": kwargs["Material"]},
)
group_output = nw.new_node(
Nodes.GroupOutput,
input_kwargs={"Geometry": set_material},
attrs={"is_active_output": True},
)
|