Skip to content

Instantly share code, notes, and snippets.

@botamochi6277
Created November 6, 2019 10:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save botamochi6277/196c5ccdea1db0680738ad785f5661e9 to your computer and use it in GitHub Desktop.
Blender SurfacePlot
import bpy
import numpy as np
import math
import mathutils
def WaveXY(x, y, x0=0, y0=0, period=1, a=1):
# Cos Wave
r = np.sqrt((x - x0) ** 2 + (y - y0) ** 2)
return a * np.cos(2.0 * np.pi * r / period)
# 初期化
cols = 101
rows = 101
mat = np.zeros((rows, cols))
x_array = np.linspace(0, 10, num=cols)
y_array = np.linspace(0, 10, num=rows)
verts = []
# Update Matrix
for y in range(0, rows):
for x in range(0, cols):
px = x_array[x]
py = y_array[y]
# 水面波の干渉
t = 2.5
mat[y, x] += WaveXY(px, py, x0=0, y0=0, period=t)
mat[y, x] += WaveXY(px, py, x0=10, y0=10, period=t)
verts.append(mathutils.Vector([px, py, mat[y, x]]))
fIndexes = [] # 面のリスト
for y in range(0, rows - 1):
for x in range(0, cols - 1):
fIndexes.append([x + y * rows,
x + 1 + y * rows,
x + 1 + (y + 1) * rows,
x + (y + 1) * rows])
mesh = bpy.data.meshes.new('wave')
mesh.from_pydata(verts, [], fIndexes) # 点と面の情報からメッシュを生成
obj = bpy.data.objects.new('wave', mesh) # メッシュ情報を新規オブジェクトに渡す
bpy.context.scene.collection.objects.link(obj) # オブジェクトをシーン上にリンク(v2.8)
# bpy.context.scene.objects.link(obj) #v2.7
obj.select = True # 作ったオブジェクトを選択状態に
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment