Skip to content
Snippets Groups Projects
createMesh.py 80.7 KiB
Newer Older
# SPDX-License-Identifier: GPL-2.0-or-later
Brendon Murphy's avatar
Brendon Murphy committed
import bpy
from mathutils import (
        Matrix,
        Vector,
Aaron Keith's avatar
Aaron Keith committed
        geometry,
        )
from math import (
Aaron Keith's avatar
Aaron Keith committed
        tan, radians,atan,degrees
from random import triangular
Aaron Keith's avatar
Aaron Keith committed
from bpy_extras.object_utils import AddObjectHelper, object_data_add
Brendon Murphy's avatar
Brendon Murphy committed

NARROW_UI = 180
MAX_INPUT_NUMBER = 50

GLOBAL_SCALE = 1       # 1 blender unit = X mm
Brendon Murphy's avatar
Brendon Murphy committed


# next two utility functions are stolen from import_obj.py

def unpack_list(list_of_tuples):
    l = []
    for t in list_of_tuples:
        l.extend(t)
    return l

Brendon Murphy's avatar
Brendon Murphy committed
def unpack_face_list(list_of_tuples):
    l = []
    for t in list_of_tuples:
        face = [i for i in t]

        if len(face) != 3 and len(face) != 4:
            raise RuntimeError("{0} vertices in face".format(len(face)))
Brendon Murphy's avatar
Brendon Murphy committed
        # rotate indices if the 4th is 0
        if len(face) == 4 and face[3] == 0:
            face = [face[3], face[0], face[1], face[2]]

        if len(face) == 3:
            face.append(0)
Brendon Murphy's avatar
Brendon Murphy committed
        l.extend(face)

    return l

Brendon Murphy's avatar
Brendon Murphy committed
Remove Doubles takes a list on Verts and a list of Faces and
removes the doubles, much like Blender does in edit mode.
It doesn't have the range function  but it will round the coordinates
and remove verts that are very close together.  The function
Aaron Keith's avatar
Aaron Keith committed
is useful because you can perform a "Remove Doubles" with out
Brendon Murphy's avatar
Brendon Murphy committed
having to enter Edit Mode. Having to enter edit mode has the
disadvantage of not being able to interactively change the properties.
def RemoveDoubles(verts, faces, Decimal_Places=4):
    new_verts = []
    new_faces = []
    dict_verts = {}
    Rounded_Verts = []
        Rounded_Verts.append([round(v[0], Decimal_Places),
                              round(v[1], Decimal_Places),
                              round(v[2], Decimal_Places)])

    for face in faces:
        new_face = []
        for vert_index in face:
            Real_co = tuple(verts[vert_index])
            Rounded_co = tuple(Rounded_Verts[vert_index])

            if Rounded_co not in dict_verts:
                dict_verts[Rounded_co] = len(dict_verts)
                new_verts.append(Real_co)
            if dict_verts[Rounded_co] not in new_face:
                new_face.append(dict_verts[Rounded_co])
        if len(new_face) == 3 or len(new_face) == 4:
            new_faces.append(new_face)
    return new_verts, new_faces
def Scale_Mesh_Verts(verts, scale_factor):
Brendon Murphy's avatar
Brendon Murphy committed
    Ret_verts = []
    for v in verts:
        Ret_verts.append([v[0] * scale_factor, v[1] * scale_factor, v[2] * scale_factor])
Brendon Murphy's avatar
Brendon Murphy committed
    return Ret_verts


# Create a matrix representing a rotation.
Brendon Murphy's avatar
Brendon Murphy committed
#
# Parameters:
Brendon Murphy's avatar
Brendon Murphy committed
#
#        * angle (float) - The angle of rotation desired.
#        * matSize (int) - The size of the rotation matrix to construct. Can be 2d, 3d, or 4d.
#        * axisFlag (string (optional)) - Possible values:
#              o "x - x-axis rotation"
#              o "y - y-axis rotation"
#              o "z - z-axis rotation"
#              o "r - arbitrary rotation around vector"
#        * axis (Vector object. (optional)) - The arbitrary axis of rotation used with "R"
#
# Returns: Matrix object.
Brendon Murphy's avatar
Brendon Murphy committed
def Simple_RotationMatrix(angle, matSize, axisFlag):
    if matSize != 4:
        print("Simple_RotationMatrix can only do 4x4")
    q = radians(angle)  # make the rotation go clockwise
Brendon Murphy's avatar
Brendon Murphy committed
    if axisFlag == 'x':
        matrix = Matrix.Rotation(q, 4, 'X')
    elif axisFlag == 'y':
        matrix = Matrix.Rotation(q, 4, 'Y')
Brendon Murphy's avatar
Brendon Murphy committed
    elif axisFlag == 'z':
        matrix = Matrix.Rotation(q, 4, 'Z')
Brendon Murphy's avatar
Brendon Murphy committed
    else:
        print("Simple_RotationMatrix can only do x y z axis")
Brendon Murphy's avatar
Brendon Murphy committed
    return matrix


# ####################################################################
#              Converter Functions For Bolt Factory
# ####################################################################
Brendon Murphy's avatar
Brendon Murphy committed

def Flat_To_Radius(FLAT):
    h = (float(FLAT) / 2) / cos(radians(30))
Brendon Murphy's avatar
Brendon Murphy committed
    return h

Brendon Murphy's avatar
Brendon Murphy committed
def Get_Phillips_Bit_Height(Bit_Dia):
    Flat_Width_half = (Bit_Dia * (0.5 / 1.82)) / 2.0
Brendon Murphy's avatar
Brendon Murphy committed
    Bit_Rad = Bit_Dia / 2.0
    x = Bit_Rad - Flat_Width_half
    y = tan(radians(60)) * x
# ####################################################################
#                    Miscellaneous Utilities
# ####################################################################
Brendon Murphy's avatar
Brendon Murphy committed

# Returns a list of verts rotated by the given matrix. Used by SpinDup
def Rot_Mesh(verts, matrix):
Aaron Keith's avatar
Aaron Keith committed
    return [(matrix @ Vector(v))[:] for v in verts]
# Returns a list of faces that has there index incremented by offset
def Copy_Faces(faces, offset):
    return [[(i + offset) for i in f] for f in faces]
# Much like Blenders built in SpinDup
def SpinDup(VERTS, FACES, DEGREE, DIVISIONS, AXIS):
    verts = []
    faces = []
Brendon Murphy's avatar
Brendon Murphy committed
    if DIVISIONS == 0:
    step = DEGREE / DIVISIONS  # set step so pieces * step = degrees in arc
Brendon Murphy's avatar
Brendon Murphy committed
    for i in range(int(DIVISIONS)):
        rotmat = Simple_RotationMatrix(step * i, 4, AXIS)  # 4x4 rotation matrix, 30d about the x axis.
        Rot = Rot_Mesh(VERTS, rotmat)
        faces.extend(Copy_Faces(FACES, len(verts)))
Brendon Murphy's avatar
Brendon Murphy committed
        verts.extend(Rot)
    return verts, faces
Brendon Murphy's avatar
Brendon Murphy committed


# Returns a list of verts that have been moved up the z axis by DISTANCE
def Move_Verts_Up_Z(VERTS, DISTANCE):
Brendon Murphy's avatar
Brendon Murphy committed
    ret = []
    for v in VERTS:
        ret.append([v[0], v[1], v[2] + DISTANCE])
Brendon Murphy's avatar
Brendon Murphy committed
    return ret


# Returns a list of verts and faces that has been mirrored in the AXIS
def Mirror_Verts_Faces(VERTS, FACES, AXIS, FLIP_POINT=0):
Brendon Murphy's avatar
Brendon Murphy committed
    ret_vert = []
    ret_face = []
Brendon Murphy's avatar
Brendon Murphy committed
    if AXIS == 'y':
        for v in VERTS:
            Delta = v[0] - FLIP_POINT
            ret_vert.append([FLIP_POINT - Delta, v[1], v[2]])
Brendon Murphy's avatar
Brendon Murphy committed
    if AXIS == 'x':
        for v in VERTS:
            Delta = v[1] - FLIP_POINT
Loading
Loading full blame...