-
-
Save zeffii/4568f4a56c801c4fb42c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import bmesh | |
import mathutils | |
from mathutils import Vector, Matrix | |
from bpy.props import BoolProperty, FloatVectorProperty | |
from node_s import * | |
from util import * | |
def default_mesh(name): | |
verts = [(1, 1, -1), (1, -1, -1), (-1, -1, -1)] | |
faces = [(0, 1, 2)] | |
mesh_data = bpy.data.meshes.new(name) | |
mesh_data.from_pydata(verts, [], faces) | |
mesh_data.update() | |
return mesh_data | |
def bmesh_from_pydata(verts=[], edges=[], faces=[]): | |
''' verts is necessary, edges/faces are optional ''' | |
if not verts: | |
print("verts data seems empty") | |
return | |
bm = bmesh.new() | |
[bm.verts.new(co) for co in verts] | |
bm.verts.index_update() | |
if faces: | |
for face in faces: | |
bm.faces.new(tuple(bm.verts[i] for i in face)) | |
bm.faces.index_update() | |
if edges: | |
for edge in edges: | |
edge_seq = tuple(bm.verts[i] for i in edge) | |
try: | |
bm.edges.new(edge_seq) | |
except ValueError: | |
# edge exists! | |
pass | |
bm.edges.index_update() | |
return bm | |
def make_bmesh_geometry(context, verts, edges, faces, matrix, name): | |
# no verts. no processing. | |
if not verts: | |
return | |
scene = context.scene | |
meshes = bpy.data.meshes | |
objects = bpy.data.objects | |
if name in objects: | |
sv_object = objects[name] | |
else: | |
temp_mesh = default_mesh(name) | |
sv_object = objects.new(name, temp_mesh) | |
scene.objects.link(sv_object) | |
scene.update() | |
# definitely verts, definitely do something. | |
bm = bmesh_from_pydata(verts, edges, faces) | |
# Finish up, write the bmesh back to the mesh | |
bm.to_mesh(sv_object.data) | |
bm.free() # free and prevent further access | |
# apply matrices if necessary | |
if matrix: | |
sv_object.data.transform(matrix) | |
class BmeshViewerNode(Node, SverchCustomTreeNode): | |
bl_idname = 'BmeshViewerNode' | |
bl_label = 'Bmesh Viewer Draw' | |
bl_icon = 'OUTLINER_OB_EMPTY' | |
activate = BoolProperty( | |
name='Show', description='Activate node?', | |
default=True, | |
update=updateNode) | |
def init(self, context): | |
self.inputs.new('VerticesSocket', 'vertices', 'vertices') | |
self.inputs.new('StringsSocket', 'edges', 'edges') | |
self.inputs.new('StringsSocket', 'faces', 'faces') | |
self.inputs.new('MatrixSocket', 'matrix', 'matrix') | |
def draw_buttons(self, context, layout): | |
row = layout.row(align=True) | |
row.prop(self, "activate", text="Show") | |
def get_geometry_from_sockets(self): | |
inputs = self.inputs | |
iv_links = inputs['vertices'].links | |
mverts, medges, mfaces, mmatrix = [], [], [], [] | |
# gather vertices from input | |
if isinstance(iv_links[0].from_socket, VerticesSocket): | |
propv = SvGetSocketAnyType(self, inputs['vertices']) | |
mverts = dataCorrect(propv) | |
# matrix might be operating on vertices, check and act on. | |
if 'matrix' in inputs: | |
im_links = inputs['matrix'].links | |
# find the transform matrices | |
if im_links and isinstance(im_links[0].from_socket, MatrixSocket): | |
propm = SvGetSocketAnyType(self, inputs['matrix']) | |
mmatrix = dataCorrect(propm) | |
data_feind = [] | |
for socket in ['edges', 'faces']: | |
try: | |
propm = SvGetSocketAnyType(self, inputs[socket]) | |
input_stream = dataCorrect(propm) | |
except: | |
input_stream = [] | |
finally: | |
data_feind.append(input_stream) | |
return mverts, data_feind[0], data_feind[1], mmatrix | |
def get_structure(self, stype, sindex): | |
if not stype: | |
return [] | |
try: | |
j = stype[sindex] | |
except IndexError: | |
j = [] | |
finally: | |
return j | |
def update(self): | |
if not self.activate or not ('vertices' in self.inputs): | |
return | |
C = bpy.context | |
r = self.get_geometry_from_sockets() | |
mverts, medges, mfaces, mmatrix = r | |
for obj_index, Verts in enumerate(mverts): | |
Edges = self.get_structure(medges, obj_index) | |
Faces = self.get_structure(mfaces, obj_index) | |
matrix = self.get_structure(mmatrix, obj_index) | |
mesh_name = "svBaseMesh_" + str(obj_index) | |
make_bmesh_geometry(C, Verts, Edges, Faces, matrix, mesh_name) | |
def update_socket(self, context): | |
self.update() | |
def register(): | |
bpy.utils.register_class(BmeshViewerNode) | |
def unregister(): | |
bpy.utils.unregister_class(BmeshViewerNode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment