From 78098e5c0aab739386362b9b2eda6bbb9484e492 Mon Sep 17 00:00:00 2001 From: sven Date: Wed, 29 May 2019 09:28:11 +0200 Subject: [PATCH 1/2] update for Blender 2.80 --- turtle.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/turtle.py b/turtle.py index ab4a716..063d828 100644 --- a/turtle.py +++ b/turtle.py @@ -16,7 +16,7 @@ def __init__(self, _linewidth, _materialindex): self.stack = [] # rotate such that heading is in +Z (we want to grow upwards in blender) # we thus have heading = +Z, left = -Y, up = +X - self.mat *= Matrix.Rotation(radians(270), 4, 'Y') + self.mat @= Matrix.Rotation(radians(270), 4, 'Y') def push(self): """Push turtle state to stack""" @@ -29,15 +29,15 @@ def pop(self): def move(self, stepsize): """Move turtle in its heading direction.""" - vec = self.mat * Vector((stepsize,0,0,0)) + vec = self.mat @ Vector((stepsize,0,0,0)) self.mat.col[3] += vec def turn(self, angle_degrees): - self.mat *= Matrix.Rotation(radians(angle_degrees), 4, 'Z') + self.mat @= Matrix.Rotation(radians(angle_degrees), 4, 'Z') def pitch(self, angle_degrees): - self.mat *= Matrix.Rotation(radians(angle_degrees), 4, 'Y') + self.mat @= Matrix.Rotation(radians(angle_degrees), 4, 'Y') def roll(self, angle_degrees): - self.mat *= Matrix.Rotation(radians(angle_degrees), 4, 'X') + self.mat @= Matrix.Rotation(radians(angle_degrees), 4, 'X') def look_at(self, target): """ @@ -129,7 +129,7 @@ def __init__(self, _linewidth, _materialindex): root.data.name = "Root" root.data.use_auto_smooth = True root.data.auto_smooth_angle = radians(85) - bpy.ops.object.mode_set(mode = 'EDIT') + bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.select_all(action = 'SELECT') bpy.ops.mesh.delete() bpy.ops.object.mode_set(mode = 'OBJECT') @@ -153,7 +153,7 @@ def push(self): bpy.ops.object.empty_add(type='ARROWS', radius=0) empty = bpy.context.object empty.name = "Node" - empty.matrix_world *= self.mat + empty.matrix_world @= self.mat self.add_child_to_current_branch_parent(empty) self.current_parent = empty bpy.ops.object.select_all(action='DESELECT') @@ -198,9 +198,9 @@ def draw_module(self, """Add object instance from given shared mesh in current turtle coordinate system.""" scene = bpy.context.scene obj = bpy.data.objects.new(name, mesh) # create new object sharing the given mesh data - scene.objects.link(obj) - scene.objects.active = obj - obj.select = True + scene.collection.objects.link(obj) + bpy.context.view_layer.objects.active = obj + obj.select_set(True) # optionally set shading if scene.bool_force_shade_flat: bpy.ops.object.shade_flat() @@ -219,13 +219,13 @@ def draw_module(self, obj.material_slots[0].link = 'OBJECT' obj.material_slots[0].material = bpy.data.materials[self.materialindex] # align object with turtle - obj.matrix_world *= self.mat + obj.matrix_world @= self.mat # set scale obj.scale = scale # add obj to existing structure if scene.bool_no_hierarchy: - self.root.select = True - scene.objects.active = self.root + self.root.select_set(True) + bpy.context.view_layer.objects.active = self.root bpy.ops.object.join() else: self.add_child_to_current_branch_parent(obj) @@ -251,7 +251,7 @@ def create_default_internode_mesh(self, vertex_count): # rotate cylinder mesh to point towards x axis and position origin at base bpy.ops.object.mode_set(mode='EDIT', toggle=False) bpy.ops.mesh.select_all(action='SELECT') - bpy.ops.transform.rotate(value=radians(90), axis=(0, 1, 0)) + bpy.ops.transform.rotate(value=radians(90), orient_axis='Y') bpy.ops.transform.translate(value=(cylinder_length/2,0,0)) bpy.ops.object.mode_set(mode='OBJECT', toggle=False) cyl.data.name = bpy.types.Scene.internode_mesh_name[1]['default'] @@ -264,7 +264,7 @@ def create_default_internode_mesh(self, vertex_count): def create_default_node_mesh(self, _subdivisions=1): """Initialize the default icosphere mesh used to draw nodes""" - bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=_subdivisions, size=0.5) + bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=_subdivisions, radius=0.5) icosphere = bpy.context.object icosphere.data.name = bpy.types.Scene.node_mesh_name[1]['default'] icosphere.data.use_auto_smooth = True From 89db03868011fa634362ae6276b96d11c02e833e Mon Sep 17 00:00:00 2001 From: sven Date: Wed, 29 May 2019 09:33:50 +0200 Subject: [PATCH 2/2] Added option to use custom turtle instance for interpretation --- turtle_interpretation.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/turtle_interpretation.py b/turtle_interpretation.py index 0122271..da6141c 100644 --- a/turtle_interpretation.py +++ b/turtle_interpretation.py @@ -13,15 +13,20 @@ def interpret(lstring, default_length = 2.0, default_width_growth_factor=1.05, default_angle = 45.0, default_materialindex = 0, - dryrun_nodraw = False): + dryrun_nodraw = False, + use_turtle = None): """Create geometrical representation of L-string via Turtle Interpretation. NOTE: Commands that are not supported will be ignored and not raise an error.""" + use_custom_turtle = (None != use_turtle and isinstance(use_turtle, turtle.Turtle)) + # the option dryrun_nodraw is set, the turtle moves but does not draw any objects. # this is useful to do state queries at different moments via the '?' command # without the overhead of the drawing functions if dryrun_nodraw: t = turtle.Turtle(default_width, default_materialindex) # turtle base class that doesnt draw #print("TURTLE INTERPRETATION DRYRUN") + elif use_custom_turtle: + t = use_turtle else: t = turtle.DrawingTurtle(default_width, default_materialindex) @@ -251,7 +256,8 @@ def interpret(lstring, default_length = 2.0, "The values 0,0,0 will be replaced by the x,y,z respective vector values.") if not dryrun_nodraw: - t.root.name = "Root" # changed to "Root.xxx" on name collision + if not use_custom_turtle: + t.root.name = "Root" # changed to "Root.xxx" on name collision bpy.context.scene.last_interpretation_result_objname = t.root.name def applyCuts(lstring):