Script for building Lego®-like bricks in Blender 2.5.(6)

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

CCLDESTY
Posts: 1
Joined: Fri Jan 14, 2011 1:56 pm

Script for building Lego®-like bricks in Blender 2.5.(6)

Postby CCLDESTY » Fri Jan 14, 2011 2:07 pm

Hi,
I wrote a little script for building Lego®-like bricks!
You can set the amount of rows/columns, the height of the bricks and if there should be inner/top-pins!
I took the gems-script as a template!
Tested it with blender 2.5.6!

And here it is:(updated version to avoid backface-culling)

Code: Select all

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_addon_info = {
    "name": "Building Bricks",
    "author": "Destroyer Patrick K.",
    "version": (0,4),
    "blender": (2, 5, 6),
    "api": 32411,
    "location": "View3D > Add > Mesh > Building Bricks",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Add Mesh"}

import bpy
from mathutils import *
from math import *
from bpy.props import *

def align_matrix(context):
    loc = Matrix.Translation(context.scene.cursor_location)
    obj_align = context.user_preferences.edit.object_align
    if (context.space_data.type == 'VIEW_3D'
        and obj_align == 'VIEW'):
        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
    else:
        rot = Matrix()
    align_matrix = loc * rot
    return align_matrix

def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
    scene = context.scene
    obj_act = scene.objects.active

    # Can't edit anything, unless we have an active obj.
    if edit and not obj_act:
        return None

    # Create new mesh
    mesh = bpy.data.meshes.new(name)

    # Make a mesh from a list of verts/edges/faces.
    mesh.from_pydata(verts, edges, faces)

    # Update mesh geometry after adding stuff.
    mesh.update()

    # Deselect all objects.
    bpy.ops.object.select_all(action='DESELECT')

    if edit:
        # Replace geometry of existing object

        # Use the active obj and select it.
        ob_new = obj_act
        ob_new.select = True

        if obj_act.mode == 'OBJECT':
            # Get existing mesh datablock.
            old_mesh = ob_new.data

            # Set object data to nothing
            ob_new.data = None

            # Clear users of existing mesh datablock.
            old_mesh.user_clear()

            # Remove old mesh datablock if no users are left.
            if (old_mesh.users == 0):
                bpy.data.meshes.remove(old_mesh)

            # Assign new mesh datablock.
            ob_new.data = mesh

    else:
        # Create new object
        ob_new = bpy.data.objects.new(name, mesh)

        # Link new object to the given scene and select it.
        scene.objects.link(ob_new)
        ob_new.select = True

        # Place the object at the 3D cursor location.
        # apply viewRotaion
        ob_new.matrix_world = align_matrix

    if obj_act and obj_act.mode == 'EDIT':
        if not edit:
            # We are in EditMode, switch to ObjectMode.
            bpy.ops.object.mode_set(mode='OBJECT')

            # Select the active object as well.
            obj_act.select = True

            # Apply location of new object.
            scene.update()

            # Join new object into the active.
            bpy.ops.object.join()

            # Switching back to EditMode.
            bpy.ops.object.mode_set(mode='EDIT')

            ob_new = obj_act

    else:
        # We are in ObjectMode.
        # Make the new object the active one.
        scene.objects.active = ob_new

    return ob_new


def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
    faces = []

    if not vertIdx1 or not vertIdx2:
        return None

    if len(vertIdx1) < 2 and len(vertIdx2) < 2:
        return None

    fan = False
    if (len(vertIdx1) != len(vertIdx2)):
        if (len(vertIdx1) == 1 and len(vertIdx2) > 1):
            fan = True
        else:
            return None

    total = len(vertIdx2)

    if closed:
        # Bridge the start with the end.
        if flipped:
            face = [
                vertIdx1[0],
                vertIdx2[0],
                vertIdx2[total - 1]]
            if not fan:
                face.append(vertIdx1[total - 1])
            faces.append(face)

        else:
            face = [vertIdx2[0], vertIdx1[0]]
            if not fan:
                face.append(vertIdx1[total - 1])
            face.append(vertIdx2[total - 1])
            faces.append(face)

    # Bridge the rest of the faces.
    for num in range(total - 1):
        if flipped:
            if fan:
                face = [vertIdx2[num], vertIdx1[0], vertIdx2[num + 1]]
            else:
                face = [vertIdx2[num], vertIdx1[num],
                    vertIdx1[num + 1], vertIdx2[num + 1]]
            faces.append(face)
        else:
            if fan:
                face = [vertIdx1[0], vertIdx2[num], vertIdx2[num + 1]]
            else:
                face = [vertIdx1[num], vertIdx2[num],
                    vertIdx2[num + 1], vertIdx1[num + 1]]
            faces.append(face)

    return faces

def Add_Brick(rows, cols, height,cs,cpins,cipins):

    centr = []
    verts = []
    faces = []
    cs = cs*0.01;
    #outer_cube
    verts.append((0.0, 0.0,0.0))                                         #    4-----------5
    verts.append((cs*8.0*rows, cs*0.0*cols,cs*0.0*height))               #    |\          |\
    verts.append((cs*8.0*rows, cs*8.0*cols,cs*0.0*height))               #    |  7--------|-6
    verts.append((cs*0.0*rows, cs*8.0*cols,cs*0.0*height))               #    0--|--------1 |
                                                                         #     \ |         \|
    verts.append((cs*0.0*rows, cs*0.0*cols,cs*9.6*height/3))             #       3 ---------2
    verts.append((cs*8.0*rows, cs*0.0*cols,cs*9.6*height/3))
    verts.append((cs*8.0*rows, cs*8.0*cols,cs*9.6*height/3))
    verts.append((cs*0.0*rows, cs*8.0*cols,cs*9.6*height/3))

    faces.append([0,1,4])
    faces.append([1,5,4])
    faces.append([1,2,5])
    faces.append([2,6,5])
    faces.append([7,2,3])
    faces.append([7,6,2])
    faces.append([4,3,0])
    faces.append([4,7,3])
    faces.append([4,5,7])
    faces.append([5,6,7])

    #inner cube
    verts.append((cs*1.5, cs*1.5,cs*0.0))                                      #    12-----------13
    verts.append((cs*8.0*rows-cs*1.5, cs*0.0*cols+cs*1.5,cs*0.0*height))       #    |\           |\
    verts.append((cs*8.0*rows-cs*1.5, cs*8.0*cols-cs*1.5,cs*0.0*height))       #    |  15--------|-14
    verts.append((cs*0.0*rows+cs*1.5, cs*8.0*cols-cs*1.5,cs*0.0*height))       #  0-8--|--------9-1|
                                                                               #     \ |          \|
    verts.append((cs*1.5, cs*1.5,cs*9.6*height/3-cs*1))                        #     3-11 ---------10-2
    verts.append((cs*8.0*rows-cs*1.5, cs*0.0*cols+cs*1.5,cs*9.6*height/3-cs*1))
    verts.append((cs*8.0*rows-cs*1.5, cs*8.0*cols-cs*1.5,cs*9.6*height/3-cs*1))
    verts.append((cs*0.0*rows+cs*1.5, cs*8.0*cols-cs*1.5,cs*9.6*height/3-cs*1))

    faces.append([8,12,9])
    faces.append([9,12,13])
    faces.append([9,13,14])
    faces.append([9,14,10])

    faces.append([11,10,15])
    faces.append([10,14,15])
    faces.append([12,8,11])
    faces.append([12,11,15])

    faces.append([12,15,13])
    faces.append([13,15,14])

    faces.append([0,3,11])
    faces.append([0,11,8])
    faces.append([0,8,9])
    faces.append([0,9,1])
    faces.append([9,10,1])
    faces.append([10,2,1])
    faces.append([11,3,10])
    faces.append([10,3,2])

    #Pin
    if cpins == True:
       for pinrow in range(rows):
         for pincol in range(cols):
            vertcount = 16+((pinrow*(cols))+pincol)*17
            cx = pinrow*cs*8.0+cs*4.0
            cy = pincol*cs*8.0+cs*4.0
            cz = cs*9.6*height/3
            for sideindx in range(8):
               verts.append((cx+cs*2.5*sin(sideindx*45/180*3.141592), cy+cs*2.5*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*2.5*sin(sideindxx*45/180*3.141592), cy+cs*2.5*cos(sideindxx*45/180*3.141592), cz+cs*2.0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+8+triangs+1,vertcount+triangs+1])
            faces.append([vertcount+7,vertcount+8,vertcount])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+8+triangs+1,vertcount+triangs])
            faces.append([vertcount+8+7,vertcount+8,vertcount+7])
            verts.append((cx,cy,cz+cs*2.0))
            for triangss in range(7):
               faces.append([vertcount+16,vertcount+8+triangss+1,vertcount+8+triangss])
            faces.append([vertcount+16,vertcount+8,vertcount+8+7])

    #lowerPin
    if rows > 1 and cols > 1 and cipins == True:
       for pinrow in range(rows-1):
         for pincol in range(cols-1):
            if cpins == True:
                  vertcount = 16+cols*rows*17+((pinrow*(cols-1))+pincol)*32
            else:
                  vertcount = 16+((pinrow*(cols-1))+pincol)*32
            cx = pinrow*cs*8.0+cs*8.0
            cy = pincol*cs*8.0+cs*8.0
            cz = cs*9.6*height/3.0-cs*1.0
            for sideindx in range(8):
               verts.append((cx+cs*3.155*sin(sideindx*45/180*3.141592), cy+cs*3.155*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*3.155*sin(sideindxx*45/180*3.141592), cy+cs*3.155*cos(sideindxx*45/180*3.141592), 0.0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+8+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+8])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+triangs,vertcount+8+triangs+1])
            faces.append([vertcount+8+7,vertcount+7,vertcount+8])

            #inner lowerpin
            vertcount = vertcount+16
            for sideindx in range(8):
               verts.append((cx+cs*2.405*sin(sideindx*45/180*3.141592), cy+cs*2.405*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*2.405*sin(sideindxx*45/180*3.141592), cy+cs*2.405*cos(sideindxx*45/180*3.141592), 0.0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+8+triangs+1,vertcount+triangs+1])
            faces.append([vertcount+7,vertcount+8,vertcount])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+8+triangs+1,vertcount+triangs])
            faces.append([vertcount+8+7,vertcount+8,vertcount+7])

            #joints inner<->outer lowerpin

            vertcount = vertcount-8
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+16+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+16])
            for triangs in range(7):
               faces.append([vertcount+16+triangs,vertcount+triangs,vertcount+16+triangs+1])
            faces.append([vertcount+16+7,vertcount+7,vertcount+16])

     #Pins for one-row-bricks
    if (rows == 1 and cols > 1)and cipins == True:
       for pincol in range(cols-1):
            if cpins == True:
                   vertcount = 16+cols*rows*17+pincol*17
            else:
                   vertcount = 16+pincol*17
            cx = cs*4.0
            cy = (pincol+1)*cs*8.0
            cz = cs*9.6*height/3-cs*1.0
            for sideindx in range(8):
               verts.append((cx+cs*1.5*sin(sideindx*45/180*3.141592), cy+cs*1.5*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*1.5*sin(sideindxx*45/180*3.141592), cy+cs*1.5*cos(sideindxx*45/180*3.141592), 0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+8+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+8])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+triangs,vertcount+8+triangs+1])
            faces.append([vertcount+8+7,vertcount+7,vertcount+8])
            verts.append((cx,cy,0))
            for triangss in range(7):
               faces.append([vertcount+16,vertcount+8+triangss,vertcount+8+triangss+1])
            faces.append([vertcount+16,vertcount+8+7,vertcount+8])

    #Pins for one-column-bricks
    if (rows > 1 and cols == 1)and cipins == True:
       for pinrow in range(rows-1):
            if cpins == True:
                   vertcount = 16+cols*rows*17+pinrow*17
            else:
                   vertcount = 16+pinrow*17
            cx = (pinrow+1)*cs*8.0
            cy = cs*4.0
            cz = cs*9.6*height/3-cs*1.0
            for sideindx in range(8):
               verts.append((cx+cs*1.5*sin(sideindx*45/180*3.141592), cy+cs*1.5*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*1.5*sin(sideindxx*45/180*3.141592), cy+cs*1.5*cos(sideindxx*45/180*3.141592), 0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+8+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+8])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+triangs,vertcount+8+triangs+1])
            faces.append([vertcount+8+7,vertcount+7,vertcount+8])
            verts.append((cx,cy,0))
            for triangss in range(7):
               faces.append([vertcount+16,vertcount+8+triangss,vertcount+8+triangss+1])
            faces.append([vertcount+16,vertcount+8+7,vertcount+8])

    return verts, faces

class AddBrick(bpy.types.Operator):
    bl_idname = "mesh.primitive_brick_add"
    bl_label = "Add Brick"
    bl_description = "Create a custom Brick"
    bl_options = {'REGISTER', 'UNDO'}

    # edit - Whether to add or update.
    edit = BoolProperty(name="",
        description="",
        default=False,
        options={'HIDDEN'})
    rows = IntProperty(name="Rows",
        description="Number of rows",
        min=1,
        max=100,
        default=2,)
    cols = IntProperty(name="Cols",
        description="Number of Columns",
        min=1,
        max=100,
        default=2,)
    height = IntProperty(name="Heigth",
        description="Heigth of Brick 3=standard",
        min=1,
        max=15,
        default=3,)

    conscale = FloatProperty(name="ConstructionScale",
        description="Set the constructionscale of the part in %",
        min=1,
        max=10000,
        default=100)
    pins = BoolProperty(name="Pins",
        description="Create Pins",
        default=True,)
    inpins = BoolProperty(name="Inner Pins",
        description="Create Inner Pins",
        default=True,)
    align_matrix = Matrix()

    def execute(self, context):

        # create mesh
        verts, faces = Add_Brick(self.rows,
                                self.cols,
                                self.height,
                                self.conscale,
                                self.pins,
                                self.inpins)



        obj = create_mesh_object(context, verts, [], faces, "Brick", self.edit, self.align_matrix)

        return {'FINISHED'}

    def invoke(self, context, event):
        self.align_matrix = align_matrix(context)
        self.execute(context)
        return {'FINISHED'}

class INFO_MT_mesh_Brick_add(bpy.types.Menu):
    bl_idname = "INFO_MT_mesh_brick_add"
    bl_label = "Brick"

    def draw(self, context):
        layout = self.layout
        layout.operator_context = 'INVOKE_REGION_WIN'
        layout.operator("mesh.primitive_brick_add",
            text="Brick")

import space_info


def menu_func(self, context):
    self.layout.menu("INFO_MT_mesh_brick_add", icon="PLUGIN")


def register():
    space_info.INFO_MT_mesh_add.append(menu_func)


def unregister():
    space_info.INFO_MT_mesh_add.remove(menu_func)

if __name__ == "__main__":
    register()


Have fun building ;D

fockkitten
Posts: 2
Joined: Sat Jan 15, 2011 9:27 pm

Postby fockkitten » Sat Jan 15, 2011 9:31 pm

thanks you very much for this script it is very useful to build bricks, but there is a mesh error also at 2883x663MO please fix it because this sucks

fockkitten
Posts: 2
Joined: Sat Jan 15, 2011 9:27 pm

Postby fockkitten » Sat Jan 15, 2011 9:41 pm

hey dude i joined especially to get your script at but there is an error occuring when i open the script in blender there is a mesh error in the cube i cant also add a texture to the bricks ?a

