def clean(self, opts=None):
        if not self.is_dirty:
            return

        # make not dirty first in case bad things happen while drawing
        self.is_dirty = False

        if self.src_bvh:
            # normal_update() will destroy normals of
            # verts not connected to faces :(
            self.tar_bmesh.normal_update()
            for bmv in self.tar_bmesh.verts:
                if len(bmv.link_faces) != 0:
                    continue
                _, n, _, _ = self.src_bvh.find_nearest(self.src_imx * bmv.co)
                bmv.normal = (self.src_mxnorm * n).normalized()

        bgl.glNewList(self.calllist, bgl.GL_COMPILE)
        # do not change attribs if they're not set
        glSetDefaultOptions(opts=opts)
        # bgl.glPushMatrix()
        # bgl.glMultMatrixf(self.bglMatrix)
        glDrawBMFaces(self.tar_bmesh.faces, opts=opts, enableShader=False)
        glDrawBMEdges(self.tar_bmesh.edges, opts=opts, enableShader=False)
        glDrawBMVerts(self.tar_bmesh.verts, opts=opts, enableShader=False)
        bgl.glDepthRange(0, 1)
        # bgl.glPopMatrix()
        bgl.glEndList()
Beispiel #2
0
    def cache(self, renderer, obj):
        scene = bpy.context.scene
        layer = Manager(obj).get_active_layer(False)

        if layer is not None:
            name = colormeta(obj)['active_line_color']

            with cc.utils.modified_mesh(obj, scene) as mesh:

                layer = Manager(obj).get_layer(name)
                samples = layer.samples
                verts = mesh.vertices

                bgl.glNewList(self.list, bgl.GL_COMPILE)
                bgl.glBegin(bgl.GL_LINES)

                for edge in mesh.edges:
                    x = verts[edge.vertices[0]]
                    y = verts[edge.vertices[1]]
                    x_color = samples[x.index].color
                    y_color = samples[y.index].color

                    bgl.glColor3f(*x_color)
                    bgl.glVertex3f(*(x.co))
                    bgl.glColor3f(*y_color)
                    bgl.glVertex3f(*(y.co))

                bgl.glEnd()
                bgl.glEndList()

            self.ignore_next_update = True
        self.update = False
Beispiel #3
0
    def clean(self, opts=None):
        if not self.is_dirty:
            return

        # make not dirty first in case bad things happen while drawing
        self.is_dirty = False

        if self.src_bvh:
            # normal_update() will destroy normals of
            # verts not connected to faces :(
            self.tar_bmesh.normal_update()
            for bmv in self.tar_bmesh.verts:
                if len(bmv.link_faces) != 0:
                    continue
                _, n, _, _ = self.src_bvh.find_nearest(self.src_imx * bmv.co)
                bmv.normal = (self.src_mxnorm * n).normalized()

        bgl.glNewList(self.calllist, bgl.GL_COMPILE)
        # do not change attribs if they're not set
        glSetDefaultOptions(opts=opts)
        # bgl.glPushMatrix()
        # bgl.glMultMatrixf(self.bglMatrix)
        glDrawBMFaces(self.tar_bmesh.faces, opts=opts, enableShader=False)
        glDrawBMEdges(self.tar_bmesh.edges, opts=opts, enableShader=False)
        glDrawBMVerts(self.tar_bmesh.verts, opts=opts, enableShader=False)
        bgl.glDepthRange(0, 1)
        # bgl.glPopMatrix()
        bgl.glEndList()
Beispiel #4
0
    def clean(self, opts=None):
        if not self.is_dirty: return

        # make not dirty first in case bad things happen while drawing
        self.is_dirty = False

        bgl.glNewList(self.calllist, bgl.GL_COMPILE)
        # do not change attribs if they're not set
        glSetDefaultOptions(opts=opts)
        glDrawBMFaces(self.bme.faces, opts=opts, enableShader=False)
        glDrawBMEdges(self.bme.edges, opts=opts, enableShader=False)
        glDrawBMVerts(self.bme.verts, opts=opts, enableShader=False)
        bgl.glDepthRange(0, 1)
        bgl.glEndList()
