From e7bae54662dd32b879bd81fe4c923c3b5898eb60 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 17 Nov 2022 13:18:08 -0500 Subject: [PATCH] Update examples to use generated enums --- api/types.py | 8 +------- book/src/api/basics/using-nodes.md | 18 +++++++++--------- book/src/tutorials/city-builder.md | 16 ++++++++-------- book/src/tutorials/voxelize.md | 6 +++--- examples/City Builder.py | 17 +++++++++++++---- examples/Mesh to LEGO.py | 8 ++++---- 6 files changed, 38 insertions(+), 35 deletions(-) diff --git a/api/types.py b/api/types.py index 30f1c33..cbefd62 100644 --- a/api/types.py +++ b/api/types.py @@ -4,13 +4,7 @@ import nodeitems_utils from .state import State def map_case_name(i): - r = i.identifier.replace('_', ' ').title().replace(' ', '') - if r == 'None': - return 'NONE' - elif not r[0].isalpha(): - return f'_{r}' - else: - return r + return i.identifier.replace(' ', '_').upper() # The base class all exposed socket types conform to. class Type: diff --git a/book/src/api/basics/using-nodes.md b/book/src/api/basics/using-nodes.md index a577670..a0a4187 100644 --- a/book/src/api/basics/using-nodes.md +++ b/book/src/api/basics/using-nodes.md @@ -26,8 +26,8 @@ Many nodes have enum properties. For example, the math node lets you choose whic ```python # Access it by Node.Enum Name.Case -math(operation=Math.Operation.Add) -math(operation=Math.Operation.Subtract) +math(operation=Math.Operation.ADD) +math(operation=Math.Operation.SUBTRACT) math(operation='MULTIPLY') # Or manually pass a string ``` @@ -37,9 +37,9 @@ Internally, this type is generated as: import enum class Math: class Operation(enum.Enum): - Add = 'ADD' - Subtract = 'SUBTRACT' - Multiply = 'MULTIPLY' + ADD = 'ADD' + SUBTRACT = 'SUBTRACT' + MULTIPLY = 'MULTIPLY' ... ... ``` @@ -51,9 +51,9 @@ The cases will appear in code completion if you setup an [external editor](../.. Some nodes use the same input name multiple times. For example, the *Math* node has three inputs named `value`. To specify each value, pass a tuple for the input: ```python -math(operation=Math.Operation.Wrap, value=(0.5, 1, 0)) # Pass all 3 -math(operation=Math.Operation.Wrap, value=(0.5, 1)) # Only pass 2/3 -math(operation=Math.Operation.Wrap, value=0.5) # Only pass 1/3 +math(operation=Math.Operation.WRAP, value=(0.5, 1, 0)) # Pass all 3 +math(operation=Math.Operation.WRAP, value=(0.5, 1)) # Only pass 2/3 +math(operation=Math.Operation.WRAP, value=0.5) # Only pass 1/3 ``` ![](./math_wrap.png) @@ -122,7 +122,7 @@ size.cube(...) The node can now be used as a function: ```python -result = capture_attribute(data_type=CaptureAttribute.DataType.Boolean, geometry=cube_geo) # Specify a property and an input +result = capture_attribute(data_type=CaptureAttribute.DataType.BOOLEAN, geometry=cube_geo) # Specify a property and an input result.geometry # Access the geometry result.attribute # Access the attribute ``` diff --git a/book/src/tutorials/city-builder.md b/book/src/tutorials/city-builder.md index e638eb3..1966d81 100644 --- a/book/src/tutorials/city-builder.md +++ b/book/src/tutorials/city-builder.md @@ -44,7 +44,7 @@ def city_builder(...): ... return building_points.instance_on_points( instance=cube().transform(translation=(0, 0, 0.5)), - scale=random_value(data_type='FLOAT_VECTOR', min=building_size_min, max=building_size_max, seed=seed), + scale=random_value(data_type=RandomValue.DataType.FLOAT_VECTOR, min=building_size_min, max=building_size_max, seed=seed), ) ``` @@ -67,10 +67,10 @@ But now the buildings are overlapping the road. We need to remove any point that def city_builder(...): ... building_points = ... - road_points = geometry.curve_to_points(mode='EVALUATED').points + road_points = geometry.curve_to_points(mode=CurveToPoints.Mode.EVALUATED).points building_points = building_points.delete_geometry( - domain='POINT', - selection=geometry_proximity(target_element='POINTS', target=road_points, source_position=position()).distance < road_width + domain=DeleteGeometry.Domain.POINT, + selection=geometry_proximity(target_element=GeometryProximity.TargetElement.POINTS, target=road_points, source_position=position()).distance < road_width ) ... ``` @@ -101,16 +101,16 @@ def city_builder( )) # Building points building_points = grid(size_x=size_x, size_y=size_y).distribute_points_on_faces(density=density, seed=seed).points - road_points = geometry.curve_to_points(mode='EVALUATED').points + road_points = geometry.curve_to_points(mode=CurveToPoints.Mode.EVALUATED).points # Delete points within the curve building_points = building_points.delete_geometry( - domain='POINT', - selection=geometry_proximity(target_element='POINTS', target=road_points, source_position=position()).distance < road_width + domain=DeleteGeometry.Domain.POINT, + selection=geometry_proximity(target_element=GeometryProximity.TargetElement.POINTS, target=road_points, source_position=position()).distance < road_width ) # Building instances yield building_points.instance_on_points( instance=cube().transform(translation=(0, 0, 0.5)), - scale=random_value(data_type='FLOAT_VECTOR', min=building_size_min, max=building_size_max, seed=seed), + scale=random_value(data_type=RandomValue.DataType.FLOAT_VECTOR, min=building_size_min, max=building_size_max, seed=seed), ) ``` diff --git a/book/src/tutorials/voxelize.md b/book/src/tutorials/voxelize.md index aaa2302..e7e277f 100644 --- a/book/src/tutorials/voxelize.md +++ b/book/src/tutorials/voxelize.md @@ -57,7 +57,7 @@ def voxelize(geometry: Geometry, resolution: Float = 0.2): interior_band_width=resolution, fill_volume=False ).distribute_points_in_volume( # Uniform grid distribution - mode='DENSITY_GRID', + mode=DistributePointsInVolume.Mode.DENSITY_GRID, spacing=resolution ) ``` @@ -73,7 +73,7 @@ def voxelize(geometry: Geometry, resolution: Float = 0.2): interior_band_width=resolution, fill_volume=False ).distribute_points_in_volume( - mode='DENSITY_GRID', + mode=DistributePointsInVolume.Mode.DENSITY_GRID, spacing=resolution ).instance_on_points( # Cube instancing instance=cube(size=resolution) @@ -97,7 +97,7 @@ def voxelize(geometry: Geometry, resolution: Float = 0.2): interior_band_width=resolution, fill_volume=False ).distribute_points_in_volume( - mode='DENSITY_GRID', + mode=DistributePointsInVolume.Mode.DENSITY_GRID, spacing=resolution ).instance_on_points( instance=cube(size=resolution) diff --git a/examples/City Builder.py b/examples/City Builder.py index 7a95f81..4015554 100644 --- a/examples/City Builder.py +++ b/examples/City Builder.py @@ -4,7 +4,16 @@ from geometry_script import * @tree("City Builder") -def city_builder(geometry: Geometry, building_size_min: Vector = (0.1, 0.1, 0.2), building_size_max: Vector = (0.3, 0.3, 1), size_x: Float = 5.0, size_y: Float = 5.0, road_width: Float = 0.25, seed: Int = 0, resolution: Int = 60): +def city_builder( + geometry: Geometry, + building_size_min: Vector = (0.1, 0.1, 0.2), + building_size_max: Vector = (0.3, 0.3, 1), + size_x: Float = 5.0, + size_y: Float = 5.0, + road_width: Float = 0.25, + seed: Int = 0, + resolution: Int = 60 +): # Road geometry from input curves road_points = geometry.curve_to_points().points yield geometry.curve_to_mesh( @@ -22,10 +31,10 @@ def city_builder(geometry: Geometry, building_size_min: Vector = (0.1, 0.1, 0.2) seed=seed # Delete invalid building points based on proximity to a road ).points.delete_geometry( - domain='POINT', - selection=road_points.geometry_proximity(target_element='POINTS', source_position=position()).distance < road_width * 2 + domain=DeleteGeometry.Domain.POINT, + selection=road_points.geometry_proximity(target_element=GeometryProximity.TargetElement.POINTS, source_position=position()).distance < road_width * 2 ) - random_scale = random_value(data_type='FLOAT_VECTOR', min=building_size_min, max=building_size_max, seed=seed + id()) + random_scale = random_value(data_type=RandomValue.DataType.FLOAT_VECTOR, min=building_size_min, max=building_size_max, seed=seed + id()) yield building_points.instance_on_points( instance=cube(size=(1, 1, 1)).transform(translation=(0, 0, 0.5)), scale=random_scale diff --git a/examples/Mesh to LEGO.py b/examples/Mesh to LEGO.py index a2053b1..960ed9a 100644 --- a/examples/Mesh to LEGO.py +++ b/examples/Mesh to LEGO.py @@ -5,12 +5,12 @@ from geometry_script import * @tree("LEGO") def lego(size: Vector, stud_radius: Float, stud_depth: Float, count_x: Int, count_y: Int): base = cube(size=size) - stud_shape = cylinder(fill_type='NGON', radius=stud_radius, depth=stud_depth, vertices=8).mesh + stud_shape = cylinder(fill_type=Cylinder.FillType.NGON, radius=stud_radius, depth=stud_depth, vertices=8).mesh stud = stud_shape.transform(translation=combine_xyz(z=(stud_depth / 2) + (size.z / 2))) hole = stud_shape.transform(translation=combine_xyz(z=(stud_depth / 2) - (size.z / 2))) segment = mesh_boolean( - operation='DIFFERENCE', - mesh_1=mesh_boolean(operation='UNION', mesh_2=[base, stud]).mesh, + operation=MeshBoolean.Operation.DIFFERENCE, + mesh_1=mesh_boolean(operation=MeshBoolean.Operation.UNION, mesh_2=[base, stud]).mesh, mesh_2=hole ).mesh return mesh_line(count=count_x, offset=(1, 0, 0)).instance_on_points( @@ -20,7 +20,7 @@ def lego(size: Vector, stud_radius: Float, stud_depth: Float, count_x: Int, coun @tree("Mesh to LEGO") def mesh_to_lego(geometry: Geometry, resolution: Float=0.2): return geometry.mesh_to_volume(interior_band_width=resolution, fill_volume=False).distribute_points_in_volume( - mode='DENSITY_GRID', + mode=DistributePointsInVolume.Mode.DENSITY_GRID, spacing=resolution ).instance_on_points( instance=lego(size=resolution, stud_radius=resolution / 3, stud_depth=resolution / 8, count_x=1, count_y=1)