stiv
Posts: 3646
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Postby stiv » Sat Jan 15, 2011 10:59 pm

I wrote a little script for building Lego®-like bricks!

You might consider uploading your script to the Extensions Tracker. Makes it easier to download than copying from a web page and who knows - it might become part of Blender!

http://wiki.blender.org/index.php/Dev:Py/Sharing

andynu
Posts: 1
Joined: Wed Oct 10, 2012 5:03 am

Postby andynu » Wed Oct 10, 2012 5:11 am

Minor update for 2.6.

Really just replacing the space_info calls with bpy.utils substitutions, lines 438 down.

Code: Select all

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_addon_info = {
    "name": "Building Bricks",
    "author": "Destroyer Patrick K.",
    "version": (0,4),
    "blender": (2, 5, 6),
    "api": 32411,
    "location": "View3D > Add > Mesh > Building Bricks",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Add Mesh"}

import bpy
from mathutils import *
from math import *
from bpy.props import *

def align_matrix(context):
    loc = Matrix.Translation(context.scene.cursor_location)
    obj_align = context.user_preferences.edit.object_align
    if (context.space_data.type == 'VIEW_3D'
        and obj_align == 'VIEW'):
        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
    else:
        rot = Matrix()
    align_matrix = loc * rot
    return align_matrix

def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
    scene = context.scene
    obj_act = scene.objects.active

    # Can't edit anything, unless we have an active obj.
    if edit and not obj_act:
        return None

    # Create new mesh
    mesh = bpy.data.meshes.new(name)

    # Make a mesh from a list of verts/edges/faces.
    mesh.from_pydata(verts, edges, faces)

    # Update mesh geometry after adding stuff.
    mesh.update()

    # Deselect all objects.
    bpy.ops.object.select_all(action='DESELECT')

    if edit:
        # Replace geometry of existing object

        # Use the active obj and select it.
        ob_new = obj_act
        ob_new.select = True

        if obj_act.mode == 'OBJECT':
            # Get existing mesh datablock.
            old_mesh = ob_new.data

            # Set object data to nothing
            ob_new.data = None

            # Clear users of existing mesh datablock.
            old_mesh.user_clear()

            # Remove old mesh datablock if no users are left.
            if (old_mesh.users == 0):
                bpy.data.meshes.remove(old_mesh)

            # Assign new mesh datablock.
            ob_new.data = mesh

    else:
        # Create new object
        ob_new = bpy.data.objects.new(name, mesh)

        # Link new object to the given scene and select it.
        scene.objects.link(ob_new)
        ob_new.select = True

        # Place the object at the 3D cursor location.
        # apply viewRotaion
        ob_new.matrix_world = align_matrix

    if obj_act and obj_act.mode == 'EDIT':
        if not edit:
            # We are in EditMode, switch to ObjectMode.
            bpy.ops.object.mode_set(mode='OBJECT')

            # Select the active object as well.
            obj_act.select = True

            # Apply location of new object.
            scene.update()

            # Join new object into the active.
            bpy.ops.object.join()

            # Switching back to EditMode.
            bpy.ops.object.mode_set(mode='EDIT')

            ob_new = obj_act

    else:
        # We are in ObjectMode.
        # Make the new object the active one.
        scene.objects.active = ob_new

    return ob_new


def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
    faces = []

    if not vertIdx1 or not vertIdx2:
        return None

    if len(vertIdx1) < 2 and len(vertIdx2) < 2:
        return None

    fan = False
    if (len(vertIdx1) != len(vertIdx2)):
        if (len(vertIdx1) == 1 and len(vertIdx2) > 1):
            fan = True
        else:
            return None

    total = len(vertIdx2)

    if closed:
        # Bridge the start with the end.
        if flipped:
            face = [
                vertIdx1[0],
                vertIdx2[0],
                vertIdx2[total - 1]]
            if not fan:
                face.append(vertIdx1[total - 1])
            faces.append(face)

        else:
            face = [vertIdx2[0], vertIdx1[0]]
            if not fan:
                face.append(vertIdx1[total - 1])
            face.append(vertIdx2[total - 1])
            faces.append(face)

    # Bridge the rest of the faces.
    for num in range(total - 1):
        if flipped:
            if fan:
                face = [vertIdx2[num], vertIdx1[0], vertIdx2[num + 1]]
            else:
                face = [vertIdx2[num], vertIdx1[num],
                    vertIdx1[num + 1], vertIdx2[num + 1]]
            faces.append(face)
        else:
            if fan:
                face = [vertIdx1[0], vertIdx2[num], vertIdx2[num + 1]]
            else:
                face = [vertIdx1[num], vertIdx2[num],
                    vertIdx2[num + 1], vertIdx1[num + 1]]
            faces.append(face)

    return faces

def Add_Brick(rows, cols, height,cs,cpins,cipins):

    centr = []
    verts = []
    faces = []
    cs = cs*0.01;
    #outer_cube
    verts.append((0.0, 0.0,0.0))                                         #    4-----------5
    verts.append((cs*8.0*rows, cs*0.0*cols,cs*0.0*height))               #    |\          |\
    verts.append((cs*8.0*rows, cs*8.0*cols,cs*0.0*height))               #    |  7--------|-6
    verts.append((cs*0.0*rows, cs*8.0*cols,cs*0.0*height))               #    0--|--------1 |
                                                                         #     \ |         \|
    verts.append((cs*0.0*rows, cs*0.0*cols,cs*9.6*height/3))             #       3 ---------2
    verts.append((cs*8.0*rows, cs*0.0*cols,cs*9.6*height/3))
    verts.append((cs*8.0*rows, cs*8.0*cols,cs*9.6*height/3))
    verts.append((cs*0.0*rows, cs*8.0*cols,cs*9.6*height/3))

    faces.append([0,1,4])
    faces.append([1,5,4])
    faces.append([1,2,5])
    faces.append([2,6,5])
    faces.append([7,2,3])
    faces.append([7,6,2])
    faces.append([4,3,0])
    faces.append([4,7,3])
    faces.append([4,5,7])
    faces.append([5,6,7])

    #inner cube
    verts.append((cs*1.5, cs*1.5,cs*0.0))                                      #    12-----------13
    verts.append((cs*8.0*rows-cs*1.5, cs*0.0*cols+cs*1.5,cs*0.0*height))       #    |\           |\
    verts.append((cs*8.0*rows-cs*1.5, cs*8.0*cols-cs*1.5,cs*0.0*height))       #    |  15--------|-14
    verts.append((cs*0.0*rows+cs*1.5, cs*8.0*cols-cs*1.5,cs*0.0*height))       #  0-8--|--------9-1|
                                                                               #     \ |          \|
    verts.append((cs*1.5, cs*1.5,cs*9.6*height/3-cs*1))                        #     3-11 ---------10-2
    verts.append((cs*8.0*rows-cs*1.5, cs*0.0*cols+cs*1.5,cs*9.6*height/3-cs*1))
    verts.append((cs*8.0*rows-cs*1.5, cs*8.0*cols-cs*1.5,cs*9.6*height/3-cs*1))
    verts.append((cs*0.0*rows+cs*1.5, cs*8.0*cols-cs*1.5,cs*9.6*height/3-cs*1))

    faces.append([8,12,9])
    faces.append([9,12,13])
    faces.append([9,13,14])
    faces.append([9,14,10])

    faces.append([11,10,15])
    faces.append([10,14,15])
    faces.append([12,8,11])
    faces.append([12,11,15])

    faces.append([12,15,13])
    faces.append([13,15,14])

    faces.append([0,3,11])
    faces.append([0,11,8])
    faces.append([0,8,9])
    faces.append([0,9,1])
    faces.append([9,10,1])
    faces.append([10,2,1])
    faces.append([11,3,10])
    faces.append([10,3,2])

    #Pin
    if cpins == True:
       for pinrow in range(rows):
         for pincol in range(cols):
            vertcount = 16+((pinrow*(cols))+pincol)*17
            cx = pinrow*cs*8.0+cs*4.0
            cy = pincol*cs*8.0+cs*4.0
            cz = cs*9.6*height/3
            for sideindx in range(8):
               verts.append((cx+cs*2.5*sin(sideindx*45/180*3.141592), cy+cs*2.5*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*2.5*sin(sideindxx*45/180*3.141592), cy+cs*2.5*cos(sideindxx*45/180*3.141592), cz+cs*2.0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+8+triangs+1,vertcount+triangs+1])
            faces.append([vertcount+7,vertcount+8,vertcount])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+8+triangs+1,vertcount+triangs])
            faces.append([vertcount+8+7,vertcount+8,vertcount+7])
            verts.append((cx,cy,cz+cs*2.0))
            for triangss in range(7):
               faces.append([vertcount+16,vertcount+8+triangss+1,vertcount+8+triangss])
            faces.append([vertcount+16,vertcount+8,vertcount+8+7])

    #lowerPin
    if rows > 1 and cols > 1 and cipins == True:
       for pinrow in range(rows-1):
         for pincol in range(cols-1):
            if cpins == True:
                  vertcount = 16+cols*rows*17+((pinrow*(cols-1))+pincol)*32
            else:
                  vertcount = 16+((pinrow*(cols-1))+pincol)*32
            cx = pinrow*cs*8.0+cs*8.0
            cy = pincol*cs*8.0+cs*8.0
            cz = cs*9.6*height/3.0-cs*1.0
            for sideindx in range(8):
               verts.append((cx+cs*3.155*sin(sideindx*45/180*3.141592), cy+cs*3.155*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*3.155*sin(sideindxx*45/180*3.141592), cy+cs*3.155*cos(sideindxx*45/180*3.141592), 0.0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+8+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+8])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+triangs,vertcount+8+triangs+1])
            faces.append([vertcount+8+7,vertcount+7,vertcount+8])

            #inner lowerpin
            vertcount = vertcount+16
            for sideindx in range(8):
               verts.append((cx+cs*2.405*sin(sideindx*45/180*3.141592), cy+cs*2.405*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*2.405*sin(sideindxx*45/180*3.141592), cy+cs*2.405*cos(sideindxx*45/180*3.141592), 0.0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+8+triangs+1,vertcount+triangs+1])
            faces.append([vertcount+7,vertcount+8,vertcount])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+8+triangs+1,vertcount+triangs])
            faces.append([vertcount+8+7,vertcount+8,vertcount+7])

            #joints inner<->outer lowerpin

            vertcount = vertcount-8
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+16+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+16])
            for triangs in range(7):
               faces.append([vertcount+16+triangs,vertcount+triangs,vertcount+16+triangs+1])
            faces.append([vertcount+16+7,vertcount+7,vertcount+16])

     #Pins for one-row-bricks
    if (rows == 1 and cols > 1)and cipins == True:
       for pincol in range(cols-1):
            if cpins == True:
                   vertcount = 16+cols*rows*17+pincol*17
            else:
                   vertcount = 16+pincol*17
            cx = cs*4.0
            cy = (pincol+1)*cs*8.0
            cz = cs*9.6*height/3-cs*1.0
            for sideindx in range(8):
               verts.append((cx+cs*1.5*sin(sideindx*45/180*3.141592), cy+cs*1.5*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*1.5*sin(sideindxx*45/180*3.141592), cy+cs*1.5*cos(sideindxx*45/180*3.141592), 0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+8+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+8])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+triangs,vertcount+8+triangs+1])
            faces.append([vertcount+8+7,vertcount+7,vertcount+8])
            verts.append((cx,cy,0))
            for triangss in range(7):
               faces.append([vertcount+16,vertcount+8+triangss,vertcount+8+triangss+1])
            faces.append([vertcount+16,vertcount+8+7,vertcount+8])

    #Pins for one-column-bricks
    if (rows > 1 and cols == 1)and cipins == True:
       for pinrow in range(rows-1):
            if cpins == True:
                   vertcount = 16+cols*rows*17+pinrow*17
            else:
                   vertcount = 16+pinrow*17
            cx = (pinrow+1)*cs*8.0
            cy = cs*4.0
            cz = cs*9.6*height/3-cs*1.0
            for sideindx in range(8):
               verts.append((cx+cs*1.5*sin(sideindx*45/180*3.141592), cy+cs*1.5*cos(sideindx*45/180*3.141592), cz))
            for sideindxx in range(8):
               verts.append((cx+cs*1.5*sin(sideindxx*45/180*3.141592), cy+cs*1.5*cos(sideindxx*45/180*3.141592), 0))
            for triangs in range(7):
               faces.append([vertcount+triangs,vertcount+triangs+1,vertcount+8+triangs+1])
            faces.append([vertcount+7,vertcount,vertcount+8])
            for triangs in range(7):
               faces.append([vertcount+8+triangs,vertcount+triangs,vertcount+8+triangs+1])
            faces.append([vertcount+8+7,vertcount+7,vertcount+8])
            verts.append((cx,cy,0))
            for triangss in range(7):
               faces.append([vertcount+16,vertcount+8+triangss,vertcount+8+triangss+1])
            faces.append([vertcount+16,vertcount+8+7,vertcount+8])

    return verts, faces

class AddBrick(bpy.types.Operator):
    bl_idname = "mesh.primitive_brick_add"
    bl_label = "Add Brick"
    bl_description = "Create a custom Brick"
    bl_options = {'REGISTER', 'UNDO'}

    # edit - Whether to add or update.
    edit = BoolProperty(name="",
        description="",
        default=False,
        options={'HIDDEN'})
    rows = IntProperty(name="Rows",
        description="Number of rows",
        min=1,
        max=100,
        default=2,)
    cols = IntProperty(name="Cols",
        description="Number of Columns",
        min=1,
        max=100,
        default=2,)
    height = IntProperty(name="Heigth",
        description="Heigth of Brick 3=standard",
        min=1,
        max=15,
        default=3,)

    conscale = FloatProperty(name="ConstructionScale",
        description="Set the constructionscale of the part in %",
        min=1,
        max=10000,
        default=100)
    pins = BoolProperty(name="Pins",
        description="Create Pins",
        default=True,)
    inpins = BoolProperty(name="Inner Pins",
        description="Create Inner Pins",
        default=True,)
    align_matrix = Matrix()

    def execute(self, context):

        # create mesh
        verts, faces = Add_Brick(self.rows,
                                self.cols,
                                self.height,
                                self.conscale,
                                self.pins,
                                self.inpins)



        obj = create_mesh_object(context, verts, [], faces, "Brick", self.edit, self.align_matrix)

        return {'FINISHED'}

    def invoke(self, context, event):
        self.align_matrix = align_matrix(context)
        self.execute(context)
        return {'FINISHED'}

class INFO_MT_mesh_Brick_add(bpy.types.Menu):
    bl_idname = "INFO_MT_mesh_brick_add"
    bl_label = "Brick"

    def draw(self, context):
        layout = self.layout
        layout.operator_context = 'INVOKE_REGION_WIN'
        layout.operator("mesh.primitive_brick_add",
            text="Brick")

#import space_info


def menu_func(self, context):
    self.layout.menu("INFO_MT_mesh_brick_add", icon="PLUGIN")


def register():
    #space_info.INFO_MT_mesh_add.append(menu_func)
    bpy.utils.register_module(__name__)


def unregister():
    #space_info.INFO_MT_mesh_add.remove(menu_func)
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()


Return to “Python”

Who is online

Users browsing this forum: Bing [Bot] and 0 guests