From 200d91ead97bc71910f0e50d49d42795d9cdf995 Mon Sep 17 00:00:00 2001 From: satabol Date: Sat, 23 Sep 2023 00:58:25 +0300 Subject: [PATCH 1/3] Viewer Index+ show right text with multiple objects and indices of objects fix #4705 --- nodes/viz/viewer_idx28.py | 83 +++++++++++++++++------------------ utils/sv_idx_viewer28_draw.py | 77 ++++++++++++++++++++------------ 2 files changed, 89 insertions(+), 71 deletions(-) diff --git a/nodes/viz/viewer_idx28.py b/nodes/viz/viewer_idx28.py index 852d3af6c1..e8a4a5cfcd 100644 --- a/nodes/viz/viewer_idx28.py +++ b/nodes/viz/viewer_idx28.py @@ -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: @@ -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""" @@ -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') diff --git a/utils/sv_idx_viewer28_draw.py b/utils/sv_idx_viewer28_draw.py index 14266dee17..52afb62275 100644 --- a/utils/sv_idx_viewer28_draw.py +++ b/utils/sv_idx_viewer28_draw.py @@ -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(): @@ -313,24 +314,24 @@ def gather_index(index, vec, type_draw): # 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') + 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') @@ -354,6 +355,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): @@ -380,8 +382,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: @@ -393,20 +394,38 @@ 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 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) From 5529c0af45dcc65f68bbf96d274c412728b16e8a Mon Sep 17 00:00:00 2001 From: satabol Date: Sat, 23 Sep 2023 01:33:50 +0300 Subject: [PATCH 2/3] fix enable/disable vertices text/indices visible. --- utils/sv_idx_viewer28_draw.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/utils/sv_idx_viewer28_draw.py b/utils/sv_idx_viewer28_draw.py index 52afb62275..bd219af9c3 100644 --- a/utils/sv_idx_viewer28_draw.py +++ b/utils/sv_idx_viewer28_draw.py @@ -313,12 +313,13 @@ def gather_index(index, vec, type_draw): if draw_bface: # blf.color(font_id, *vert_idx_color) - 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') + 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: @@ -395,14 +396,15 @@ def gather_index(index, vec, type_draw): cache_edge_indices.add(tuple(sorted([polygon[-1], polygon[0]]))) # vertices - 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') + 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: From 97cc43368a492df02c3baeb2e94c6955df43bc15 Mon Sep 17 00:00:00 2001 From: satabol Date: Sat, 23 Sep 2023 11:01:47 +0300 Subject: [PATCH 3/3] If no faces then backface culling do not show anything. Fixed --- utils/sv_idx_viewer28_draw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/sv_idx_viewer28_draw.py b/utils/sv_idx_viewer28_draw.py index bd219af9c3..6a3694f97e 100644 --- a/utils/sv_idx_viewer28_draw.py +++ b/utils/sv_idx_viewer28_draw.py @@ -310,7 +310,7 @@ 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 display_vert_index: