|
| 1 | +import compas_blender |
| 2 | +from compas_blender.artists import BaseArtist |
| 3 | + |
| 4 | + |
| 5 | +__all__ = ['FrameArtist'] |
| 6 | + |
| 7 | + |
| 8 | +class FrameArtist(BaseArtist): |
| 9 | + """Artist for drawing frames. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + frame: :class:`compas.geometry.Frame` |
| 14 | + A COMPAS frame. |
| 15 | + collection: str |
| 16 | + The name of the frame's collection. |
| 17 | + scale: float, optional |
| 18 | + Scale factor that controls the length of the axes. |
| 19 | +
|
| 20 | + Attributes |
| 21 | + ---------- |
| 22 | + frame: :class:`compas.geometry.Frame` |
| 23 | + A COMPAS frame. |
| 24 | + collection: str |
| 25 | + The name of the frame's collection. |
| 26 | + scale : float |
| 27 | + Scale factor that controls the length of the axes. |
| 28 | + Default is ``1.0``. |
| 29 | + color_origin : tuple of 3 int between 0 and 255 |
| 30 | + Default is ``(0, 0, 0)``. |
| 31 | + color_xaxis : tuple of 3 int between 0 and 255 |
| 32 | + Default is ``(255, 0, 0)``. |
| 33 | + color_yaxis : tuple of 3 int between 0 and 255 |
| 34 | + Default is ``(0, 255, 0)``. |
| 35 | + color_zaxis : tuple of 3 int between 0 and 255 |
| 36 | + Default is ``(0, 0, 255)``. |
| 37 | +
|
| 38 | + Examples |
| 39 | + -------- |
| 40 | + .. code-block:: python |
| 41 | +
|
| 42 | + from compas.geometry import Pointcloud |
| 43 | + from compas.geometry import Frame |
| 44 | +
|
| 45 | + from compas_blender.artists import FrameArtist |
| 46 | +
|
| 47 | + pcl = Pointcloud.from_bounds(10, 10, 10, 100) |
| 48 | + tpl = Frame([0, 0, 0], [1, 0, 0], [0, 1, 0]) |
| 49 | +
|
| 50 | +
|
| 51 | + for point in pcl.points: |
| 52 | + frame = tpl.copy() |
| 53 | + frame.point = point |
| 54 | + artist = FrameArtist(frame) |
| 55 | + artist.draw() |
| 56 | +
|
| 57 | + """ |
| 58 | + def __init__(self, frame, collection=None, scale=1.0): |
| 59 | + super(FrameArtist, self).__init__() |
| 60 | + self.collection = collection |
| 61 | + self.frame = frame |
| 62 | + self.scale = scale or 1.0 |
| 63 | + self.color_origin = (0, 0, 0) |
| 64 | + self.color_xaxis = (255, 0, 0) |
| 65 | + self.color_yaxis = (0, 255, 0) |
| 66 | + self.color_zaxis = (0, 0, 255) |
| 67 | + |
| 68 | + def draw(self): |
| 69 | + """Draw the frame. |
| 70 | +
|
| 71 | + Returns |
| 72 | + ------- |
| 73 | + ``None`` |
| 74 | + """ |
| 75 | + self.clear() |
| 76 | + self.draw_origin() |
| 77 | + self.draw_axes() |
| 78 | + |
| 79 | + def draw_origin(self): |
| 80 | + """Draw the origin of the frame. |
| 81 | +
|
| 82 | + Returns |
| 83 | + ------- |
| 84 | + list of :class:`bpy.types.Object` |
| 85 | + """ |
| 86 | + points = [{ |
| 87 | + 'pos': self.frame.point, |
| 88 | + 'name': f"{self.frame.name}.origin", |
| 89 | + 'color': self.color_origin, |
| 90 | + 'radius': 0.01 |
| 91 | + }] |
| 92 | + objects = compas_blender.draw_points(points, self.collection) |
| 93 | + self.objects += objects |
| 94 | + return objects |
| 95 | + |
| 96 | + def draw_axes(self): |
| 97 | + """Draw the axes of the frame. |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + list of :class:`bpy.types.Object` |
| 102 | + """ |
| 103 | + origin = list(self.frame.point) |
| 104 | + X = list(self.frame.point + self.frame.xaxis.scaled(self.scale)) |
| 105 | + Y = list(self.frame.point + self.frame.yaxis.scaled(self.scale)) |
| 106 | + Z = list(self.frame.point + self.frame.zaxis.scaled(self.scale)) |
| 107 | + lines = [ |
| 108 | + {'start': origin, 'end': X, 'color': self.color_xaxis, 'name': f"{self.frame.name}.xaxis"}, |
| 109 | + {'start': origin, 'end': Y, 'color': self.color_yaxis, 'name': f"{self.frame.name}.yaxis"}, |
| 110 | + {'start': origin, 'end': Z, 'color': self.color_zaxis, 'name': f"{self.frame.name}.zaxis"}, |
| 111 | + ] |
| 112 | + objects = compas_blender.draw_lines(lines, self.collection) |
| 113 | + self.objects += objects |
| 114 | + return objects |
0 commit comments