コード例 #1
0
ファイル: convexhull.py プロジェクト: Italic-/blenderpython
    def draw(context, event, glsettings):
        if context.mode != 'EDIT_MESH':
            return
        ob = context.active_object
        bm = bmesh.from_edit_mesh(ob.data)

        # convex_hull (2D)
        convex_hull_2d_coords = []
        project = bpy_extras.view3d_utils.location_3d_to_region_2d
        mat = ob.matrix_world
        region_coords = []
        for eve in bm.verts:
            v = project(context.region, context.region_data, mat * eve.co)
            region_coords.append(v)
        indices = convex_hull(region_coords)
        indices = convexhull.convex_hull(region_coords)
        for i in indices:
            convex_hull_2d_coords.append(region_coords[i])

        # obb (2D)
        obb_mat_2d, obb_scale_2d = OBB(region_coords)
        obb_mat_2d, obb_scale_2d = convexhull.OBB(region_coords)
        obb_mat_2d = Matrix(obb_mat_2d)
        obb_scale_2d = Vector(obb_scale_2d)

        # draw 3d obb
        cm = glsettings.region_view3d_space().enter()
        x, y, z = obb_scale / 2
        vecs = [obb_mat * Vector(v) for v in
                [(-x, -y, -z),
                 (x, -y, -z),
                 (x, y, -z),
                 (-x, y, -z),
                 (-x, -y, z),
                 (x, -y, z),
                 (x, y, z),
                 (-x, y, z)]
        ]
        def draw_line(v1, v2):
            if len(v1) == 3:
                bgl.glVertex3f(*v1)
                bgl.glVertex3f(*v2)
            else:
                bgl.glVertex2f(*v1)
                bgl.glVertex2f(*v2)

        bgl.glLineWidth(2.0)
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        for v in vecs:
            bgl.glVertex3f(*v)
        bgl.glEnd()

        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        draw_line(vecs[5], vecs[6])
        draw_line(vecs[7], vecs[4])
        bgl.glEnd()

        # Z
        bgl.glColor3f(0.0, 0.0, 1.0)
        bgl.glBegin(bgl.GL_LINES)
        for i in range(4):
            draw_line(vecs[i], vecs[i + 4])
        bgl.glEnd()

        cm.exit()

        # draw 2d
        bgl.glLineWidth(1.0)
        bgl.glColor3f(1.0, 1.0, 1.0)
        bgl.glBegin(bgl.GL_LINE_LOOP)
        for v in convex_hull_2d_coords:
            bgl.glVertex2f(*v)
        bgl.glEnd()

        # draw 2d obb
        bgl.glColor3f(0.0, 1.0, 0.0)
        x, y = obb_scale_2d / 2
        m = Matrix.Identity(4)
        m.col[0][:2] = obb_mat_2d.col[0][:2]
        m.col[1][:2] = obb_mat_2d.col[1][:2]
        m.col[3][:2] = obb_mat_2d.col[2][:2]
        vecs = [(m * Vector(v).to_3d()).to_2d() for v in
                [(-x, -y),
                 (x, -y),
                 (x, y),
                 (-x, y)]
        ]
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[0], vecs[1])
        draw_line(vecs[2], vecs[3])
        bgl.glEnd()
        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        bgl.glEnd()
