Welcome to my thread , we will document here all sample code for bgl and opengl in blender 2.6
A huge thank you to ideasman, he documented bgl for Blender 2.5. In case you did not know the bgl module allow you to paint 2d and 3d graphics on top of your viewport , it has access to many opengl functions so some knowledge of opengl is required but in short it opens up for a universe of possibilities for blender.
Here is the link
http://www.blender.org/documentation/blender_python_api_2_58_release/bgl.html
Hi kilon,
Thanks for the effort, you’ve given us plenty to research and learn from. OpenGL coding is very interesting and Chromoly’s scripts are an inspiration. As the knowledge spreads, we’ll soon be sharing drop-in functions to perform standard things like reporting values, drawing shapes, etc.
yes this is the purpose of this thread, bgl documentation is not enough and there loads of strange but useful scripts out there with unique approached to how they use bgl. I might do some tutorials for bgl and opengl in blender myself , when I learn enough to feel qualified for it. The script I am working on will use bgl so , I am already doing a research for it and I have collected some links that are difficult to fish out with google or this forum search function so I thought it would benefit others to share them. Will keep you posted with any new knowledge I acquire through this saga.
wow hate to bump this up so soon , but this thing I found is plain amazing. I found a search engine for python source code!!! Did not even knew that something like this existed, if that is old news for you , ignore this post , but if you like me you were living under a rock you will love the next link. The search engine is called nullege and this link is for bgl.
http://www.nullege.com/codes/search?cq=Blender.BGL
Its mainly 2.49 stuff but since bgl has changed so little , you can use it . What is cool about this link is that it goes one by one the bgl commands and gives example scripts for each one of them. The great news is that you can use the same engine to find any kind of python source code. If you use it for blender stuff its most likely to find you 2.49 scripts since there are many more of them but is certainly better than nothing.
As I suspected google has a similar search engine though I like nullege alot more because it did not show a tree like structure with every possible function like nullege did and it did not gave me as many results either. The huge advantage of nullege is that it can understand python and especially designed only for python, so it is not just a regular search engine like the google code search engine.
So searching for blender python source code just got alot easier, check it out you will be amazed like I am now, even if you are not interested in bgl, it can be used for any python module blender or not.
The main flaw I have found with BGL for Blender is that you can not get the size/RECT of the box you are drawing within. Mainly I want to draw in panels but they are scrollable so I can never figure out how to make the OpenGL part move with the panel (without knowing the box panel top,left, etc…).
Perhaps there is a way?
Has anyone else been able to draw in a panel and make the BGL drawn part scroll with the panel?
Actually you can , if you observe my code closely you will see that I draw my rect is such way so it can resize together with the viewport window and I do a similar thing with the texture to. My aim is to build a gui that resizes entirely with the window, so I can exploit every cm of window space.
bgl.glRecti(5,5,bpy.context.area.regions[4].width-5, bpy.context.area.regions[4].height-5)
obviously here the trick is done by the regions , an area has several regions so it take abit of epxerimenting to find which is which , but regions[4] gives the actually window where the viewport is diplaying the 3d geometry and it excludes the side panels, so it easy for me from there on to tell my rect to leave a space of 5 pixels from all sides.
The second part of your question on how to draw on the side panel, I dont think its possible to draw on top of panels, you can however take their size in similar way.
EDIT: ideasman confirmed my suspicion, its not possible to draw on panels because the panels dont call a draw() but a setup_buttons_to_draw_later() , he told me that they could have build a python widget that could do that in its own space inside the panels (not the entire panel) but its unlikely this will happen any time soon.
I will like to add that a solution could be ctypes , but ctypes are generally dark magic that should be exercised with caution and its also preferable to have a good understanding of blender gui source code. But in any case, drawing inside the widow makes more sense cause there always too much non exploited free space and of course usually a user does not operate the gui at the same time he is modeling /animating etc.
great work! go on, it’s very interesting issue and i’m very poor in openGL
thanks!
paolo
EDIT: ideasman confirmed my suspicion, its not possible to draw on panels because the panels dont call a draw() but a setup_buttons_to_draw_later() , he told me that they could have build a python widget that could do that in its own space inside the panels (not the entire panel) but its unlikely this will happen any time soon.
I think you can’t solve this as there’s no info about ‘scrolling’ for region in the bpy.
Awesome. Thanks for getting this thread going! I’m guessing I’ll have something to add in about a month or so
Curious if anybody has ever used bgl.glRotatef or bgl.glTranslatef at all - I can’t seem to make them work at all - suggestions?
I just finished a patch that exposes Panel offset (distance from top of panel stack) and View2D offset (which is the amount of scroll) to Python (read only). The combination of the two can be used to draw in a panel and account for scrolling / other panels above being opened/closed…
bpy.types.Panel.offset_x
bpy.types.Panel.offset_y
bpy.types.View2D.offset_x
bpy.types.View2D.offset_y
To make room for drawing one has to use several empty “UILayout.label” at the moment (hack).
Thanks for the patch. I guess I’ll wait for it to be integrated into Blender and officially released.
Ill leave a message here if/when the patch gets merged into trunk.
this patch would allow for python-scripted UI elements, nice job! Not sure if it will make it into trunk, as it could be abused to clutter the UI with stuff against the design rules. Either way, your patch seems to lack Panel.width and Panel.height, maybe also View2D.width and View2D.height. Without these, one would overdraw panels below.
What would View2D.width and View2D.height be used for?
Thx for the remarks!
Made new version of the patch.
Added:
bpy.types.Panel.width
bpy.types.Panel.height
bpy.types.View2D.width
bpy.types.View2D.height
bpy.types.View2D.zoom_x
bpy.types.View2D.zoom_y
And learned some new things in the process!