Beispiel #5
0
    def __init__(self, scene):
        Handler.__init__(self)
        self.name = scene.name

        # Bpy Object
        self.scene = scene

        # Spr Object
        self.fwScene = None

        # Linked Handlers
        pass

        # Others
        self.rule = ScriptEngine()
        self.rule.set_name("spr_script")
        self.first_step = True

        self.displist = [5001, 5002]
        bgl.glNewList(self.displist[0], bgl.GL_COMPILE)
        bgl.glEndList()

        # -- Synchronizers
        self.gravity_sync = SceneGravitySync(self)
        self.numiteration_sync = SceneNumIterationSync(self)
        self.timestep_sync = SceneTimeStepSync(self)
        self.impactthreshold_sync = SceneImpactThresholdSync(self)
        self.frictionthreshold_sync = SceneFrictionThresholdSync(self)
        self.contacttolerance_sync = SceneContactToleranceSync(self)
        self.maxvelocity_sync = SceneMaxVelocitySync(self)
        self.maxangularvelocity_sync = SceneMaxAngularVelocitySync(self)
        self.ikisenabled_sync = SceneIKIsEnabledSync(self)
        self.iknumiter_sync = SceneIKNumIterSync(self)
        self.ikmaxvelocity_sync = SceneIKMaxVelocitySync(self)
        self.ikmaxangularvelocity_sync = SceneIKMaxAngularVelocitySync(self)
        self.ikregularizeparam_sync = SceneIKRegularizeParamSync(self)
        self.ikitercutoff_sync = SceneIKIterCutoffSync(self)
        self.pliantisenabled_sync = ScenePliantIsEnabledSync(self)
        self.drawsolid_sync = SceneDrawSolidEnabledSync(self)
        self.drawlimit_sync = SceneDrawLimitEnabledSync(self)
        self.drawik_sync = SceneDrawIKEnabledSync(self)
        self.drawforce_sync = SceneDrawForceEnabledSync(self)
Beispiel #6
0
    def draw(self, opts=None):
        if self.is_dirty:
            # make not dirty first in case bad things happen while drawing
            self.is_dirty = False

            bgl.glNewList(self.calllist, bgl.GL_COMPILE)
            # do not change attribs if they're not set
            glSetDefaultOptions(opts=opts)
            if self.mx:
                bgl.glPushMatrix()
                bgl.glMultMatrixf(self.bglMatrix)
            glDrawBMFaces(self.bmesh.faces, opts=opts)
            glDrawBMEdges(self.bmesh.edges, opts=opts)
            glDrawBMVerts(self.bmesh.verts, opts=opts)
            bgl.glDepthRange(0, 1)
            if self.mx:
                bgl.glPopMatrix()
            bgl.glEndList()

        bgl.glCallList(self.calllist)
 def draw(self, opts=None):
     if self.is_dirty:
         # make not dirty first in case bad things happen while drawing
         self.is_dirty = False
         
         bgl.glNewList(self.calllist, bgl.GL_COMPILE)
         # do not change attribs if they're not set
         glSetDefaultOptions(opts=opts)
         if self.mx:
             bgl.glPushMatrix()
             bgl.glMultMatrixf(self.bglMatrix)
         glDrawBMFaces(self.bmesh.faces, opts=opts)
         glDrawBMEdges(self.bmesh.edges, opts=opts)
         glDrawBMVerts(self.bmesh.verts, opts=opts)
         bgl.glDepthRange(0, 1)
         if self.mx:
             bgl.glPopMatrix()
         bgl.glEndList()
     
     bgl.glCallList(self.calllist)
