Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom turtle #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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']
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions turtle_interpretation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down