Skip to content

Commit 5e3f3a9

Browse files
author
andrewtliew
committed
- Few more drawing functions finished.
1 parent 6e11004 commit 5e3f3a9

File tree

1 file changed

+81
-68
lines changed

1 file changed

+81
-68
lines changed

src/compas_blender/utilities/drawing.py

Lines changed: 81 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from __future__ import division
44
from __future__ import print_function
55

6+
from compas_blender.utilities import delete_object
67
from compas_blender.utilities import set_objects_layer
78
# from compas_blender.utilities import set_deselect
89
# from compas_blender.utilities import set_objects_visible
9-
# from compas_blender.utilities import delete_object
1010

1111
from compas.geometry import centroid_points
12-
# from compas.geometry import distance_point_point
12+
from compas.geometry import distance_point_point
1313
from compas.geometry import subtract_vectors
1414

1515
try:
@@ -24,7 +24,7 @@
2424
__all__ = [
2525
'create_material',
2626
'xdraw_points',
27-
# 'xdraw_lines',
27+
'xdraw_lines',
2828
# 'xdraw_geodesics',
2929
# 'xdraw_breps',
3030
# 'xdraw_cylinders',
@@ -36,23 +36,25 @@
3636
# 'xdraw_faces',
3737
# 'xdraw_pointcloud',
3838
# 'xdraw_texts',
39-
# 'draw_cylinder',
39+
'draw_cylinder',
4040
# 'draw_plane',
4141
# 'draw_text',
4242
# 'draw_line',
4343
]
4444

4545

46-
# def _link_objects(objects, copy=None):
46+
def _link_objects(objects, copy=None, layer=None):
4747

48-
# for object in objects:
49-
# bpy.context.collection.objects.link(object)
48+
for object in objects:
49+
bpy.context.collection.objects.link(object)
5050

51-
# set_deselect()
52-
# if copy:
53-
# set_objects_visible(objects=[copy], visible=False) # temp hack
51+
if copy:
52+
delete_object(object=copy)
5453

55-
# return objects
54+
if layer:
55+
set_objects_layer(objects=objects, layer=layer)
56+
57+
return objects
5658

5759

5860
def create_material(color, alpha=1):
@@ -68,26 +70,27 @@ def create_material(color, alpha=1):
6870
return bpy.data.materials[ckey]
6971

7072

71-
# def xdraw_points(points, **kwargs):
73+
def xdraw_points(points, layer=None):
7274

73-
# bpy.ops.object.empty_add(type='SPHERE', radius=1, location=[0, 0, 0])
74-
# copy = bpy.context.object
75+
bpy.ops.object.empty_add(type='SPHERE', radius=1, location=[0, 0, 0])
76+
copy = bpy.context.object
7577

76-
# objects = [0] * len(points)
78+
objects = [0] * len(points)
7779

78-
# for c, data in enumerate(points):
80+
for c, data in enumerate(points):
7981

80-
# object = copy.copy()
81-
# object.scale *= data.get('radius', 1)
82-
# object.location = data.get('pos', [0, 0, 0])
83-
# object.name = data.get('name', 'point')
84-
# # layer
85-
# objects[c] = object
82+
object = copy.copy()
83+
object.scale *= data.get('radius', 1)
84+
object.location = data.get('pos', [0, 0, 0])
85+
object.name = data.get('name', 'point')
86+
objects[c] = object
87+
88+
return _link_objects(objects, copy, layer)
8689

87-
# return _link_objects(objects, copy)
8890

91+
def xdraw_lines(lines, centroid=True, **kwargs):
8992

90-
# def xdraw_lines(lines, centroid=True, **kwargs):
93+
pass
9194

9295
# objects = [0] * len(lines)
9396

@@ -281,23 +284,25 @@ def xdraw_mesh(vertices, edges=None, faces=None, name='mesh', color=[1, 1, 1], c
281284
# return _link_objects(objects, copy)
282285

283286

284-
# def draw_cylinder(start, end, radius=1, color=[1, 1, 1], layer=None, div=10, **kwargs):
287+
def draw_cylinder(start, end, radius=1, color=[1, 1, 1], layer=None, div=10, name='cylinder'):
285288

286-
# bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=1, vertices=div, location=[0, 0, 0])
287-
# object = bpy.context.object
289+
bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=1, vertices=div, location=[0, 0, 0])
288290

289-
# L = distance_point_point(start, end)
290-
# pos = centroid_points([start, end])
291+
L = distance_point_point(start, end)
292+
pos = centroid_points([start, end])
293+
294+
object = bpy.context.object
295+
object.name = name
296+
object.rotation_euler[1] = acos((end[2] - start[2]) / L)
297+
object.rotation_euler[2] = atan2(end[1] - start[1], end[0] - start[0])
298+
object.location = pos
299+
object.scale = ((radius, radius, L))
300+
object.data.materials.append(create_material(color=color))
291301

292-
# object.name = 'cylinder'
293-
# object.rotation_euler[1] = acos((end[2] - start[2]) / L)
294-
# object.rotation_euler[2] = atan2(end[1] - start[1], end[0] - start[0])
295-
# object.location = pos
296-
# object.scale = ((radius, radius, L))
297-
# object.data.materials.append(create_material(color=color))
298-
# # layer
302+
if layer:
303+
set_objects_layer(objects=[object], layer=layer)
299304

300-
# return object
305+
return object
301306

302307