コード例 #2
0
    def draw(context, event, glsettings):
        if context.mode != 'EDIT_MESH':
            return
        ob = context.active_object
        bm = bmesh.from_edit_mesh(ob.data)

        # convex_hull (2D)
        convex_hull_2d_coords = []
        project = bpy_extras.view3d_utils.location_3d_to_region_2d
        mat = ob.matrix_world
        region_coords = []
        for eve in bm.verts:
            v = project(context.region, context.region_data, mat * eve.co)
            region_coords.append(v)
        indices = convex_hull(region_coords)
        indices = convexhull.convex_hull(region_coords)
        for i in indices:
            convex_hull_2d_coords.append(region_coords[i])

        # obb (2D)
        obb_mat_2d, obb_scale_2d = OBB(region_coords)
        obb_mat_2d, obb_scale_2d = convexhull.OBB(region_coords)
        obb_mat_2d = Matrix(obb_mat_2d)
        obb_scale_2d = Vector(obb_scale_2d)

        # draw 3d obb
        cm = glsettings.region_view3d_space().enter()
        x, y, z = obb_scale / 2
        vecs = [
            obb_mat * Vector(v)
            for v in [(-x, -y,
                       -z), (x, -y, -z), (x, y,
                                          -z), (-x, y,
                                                -z), (-x, -y,
                                                      z), (x, -y,
                                                           z), (x, y,
                                                                z), (-x, y, z)]
        ]

        def draw_line(v1, v2):
            if len(v1) == 3:
                bgl.glVertex3f(*v1)
                bgl.glVertex3f(*v2)
            else:
                bgl.glVertex2f(*v1)
                bgl.glVertex2f(*v2)

        bgl.glLineWidth(2.0)
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        for v in vecs:
            bgl.glVertex3f(*v)
        bgl.glEnd()

        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        draw_line(vecs[5], vecs[6])
        draw_line(vecs[7], vecs[4])
        bgl.glEnd()

        # Z
        bgl.glColor3f(0.0, 0.0, 1.0)
        bgl.glBegin(bgl.GL_LINES)
        for i in range(4):
            draw_line(vecs[i], vecs[i + 4])
        bgl.glEnd()

        cm.exit()

        # draw 2d
        bgl.glLineWidth(1.0)
        bgl.glColor3f(1.0, 1.0, 1.0)
        bgl.glBegin(bgl.GL_LINE_LOOP)
        for v in convex_hull_2d_coords:
            bgl.glVertex2f(*v)
        bgl.glEnd()

        # draw 2d obb
        bgl.glColor3f(0.0, 1.0, 0.0)
        x, y = obb_scale_2d / 2
        m = Matrix.Identity(4)
        m.col[0][:2] = obb_mat_2d.col[0][:2]
        m.col[1][:2] = obb_mat_2d.col[1][:2]
        m.col[3][:2] = obb_mat_2d.col[2][:2]
        vecs = [(m * Vector(v).to_3d()).to_2d()
                for v in [(-x, -y), (x, -y), (x, y), (-x, y)]]
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[0], vecs[1])
        draw_line(vecs[2], vecs[3])
        bgl.glEnd()
        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        bgl.glEnd()
