備忘錄_20160105(定位) 修改 回首頁

程式 2021-02-08 22:52:34 1612795954 100
blender 2.91.2 python 新增一個mesh、新增一個彎曲的水溝(右上角)

blender 2.91.2 python 新增一個mesh、新增一個彎曲的水溝(右上角)

#新增一個mesh
import bpy

verts1=[
  (0,0,0),
  (1,0,0),
  (1,1,0),
  (0,1,0)
]

edges1=[]
faces1=[[0,1,2,3]]

mesh1=bpy.data.meshes.new('mesh1')
mesh1.from_pydata(verts1, edges1, faces1)
mesh1.update()

object1=bpy.data.objects.new('object1',mesh1)

# 用舊的 collection
collection1=bpy.data.collections[0]

# 用新的 collection
#collection1=bpy.data.collections.new('collection1')
#bpy.context.scene.collection.children.link(collection1)

collection1.objects.link(object1)

# 新增一個彎曲的水溝(右上角)
import bpy
import math

# 畫出來的 mesh 將在 (0,0)-(width,height) 且 z=0 的平面上
width=5
height=7
tubeWidth=0.8

# 請自己指定 outerR,這邊只是一個自動抓 outerR 最大值而已
# outerR=3
outerR=min(width,height)

innerR=outerR-tubeWidth
steps=40 # 彎曲部分的切割數目

# 點
verts2=[]

# 外圈
verts2.append((0,height,0))

for index in range(steps):
  degree=90*(1-(index/(steps-1)))
  theta=math.pi/180*degree
  x=width-outerR+outerR*math.cos(theta)
  y=height-outerR+outerR*math.sin(theta)
  verts2.append((x,y,0))

verts2.append((width,0,0))

# 內圈
verts2.append((0,height-tubeWidth,0))

for index in range(steps):
  degree=90*(1-(index/(steps-1)))
  theta=math.pi/180*degree
  x=width-outerR+innerR*math.cos(theta)
  y=height-outerR+innerR*math.sin(theta)
  verts2.append((x,y,0))

verts2.append((width-tubeWidth,0,0))

# 線

edges2=[]

# 面

faces2=[]

for index in range(steps+1):
  idx0=index
  idx1=idx0+1
  idx2=idx0+steps+3
  idx3=idx0+steps+2
  faces2.append([idx0,idx1,idx2,idx3])

# 加入 mesh
mesh2=bpy.data.meshes.new('mesh2')
mesh2.from_pydata(verts2, edges2, faces2)
mesh2.update()

object2=bpy.data.objects.new('object2',mesh2)
bpy.data.collections[0].objects.link(object2)