Special shading in the viewport?

Hi All.
I have gathered a long list of improvements I would like to see in Blender and will begin to implement some of these.
Now before I start I will sort all these small projects by difficulty and specificly if they are “Python Possible” or I need to dig into C.

In this regard I would like to ask about two of my ideas.
They are both about the shading of the meshes in the viewport:

  • Are there any means of making your own shading in the viewport?

IE: If I want to show the Object Index by different colors on all the objects in the viewport?
Is this possible by python?

If the question is not clear I will elaborate.

Thank you.

object indices sound like a text over in the view3d, which is definately python-possible by using a draw handler (there are templates in the text editor)

That could be a possible solution. Yet a colorbased solution is not possible?

not sure what you mean? change the colors of objects?

Totally doable. The example template script Operator Modal Draw might be a place to start.


import bpy
import bgl
import blf


def draw_callback_px(self, context):
    print("mouse points", len(self.mouse_path))

    font_id = 0  # XXX, need to find out how best to get this.

    # draw some text
    blf.position(font_id, 15, 30, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))

    # 50% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
    bgl.glLineWidth(2)

    bgl.glBegin(bgl.GL_LINE_STRIP)
    for x, y in self.mouse_path:
        bgl.glVertex2i(x, y)

    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)


class ModalDrawOperator(bpy.types.Operator):
    """Draw a line with the mouse"""
    bl_idname = "view3d.modal_operator"
    bl_label = "Simple Modal View3D Operator"

    def modal(self, context, event):
        context.area.tag_redraw()

        if event.type == 'MOUSEMOVE':
            self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))

        elif event.type == 'LEFTMOUSE':
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'FINISHED'}

        elif event.type in {'RIGHTMOUSE', 'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'CANCELLED'}

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            # the arguments we pass the the callback
            args = (self, context)
            # Add the region OpenGL drawing callback
            # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
            self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, args, 'WINDOW', 'POST_PIXEL')

            self.mouse_path = []

            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "View3D not found, cannot run operator")
            return {'CANCELLED'}


def register():
    bpy.utils.register_class(ModalDrawOperator)


def unregister():
    bpy.utils.unregister_class(ModalDrawOperator)

if __name__ == "__main__":
    register()


You’ll have to butcher it.

Too busy ATM to do it, but give me a couple hours.

While writing the index number is an easy and ok workaround I was actually thinking of something more complex.
I would love to be able to get a preview of different passes. Maybe a viewmode for previewing some of the simple passes like, OBIndex, MATIndex, Alpha, z-depth? (All the things that are possible to render with openGL)

This would make it much easier selecting and deciding the passes for masking etc…
I know this is a dirty mockup and that it is not a trivial feature to implement, but just to illustrate:


best done in C, but i guess you could also draw a solid color in 2d over the object. It’s just a little bit math.

Hmm thought so. Well I guess that is beyond my level yet. But one day I will get there :slight_smile: Thanks for the help though :slight_smile:

Another problem with using the viewport for rendered passes and output is that there is no way to specify a render size. The size is just whatever you happen to see on the screen. The API needs extended to support specific output requests as we all are thinking about how we can use viewport rendering to speed through basic render tasks.

What with Furryball and Lumion out there Blender could easily crank out fast game like images with only a few small additions to the API. Accelerated rendering is the future of the BGE (IMHO).