コード例 #3
0
ファイル: convexhull.py プロジェクト: Italic-/blenderpython
def test(use_random=True, random_count=10, lifetime=3.0):
    import bpy_extras
    import bgl
    from . import testops

    import localutils
    from localutils import convexhull
    # convexhull = localutils.convexhull

    context = bpy.context
    if context.mode != 'EDIT_MESH':
        return
    ob = context.active_object

    bm = bmesh.from_edit_mesh(ob.data)
    if use_random:
        bm.clear()

        for i in range(random_count):
            bm.verts.new([random.uniform(-1, 1) for i in range(3)])
        # bmesh.update_edit_mesh(ob.data)

    mat = ob.matrix_world
    coords = [mat * v.co for v in bm.verts]

    # convex_hull (3D)
    indices = convex_hull(coords)
    indices = convexhull.convex_hull(coords)
    if indices:
        for tri in indices:
            verts = [bm.verts[i] for i in tri]
            bm.faces.new(verts)
        bm.normal_update()
        bmesh.update_edit_mesh(ob.data)

    # obb (3D)
    obb_mat, obb_scale = OBB(coords)
    obb_mat, obb_scale = convexhull.OBB(coords)
    obb_mat = Matrix(obb_mat)
    obb_scale = Vector(obb_scale)

    def draw(context, event, glsettings):
        if context.mode != 'EDIT_MESH':
            return
        ob = context.active_object
        bm = bmesh.from_edit_mesh(ob.data)

        # convex_hull (2D)
        convex_hull_2d_coords = []
        project = bpy_extras.view3d_utils.location_3d_to_region_2d
        mat = ob.matrix_world
        region_coords = []
        for eve in bm.verts:
            v = project(context.region, context.region_data, mat * eve.co)
            region_coords.append(v)
        indices = convex_hull(region_coords)
        indices = convexhull.convex_hull(region_coords)
        for i in indices:
            convex_hull_2d_coords.append(region_coords[i])

        # obb (2D)
        obb_mat_2d, obb_scale_2d = OBB(region_coords)
        obb_mat_2d, obb_scale_2d = convexhull.OBB(region_coords)
        obb_mat_2d = Matrix(obb_mat_2d)
        obb_scale_2d = Vector(obb_scale_2d)

        # draw 3d obb
        cm = glsettings.region_view3d_space().enter()
        x, y, z = obb_scale / 2
        vecs = [obb_mat * Vector(v) for v in
                [(-x, -y, -z),
                 (x, -y, -z),
                 (x, y, -z),
                 (-x, y, -z),
                 (-x, -y, z),
                 (x, -y, z),
                 (x, y, z),
                 (-x, y, z)]
        ]
        def draw_line(v1, v2):
            if len(v1) == 3:
                bgl.glVertex3f(*v1)
                bgl.glVertex3f(*v2)
            else:
                bgl.glVertex2f(*v1)
                bgl.glVertex2f(*v2)

        bgl.glLineWidth(2.0)
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        for v in vecs:
            bgl.glVertex3f(*v)
        bgl.glEnd()

        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        draw_line(vecs[5], vecs[6])
        draw_line(vecs[7], vecs[4])
        bgl.glEnd()

        # Z
        bgl.glColor3f(0.0, 0.0, 1.0)
        bgl.glBegin(bgl.GL_LINES)
        for i in range(4):
            draw_line(vecs[i], vecs[i + 4])
        bgl.glEnd()

        cm.exit()

        # draw 2d
        bgl.glLineWidth(1.0)
        bgl.glColor3f(1.0, 1.0, 1.0)
        bgl.glBegin(bgl.GL_LINE_LOOP)
        for v in convex_hull_2d_coords:
            bgl.glVertex2f(*v)
        bgl.glEnd()

        # draw 2d obb
        bgl.glColor3f(0.0, 1.0, 0.0)
        x, y = obb_scale_2d / 2
        m = Matrix.Identity(4)
        m.col[0][:2] = obb_mat_2d.col[0][:2]
        m.col[1][:2] = obb_mat_2d.col[1][:2]
        m.col[3][:2] = obb_mat_2d.col[2][:2]
        vecs = [(m * Vector(v).to_3d()).to_2d() for v in
                [(-x, -y),
                 (x, -y),
                 (x, y),
                 (-x, y)]
        ]
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[0], vecs[1])
        draw_line(vecs[2], vecs[3])
        bgl.glEnd()
        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        bgl.glEnd()

    testops.add_callback(draw, lifetime, 'POST_PIXEL')
