def process(self): outputs = self.outputs if not outputs['C'].is_linked: return inputs = self.inputs id_mat = Matrix_listing([Matrix.Identity(4)]) A = Matrix_generate(inputs['A'].sv_get(default=id_mat)) if self.operation in {"MULTIPLY"}: # two matrix inputs available B = Matrix_generate(inputs['B'].sv_get(default=id_mat)) if self.prePost == "PRE": parameters = match_long_repeat([A, B]) else: parameters = match_long_repeat([B, A]) else: # one matrix input available parameters = [A] operation = self.get_operation() matrixList = [] for params in zip(*parameters): c = operation(*params) matrixList.append(c) matrices = Matrix_listing(matrixList) outputs['C'].sv_set(matrices)
def process(self): if not self.inputs['Vertices'].is_linked: return if not any(s.is_linked for s in self.outputs): return has_mat_out = bool(self.outputs['Center'].is_linked) has_mean = bool(self.outputs['Mean'].is_linked) has_vert_out = bool(self.outputs['Vertices'].is_linked) vert = self.inputs['Vertices'].sv_get(deepcopy=False) vert = dataCorrect(vert, nominal_dept=2) if vert: verts_out = [] edges_out = [] edges = [ (0, 1), (1, 3), (3, 2), (2, 0), # bottom edges (4, 5), (5, 7), (7, 6), (6, 4), # top edges (0, 4), (1, 5), (2, 6), (3, 7) # sides ] mat_out = [] mean_out = [] for v in vert: if has_mat_out or has_vert_out: maxmin = list(zip(map(max, *v), map(min, *v))) out = list(product(*reversed(maxmin))) verts_out.append([l[::-1] for l in out[::-1]]) edges_out.append(edges) if has_mat_out: center = [(u + v) * .5 for u, v in maxmin] mat = Matrix.Translation(center) scale = [(u - v) * .5 for u, v in maxmin] for i, s in enumerate(scale): mat[i][i] = s mat_out.append(mat) if has_mean: avr = list(map(sum, zip(*v))) avr = [n / len(v) for n in avr] mean_out.append([avr]) if has_vert_out: self.outputs['Vertices'].sv_set(verts_out) if self.outputs['Edges'].is_linked: self.outputs['Edges'].sv_set(edges_out) if has_mean: self.outputs['Mean'].sv_set(mean_out) if self.outputs['Center'].is_linked: self.outputs['Center'].sv_set(Matrix_listing(mat_out))
def process(self): if not self.outputs['Matrix'].is_linked: return loc_ = self.inputs['Location'].sv_get() loc = Vector_generate(loc_) scale_ = self.inputs['Scale'].sv_get() scale = Vector_generate(scale_) rot_ = self.inputs['Rotation'].sv_get() rot = Vector_generate(rot_) rotA = [[]] angle = [[0.0]] # it isn't a good idea to hide things like this if self.inputs['Angle'].is_linked: other = get_other_socket(self.inputs['Angle']) if isinstance(other, StringsSocket): angle = self.inputs['Angle'].sv_get() elif isinstance(other, VerticesSocket): rotA_ = self.inputs['Angle'].sv_get() rotA = Vector_generate(rotA_) max_l = max(len(loc[0]), len(scale[0]), len(rot[0]), len(angle[0]), len(rotA[0])) orig = [] for l in range(max_l): M = mathutils.Matrix() orig.append(M) matrixes_ = matrixdef(orig, loc, scale, rot, angle, rotA) matrixes = Matrix_listing(matrixes_) SvSetSocketAnyType(self, 'Matrix', matrixes)
def process(self): if not self.outputs['Matrix'].is_linked: return # inputs factor1 = self.inputs['Factor1'].sv_get() factor2 = self.inputs['Factor2'].sv_get() # outputs max_l = max(len(factor1), len(factor2)) fullList(factor1, max_l) fullList(factor2, max_l) matrixes_ = [] for i in range(max_l): max_inner = max(len(factor1[i]), len(factor2[i])) fullList(factor1[i], max_inner) fullList(factor2[i], max_inner) for j in range(max_inner): matrixes_.append( Matrix.Shear(self.plane_, 4, (factor1[i][j], factor2[i][j]))) matrixes = Matrix_listing(matrixes_) self.outputs['Matrix'].sv_set(matrixes)
def process(self): O, L, S, R, A = self.inputs Om = self.outputs[0] if Om.is_linked: if O.is_linked: orig = Matrix_generate(O.sv_get()) else: return if L.is_linked: loc = Vector_generate(L.sv_get()) else: loc = [[]] if S.is_linked: scale = Vector_generate(S.sv_get()) else: scale = [[]] if R.is_linked: rot = Vector_generate(R.sv_get()) else: rot = [[]] rotA, angle = [[]], [[0.0]] # ability to add vector & vector difference instead of only rotation values if A.is_linked: if A.links[0].from_socket.bl_idname == 'VerticesSocket': rotA = Vector_generate(A.sv_get()) angle = [[]] else: angle = A.sv_get() rotA = [[]] matrixes_ = matrixdef(orig, loc, scale, rot, angle, rotA) matrixes = Matrix_listing(matrixes_) Om.sv_set(matrixes)
def sv_main(rseed=21, width=21, height=11, scale=1.0, braid=0.0): in_sockets = [['s', 'rseed', rseed], ['s', 'width', width], ['s', 'height', height], ['s', 'scale', scale], ['s', 'braid', braid]] maze_3d.random.seed(rseed) grid = SvGrid(width, height) grid = maze_3d.initRecursiveBacktrackerMaze(grid) grid.braid(braid) print(grid) mats, mask = grid.pathMatrices() #scale locations for m in mats: for i in range(3): m[i][3] = m[i][3] * scale mat_out = Matrix_listing(mats) out_sockets = [['m', 'matrices', mat_out], ['s', 'mask', [mask]]] return in_sockets, out_sockets
def sv_main(rseed=21, sizeX=4, sizeY=4, sizeZ=4, scaleXY=1.0, scaleZ=1.0, braid=0.0): in_sockets = [['s', 'rseed', rseed], ['s', 'size X', sizeX], ['s', 'size Y', sizeY], ['s', 'size Z', sizeZ], ['s', 'scale XY', scaleXY], ['s', 'scale Z', scaleZ], ['s', 'braid', braid]] maze_3d.random.seed(rseed) grid = SvGrid(sizeZ, sizeX, sizeY) grid = maze_3d.initRecursiveBacktrackerMaze(grid) grid.braid(braid) #print(grid) mats, mask = grid.pathMatrices() #scale locations for m in mats: for i in range(2): m[i][3] = m[i][3] * scaleXY m[2][3] = m[2][3] * scaleZ mat_out = Matrix_listing(mats) out_sockets = [['m', 'matrices', mat_out], ['s', 'mask', [mask]]] return in_sockets, out_sockets
def process(self): L, S, R, A = self.inputs Ma = self.outputs[0] if not Ma.is_linked: return loc = Vector_generate(L.sv_get()) scale = Vector_generate(S.sv_get()) rot = Vector_generate(R.sv_get()) rotA, angle = [[]], [[0.0]] # ability to add vector & vector difference instead of only rotation values if A.is_linked: if A.links[0].from_socket.bl_idname == 'VerticesSocket': rotA = Vector_generate(A.sv_get()) angle = [[]] else: angle = A.sv_get() rotA = [[]] max_l = max(len(loc[0]), len(scale[0]), len(rot[0]), len(angle[0]), len(rotA[0])) orig = [] for l in range(max_l): M = mathutils.Matrix() orig.append(M) matrixes_ = matrixdef(orig, loc, scale, rot, angle, rotA) matrixes = Matrix_listing(matrixes_) Ma.sv_set(matrixes)
def process(self): outputs = self.outputs if not any(s.is_linked for s in outputs): return I = [] # collect the inputs from the connected sockets for s in filter(lambda s: s.is_linked, self.inputs): I.append([Matrix(m) for m in s.sv_get(default=id_mat)]) operation = self.get_operation() if self.operation in {"MULTIPLY"}: # multiple input operations if self.prePost == "PRE": # A op B : keep input order parameters = match_long_repeat(I) else: # B op A : reverse input order parameters = match_long_repeat(I[::-1]) matrixList = [operation(params) for params in zip(*parameters)] matrices = Matrix_listing(matrixList) outputs['C'].sv_set(matrices) else: # single input operations parameters = I[0] print("parameters=", parameters) if self.operation == "BASIS": xList = [] yList = [] zList = [] for a in parameters: Rx, Ry, Rz = operation(a) xList.append(Rx) yList.append(Ry) zList.append(Rz) outputs['X'].sv_set(xList) outputs['Y'].sv_set(yList) outputs['Z'].sv_set(zList) matrices = Matrix_listing(parameters) outputs['C'].sv_set(matrices) else: # INVERSE / FILTER matrixList = [operation(a) for a in parameters] matrices = Matrix_listing(matrixList) outputs['C'].sv_set(matrices)
def get_all(data): for item in data: if isinstance(item, (tuple, list)) and len(item) == 4 and isinstance( item[0], (float, int)): mat = Quaternion(item).to_matrix().to_4x4() collect_matrix(Matrix_listing([mat])[0]) else: get_all(item)
def bp_verts_edges_n(self): """ returns branchpoints verts as a list of positions and edges as index to connect the branch points and leaves matrices """ verts = [] edges = [] ends = [] ends_inds = [] for i, b in enumerate(self.bpalln): bp_parent = self.bpp[i] verts.append(list(b)) if bp_parent != None: edges.append((bp_parent, i)) if self.bpc[i] == 0: ends.append(True) ends_inds.append(i) else: ends.append(False) process = ends_inds # branch radii br = [int(t) for t in ends] finished = [] while len(process) > 0: process.sort() i = process.pop() finished.append(i) p = self.bpp[i] if p != None: br[p] = br[p] + br[i] if p not in process: if p not in finished: process.insert(0, p) mats = [] for edge in edges: if ends[edge[1]]: #calculate leaf directions #end will always be edge[1] v0 = Vector(verts[edge[0]]) v1 = Vector(verts[edge[1]]) dir1 = (v1 - v0).normalized() dir2 = (dir1.cross(Vector((0.0, 0.0, 1.0)))).normalized() dir3 = -(dir1.cross(dir2)).normalized() m = Matrix.Identity(4) m[0][0:3] = dir1 m[1][0:3] = dir2 m[2][0:3] = dir3 m[3][0:3] = v1 m.transpose() mats.append(m) mats_out = Matrix_listing(mats) return verts, edges, ends, br, mats_out
def process(self): self.read_xml() self.make_sockets() self.xml_text_format() if not hasattr(self, 'xml_tree'): return if any(output.is_linked for output in self.outputs): lsys = LSystem(self.xml_tree, self.maxmats) shapes = lsys.evaluate(seed=self.rseed) mat_sublist = [] edges_out = [] verts_out = [] faces_out = [] # make last entry in shapes None # to allow make tube to finish last tube if shapes[-1]: shapes.append(None) # dictionary for matrix lists shape_names = set([ x.attrib.get('shape') for x in self.xml_tree.iter('instance') ]) mat_dict = {s: [] for s in shape_names} if self.inputs['Vertices'].is_linked: verts = Vector_generate( SvGetSocketAnyType(self, self.inputs['Vertices'])) for i, shape in enumerate(shapes): if shape: mat_sublist.append(shape[1]) mat_dict[shape[0]].append(shape[1]) else: if len(mat_sublist) > 0: if self.inputs['Vertices'].is_linked: v, e, f = lsys.make_tube(mat_sublist, verts) if v: verts_out.append(v) edges_out.append(e) faces_out.append(f) mat_sublist = [] if self.outputs['Vertices'].is_linked: SvSetSocketAnyType(self, 'Vertices', verts_out) if self.outputs['Edges'].is_linked: SvSetSocketAnyType(self, 'Edges', edges_out) if self.outputs['Faces'].is_linked: SvSetSocketAnyType(self, 'Faces', faces_out) for shape in shape_names: if self.outputs[shape].is_linked: SvSetSocketAnyType(self, shape, Matrix_listing(mat_dict[shape]))
def process(self): # inputs if 'Matrix' in self.outputs and self.outputs['Matrix'].links: if self.inputs['Original'].links and \ type(self.inputs['Original'].links[0].from_socket) == MatrixSocket: orig_ = SvGetSocketAnyType(self, self.inputs['Original']) orig = Matrix_generate(orig_) else: return if 'Location' in self.inputs and self.inputs['Location'].links and \ type(self.inputs['Location'].links[0].from_socket) == VerticesSocket: loc_ = SvGetSocketAnyType(self, self.inputs['Location']) loc = Vector_generate(loc_) else: loc = [[]] if 'Scale' in self.inputs and self.inputs['Scale'].links and \ type(self.inputs['Scale'].links[0].from_socket) == VerticesSocket: scale_ = SvGetSocketAnyType(self, self.inputs['Scale']) scale = Vector_generate(scale_) else: scale = [[]] if 'Rotation' in self.inputs and self.inputs['Rotation'].links and \ type(self.inputs['Rotation'].links[0].from_socket) == VerticesSocket: rot_ = SvGetSocketAnyType(self, self.inputs['Rotation']) rot = Vector_generate(rot_) #print ('matrix_def', str(rot_)) else: rot = [[]] rotA = [[]] angle = [[0.0]] if 'Angle' in self.inputs and self.inputs['Angle'].links: if type(self.inputs['Angle'].links[0].from_socket ) == StringsSocket: angle = SvGetSocketAnyType(self, self.inputs['Angle']) elif type(self.inputs['Angle'].links[0].from_socket ) == VerticesSocket: rotA_ = SvGetSocketAnyType(self, self.inputs['Angle']) rotA = Vector_generate(rotA_) # outputs #print(loc) matrixes_ = matrixdef(orig, loc, scale, rot, angle, rotA) matrixes = Matrix_listing(matrixes_) SvSetSocketAnyType(self, 'Matrix', matrixes)
def process(self): if not self.outputs['Matrix'].is_linked: return inputs = self.inputs param = [s.sv_get()[0] for s in inputs] mats = [] for angles in zip(*match_long_repeat(param)): a_r = [radians(x) for x in angles] mat = Euler(a_r, self.order).to_matrix().to_4x4() mats.append(mat) self.outputs['Matrix'].sv_set(Matrix_listing(mats))
def sv_main(verts=[], polygons=[], matrix=[], ver=3, hor=3): in_sockets = [['v', 'verts', verts], ['s', 'polygons', polygons], ['m', 'matrix', matrix], ['s', 'vertical', ver], ['s', 'horisontal', hor]] verts_out = [] matrixes = [] ver = max(3, ver) hor = max(3, hor) def out_sockets(): return [['v', 'verts_out', verts_out], ['m', 'matrixes', matrixes]] if not all([verts, polygons]): return in_sockets, out_sockets() def make_centers(v, p, m): out = [] matrixes = [] vlh = v[pol[1]] - v[pol[0]] vlv = v[pol[3]] - v[pol[0]] for i in range(hor - 1): per_h = (1 / hor) * (i + 1) for k in range(ver - 1): per_v = (1 / ver) * (k + 1) v_loc = v[pol[0]] + vlh * per_h + vlv * per_v m.translation = v_loc matrixes.append(m.copy()) out.append(v_loc) return out, matrixes # paradigm change verts = Vector_generate(verts) matrix = Matrix_generate(matrix) centers = [] for p, v in zip(polygons, verts): centers_ = [] for pol, m in zip(p, matrix): cout, mout = make_centers(v, pol, m) centers_.extend(cout) matrixes.extend(mout) centers.append(centers_) #print(centers,matrixes) #bpy.data.shape_keys['Key'].key_blocks['Key 1'].value verts_out = Vector_degenerate(centers) matrixes = Matrix_listing(matrixes) return in_sockets, out_sockets()
def process(self): # inputs if not self.outputs['C'].is_linked: return id_mat = Matrix_listing([Matrix.Identity(4)]) A = Matrix_generate(self.inputs['A'].sv_get(default=id_mat)) B = Matrix_generate(self.inputs['B'].sv_get(default=id_mat)) factor = self.inputs['Factor'].sv_get() matrixes_ = [] # match inputs, first matrix A and B using fullList # then extend the factor list if necessary, # A and B should control length of list, not interpolation lists max_l = max(len(A), len(B)) fullList(A, max_l) fullList(B, max_l) if len(factor) < max_l: fullList(factor, max_l) for i in range(max_l): for k in range(len(factor[i])): matrixes_.append(A[i].lerp(B[i], factor[i][k])) matrixes = Matrix_listing(matrixes_) self.outputs['C'].sv_set(matrixes)
def sv_main(rseed=21): in_sockets = [['s', 'rseed', rseed]] imp.reload(GA_xml) tree = GA_xml.Library["Default"] lsys = LSystem.LSystem(tree, max_mats) shapes = lsys.evaluate(seed=rseed) mats = [shape[1] for shape in shapes if shape] mat_out = Matrix_listing(mats) out_sockets = [['m', 'matrices', mat_out]] return in_sockets, out_sockets
def process(self): if 'Matrix' in self.outputs and self.outputs['Matrix'].is_linked: m_out = Matrix_listing([self.matrix]) self.outputs[0].sv_set(m_out)
from mathutils import Matrix from functools import reduce from sverchok.node_tree import SverchCustomTreeNode, MatrixSocket, StringsSocket from sverchok.data_structure import (updateNode, match_long_repeat, Matrix_listing, Matrix_generate) operationItems = [("MULTIPLY", "Multiply", "Multiply two matrices", 0), ("INVERT", "Invert", "Invert matrix", 1), ("FILTER", "Filter", "Filter matrix components", 2), ("BASIS", "Basis", "Extract Basis vectors", 3)] prePostItems = [("PRE", "Pre", "Calculate A op B", 0), ("POST", "Post", "Calculate B op A", 1)] id_mat = Matrix_listing([Matrix.Identity(4)]) ABC = tuple('ABCDEFGHIJKLMNOPQRSTUVWXYZ') class SvMatrixMathNode(bpy.types.Node, SverchCustomTreeNode): ''' Math operation on matrices ''' bl_idname = 'SvMatrixMathNode' bl_label = 'Matrix Math' bl_icon = 'OUTLINER_OB_EMPTY' def update_operation(self, context): self.label = "Matrix " + self.operation.title() self.update_sockets() updateNode(self, context) prePost = EnumProperty(
def process(self): if not self.filename: return #xml text must be an internal file if not (self.filename in bpy.data.texts): return internal_file = bpy.data.texts[self.filename] self.xml_text = internal_file.as_string() #nvars = len(set([name for text, name, spec, conv in string.Formatter().parse(self.xml_text)])) nvars = self.xml_text.count( '{') #this may be too large because of repeats if ((self.outputs['Matrices'].is_linked) or (self.outputs['Vertices'].is_linked)): slots = [] for socket in self.inputs[1:]: if socket.is_linked: slots.append(SvGetSocketAnyType(self, socket)) #flatten vars if slots: slots = list(flat(slots)) fullList(slots, nvars) else: slots = [0] * nvars self.xml_text = self.xml_text.format(*slots) lsys = LSystem(self.xml_text, self.maxmats) shapes = lsys.evaluate(seed=self.rseed) names = [shape[0] for shape in shapes if shape] #convert names to integer list iddict = {k: v for v, k in enumerate(sorted(set(names)))} mat_sublist = [] mat_list = [] mask_list = [] edges_out = [] verts_out = [] faces_out = [] #make last entry in shapes None to allow make tube to finish last tube if shapes[-1]: shapes.append(None) for i, shape in enumerate(shapes): if shape: mat_sublist.append(shape[1]) mat_list.append(shape[1]) mask_list.append(iddict[shape[0]]) else: if len(mat_sublist) > 0: if self.inputs['Vertices'].is_linked: verts = Vector_generate( SvGetSocketAnyType(self, self.inputs['Vertices'])) v, e, f = lsys.make_tube(mat_sublist, verts) if v: verts_out.append(v) edges_out.append(e) faces_out.append(f) mat_sublist = [] if self.outputs['Matrices'].is_linked: SvSetSocketAnyType(self, 'Matrices', Matrix_listing(mat_list)) if self.outputs['Mask'].is_linked: SvSetSocketAnyType(self, 'Mask', [mask_list]) if self.outputs['Vertices'].is_linked: SvSetSocketAnyType(self, 'Vertices', verts_out) if self.outputs['Edges'].is_linked: SvSetSocketAnyType(self, 'Edges', edges_out) if self.outputs['Faces'].is_linked: SvSetSocketAnyType(self, 'Faces', faces_out)
def process(self): if 'Matrix' in self.outputs and self.outputs['Matrix'].is_linked: m_out = Matrix_listing([self.matrix]) SvSetSocketAnyType(self, 'Matrix', m_out)
def sv_main(rseed=21, max_mats=5000, vars=[], verts=[]): in_sockets = [['s', 'rseed', rseed], ['s', 'max matrices', max_mats], ['s', 'variables', vars], ['v', 'Vertices', verts]] imp.reload(GA_xml) tree = GA_xml.Library["Default"] nvars = tree.count('{') #this may be too large because of repeats #flatten vars if vars: vars = list(flat(vars)) fullList(vars, nvars) else: vars = [0] * nvars tree = tree.format(*vars) lsys = LSystem.LSystem(tree, max_mats) shapes = lsys.evaluate(seed=rseed) names = [shape[0] for shape in shapes if shape] #convert names to integer list iddict = {k: v for v, k in enumerate(set(names))} mat_sublist = [] mat_list = [] mask = [] mask_sub = [] for i, shape in enumerate(shapes): if shape: mat_sublist.append(shape[1]) mask_sub.append(iddict[shape[0]]) else: if len(mat_sublist) > 0: mat_list.append(Matrix_listing(mat_sublist)) mask.append(mask_sub) mat_sublist = [] mask_sub = [] edges_out = [] verts_out = [] faces_out = [] if verts: #make tubes for sublist in mat_list: v, e, f = lsys.make_tube(sublist, verts) if v: verts_out.append(v) edges_out.append(e) faces_out.append(f) mat_out = mat_list out_sockets = [['m', 'matrices', mat_out], ['s', 'mask', mask], ['v', 'Vertices', verts_out], ['s', 'Edges', edges_out], ['s', 'Faces', faces_out]] return in_sockets, out_sockets