Last active
January 4, 2017 07:52
-
-
Save 12funkeys/92ab4fc659707fb9c582e3e256474c84 to your computer and use it in GitHub Desktop.
3Dview上の頂点位置をopenGLで表示する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import bgl # OpenGLをBlender内部から利用するために必要 | |
from bpy_extras import view3d_utils | |
bl_info = { | |
"name": "", | |
"author": "", | |
"version": (1, 0), | |
"blender": (2, 74, 0), | |
"location": "", | |
"description": "", | |
"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(): | |
oa = bpy.context.active_object | |
for area in bpy.context.screen.areas: | |
if area.type == 'VIEW_3D': | |
viewport = area.regions[4] | |
for v in bpy.context.object.data.vertices : | |
#coo in 3d space | |
co_3d = oa.matrix_world * 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] | |
positions = [ | |
[cx-10.0, cy-10.0], # 左下 | |
[cx-10.0, cy+10.0], # 左上 | |
[cx+10.0, cy+10.0], # 右上 | |
[cx+10.0, cy-10.0] # 右下 | |
] | |
# 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() # 四角形の描画を終了 | |
# スクリプトインストール時の処理 | |
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