コード例 #4
0
def test(use_random=True, random_count=10, lifetime=3.0):
    import bpy_extras
    import bgl
    from . import testops

    import localutils
    from localutils import convexhull
    # convexhull = localutils.convexhull

    context = bpy.context
    if context.mode != 'EDIT_MESH':
        return
    ob = context.active_object

    bm = bmesh.from_edit_mesh(ob.data)
    if use_random:
        bm.clear()

        for i in range(random_count):
            bm.verts.new([random.uniform(-1, 1) for i in range(3)])
        # bmesh.update_edit_mesh(ob.data)

    mat = ob.matrix_world
    coords = [mat * v.co for v in bm.verts]

    # convex_hull (3D)
    indices = convex_hull(coords)
    indices = convexhull.convex_hull(coords)
    if indices:
        for tri in indices:
            verts = [bm.verts[i] for i in tri]
            bm.faces.new(verts)
        bm.normal_update()
        bmesh.update_edit_mesh(ob.data)

    # obb (3D)
    obb_mat, obb_scale = OBB(coords)
    obb_mat, obb_scale = convexhull.OBB(coords)
    obb_mat = Matrix(obb_mat)
    obb_scale = Vector(obb_scale)

    def draw(context, event, glsettings):
        if context.mode != 'EDIT_MESH':
            return
        ob = context.active_object
        bm = bmesh.from_edit_mesh(ob.data)

        # convex_hull (2D)
        convex_hull_2d_coords = []
        project = bpy_extras.view3d_utils.location_3d_to_region_2d
        mat = ob.matrix_world
        region_coords = []
        for eve in bm.verts:
            v = project(context.region, context.region_data, mat * eve.co)
            region_coords.append(v)
        indices = convex_hull(region_coords)
        indices = convexhull.convex_hull(region_coords)
        for i in indices:
            convex_hull_2d_coords.append(region_coords[i])

        # obb (2D)
        obb_mat_2d, obb_scale_2d = OBB(region_coords)
        obb_mat_2d, obb_scale_2d = convexhull.OBB(region_coords)
        obb_mat_2d = Matrix(obb_mat_2d)
        obb_scale_2d = Vector(obb_scale_2d)

        # draw 3d obb
        cm = glsettings.region_view3d_space().enter()
        x, y, z = obb_scale / 2
        vecs = [
            obb_mat * Vector(v)
            for v in [(-x, -y,
                       -z), (x, -y, -z), (x, y,
                                          -z), (-x, y,
                                                -z), (-x, -y,
                                                      z), (x, -y,
                                                           z), (x, y,
                                                                z), (-x, y, z)]
        ]

        def draw_line(v1, v2):
            if len(v1) == 3:
                bgl.glVertex3f(*v1)
                bgl.glVertex3f(*v2)
            else:
                bgl.glVertex2f(*v1)
                bgl.glVertex2f(*v2)

        bgl.glLineWidth(2.0)
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        for v in vecs:
            bgl.glVertex3f(*v)
        bgl.glEnd()

        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        draw_line(vecs[5], vecs[6])
        draw_line(vecs[7], vecs[4])
        bgl.glEnd()

        # Z
        bgl.glColor3f(0.0, 0.0, 1.0)
        bgl.glBegin(bgl.GL_LINES)
        for i in range(4):
            draw_line(vecs[i], vecs[i + 4])
        bgl.glEnd()

        cm.exit()

        # draw 2d
        bgl.glLineWidth(1.0)
        bgl.glColor3f(1.0, 1.0, 1.0)
        bgl.glBegin(bgl.GL_LINE_LOOP)
        for v in convex_hull_2d_coords:
            bgl.glVertex2f(*v)
        bgl.glEnd()

        # draw 2d obb
        bgl.glColor3f(0.0, 1.0, 0.0)
        x, y = obb_scale_2d / 2
        m = Matrix.Identity(4)
        m.col[0][:2] = obb_mat_2d.col[0][:2]
        m.col[1][:2] = obb_mat_2d.col[1][:2]
        m.col[3][:2] = obb_mat_2d.col[2][:2]
        vecs = [(m * Vector(v).to_3d()).to_2d()
                for v in [(-x, -y), (x, -y), (x, y), (-x, y)]]
        # X
        bgl.glColor3f(1.0, 0.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[0], vecs[1])
        draw_line(vecs[2], vecs[3])
        bgl.glEnd()
        # Y
        bgl.glColor3f(0.0, 1.0, 0.0)
        bgl.glBegin(bgl.GL_LINES)
        draw_line(vecs[1], vecs[2])
        draw_line(vecs[3], vecs[0])
        bgl.glEnd()

    testops.add_callback(draw, lifetime, 'POST_PIXEL')