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

fix #4705. Viewer Index+ show right text with multiple objects and indices of objects #5000

Open
wants to merge 3 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
83 changes: 41 additions & 42 deletions nodes/viz/viewer_idx28.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ def get_geometry(self):
fixed_text = []
if geom.text:
for obj_index, final_verts in enumerate(geom.verts):
fixed_text_elem = []
text_size = max(len(final_verts),
len(geom.edges[obj_index] if geom.edges else []),
len(geom.faces[obj_index] if geom.faces else []))

text_items = self.get_text_of_correct_length(obj_index, geom, text_size)
for text_item in text_items:

Expand All @@ -234,47 +236,44 @@ def get_geometry(self):
# 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:
# pass only data onto the draw callback that you intend to show.
display_topology = lambda: None
display_topology.vert_data = []
display_topology.edge_data = []
display_topology.face_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

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

# can't display vert idx and text simultaneously - ...
if self.display_vert_index:
for idx, vpos in enumerate(final_verts):
chars = prefix_if_needed(obj_index, idx)
concat_vert((chars, vpos))

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)
chars = prefix_if_needed(obj_index, edge_index)
concat_edge((chars, loc))

if self.display_face_index and obj_index < len(geom.faces):
for face_index, f in enumerate(geom.faces[obj_index]):
poly_verts = [final_verts[idx] for idx in f]
median = calc_median(poly_verts)
chars = prefix_if_needed(obj_index, face_index)
concat_face((chars, median))

return display_topology
fixed_text_elem.append(chars)

fixed_text.append(fixed_text_elem)

display_topology = lambda: None
display_topology = geom
display_topology.vert_data = []
display_topology.edge_data = []
display_topology.face_data = []
display_topology.text_data = fixed_text
display_topology.face_medians, display_topology.face_normals = self.get_face_extras(geom)

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

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

# can't display vert idx and text simultaneously - ...
if True or self.display_vert_index:
for idx, vpos in enumerate(final_verts):
chars = prefix_if_needed(obj_index, idx)
concat_vert((chars, vpos, obj_index, idx))

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)
chars = prefix_if_needed(obj_index, edge_index)
concat_edge((chars, loc, obj_index, tuple(sorted( [idx1, idx2] )), edge_index ))

if self.display_face_index and obj_index < len(geom.faces):
for face_index, f in enumerate(geom.faces[obj_index]):
poly_verts = [final_verts[idx] for idx in f]
median = calc_median(poly_verts)
chars = prefix_if_needed(obj_index, face_index)
concat_face((chars, median, obj_index, face_index))

return display_topology

def get_text_of_correct_length(self, obj_index, geom, num_elements_to_fill):
""" get text elements, and extend if needed"""
Expand Down Expand Up @@ -316,7 +315,7 @@ def process(self):

draw_data = {
'tree_name': self.id_data.name[:],
'custom_function': draw_indices_2D_wbg if self.draw_bg else draw_indices_2D,
'custom_function': draw_indices_2D_wbg,
'args': (geom, config)}

callback_enable(n_id, draw_data, overlay='POST_PIXEL')
Expand Down
89 changes: 55 additions & 34 deletions utils/sv_idx_viewer28_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,12 @@ def draw_all_text_at_once(final_draw_data):
add_vert_list(pts)
add_vcol((col,) * 6)

# draw background
shader_name = f'{"2D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR'
shader = gpu.shader.from_builtin(shader_name)
batch = batch_for_shader(shader, 'TRIS', {"pos": full_bg_Verts, "color": full_bg_colors})
batch.draw(shader)
if draw_bg:
# draw background
shader_name = f'{"2D_" if bpy.app.version < (3, 4) else ""}SMOOTH_COLOR'
shader = gpu.shader.from_builtin(shader_name)
batch = batch_for_shader(shader, 'TRIS', {"pos": full_bg_Verts, "color": full_bg_colors})
batch.draw(shader)

# draw text
for counter, (index_str, pos_x, pos_y, txt_width, txt_height, type_draw, pts) in final_draw_data.items():
Expand Down Expand Up @@ -309,28 +310,29 @@ def gather_index(index, vec, type_draw):

# THIS SECTION IS ONLY EXECUTED IF BOTH FORWARD AND BACKFACING ARE DRAWN

if draw_bface:
if draw_bface or not draw_bface and len(geom.faces)==0:

# blf.color(font_id, *vert_idx_color)
if geom.vert_data and geom.text_data:
for text_item, (idx, location) in zip(geom.text_data, geom.vert_data):
gather_index(text_item, location, 'verts')
else:
for vidx in geom.vert_data:
gather_index(vidx[0], vidx[1], 'verts')
if display_vert_index:
if geom.vert_data and geom.text_data:
for (idx, location, obj_index, elem_index) in geom.vert_data:
gather_index(geom.text_data[obj_index][elem_index], location, 'verts')
else:
for vidx in geom.vert_data:
gather_index(vidx[0], vidx[1], 'verts')

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

# blf.color(font_id, *face_idx_color)
if geom.text_data:
for text_item, fidx in zip(geom.text_data, geom.face_data):
gather_index(text_item, fidx[1], 'faces')
for (idx, location, obj_index, elem_index) in geom.face_data:
gather_index(geom.text_data[obj_index][elem_index], location, 'faces')
else:
for fidx in geom.face_data:
gather_index(fidx[0], fidx[1], 'faces')
Expand All @@ -354,6 +356,7 @@ def gather_index(index, vec, type_draw):

cache_vert_indices = set()
cache_edge_indices = set()
cache_face_indices = set()

# blf.color(font_id, *face_idx_color)
for idx, polygon in enumerate(polygons):
Expand All @@ -380,8 +383,7 @@ def gather_index(index, vec, type_draw):
if hit:
if hit[2] == idx:
if display_face_index:
text = geom.text_data[idx] if geom.text_data else idx
gather_index(text, world_coordinate, 'faces')
cache_face_indices.add(idx)

if display_vert_index:
for j in polygon:
Expand All @@ -393,20 +395,39 @@ def gather_index(index, vec, type_draw):
cache_edge_indices.add(tuple(sorted([polygon[j], polygon[j+1]])))
cache_edge_indices.add(tuple(sorted([polygon[-1], polygon[0]])))

# blf.color(font_id, *vert_idx_color)
for idx in cache_vert_indices:
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)
text = geom.text_data[idx] if geom.text_data else idx
gather_index(text, loc, 'edges')
cache_edge_indices.remove(sorted_edge)
# vertices
if display_vert_index:
if geom.vert_data and geom.text_data:
for idx, location, obj_elem_index, elem_index in geom.vert_data:
if obj_elem_index==obj_index and elem_index in cache_vert_indices:
gather_index(geom.text_data[obj_index][elem_index], location, 'verts')
else:
for idx, location, obj_elem_index, elem_index in geom.vert_data:
if obj_elem_index==obj_index and elem_index in cache_vert_indices:
gather_index(idx, location, 'verts')

# edges
if geom.text_data:
edge_index = 0
for i, (idx, location, obj_elem_index, edge_indices, _) in enumerate(geom.edge_data):
if obj_elem_index==obj_index:
if edge_indices in cache_edge_indices:
gather_index(geom.text_data[obj_index][edge_index], location, 'edges')
edge_index+=1
else:
for idx, location, obj_elem_index, edge_indices, edge_index in geom.edge_data:
if obj_elem_index==obj_index and edge_indices in cache_edge_indices:
gather_index(idx, location, 'edges')

# faces
if geom.text_data:
for (idx, location, obj_elem_index, elem_index) in geom.face_data:
if obj_elem_index==obj_index and elem_index in cache_face_indices:
gather_index(geom.text_data[obj_index][elem_index], location, 'faces')
else:
for idx, location, obj_elem_index, elem_index in geom.face_data:
if obj_elem_index==obj_index and elem_index in cache_face_indices:
gather_index(idx, location, 'faces')

draw_all_text_at_once(final_draw_data)

Expand Down
Loading