データベースを使ってみる(sqlite3)
対応バージョン
実行形式
Note
2.69
テキストエディタから実行
Blenderからデータベースを使ってみるサンプル
データベースを使う
今回はオンメモリでデータベースを使ってみます。Pythonパッケージとして提供されているSQLite3を使用。
データベースのデータには今回Blenderの画面の区画を使うこととしました。かろうじてBlenderPythonAPIつながり。
(特にコードに対する解説はありません)
ソースコード
import bpy
import sqlite3
#connect to database
conn = sqlite3.connect(':memory:')
#define cursor
cur = conn.cursor()
cur.execute("create table areas (type,x,y,width,height)")
wn = bpy.context.window
sc = bpy.context.screen
for x in sc.areas:
cur.execute("insert into areas values (?, ?, ?, ?, ?)" ,
(x.type, x.x, wn.height - x.y - x.height +1, x.width ,x.height))
cur.execute("select type,x,y,width,height from areas order by x,y ")
print ("---[Area Size]---")
for row in cur:
print(row)
print ("---[Window Size]---")
print(wn.x)
print(wn.y)
print(wn.width)
print(wn.height)
print ("---[END]---")
実行結果
あくまで例です。
---[Area Size]---
('INFO', 0, 0, 1673, 30)
('VIEW_3D', 0, 31, 1450, 580)
('TEXT_EDITOR', 0, 612, 1450, 318)
('TIMELINE', 0, 931, 1450, 93)
('OUTLINER', 1451, 31, 222, 169)
('PROPERTIES', 1451, 201, 222, 823)
---[Window Size]---
4
234
1672
1023
---[END]---