Skip to content

Commit bb14c31

Browse files
author
Lawrence D'Oliveiro
committed
some tidyup and extra comments
1 parent 4a1d4c3 commit bb14c31

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"name" : "Spaceship Generator",
44
"author" : "Michael Davies, Lawrence D'Oliveiro",
5-
"version" : (1, 2, 1),
5+
"version" : (1, 2, 2),
66
"blender" : (2, 82, 0),
77
"location" : "View3D > Add > Mesh",
88
"description" : "Procedurally generate 3D spaceships from a random seed.",

spaceship_generator.py

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from colorsys import \
2323
hls_to_rgb
2424

25-
deg = math.pi / 180 # conversion factor
25+
deg = math.pi / 180 # angle unit conversion factor
2626

2727
DIR = os.path.dirname(os.path.abspath(__file__))
2828

@@ -50,52 +50,46 @@ class NodeContext :
5050
"convenience class for assembling a nicely-laid-out node graph."
5151

5252
def __init__(self, graph, location) :
53+
"“graph” is the node tree for which to manage the addition of nodes." \
54+
" “location” is the initial location at which to start placing new nodes."
5355
self.graph = graph
54-
self.location = [location[0], location[1]]
55-
self.pos_save = []
56+
self._location = [location[0], location[1]]
5657
#end __init__
5758

5859
def step_across(self, width) :
5960
"returns the current position and advances it across by width."
60-
result = self.location[:]
61-
self.location[0] += width
61+
result = self._location[:]
62+
self._location[0] += width
6263
return result
6364
#end step_across
6465

6566
def step_down(self, height) :
6667
"returns the current position and advances it down by height."
67-
result = self.location[:]
68-
self.location[1] -= height
68+
result = self._location[:]
69+
self._location[1] -= height
6970
return result
7071
#end step_down
7172

7273
@property
7374
def pos(self) :
74-
return (self.location[0], self.location[1])
75+
"the current position (read/write)."
76+
return (self._location[0], self._location[1])
7577
#end pos
7678

7779
@pos.setter
7880
def pos(self, pos) :
79-
self.location[:] = [pos[0], pos[1]]
81+
self._location[:] = [pos[0], pos[1]]
8082
#end pos
8183

82-
def push_pos(self) :
83-
"saves the current location on the stack."
84-
self.pos_save.append(list(self.location))
85-
#end push_pos
86-
87-
def pop_pos(self) :
88-
"restores the last-stacked location."
89-
self.location[:] = self.pos_save.pop()
90-
#end pop_pos
91-
92-
def new_node(self, type, pos) :
84+
def node(self, type, pos) :
85+
"creates a new node of type “type” at position “pos”, and returns it."
9386
node = self.graph.nodes.new(type)
9487
node.location = (pos[0], pos[1])
9588
return node
96-
#end new_node
89+
#end node
9790

9891
def link(self, frôm, to) :
92+
"creates a link from output “frôm” to input “to”."
9993
self.graph.links.new(frôm, to)
10094
#end link
10195

@@ -217,12 +211,12 @@ def get_aspect_ratio(face) :
217211
#end get_aspect_ratio
218212

219213
def is_rear_face(face) :
220-
# is this face is pointing behind the ship
214+
# is this face pointing behind the ship
221215
return face.normal.x < -0.95
222216
#end is_rear_face
223217