def draw_callback_view(n_id, cached_view, options):
    def Vector_generate2(prop):
        # try:
        #     return [[Vector(v[:3]) for v in obj] for obj in prop]
        # except ValueEror:
        #     return []
        return [[Vector(v) for v in obj] for obj in prop]

    # context = bpy.context
    if options["timings"]:
        start = time.perf_counter()

    if options['draw_list'] == 0:

        sl1 = cached_view[n_id + 'v']
        sl2 = cached_view[n_id + 'ep']
        sl3 = cached_view[n_id + 'm']

        if sl1:
            data_vector = Vector_generate2(sl1)
            verlen = len(data_vector) - 1
        else:
            if not sl3:
                # end early: no matrix and no vertices
                callback_disable(n_id)
                return

            # display matrix repr only.
            data_vector = []
            verlen = 0

        options['verlen'] = verlen
        data_polygons = []
        data_edges = []

        if sl2 and sl2[0]:
            if isinstance(sl2[0], int):
                #callback_disable(n_id)
                return

            len_sl2 = len(sl2[0][0])
            if len_sl2 == 2:
                data_edges = sl2
            elif len_sl2 > 2:
                data_polygons = sl2

        if sl3:
            data_matrix = Matrix_generate(sl3)
        else:
            data_matrix = [Matrix() for i in range(verlen + 1)]

        if (data_vector, data_polygons, data_matrix, data_edges) == (0, 0, 0,
                                                                     0):
            #callback_disable(n_id)
            return
        try:
            the_display_list = glGenLists(1)
            glNewList(the_display_list, GL_COMPILE)
            draw_geometry(n_id, options, data_vector, data_polygons,
                          data_matrix, data_edges)
        except Exception as err:
            print("Error in callback!:")
            traceback.print_exc()
            options['error'] = True
        finally:
            glEndList()

        options['genlist'] = the_display_list

    elif options['draw_list'] == 1:
        the_display_list = options['genlist']

    if not 'error' in options:
        glCallList(the_display_list)
        glFlush()

    # restore to system state
    glLineWidth(1)

    if options["timings"]:
        stop = time.perf_counter()
        print("callback drawn in {:4f}".format(stop - start))

    # has drawn once with success.
    options['draw_list'] = 1
