def split_by_vertices(verts, edges=None, faces=None, selected_verts: List[bool] = None, face_data=None): """it ignores edges for now""" edges = edges or [] faces = faces or [] if not selected_verts: selected_verts = [True] * len(verts) elif len(selected_verts) != len(verts): selected_verts = list(fixed_iter(selected_verts, len(verts))) out_verts = [] out_faces = [] old_new_verts: Dict[int, int] = dict() for face in faces: new_face = [] for i in face: if selected_verts[i]: out_verts.append(verts[i]) new_face.append(len(out_verts) - 1) else: if i in old_new_verts: new_face.append(old_new_verts[i]) else: out_verts.append(verts[i]) old_new_verts[i] = len(out_verts) - 1 new_face.append(len(out_verts) - 1) out_faces.append(new_face) out_edges = polygons_to_edges_np([out_faces], unique_edges=True)[0] return out_verts, out_edges, out_faces, face_data
def process(self): if not self.outputs[0].is_linked: return polygons_ = self.inputs['pols'].sv_get(deepcopy=False) polygons = dataCorrect_np(polygons_) self.outputs['edgs'].sv_set(polygons_to_edges_np(polygons, self.unique_edges, self.output_numpy))
def process(self): if not self.outputs[0].is_linked: return polygons_ = self.inputs['pols'].sv_get(deepcopy=False) polygons = dataCorrect_np(polygons_) if self.output_numpy or isinstance(polygons[0], ndarray) or self.regular_pols: result = polygons_to_edges_np(polygons, self.unique_edges, self.output_numpy) else: result = polygons_to_edges(polygons, self.unique_edges) self.outputs['edgs'].sv_set(result)
def process(self): n_id = node_id(self) nvBGL.callback_disable(n_id) inputs = self.inputs # end early if not self.activate: return if self.mode == 'Number': if not inputs['Number'].is_linked: return numbers = inputs['Number'].sv_get(default=[[]]) elif self.mode == 'Curve': if not inputs['Curve'].is_linked: return curves = inputs['Curve'].sv_get(default=[[]]) else: if not inputs['Vecs'].is_linked: return vecs = inputs['Vecs'].sv_get(default=[[]]) edges = inputs['Edges'].sv_get(default=[[]]) polygons = inputs['Polygons'].sv_get(default=[[]]) vector_color = inputs['Vector Color'].sv_get( default=[[self.vector_color]]) edge_color = inputs['Edge Color'].sv_get(default=[[self.edge_color]]) poly_color = inputs['Polygon Color'].sv_get( default=[[self.polygon_color]]) seed_set(self.random_seed) config = self.create_config() config.vector_color = vector_color config.edge_color = edge_color config.poly_color = poly_color config.edges = edges if self.mode == 'Number': config.size = self.drawing_size geom = generate_number_geom(config, numbers) elif self.mode == 'Path': geom = generate_graph_geom(config, vecs) elif self.mode == 'Curve': paths = [] for curve in curves: t_min, t_max = curve.get_u_bounds() ts = np_linspace(t_min, t_max, num=self.curve_samples, dtype=np_float64) paths.append(curve.evaluate_array(ts).tolist()) geom = generate_graph_geom(config, paths) else: config.polygons = polygons if not inputs['Edges'].is_linked and self.edge_toggle: config.edges = polygons_to_edges_np(polygons, unique_edges=True) geom = generate_mesh_geom(config, vecs) draw_data = { 'mode': 'custom_function', 'tree_name': self.id_data.name[:], 'node_name': self.name[:], 'loc': get_drawing_location, 'custom_function': view_2d_geom, 'args': (geom, config) } nvBGL.callback_enable(n_id, draw_data)
def process(self): if bpy.app.background: return self.handle_attr_socket() if not (self.id_data.sv_show and self.activate): callback_disable(node_id(self)) return n_id = node_id(self) callback_disable(n_id) inputs = self.inputs # end early if not self.activate: return if not any([inputs['Vertices'].is_linked, inputs['Matrix'].is_linked]): return if inputs['Vertices'].is_linked: vecs = inputs['Vertices'].sv_get(deepcopy=False, default=[[]]) total_verts = sum(map(len, vecs)) if not total_verts: raise LookupError("Empty vertices list") edges = inputs['Edges'].sv_get(deepcopy=False, default=[[]]) polygons = inputs['Polygons'].sv_get(deepcopy=False, default=[[]]) matrix = inputs['Matrix'].sv_get(deepcopy=False, default=[[]]) vector_color = inputs['Vector Color'].sv_get(deepcopy=False, default=[[self.vector_color]]) edge_color = inputs['Edge Color'].sv_get(deepcopy=False, default=[[self.edge_color]]) poly_color = inputs['Polygon Color'].sv_get(deepcopy=False, default=[[self.polygon_color]]) seed_set(self.random_seed) config = self.create_config() config.vector_color = vector_color config.edge_color = edge_color config.poly_color = poly_color config.edges = edges if self.use_dashed: add_dashed_shader(config) config.polygons = polygons config.matrix = matrix if not inputs['Edges'].is_linked and self.display_edges: config.edges = polygons_to_edges_np(polygons, unique_edges=True) geom = generate_mesh_geom(config, vecs) draw_data = { 'tree_name': self.id_data.name[:], 'custom_function': view_3d_geom, 'args': (geom, config) } callback_enable(n_id, draw_data) elif inputs['Matrix'].is_linked: matrices = inputs['Matrix'].sv_get(deepcopy=False, default=[Matrix()]) gl_instructions = { 'tree_name': self.id_data.name[:], 'custom_function': draw_matrix, 'args': (matrices, self.matrix_draw_scale)} callback_enable(n_id, gl_instructions)
def process(self): verts = self.inputs['Vertices'].sv_get(default=[]) edges = self.inputs['Edges'].sv_get(default=[]) faces = self.inputs['Faces'].sv_get(default=[]) e_mask = self.inputs['EdgeMask'].sv_get(deepcopy=False, default=[[True]]) factor = self.inputs['Factor'].sv_get(deepcopy=False) cuts = self.inputs['Cuts'].sv_get(deepcopy=False) if faces and not edges: edges = polygons_to_edges_np(faces, True, False) obj_n = max(len(verts), len(e_mask), len(factor), len(cuts)) out_v = [] out_e = [] out_f = [] def vec(arr): return fixed_iter(arr, obj_n, []) for v, e, face, m, fact, c in zip(vec(verts), vec(edges), vec(faces), vec(e_mask), vec(factor), vec(cuts)): if not all((v, e)): break new_faces = list(face) faces_per_edge = defaultdict(list) for face_idx, f in enumerate(new_faces): for i, j in zip(f, rotate_list(f)): faces_per_edge[(i, j)].append((i, False, face_idx)) faces_per_edge[(j, i)].append((j, True, face_idx)) def insert_after(_face, _vert_idx, _new_vert_idx): idx = _face.index(_vert_idx) _face.insert(idx + 1, _new_vert_idx) def insert_before(_face, _vert_idx, _new_vert_idx): idx = _face.index(_vert_idx) _face.insert(idx, _new_vert_idx) # sanitize the input input_f = list(map(lambda _f: min(1, max(0, _f)), fact)) counts = repeat_last_for_length(c, len(e)) mask = repeat_last_for_length(m, len(e)) params = match_long_repeat([e, mask, input_f, counts]) offset = len(v) new_verts = list(v) new_edges = [] i = 0 for edge, ok, factor, count in zip(*params): if not ok: new_edges.append(edge) continue i0 = edge[0] i1 = edge[1] v0 = v[i0] v1 = v[i1] if self.mode == 'MIRROR': factor = factor / 2 vx = v0[0] * (1 - factor) + v1[0] * factor vy = v0[1] * (1 - factor) + v1[1] * factor vz = v0[2] * (1 - factor) + v1[2] * factor va = [vx, vy, vz] new_verts.append(va) vx = v0[0] * factor + v1[0] * (1 - factor) vy = v0[1] * factor + v1[1] * (1 - factor) vz = v0[2] * factor + v1[2] * (1 - factor) vb = [vx, vy, vz] new_verts.append(vb) new_edges.append([i0, offset + i]) # v0 - va new_edges.append([offset + i, offset + i + 1]) # va - vb new_edges.append([offset + i + 1, i1]) # vb - v1 for vert_idx, before, face_idx in faces_per_edge[tuple( edge)]: if before: insert_before(new_faces[face_idx], vert_idx, offset + i) insert_before(new_faces[face_idx], offset + i, offset + i + 1) else: insert_after(new_faces[face_idx], vert_idx, offset + i) insert_after(new_faces[face_idx], offset + i, offset + i + 1) i = i + 2 elif self.mode == 'SIMPLE': vx = v0[0] * (1 - factor) + v1[0] * factor vy = v0[1] * (1 - factor) + v1[1] * factor vz = v0[2] * (1 - factor) + v1[2] * factor va = [vx, vy, vz] new_verts.append(va) new_edges.append([i0, offset + i]) # v0 - va new_edges.append([offset + i, i1]) # va - v1 for vert_idx, before, face_idx in faces_per_edge[tuple( edge)]: if before: insert_before(new_faces[face_idx], vert_idx, offset + i) else: insert_after(new_faces[face_idx], vert_idx, offset + i) i = i + 1 else: # MULTI if count > 0: new_vert_idxs = [] j = offset + i for p in np.linspace(0.0, 1.0, num=count + 1, endpoint=False)[1:]: vx = v0[0] * (1 - p) + v1[0] * p vy = v0[1] * (1 - p) + v1[1] * p vz = v0[2] * (1 - p) + v1[2] * p va = [vx, vy, vz] new_verts.append(va) new_vert_idxs.append(j) j += 1 if new_vert_idxs: vert_idxs = [i0] + new_vert_idxs[:] edges = list(zip(vert_idxs, vert_idxs[1:])) edges.append((vert_idxs[-1], i1)) new_edges.extend(edges) for vert_idx, before, face_idx in faces_per_edge[ tuple(edge)]: prev_vert_idx = vert_idx for new_vert_idx in new_vert_idxs: if before: insert_before(new_faces[face_idx], prev_vert_idx, new_vert_idx) else: insert_after(new_faces[face_idx], prev_vert_idx, new_vert_idx) prev_vert_idx = new_vert_idx else: new_edges.append(edge) i = i + count out_v.append(new_verts) out_e.append(new_edges) out_f.append(new_faces) self.outputs['Vertices'].sv_set(out_v) self.outputs['Edges'].sv_set(out_e) self.outputs['Faces'].sv_set(out_f)
def process_data(self, params): outputs = self.outputs mesh_s = params[0] vertices_out, verts_normals_out, verts_colors_out = [], [], [] edges_out = [] faces_out, f_normals_out, f_centers_out, f_areas_out = [], [], [], [] uv_verts_out, uv_faces_out, material_id_out = [], [], [] for mesh in mesh_s: if outputs['Vertices'].is_linked: vertices_out.append( np.asarray(mesh.vertices) if self.out_np[0] else np. asarray(mesh.vertices).tolist()) if (outputs['Faces'].is_linked or outputs['Edges'].is_linked) and mesh.has_triangles(): tris = np.asarray(mesh.triangles) if outputs['Edges'].is_linked: edges = polygons_to_edges_np( [tris], unique_edges=True, output_numpy=self.out_np[1])[0] edges_out.append(edges) if outputs['Faces'].is_linked: faces_out.append( tris if self.out_np[2] else tris.tolist()) else: faces_out.append([]) edges_out.append([]) needs_v_normal_calc = outputs[ 'Vertex Normal'].is_linked and not mesh.has_vertex_normals( ) needs_f_normal_calc = needs_v_normal_calc or ( outputs['Face Normal'].is_linked and not mesh.has_triangle_normals()) if needs_f_normal_calc: if needs_v_normal_calc: face_normals, v_normals = calc_normals(mesh, v_normals=True) else: face_normals = calc_normals(mesh, v_normals=False) if outputs['Vertex Normal'].is_linked: if mesh.has_vertex_normals(): verts_normals_out.append( np.asarray(mesh.vertex_normals) if self.out_np[3] else np.asarray(mesh.vertex_normals).tolist()) else: verts_normals_out.append(v_normals if self.out_np[3] else v_normals.tolist()) if outputs[ 'Vertex Color'].is_linked and mesh.has_vertex_colors(): colors = np.asarray(mesh.vertex_colors) colors_a = np.ones((colors.shape[0], 4)) colors_a[:, :3] = colors verts_colors_out.append( colors_a if self.out_np[4] else colors_a.tolist()) else: verts_colors_out.append([]) if outputs['Face Normal'].is_linked: if mesh.has_triangle_normals(): f_normals_out.append( np.asarray(mesh.triangle_normals) if self.out_np[5] else np.asarray(mesh.triangle_normals).tolist()) else: f_normals_out.append(face_normals if self.out_np[5] else face_normals.tolist()) if outputs['Face Center'].is_linked: f_centers_out.append( calc_centers(mesh, output_numpy=self.out_np[6])) if outputs['Face Area'].is_linked: f_areas_out.append( calc_mesh_tris_areas(mesh, output_numpy=self.out_np[7])) if mesh.has_triangle_uvs() and (outputs['UV Verts'].is_linked or outputs['UV Faces'].is_linked): uvs = np.asarray(mesh.triangle_uvs) new_uvs = np.zeros((uvs.shape[0], 3), dtype='float') new_uvs[:, :2] = uvs uv_verts_out.append( new_uvs if self.out_np[8] else new_uvs.tolist()) uv_faces = np.arange(new_uvs.shape[0], dtype='int').reshape(-1, 3) uv_faces_out.append( uv_faces if self.out_np[9] else uv_faces.tolist()) else: uv_verts_out.append([]) uv_faces_out.append([]) if mesh.has_triangle_material_ids( ) and self.outputs['Material Id'].is_linked: material_id_out.append( np.asarray(mesh.triangle_material_ids) if self. out_np[10] else np.asarray(mesh.triangle_material_ids ).tolist()) else: material_id_out.append([]) return (vertices_out, edges_out, faces_out, verts_normals_out, verts_colors_out, f_normals_out, f_centers_out, f_areas_out, uv_verts_out, uv_faces_out, material_id_out)