Skip to content

Commit

Permalink
Add custom indexes for edges and faces (Viewer Index node) (#4595)
Browse files Browse the repository at this point in the history
* add custom indexes for edges and faces

* fix problem not displaying custom text when node.draw_bface property was disabled

* update documentation

* fix list doc element
  • Loading branch information
Durman authored Aug 1, 2022
1 parent 3360ae8 commit 7c70274
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 42 deletions.
39 changes: 33 additions & 6 deletions docs/nodes/viz/viewer_idx28.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,46 @@ This node's primary purpose is for display the index information of geometry and
- Edge indices are drawn on midpoint of the Edge
- Face indices are drawn at the average location of the Vertices associated with the face.

Additionally
Additionally

- the input geometry can be transformed using the Matrix socket.
- the Node can draw arbitrary (non renderable) text into the 3dview at the location of incoming verts. There must be a one-to-one supply of vertices and text to draw, else the node will decide where to draw the excess text elements.
- the Node can draw arbitrary (non renderable) text into the 3dview at the
location of incoming verts, edges and faces. If incoming data is shorter than
number of elements the last element will be displayed on rest elements.

.. image:: https://user-images.githubusercontent.com/619340/127735798-4c3a4222-28ce-418a-b3f0-13c8149a590c.png
Because it can be difficult to read indices when there are many geometric elements visible there is an option to draw a small background under the text element.

- (available from N Panel) the Node can display the Object index associated with the element ( the first object, first index will be drawn as ``0: 0`` )
Parameters
----------

Because it can be difficult to read indices when there are many geometric elements visible there is an option to draw a small background under the text element.
Activate
Enabling the node.

Draw background
Hide background around text.

Draw bface
(using the Ghost icon in the Node UI) if you attach verts + faces, you can
also hide backfacing indices from being displayed. Adding the faces
information gives the node enough information to detect what can be seen
directly by the viewport "eye" location.

- (using the Ghost icon in the Node UI) if you attach verts + faces, you can also hide backfacing indices from being displayed. Adding the faces information gives the node enough information to detect what can be seen directly by the viewport "eye" location.
Draw object index
(available from N Panel) the Node can display the Object index associated
with the element ( the first object, first index will be drawn as ``0: 0`` )

Examples
--------

Hide text behind geometry

.. image:: https://user-images.githubusercontent.com/619340/127735702-7db6c27a-9f59-4ad4-83f8-44fe96fcbd0c.png
:width: 800px

Show custom text

.. image:: https://user-images.githubusercontent.com/619340/127735798-4c3a4222-28ce-418a-b3f0-13c8149a590c.png
:width: 800px



Expand Down
48 changes: 26 additions & 22 deletions nodes/viz/viewer_idx28.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,33 @@ def get_geometry(self):

setattr(geom, socket, input_stream)

prefix_if_needed = lambda obj_index, chars: (f'{obj_index}: {chars}') if self.draw_obj_idx else chars

fixed_text = []
if geom.text:
for obj_index, final_verts in enumerate(geom.verts):
text_size = max(len(final_verts),
len(geom.edges[obj_index]),
len(geom.faces[obj_index]))
text_items = self.get_text_of_correct_length(obj_index, geom, text_size)
for text_item in text_items:

# yikes, don't feed this function nonsense :)

if isinstance(text_item, float):
chars = prefix_if_needed(obj_index, text_item)
elif isinstance(text_item, list) and len(text_item) == 1:
chars = prefix_if_needed(obj_index, text_item[0])

else:
# in case it receives [0, 0, 0] or (0, 0, 0).. etc
chars = prefix_if_needed(obj_index, text_item)

fixed_text.append(chars)

if not self.draw_bface:
geom.face_medians, geom.face_normals = self.get_face_extras(geom)
geom.text_data = fixed_text
return geom

else:
Expand All @@ -213,15 +238,11 @@ def get_geometry(self):
display_topology.vert_data = []
display_topology.edge_data = []
display_topology.face_data = []
display_topology.text_data = []
display_topology.text_data = fixed_text

concat_vert = display_topology.vert_data.append
concat_edge = display_topology.edge_data.append
concat_face = display_topology.face_data.append
concat_text = display_topology.text_data.append

prefix_if_needed = lambda obj_index, chars: (f'{obj_index}: {chars}') if self.draw_obj_idx else chars


for obj_index, final_verts in enumerate(geom.verts):

Expand All @@ -231,23 +252,6 @@ def get_geometry(self):
chars = prefix_if_needed(obj_index, idx)
concat_vert((chars, vpos))

if geom.text:
text_items = self.get_text_of_correct_length(obj_index, geom, len(final_verts))
for text_item, vpos in zip(text_items, final_verts):

# yikes, don't feed this function nonsense :)

if isinstance(text_item, float):
chars = prefix_if_needed(obj_index, text_item)
elif isinstance(text_item, list) and len(text_item) == 1:
chars = prefix_if_needed(obj_index, text_item[0])

else:
# in case it receives [0, 0, 0] or (0, 0, 0).. etc
chars = prefix_if_needed(obj_index, text_item)

concat_text((chars))

if self.display_edge_index and obj_index < len(geom.edges):
for edge_index, (idx1, idx2) in enumerate(geom.edges[obj_index]):
loc = final_verts[idx1].lerp(final_verts[idx2], 0.5)
Expand Down
50 changes: 36 additions & 14 deletions utils/sv_idx_viewer28_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,20 @@ def draw_index(index, vec):
draw_index(*vidx)

blf.color(font_id, *edge_idx_color)
for eidx in geom.edge_data:
draw_index(*eidx)
if geom.text_data:
for text_item, (_, location) in zip(geom.text_data, geom.edge_data):
draw_index(text_item, location)
else:
for eidx in geom.edge_data:
draw_index(*eidx)

blf.color(font_id, *face_idx_color)
for fidx in geom.face_data:
draw_index(*fidx)
if geom.text_data:
for text_item, (_, location) in zip(geom.text_data, geom.face_data):
draw_index(text_item, location)
else:
for fidx in geom.face_data:
draw_index(*fidx)

# if drawing all geometry, we end early.
return
Expand Down Expand Up @@ -169,7 +177,8 @@ def draw_index(index, vec):
if hit:
if hit[2] == idx:
if display_face_index:
draw_index(idx, world_coordinate)
text = geom.text_data[idx] if geom.text_data else idx
draw_index(text, world_coordinate)

if display_vert_index:
for j in polygon:
Expand All @@ -183,15 +192,17 @@ def draw_index(index, vec):

blf.color(font_id, *vert_idx_color)
for idx in cache_vert_indices:
draw_index(idx, vertices[idx])
text = geom.text_data[idx] if geom.text_data else idx
draw_index(text, vertices[idx])

blf.color(font_id, *edge_idx_color)
for idx, edge in enumerate(edges):
sorted_edge = tuple(sorted(edge))
if sorted_edge in cache_edge_indices:
idx1, idx2 = sorted_edge
loc = vertices[idx1].lerp(vertices[idx2], 0.5)
draw_index(idx, loc)
text = geom.text_data[idx] if geom.text_data else idx
draw_index(text, loc)
cache_edge_indices.remove(sorted_edge)

except Exception as err:
Expand Down Expand Up @@ -308,12 +319,20 @@ def gather_index(index, vec, type_draw):
gather_index(vidx[0], vidx[1], 'verts')

# blf.color(font_id, *edge_idx_color)
for eidx in geom.edge_data:
gather_index(eidx[0], eidx[1], 'edges')
if geom.text_data:
for text_item, eidx in zip(geom.text_data, geom.edge_data):
gather_index(text_item, eidx[1], 'edges')
else:
for eidx in geom.edge_data:
gather_index(eidx[0], eidx[1], 'edges')

# blf.color(font_id, *face_idx_color)
for fidx in geom.face_data:
gather_index(fidx[0], fidx[1], 'faces')
if geom.text_data:
for text_item, fidx in zip(geom.text_data, geom.face_data):
gather_index(text_item, fidx[1], 'faces')
else:
for fidx in geom.face_data:
gather_index(fidx[0], fidx[1], 'faces')

draw_all_text_at_once(final_draw_data)
# if drawing all geometry, we end early.
Expand Down Expand Up @@ -360,7 +379,8 @@ def gather_index(index, vec, type_draw):
if hit:
if hit[2] == idx:
if display_face_index:
gather_index(idx, world_coordinate, 'faces')
text = geom.text_data[idx] if geom.text_data else idx
gather_index(text, world_coordinate, 'faces')

if display_vert_index:
for j in polygon:
Expand All @@ -374,15 +394,17 @@ def gather_index(index, vec, type_draw):

# blf.color(font_id, *vert_idx_color)
for idx in cache_vert_indices:
gather_index(idx, vertices[idx], 'verts')
text = geom.text_data[idx] if geom.text_data else idx
gather_index(text, vertices[idx], 'verts')

# blf.color(font_id, *edge_idx_color)
for idx, edge in enumerate(edges):
sorted_edge = tuple(sorted(edge))
if sorted_edge in cache_edge_indices:
idx1, idx2 = sorted_edge
loc = vertices[idx1].lerp(vertices[idx2], 0.5)
gather_index(idx, loc, 'edges')
text = geom.text_data[idx] if geom.text_data else idx
gather_index(text, loc, 'edges')
cache_edge_indices.remove(sorted_edge)

draw_all_text_at_once(final_draw_data)
Expand Down

0 comments on commit 7c70274

Please sign in to comment.