224218
class MATERIAL(IntEnum) :
225-
"names for material slot indices."
219+
"names for material slot indices. Must be densely-assigned from 0."
226220
HULL = 0 # Plain spaceship hull
227221
HULL_LIGHTS = 1 # Spaceship hull with emissive windows
228222
HULL_DARK = 2 # Plain Spaceship hull, darkened
@@ -631,8 +625,8 @@ def define_tex_coords_common() :
631625
# for all my image textures.
632626
tex_coords_common = bpy.data.node_groups.new("SpaceShip.TexCoordsCommon", "ShaderNodeTree")
633627
ctx = NodeContext(tex_coords_common, (-100, 0))
634-
tex_coords = ctx.new_node("ShaderNodeTexCoord", ctx.step_across(200))
635-
group_output = ctx.new_node("NodeGroupOutput", ctx.step_across(200))
628+
tex_coords = ctx.node("ShaderNodeTexCoord", ctx.step_across(200))
629+
group_output = ctx.node("NodeGroupOutput", ctx.step_across(200))
636630
tex_coords_common.outputs.new("NodeSocketVector", "Coords")
637631
# work around intermittent crash on following line
638632
ctx.link(tex_coords.outputs["Object"], group_output.inputs[0])
@@ -647,9 +641,9 @@ def create_texture(ctx, filename, use_alpha, is_color) :
647641
# “textures” subdirectory. Returns the output terminal to be linked
648642
# to wherever the texture colour is needed.
649643
img = load_image(filename, use_alpha, is_color)
650-
coords = ctx.new_node("ShaderNodeGroup", ctx.step_across(200))
644+
coords = ctx.node("ShaderNodeGroup", ctx.step_across(200))
651645
coords.node_tree = tex_coords_common
652-
tex = ctx.new_node("ShaderNodeTexImage", ctx.step_across(300))
646+
tex = ctx.node("ShaderNodeTexImage", ctx.step_across(300))
653647
tex.image = img
654648
tex.projection = "BOX"
655649
ctx.link(coords.outputs[0], tex.inputs[0])
@@ -668,10 +662,10 @@ def define_normals_common() :
668662
use_alpha = True,
669663
is_color = False
670664
)
671-
normal_map = ctx.new_node("ShaderNodeNormalMap", ctx.step_across(200))
665+
normal_map = ctx.node("ShaderNodeNormalMap", ctx.step_across(200))
672666
ctx.link(tex_out, normal_map.inputs["Color"])
673667
normal_map.inputs["Strength"].default_value = 1
674-
group_output = ctx.new_node("NodeGroupOutput", ctx.step_across(200))
668+
group_output = ctx.node("NodeGroupOutput", ctx.step_across(200))
675669
normals_common.outputs.new("NodeSocketVector", "Normal")
676670
# work around intermittent crash on following line
677671
ctx.link(normal_map.outputs["Normal"], group_output.inputs[0])
@@ -687,20 +681,20 @@ def set_hull_mat_basics(mat, base_color) :
687681
main_shader = find_main_shader(mat)
688682
ctx = NodeContext(mat.node_tree, tuple(main_shader.location))
689683
ctx.step_across(-300)
690-
ctx.push_pos()
684+
save_pos = ctx.pos
691685
ctx.step_down(200)
692686
main_shader.inputs["Base Color"].default_value = base_color
693687
main_shader.inputs["Specular"].default_value = 0.1
694-
normal_map = ctx.new_node("ShaderNodeGroup", ctx.step_across(200))
688+
normal_map = ctx.node("ShaderNodeGroup", ctx.step_across(200))
695689
normal_map.node_tree = normals_common
696690
ctx.link(normal_map.outputs[0], main_shader.inputs["Normal"])
697-
ctx.pop_pos()
691+
ctx.pos = save_pos
698692
deselect_all(mat.node_tree)
699693
return ctx, main_shader # for adding further nodes if needed
700694
#end set_hull_mat_basics
701695

702696
def set_hull_mat_emissive(mat, color, strength) :
703-
# does common setup for emissive hull materials (windows, engines and other lights)
697+
# does common setup for very basic emissive hull materials (engines, landing discs)
704698
main_shader = find_main_shader(mat)
705699
main_shader.inputs["Emission"].default_value = tuple(c * strength for c in color)
706700
deselect_all(mat.node_tree)
@@ -745,8 +739,9 @@ def set_hull_mat_emissive(mat, color, strength) :
745739
use_alpha = True,
746740
is_color = True
747741
)
748-
mixer = ctx.new_node("ShaderNodeMixRGB", ctx.step_across(200))
742+
mixer = ctx.node("ShaderNodeMixRGB", ctx.step_across(200))
749743
mixer.blend_type = "ADD"
744+
# maybe “MULTIPLY” makes more sense, but then the unlit area looks darker than the hull
750745
mixer.inputs[0].default_value = 1.0
751746
ctx.link(base_window, mixer.inputs[1])
752747
mixer.inputs[2].default_value = \
@@ -760,11 +755,11 @@ def set_hull_mat_emissive(mat, color, strength) :
760755
+
761756
(1,)
762757
)
763-
color_shader = ctx.new_node("ShaderNodeBsdfDiffuse", ctx.step_across(200))
758+
color_shader = ctx.node("ShaderNodeBsdfDiffuse", ctx.step_across(200))
764759
ctx.link(mixer.outputs[0], color_shader.inputs["Color"])
765-
add_shader = ctx.new_node("ShaderNodeAddShader", ctx.step_across(200))
760+
add_shader = ctx.node("ShaderNodeAddShader", ctx.step_across(200))
766761
ctx.link(color_shader.outputs[0], add_shader.inputs[0])
767-
material_output = ctx.new_node("ShaderNodeOutputMaterial", ctx.step_across(200))
762+
material_output = ctx.node("ShaderNodeOutputMaterial", ctx.step_across(200))
768763
ctx.link(add_shader.outputs[0], material_output.inputs[0])
769764
ctx.pos = save_pos
770765
ctx.step_down(300)
@@ -776,7 +771,7 @@ def set_hull_mat_emissive(mat, color, strength) :
776771
use_alpha = False,
777772
is_color = True
778773
)
779-
light_shader = ctx.new_node("ShaderNodeEmission", ctx.step_across(200))
774+
light_shader = ctx.node("ShaderNodeEmission", ctx.step_across(200))
780775
ctx.link(window_light, light_shader.inputs["Color"])
781776
light_shader.inputs["Strength"].default_value = 2.0
782777
ctx.link(light_shader.outputs[0], add_shader.inputs[1])

0 commit comments

Comments
 (0)