Beispiel #9
0
def draw_stuff():
    # print("draw_stuff")
    # FIXME this should use glPushAttrib
    from bgl import glColor3f, glVertex3f, glBegin, glEnd, GL_POLYGON, GL_LINES
    global draw_stuff_dirty, draw_stuff_objects
    ctx = bpy.context
    if not len(ctx.selected_objects) and not ctx.object:
        return
    if not bpy.context.window_manager.thug_show_face_collision_colors:
        return

    VERT_FLAG = FACE_FLAGS["mFD_VERT"]
    WALLRIDABLE_FLAG = FACE_FLAGS["mFD_WALL_RIDABLE"]
    TRIGGER_FLAG = FACE_FLAGS["mFD_TRIGGER"]
    NON_COLLIDABLE_FLAG = FACE_FLAGS["mFD_NON_COLLIDABLE"]

    bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS)
    try:
        _tmp_buf = bgl.Buffer(bgl.GL_FLOAT, 1)
        bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_FACTOR, _tmp_buf)
        old_offset_factor = _tmp_buf[0]
        bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_UNITS, _tmp_buf)
        old_offset_units = _tmp_buf[0]
        del _tmp_buf

        objects = set([ob.name for ob in ctx.selected_objects] if ctx.mode == "OBJECT" else [ctx.object.name])
        if draw_stuff_objects != objects:
            draw_stuff_dirty = True
        # print("draw_stuff2")

        if not draw_stuff_dirty:
            bgl.glCallList(draw_stuff_display_list_id)
            bgl.glPolygonOffset(old_offset_factor, old_offset_units)
            bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
            bgl.glDisable(bgl.GL_CULL_FACE)
            return

        bm = None
        bgl.glNewList(draw_stuff_display_list_id, bgl.GL_COMPILE_AND_EXECUTE)
        try:
            bgl.glCullFace(bgl.GL_BACK)
            bgl.glEnable(bgl.GL_CULL_FACE)
            bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
            #bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE)
            bgl.glPolygonOffset(-2, -2)

            bgl.glEnable(bgl.GL_BLEND)
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA);

            prefs = ctx.user_preferences.addons[ADDON_NAME].preferences

            bgl.glLineWidth(prefs.line_width)

            draw_stuff_objects = objects
            bdobs = {ob.name: ob for ob in bpy.data.objects}
            for ob in objects:
                ob = bdobs[ob]
                if not bm: bm = bmesh.new()
                if (ob and
                        ob.type == "CURVE" and
                        ob.data.splines and
                        ob.data.splines[-1].points and
                        ob.thug_path_type == "Rail" and
                        ob.thug_rail_connects_to):
                    connects_to = bdobs[ob.thug_rail_connects_to]
                    if (connects_to and
                            connects_to.type == "CURVE" and
                            connects_to.data.splines and
                            connects_to.data.splines[0].points):
                        glBegin(GL_LINES)
                        bgl.glColor4f(*prefs.rail_end_connection_color)
                        v = ob.matrix_world * ob.data.splines[-1].points[-1].co.to_3d()
                        glVertex3f(v[0], v[1], v[2])
                        v = connects_to.matrix_world * connects_to.data.splines[0].points[0].co.to_3d()
                        glVertex3f(v[0], v[1], v[2])
                        glEnd()

                # Draw previews for area lights - tube/sphere lights and area lights
                if (ob and ob.type == 'LAMP'):
                    if ob.data.thug_light_props.light_type == 'TUBE':
                        if ob.data.thug_light_props.light_end_pos != (0, 0, 0):
                            glBegin(GL_LINES)
                            bgl.glColor4f(1.0, 0.75, 0.25, 1.0)
                            glVertex3f(ob.location[0], ob.location[1], ob.location[2])
                            glVertex3f(ob.location[0] + ob.data.thug_light_props.light_end_pos[0], ob.location[1] + ob.data.thug_light_props.light_end_pos[1], ob.location[2] + ob.data.thug_light_props.light_end_pos[2])
                            glEnd()
                        continue
                    elif ob.data.thug_light_props.light_type == 'SPHERE':
                        continue
                    elif ob.data.thug_light_props.light_type == 'AREA':
                        continue
                    else:
                        continue
                elif (ob and ob.type == 'EMPTY'):
                    if ob.thug_empty_props.empty_type == 'LightVolume' or ob.thug_empty_props.empty_type == 'CubemapProbe':
                        # Draw light volume bbox!
                        bbox, bbox_min, bbox_max, bbox_mid = get_bbox_from_node(ob)
                        
                        # 50% alpha, 2 pixel width line
                        bgl.glEnable(bgl.GL_BLEND)
                        bgl.glColor4f(1.0, 0.0, 0.0, 0.5)
                        bgl.glLineWidth(4)
                        
                        glBegin(bgl.GL_LINE_STRIP)
                        bgl.glVertex3f(*bbox[0])
                        bgl.glVertex3f(*bbox[1])
                        bgl.glVertex3f(*bbox[2])
                        bgl.glVertex3f(*bbox[3])
                        bgl.glVertex3f(*bbox[0])
                        bgl.glVertex3f(*bbox[4])
                        bgl.glVertex3f(*bbox[5])
                        bgl.glVertex3f(*bbox[6])
                        bgl.glVertex3f(*bbox[7])
                        bgl.glVertex3f(*bbox[4])
                        bgl.glEnd()

                        bgl.glBegin(bgl.GL_LINES)
                        bgl.glVertex3f(*bbox[1])
                        bgl.glVertex3f(*bbox[5])
                        bgl.glVertex3f(*bbox[2])
                        bgl.glVertex3f(*bbox[6])
                        bgl.glVertex3f(*bbox[3])
                        bgl.glVertex3f(*bbox[7])
                        glEnd()
                        
                if not ob or ob.type != "MESH":
                    continue

                if ob.mode == "EDIT":
                    bm.free()
                    bm = bmesh.from_edit_mesh(ob.data).copy()
                else:
                    bm.clear()
                    if ob.modifiers:
                        final_mesh = ob.to_mesh(bpy.context.scene, True, "PREVIEW")
                        try:
                            bm.from_mesh(final_mesh)
                        finally:
                            bpy.data.meshes.remove(final_mesh)
                    else:
                        bm.from_mesh(ob.data)

                arl = bm.edges.layers.int.get("thug_autorail")
                if arl:
                    bgl.glColor4f(*prefs.autorail_edge_color)
                    glBegin(GL_LINES)
                    for edge in bm.edges:
                        if edge[arl] == AUTORAIL_NONE:
                            continue

                        for vert in edge.verts:
                            v = ob.matrix_world * vert.co
                            glVertex3f(v[0], v[1], v[2])
                    glEnd()

                cfl = bm.faces.layers.int.get("collision_flags")
                flag_stuff = ((VERT_FLAG, prefs.vert_face_color),
                              (WALLRIDABLE_FLAG, prefs.wallridable_face_color),
                              (TRIGGER_FLAG, prefs.trigger_face_color),
                              (NON_COLLIDABLE_FLAG, prefs.non_collidable_face_color))
                if cfl:
                    bmesh.ops.triangulate(bm, faces=bm.faces)

                    for face in bm.faces:
                        drawn_face = False
                        if prefs.show_bad_face_colors:
                            if (face[cfl] & (VERT_FLAG | WALLRIDABLE_FLAG | NON_COLLIDABLE_FLAG) not in
                                (VERT_FLAG, WALLRIDABLE_FLAG, NON_COLLIDABLE_FLAG, 0)):
                                bgl.glColor4f(*prefs.bad_face_color)
                                glBegin(GL_POLYGON)
                                for vert in face.verts:
                                    v = ob.matrix_world * vert.co
                                    glVertex3f(v[0], v[1], v[2])
                                glEnd()
                                continue

                        for face_flag, face_color in flag_stuff:
                            if face[cfl] & face_flag and (not drawn_face or prefs.mix_face_colors):
                                bgl.glColor4f(*face_color)
                                drawn_face = True

                                glBegin(GL_POLYGON)
                                for vert in face.verts:
                                    v = ob.matrix_world * vert.co
                                    glVertex3f(v[0], v[1], v[2])
                                glEnd()
        finally:
            draw_stuff_dirty = False
            bgl.glEndList()
            if bm: bm.free()
            bgl.glPolygonOffset(old_offset_factor, old_offset_units)
            bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
    finally:
        bgl.glPopAttrib()
