Blenderでリグ等に文字列を表示する場合に
テキストオブジェクトからメッシュを作成するものの
フォント選んで 文字入力して 変換して と手順を踏むものの そんな手間かけるのも煩わしいし
もっとシンプルな見た目の文字が欲しかったので
自分でポリゴンのフォントを作って 文字列のオブジェクトを作成するスクリプトを作ってみた
パブリックドメインのドットフォントを参考に 各文字は1つの繋がったメッシュになるようにした
import bpy
# メッシュを作成する文字列
str = "AbCD4fhjk"
# 文字メッシュのデータ
mesh_dic = {
"A":([[1, 2], [1, 7], [5, 7], [5, 2], [1, 5], [5, 5], [2, 8], [4, 8]], [[1, 4], [3, 5], [6, 1], [2, 7], [4, 5], [7, 6], [4, 0], [5, 2]]),
"B":([[5, 3], [4, 2], [5, 4], [4, 5], [5, 6], [1, 5], [1, 2], [1, 8], [4, 8], [5, 7]], [[1, 0], [0, 2], [2, 3], [3, 4], [5, 3], [5, 6], [7, 5], [8, 7], [9, 8], [4, 9], [6, 1]]),
"C":([[1, 7], [1, 3], [2, 8], [4, 8], [5, 7], [2, 2], [4, 2], [5, 3]], [[0, 1], [2, 0], [3, 2], [4, 3], [1, 5], [5, 6], [6, 7]]),
"D":([[4, 8], [5, 7], [1, 8], [1, 2], [4, 2], [5, 3]], [[1, 0], [0, 2], [2, 3], [3, 4], [4, 5], [1, 5]]),
"E":([[5, 8], [1, 8], [1, 5], [4, 5], [1, 2], [5, 2]], [[0, 1], [1, 2], [2, 3], [2, 4], [4, 5]]),
"F":([[5, 7], [1, 7], [1, 4], [4, 4], [1, 1]], [[0, 1], [1, 2], [2, 3], [2, 4]]),
"G":([[2, 8], [4, 8], [1, 7], [1, 3], [2, 2], [4, 2], [5, 3], [5, 5], [3, 5], [5, 7]], [[1, 0], [0, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [9, 1]]),
"H":([[1, 2], [1, 5], [1, 8], [5, 5], [5, 8], [5, 2]], [[1, 0], [2, 1], [1, 3], [4, 3], [3, 5]]),
"I":([[2, 8], [3, 8], [4, 8], [3, 2], [4, 2], [2, 2]], [[1, 0], [2, 1], [1, 3], [4, 3], [3, 5]]),
"J":([[1, 2], [0, 3], [3, 2], [4, 3], [4, 8], [3, 8]], [[1, 0], [0, 2], [2, 3], [3, 4], [4, 5]]),
"M":([[5, 2], [5, 8], [3, 6], [1, 8], [1, 2]], [[0, 1], [1, 2], [2, 3], [3, 4]]),
"K":([[1, 8], [1, 5], [1, 2], [5, 8], [5, 3], [5, 2]], [[0, 1], [1, 2], [1, 3], [1, 4], [4, 5]]),
"L":([[1, 8], [1, 2], [4, 2], [5, 3]], [[0, 1], [1, 2], [2, 3]]),
"N":([[5, 8], [5, 2], [1, 8], [1, 2]], [[1, 0], [1, 2], [2, 3]]),
"O":([[4, 8], [2, 8], [5, 7], [5, 3], [4, 2], [2, 2], [1, 3], [1, 7]], [[0, 1], [2, 0], [3, 2], [4, 3], [5, 4], [6, 5], [7, 6], [1, 7]]),
"P":([[1, 8], [4, 8], [1, 5], [4, 5], [1, 2], [5, 6], [5, 7]], [[1, 0], [0, 2], [2, 3], [2, 4], [3, 5], [5, 6], [6, 1]]),
"Q":([[1, 4], [2, 3], [1, 7], [2, 8], [4, 8], [5, 7], [5, 4], [4, 3], [5, 2], [3, 3]], [[0, 1], [2, 0], [3, 2], [4, 3], [5, 4], [6, 5], [7, 6], [7, 8], [9, 7], [1, 9]]),
"R":([[1, 1], [1, 4], [1, 7], [4, 4], [5, 5], [5, 2], [5, 1], [5, 6], [4, 7]], [[1, 0], [2, 1], [1, 3], [3, 4], [3, 5], [5, 6], [4, 7], [7, 8], [8, 2]]),
"S":([[4, 2], [3, 1], [4, 3], [3, 4], [1, 4], [0, 5], [0, 6], [1, 7], [3, 7], [1, 1], [0, 2]], [[1, 0], [0, 2], [2, 3], [4, 3], [4, 5], [5, 6], [6, 7], [8, 7], [9, 1], [10, 9]]),
"T":([[5, 8], [3, 8], [3, 2], [1, 8]],[[0, 1], [1, 2], [1, 3]]),
"U":([[5, 2], [5, 7], [4, 1], [2, 1], [1, 2], [1, 7]], [[1, 0], [0, 2], [2, 3], [3, 4], [4, 5]]),
"V":([[5, 7], [5, 4], [3, 1], [1, 4], [1, 7]], [[0, 1], [1, 2], [2, 3], [3, 4]]),
"W":([[5, 7], [5, 1], [3, 3], [1, 1], [1, 7]], [[0, 1], [1, 2], [2, 3], [3, 4]]),
"X":([[1, 2], [1, 4], [3, 6], [5, 4], [1, 8], [5, 8], [5, 2]], [[1, 0], [2, 1], [2, 3], [2, 4], [5, 2], [3, 6]]),
"Y":([[3, 2], [3, 6], [5, 8], [1, 8]], [[1, 0], [2, 1], [1, 3]]),
"Z":([[1, 8], [5, 8], [1, 2], [6, 2]], [[1, 0], [1, 2], [2, 3]]),
"a":([[4, 3], [4, 4], [3, 2], [1, 2], [0, 3], [1, 4], [4, 5], [3, 6], [2, 6]], [[0, 1], [2, 0], [3, 2], [4, 3], [5, 4], [1, 5], [1, 6], [6, 7], [7, 8]]),
"b":([[1, 8], [1, 5], [4, 5], [1, 2], [4, 2], [5, 3], [5, 4]], [[0, 1], [1, 2], [1, 3], [3, 4], [4, 5], [5, 6], [6, 2]]),
"c":([[2, 6], [4, 6], [1, 5], [1, 3], [2, 2], [4, 2], [5, 3], [5, 5]], [[1, 0], [0, 2], [2, 3], [3, 4], [4, 5], [5, 6], [7, 1]]),
"d":([[1, 3], [1, 4], [2, 2], [5, 2], [5, 5], [2, 5], [5, 8]], [[1, 0], [0, 2], [2, 3], [3, 4], [4, 5], [4, 6], [5, 1]]),
"e":([[1, 5], [2, 6], [1, 4], [5, 4], [1, 3], [2, 2], [4, 2], [5, 3], [5, 5], [4, 6]], [[1, 0], [0, 2], [2, 3], [2, 4], [4, 5], [5, 6], [6, 7], [8, 3], [8, 9], [9, 1]]),
"f":([[2, 6], [3, 6], [3, 2], [3, 7], [5, 6], [4, 8], [5, 8]], [[1, 0], [1, 2], [1, 3], [4, 1], [3, 5], [5, 6]]),
"g":([[2, 6], [1, 5], [5, 6], [5, 2], [4, 1], [5, 3], [2, 3], [1, 4], [1, 1]], [[0, 1], [2, 0], [3, 2], [3, 4], [5, 3], [6, 5], [7, 6], [1, 7], [4, 8]]),
"h":([[5, 2], [5, 5], [4, 6], [3, 6], [1, 5], [1, 2], [1, 8]],[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [4, 6]]),
"i":([[3, 2], [3, 6], [2, 6]],[[0, 1], [1, 2]]),
"j":([[3, 1], [2, 1], [4, 2], [4, 6], [3, 7], [1, 2]], [[0, 1], [2, 0], [2, 3], [3, 4], [1, 5]]),
"k":([[2, 8], [2, 4], [2, 2], [5, 6], [5, 2]], [[0, 1], [1, 2], [1, 3], [1, 4]]),
"l":([[3, 8], [3, 3], [4, 2]], [[0, 1], [1, 2]]),
"m":([[5, 2], [5, 5], [4, 6], [3, 5], [2, 6], [3, 3], [1, 6], [1, 2]], [[0, 1], [1, 2], [2, 3], [3, 4], [3, 5], [4, 6], [6, 7]]),
"n":([[5, 2], [5, 5], [4, 6], [1, 6], [1, 2]], [[0, 1], [1, 2], [2, 3], [3, 4]]),
"o":([[1, 3], [2, 2], [1, 5], [2, 6], [4, 6], [5, 5], [5, 3], [4, 2]], [[0, 1], [2, 0], [3, 2], [4, 3], [5, 4], [6, 5], [7, 6], [1, 7]]),
"p":([[1, 4], [4, 3], [1, 6], [1, 1], [4, 6], [5, 5], [5, 4], [2, 3]], [[7, 1], [2, 0], [0, 3], [4, 2], [5, 4], [6, 5], [1, 6], [0, 7]]),
"q":([[2, 3], [1, 4], [5, 4], [5, 1], [5, 6], [2, 6], [1, 5], [3, 3]], [[1, 0], [7, 2], [2, 3], [2, 4], [4, 5], [5, 6], [6, 1], [0, 7]]),
"r":([[2, 5], [2, 6], [2, 2], [3, 6], [4, 6]], [[1, 0], [0, 2], [0, 3], [3, 4]]),
"s":([[1, 2], [4, 2], [5, 3], [4, 4], [2, 4], [1, 5], [2, 6], [4, 6]], [[0, 1], [1, 2], [2, 3], [4, 3], [5, 4], [6, 5], [7, 6]]),
"u":([[5, 2], [5, 6], [2, 2], [1, 3], [1, 6]], [[1, 0], [0, 2], [2, 3], [3, 4]]),
"t":([[2, 6], [3, 6], [3, 3], [3, 8], [5, 6], [4, 2]], [[1, 0], [1, 2], [1, 3], [4, 1], [2, 5]]),
"v":([[5, 5], [5, 6], [3, 2], [1, 5], [1, 6]], [[1, 0], [0, 2], [2, 3], [3, 4]]),
"w":([[4, 2], [5, 6], [3, 4], [2, 2], [1, 6]], [[1, 0], [0, 2], [2, 3], [3, 4]]),
"y":([[2, 3], [1, 4], [5, 3], [5, 6], [5, 2], [4, 1], [1, 1], [1, 6]], [[0, 1], [2, 0], [3, 2], [2, 4], [4, 5], [5, 6], [1, 7]]),
"x":([[5, 2], [3, 4], [1, 2], [1, 6], [5, 6]], [[1, 0], [1, 2], [1, 3], [4, 1]]),
"z":([[1, 6], [5, 6], [1, 2], [5, 2]], [[1, 0], [1, 2], [2, 3]]),
"0":([[5, 3], [4, 2], [5, 7], [4, 8], [1, 3], [1, 7], [2, 2], [2, 8]], [[1, 0], [0, 2], [2, 3], [4, 2], [5, 4], [4, 6], [6, 1], [7, 5], [3, 7]]),
"1":([[1, 7], [2, 8], [2, 2], [1, 2], [3, 2]], [[1, 0], [1, 2], [2, 3], [4, 2]]),
"2":([[4, 8], [5, 7], [2, 8], [1, 7], [5, 6], [4, 5], [1, 2], [6, 2]], [[1, 0], [0, 2], [2, 3], [4, 1], [5, 4], [6, 5], [7, 6]]),
"3":([[4, 8], [2, 8], [5, 7], [5, 6], [4, 5], [3, 5], [5, 4], [5, 3], [4, 2], [2, 2], [1, 3], [1, 7]], [[0, 1], [2, 0], [3, 2], [4, 3], [5, 4], [6, 4], [7, 6], [8, 7], [9, 8], [10, 9], [1, 11]]),
"4":([[1, 5], [1, 4], [5, 8], [5, 4], [5, 2]], [[0, 1], [2, 0], [3, 2], [1, 3], [3, 4]]),
"5":([[5, 8], [1, 8], [1, 6], [4, 6], [5, 5], [5, 3], [4, 2], [2, 2], [1, 3]], [[0, 1], [1, 2], [2, 3], [4, 3], [5, 4], [6, 5], [7, 6], [8, 7]]),
"6":([[4, 5], [1, 5], [5, 4], [5, 3], [4, 2], [2, 2], [1, 3], [1, 6], [3, 8]], [[1, 0], [2, 0], [3, 2], [4, 3], [5, 4], [6, 5], [6, 1], [7, 1], [8, 7]]),
"7":([[1, 8], [1, 7], [5, 8], [3, 4], [3, 2]], [[0, 1], [2, 0], [2, 3], [3, 4]]),
"8":([[5, 3], [4, 2], [5, 4], [4, 5], [5, 6], [2, 5], [1, 6], [1, 4], [1, 3], [2, 2], [1, 7], [2, 8], [4, 8], [5, 7]], [[1, 0], [0, 2], [2, 3], [3, 4], [5, 3], [6, 5], [5, 7], [7, 8], [8, 9], [9, 1], [10, 6], [11, 10], [12, 11], [13, 12], [4, 13]]),
"9":([[2, 5], [5, 5], [1, 6], [1, 7], [2, 8], [4, 8], [5, 7], [5, 4], [3, 2], [2, 2]], [[0, 1], [2, 0], [3, 2], [4, 3], [5, 4], [6, 5], [1, 6], [7, 1], [8, 7], [9, 8]]),
".":([[3, 4], [4, 3]], [[0, 1]]),
",":([[3, 4], [2, 2]], [[0, 1]]),
"/":([[1, 2], [5, 8]], [[0, 1]]),
"?":([[1, 7], [2, 8], [5, 8], [6, 7], [6, 6], [5, 5], [3, 4], [3, 2], [4, 1], [2, 3]], [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [7, 9]]),
"&":([[2, 7], [3, 8], [3, 6], [4, 7], [1, 4], [5, 3], [6, 2], [6, 5], [4, 2], [2, 2], [1, 3]],[[0, 1], [2, 0], [3, 2], [2, 4], [5, 2], [5, 6], [5, 7], [8, 5], [9, 8], [10, 9], [4, 10], [1, 3]]),
"\'":([[4, 8], [3, 6]],[[0, 1]]),
"+":([[3, 5], [3, 3], [3, 7], [5, 5], [1, 5]] ,[[0, 1], [2, 0], [0, 3], [0, 4]]),
"-":([[5, 5], [1, 5]], [[0, 1]]),
"*":([[3, 5], [3, 3], [3, 7], [5, 5], [1, 5], [5, 3], [1, 7], [5, 7], [1, 3]], [[0, 1], [2, 0], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8]]),
"Null":([[5, 8], [5, 2], [1, 8], [1, 2]], [[0, 1], [0, 2], [2, 3], [3, 1]]),
"_":([[5, 2], [1, 2]],[[1, 0]]),
"!":([[2, 3], [3, 2], [4, 1], [3, 8]], [[1, 0], [2, 1], [3, 1]]),
"#":([[4, 6], [4, 4], [2, 6], [4, 8], [5, 6], [2, 4], [2, 8], [1, 6], [2, 2], [1, 4], [4, 2], [5, 4]], [[0, 1], [0, 2], [3, 0], [4, 0], [2, 5], [2, 6], [7, 2], [5, 1], [8, 5], [5, 9], [1, 10], [1, 11]]),
")":([[3, 8], [4, 7], [4, 3], [3, 2]], [[1, 0], [1, 2], [3, 2]]),
"(":([[4, 8], [3, 7], [3, 3], [4, 2]], [[1, 0], [1, 2], [3, 2]]),
"@":([[5, 4], [5, 7], [4, 4], [4, 4], [3, 4], [4, 6], [3, 6], [2, 5], [5, 7], [4, 8], [2, 8], [1, 7], [1, 3], [2, 2], [5, 2]], [[0, 1], [2, 0], [3, 2], [4, 3], [5, 3], [5, 6], [6, 7], [7, 4], [1, 8], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14]]),
"<":([[4, 8], [1, 5], [4, 2]], [[1, 0], [2, 1]]),
">":([[2, 8], [5, 5], [2, 2]], [[1, 0], [2, 1]]),
"^":([[1, 6], [3, 8], [5, 6]], [[1, 0], [2, 1]]),
}
scale = 0.1
vertex_count = 0
vertices = []
edges = []
for i,s in enumerate(str):
if s in mesh_dic:
(v_list, e_list) = mesh_dic[s]
else:
(v_list, e_list) = mesh_dic["Null"]
offset = 5 *i *scale
vertices += [(float(v[0]) *scale + offset, float(v[1] *scale), 0) for v in v_list]
edges += [[e[0] +vertex_count, e[1] +vertex_count] for e in e_list]
vertex_count += len(v_list)
name = str
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh)
bpy.context.scene.collection.objects.link(obj)
mesh.from_pydata(vertices, edges,[])
mesh.update()
ちなみに文字のメッシュの情報の書き出しはこんな感じ
選択したオブジェクト名を辞書のキーにするようにメッシュのデータを出力してるだけ
うまく使えば ファンタジーの独自文字をモデリングしておいて変換表を作れるかも?
import bpy
objects = bpy.context.selected_objects
print("{")
for obj in objects:
mesh = obj.data
#頂点座標
v_data_list = []
for v in mesh.vertices:
pos = v.co
v_data_list.append([pos[0],pos[1]])
#稜線
e_data_list = []
for e in mesh.edges:
e_data_list.append([e.vertices[0],e.vertices[1]])
print("\"%s\":(%s, %s)," % ( obj.name, str(v_data_list), str(e_data_list) ))
print("}")