303308
# def draw_plane(Lx=1, Ly=1, dx=0.5, dy=0.5, name='plane', layer=None, color=[1, 1, 1]):
@@ -341,45 +346,49 @@ def xdraw_mesh(vertices, edges=None, faces=None, name='mesh', color=[1, 1, 1], c
341346
# return xdraw_mesh(name=name, vertices=vertices, faces=faces, layer=layer, color=color, centroid=False)
342347

343348

344-
# def draw_text(radius=1, pos=[0, 0, 0], text='text', layer=None, color=[1, 1, 1]):
349+
def draw_text(radius=1, pos=[0, 0, 0], text='text', layer=None, color=[1, 1, 1]):
350+
351+
bpy.ops.object.text_add(view_align=False)
345352

346-
# bpy.ops.object.text_add(view_align=False)
347-
# object = bpy.context.object
353+
object = bpy.context.object
354+
object.scale *= radius
355+
object.location = pos
356+
object.data.body = text
357+
object.data.materials.append(create_material(color=color))
358+
359+
if layer:
360+
set_objects_layer(objects=[object], layer=layer)
348361

349-
# object.scale *= radius
350-
# object.location = pos
351-
# object.data.body = text
352-
# object.data.materials.append(create_material(color=color))
353-
# # layer
362+
return object
354363

355-
# return object
356364

365+
def draw_line(start=[0, 0, 0], end=[1, 1, 1], width=0.05, centroid=True, name='line', color=[1, 1, 1], layer=None):
357366

358-
# def draw_line(start=[0, 0, 0], end=[1, 1, 1], width=0.05, centroid=True, name='line', color=[1, 1, 1], **kwargs):
367+
mp = centroid_points([start, end]) if centroid else [0, 0, 0]
359368

360-
# mp = centroid_points([start, end]) if centroid else [0, 0, 0]
369+
curve = bpy.data.curves.new(name, type='CURVE')
370+
curve.dimensions = '3D'
371+
object = bpy.data.objects.new(name, curve)
372+
object.location = mp
361373

362-
# curve = bpy.data.curves.new(name, type='CURVE')
363-
# curve.dimensions = '3D'
364-
# object = bpy.data.objects.new(name, curve)
365-
# object.location = mp
374+
spline = curve.splines.new('NURBS')
375+
spline.points.add(2)
376+
spline.points[0].co = list(subtract_vectors(start, mp)) + [1]
377+
spline.points[1].co = list(subtract_vectors(end, mp)) + [1]
378+
spline.order_u = 1
366379

367-
# spline = curve.splines.new('NURBS')
368-
# spline.points.add(2)
369-
# spline.points[0].co = list(subtract_vectors(start, mp)) + [1]
370-
# spline.points[1].co = list(subtract_vectors(end, mp)) + [1]
371-
# spline.order_u = 1
380+
object.data.fill_mode = 'FULL'
381+
object.data.bevel_depth = width
382+
object.data.bevel_resolution = 0
383+
object.data.resolution_u = 2
384+
object.data.materials.append(create_material(color=color))
372385

373-
# object.data.fill_mode = 'FULL'
374-
# object.data.bevel_depth = width
375-
# object.data.bevel_resolution = 0
376-
# object.data.resolution_u = 2
377-
# object.data.materials.append(create_material(color=color))
378-
# # layer
386+
if layer:
387+
set_objects_layer(objects=[object], layer=layer)
379388

380-
# bpy.context.collection.objects.link(object)
389+
bpy.context.collection.objects.link(object)
381390

382-
# return object
391+
return object
383392

384393

385394
# ==============================================================================
@@ -395,9 +404,9 @@ def xdraw_mesh(vertices, edges=None, faces=None, name='mesh', color=[1, 1, 1], c
395404

396405
# clear_layer(layer='Collection 1')
397406

398-
# n = 5
407+
n = 10
399408

400-
# points = [{'pos': [0, 0, i], 'radius': 0.2, 'name': 'pt'} for i in range(n)]
409+
points = [{'pos': [0, 0, i], 'radius': 0.2, 'name': 'pt'} for i in range(n)]
401410
# lines = [{'start': [1, 1, i], 'end': [1, 0, i], 'radius': 0.1, 'color': [1, 0, 1]} for i in range(n)]
402411
# cyls = [{'start': [2, 1, i], 'end': [2, 0, i], 'radius': 0.1, 'color': [0, 0, 1]} for i in range(n)]
403412
# spheres = [{'pos': [3, 0, i], 'radius': 0.5, 'color': [0, 1, 0]} for i in range(n)]
@@ -406,7 +415,7 @@ def xdraw_mesh(vertices, edges=None, faces=None, name='mesh', color=[1, 1, 1], c
406415

407416
# tic = time()
408417

409-
# xdraw_points(points=points)
418+
xdraw_points(points=points)
410419
# xdraw_lines(lines=lines)
411420
# xdraw_cylinders(cylinders=cyls)
412421
# xdraw_spheres(spheres=spheres)
@@ -421,5 +430,9 @@ def xdraw_mesh(vertices, edges=None, faces=None, name='mesh', color=[1, 1, 1], c
421430
faces = [[0, 1, 2], [2, 3, 0]]
422431
mesh = xdraw_mesh(name='mesh', vertices=vertices, faces=faces, layer='Collection 2', color=[1, 0, 1])
423432

433+
draw_cylinder(start=[0, 0, 0], end=[1, 1, 1], radius=0.1, color=[1, 0, 1], layer='Collection 2')
434+
draw_line(start=[2, 2, 2], end=[1, 1, 1], width=0.05, name='line', color=[1, 1, 0])
435+
draw_text(radius=1, pos=[0, 0, 0], text='text', layer='Collection 2', color=[1, 0, 0])
436+
424437
# objects = xdraw_pointcloud(points=points)
425438
# set_objects_show_names(objects=objects, show=True)

0 commit comments

Comments
 (0)