def draw_callback_view(n_id, cached_view, options):

    def Vector_generate2(prop):
        # try:
        #     return [[Vector(v[:3]) for v in obj] for obj in prop]
        # except ValueEror:
        #     return []
        return [[Vector(v) for v in obj] for obj in prop]

    # context = bpy.context
    if options["timings"]:
        start = time.perf_counter()

    if options['draw_list'] == 0:

        sl1 = cached_view[n_id + 'v']
        sl2 = cached_view[n_id + 'ep']
        sl3 = cached_view[n_id + 'm']

        if sl1:
            data_vector = Vector_generate2(sl1)
            verlen = len(data_vector)-1
        else:
            if not sl3:
                # end early: no matrix and no vertices
                callback_disable(n_id)
                return

            # display matrix repr only.
            data_vector = []
            verlen = 0

        options['verlen'] = verlen
        data_polygons = []
        data_edges = []

        if sl2 and sl2[0]:
            if isinstance(sl2[0], int):
                callback_disable(n_id)
                return

            len_sl2 = len(sl2[0][0])
            if len_sl2 == 2:
                data_edges = sl2
            elif len_sl2 > 2:
                data_polygons = sl2

        if sl3:
            data_matrix = Matrix_generate(sl3)
        else:
            data_matrix = [Matrix() for i in range(verlen+1)]

        if (data_vector, data_polygons, data_matrix, data_edges) == (0, 0, 0, 0):
            callback_disable(n_id)
            return
        try:
            the_display_list = glGenLists(1)
            glNewList(the_display_list, GL_COMPILE)
            draw_geometry(n_id, options, data_vector, data_polygons, data_matrix, data_edges)
        except Exception as err:
            print("Error in callback!:")
            traceback.print_exc()
            options['error'] = True
        finally:
            glEndList()

        options['genlist'] = the_display_list

    elif options['draw_list'] == 1:
        the_display_list = options['genlist']
    
    if not 'error' in options:
        glCallList(the_display_list)
        glFlush()

    # restore to system state
    glLineWidth(1)

    if options["timings"]:
        stop = time.perf_counter()
        print("callback drawn in {:4f}".format(stop-start))

    # has drawn once with success.
    options['draw_list'] = 1