Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Blenderでトーラスを切断したような立体をモデリングするためのPython3のプログラム。
#!/usr/bin/env python3
#
# See https://pandanote.info/?p=7456
#
import bpy
import bmesh
import math
def verts_to_ngon(edgeslist, verts):
converted = []
verts.append(verts[0])
pos = 0
while pos < len(verts)-1:
vs = verts[pos]
ve = verts[pos+1]
for edge in edgeslist:
if (edge.verts[0] == vs and edge.verts[1] == ve) \
or (edge.verts[0] == ve and edge.verts[1] == vs):
converted.append(edge)
pos = pos + 1
return converted
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
bpy.ops.mesh.primitive_circle_add(location=(0.9, 0, 0), radius=0.3,
fill_type='NGON')
bpy.ops.transform.rotate(value=math.pi/2, orient_axis='X')
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.ops.object.modifier_add(type='SCREW')
scr = bpy.context.object.modifiers["スクリュー"]
scr.angle = math.pi*0.33333
scr.axis = 'Y'
bpy.ops.object.convert(target='MESH')
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
ccount = {}
for edge in bm.edges:
edge.verts[0].select = False
edge.verts[1].select = False
edge.select = False
if edge.verts[0] not in ccount:
ccount[edge.verts[0]] = []
ccount[edge.verts[0]].append(edge.verts[1])
if edge.verts[1] not in ccount:
ccount[edge.verts[1]] = []
ccount[edge.verts[1]].append(edge.verts[0])
candidatelist = {}
for (k, v) in ccount.items():
if len(v) <= 3:
candidatelist[k] = v
cap_a = list(candidatelist.keys())
cap_b = []
isDone = False
cap_b.append(cap_a.pop())
current = cap_b[0]
while isDone is False:
next = None
for vv in candidatelist[current]:
if vv in cap_a:
next = vv
if next is not None:
cap_b.append(next)
cap_a.remove(next)
current = next
else:
isDone = True
edge_a = verts_to_ngon(bm.edges, cap_a)
edge_aa = []
lq = int(len(edge_a)/4)
lh = int(len(edge_a)/2)
for i in range(lq):
edge_a[i].select = True
edge_aa.append(edge_a[i])
for i in range(lq):
edge_a[i+lh].select = True
edge_aa.append(edge_a[i+lh])
# bmesh.ops.grid_fill(bm, edges=edge_aa)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment