Skip to content

Instantly share code, notes, and snippets.

@12funkeys
Created February 23, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 12funkeys/cccb21ed3ef821fc07c91188f91e4619 to your computer and use it in GitHub Desktop.
Save 12funkeys/cccb21ed3ef821fc07c91188f91e4619 to your computer and use it in GitHub Desktop.
openGL_test.pyの更新版。ポーズモードに対応。
import bpy
import bgl # OpenGLをBlender内部から利用するために必要
from bpy_extras import view3d_utils
bl_info = {
"name": "Tutorial: OpenGL on Blender",
"author": "12funkeys",
"version": (1, 0),
"blender": (2, 74, 0),
"location": "View3D > Tutorial: OpenGL on Blender",
"description": "Tutorial: Use Blender's OpenGL API.",
"warning": "",
"support": "COMMUNITY",
"wiki_url": "",
"tracker_url": "",
"category": "3D View"
}
class RectRenderer(bpy.types.Operator):
"""四角形を描画する"""
bl_idname = "view3d.rect_renderer"
bl_label = "Rect renderer"
__handle = None # 描画関数
# 「View3D」領域の描画関数を登録
@staticmethod
def handle_add():
RectRenderer.__handle = bpy.types.SpaceView3D.draw_handler_add(
RectRenderer.render_rect,
(), 'WINDOW', 'POST_PIXEL')
# 「View3D」領域の描画関数を登録解除
@staticmethod
def handle_remove():
if RectRenderer.__handle is not None:
bpy.types.SpaceView3D.draw_handler_remove(
RectRenderer.__handle, 'WINDOW')
RectRenderer.__handle = None
# 「View3D」領域の描画関数本体
@staticmethod
def render_rect():
C = bpy.context
oa = bpy.context.active_object
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
viewport = area.regions[4]
me = oa.to_mesh(scene=C.scene, apply_modifiers=True, settings='PREVIEW')
me.transform(oa.matrix_world)
print(me.vertices[0].co, " - Vert 0 (deformed/modified)")
for v in me.vertices :
#coo in 3d space
co_3d = v.co
#coo in the 3d view area (2d)
co_2d = view3d_utils.location_3d_to_region_2d(viewport, area.spaces[0].region_3d, co_3d)
#coo in the blender window
#co_2d_w = (co_2d[0]+viewport.x, co_2d[1]+viewport.y)
#coo in the system screen
#co_2d_s = (co_2d_w[0]+bpy.context.window.x, co_2d_w[1]+bpy.context.window.y)
#display
#print("\n\n3d space co :",co_3d,"\n2d in viewport co :",co_2d)
#print("2d in window co :",co_2d_w,"\n2d system co :",co_2d_s)
# 描画領域の作成
cx = co_2d[0]
cy = co_2d[1]
ms = 3.0 #markingsize
positions = [
[cx-ms/2, cy-ms/2], # 左下
[cx-ms/2, cy+ms/2], # 左上
[cx+ms/2, cy+ms/2], # 右上
[cx+ms/2, cy-ms/2] # 右下
]
# OpenGLによる四角形の描画
bgl.glEnable(bgl.GL_BLEND) # アルファブレンドの有効化
bgl.glBegin(bgl.GL_QUADS) # 四角形の描画を開始
bgl.glColor4f(0.7, 0.5, 0.3, 0.6) # 描画する四角形の色を指定
for (v1, v2) in positions:
bgl.glVertex2f(v1, v2) # 頂点の登録
bgl.glEnd() # 四角形の描画を終了
bpy.data.meshes.remove(me)
# スクリプトインストール時の処理
def register():
bpy.utils.register_module(__name__)
RectRenderer.handle_add()
# スクリプトアンインストール時の処理
def unregister():
bpy.utils.unregister_module(__name__)
RectRenderer.handle_remove()
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment