Blender 3D to 2d DXF

Small Python script witch can make 2d from selected 3D objects in Blender. Useful for furniture. For 2D I use qCad (free version).

import bpy, os

# get the current selection
selection = bpy.context.selected_objects

# initialize a blank result variable
element_width = "-x"
element_height = "z"

list_dims = []

# Put Origin to middle
#bpy.ops.object.origin_set( type = 'ORIGIN_GEOMETRY' )
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_VOLUME', center='MEDIAN')


# iterate through the selected objects
for sel in selection:
    # get the current object's dimensions
    dims = sel.dimensions
    
    # fix dimensions
    a = dims.x*1000
    b = dims.y*1000
    c = dims.z*1000
    
    # fix locations
    lcx = sel.location.x*1000
    lcy = sel.location.y*1000
    lcz = sel.location.z*1000
    
    w = 0
    h = 0
    
    lx = 0
    ly = 0

    x_and_width = {
    	# w, lx
    	"x": [round(a, 1), round(lcx - a/2, 3)],
    	"y": [round(b, 1), round(lcy - b/2, 3)],
    	"z": [round(c, 1), round(lcz - c/2, 3)],
    	"-x": [round(a, 1), round(-(lcx) - a/2, 3)],
    	"-y": [round(b, 1), round(-(lcy) - b/2, 3)],
    	"-z": [round(c, 1), round(-(lcz) - c/2, 3)]
    }

    y_and_height = {
    	"x": [round(a, 1), round(lcx - a/2, 3)],
    	"y": [round(b, 1), round(lcy - b/2, 3)],
    	"z": [round(c, 1), round(lcz - c/2, 3)],
    	"-x": [round(a, 1), round(-(lcx) - a/2, 3)],
    	"-y": [round(b, 1), round(-(lcy) - b/2, 3)],
    	"-z": [round(c, 1), round(-(lcz) - c/2, 3)]
    }

    celX = x_and_width[element_width]
    celY = y_and_height[element_height]

    list_dims.append( [ celX[0], celY[0], celX[1], celY[1] ] )


print("");
prnt_rect_funct = """
function rect(x, z, m){
    var doc = getDocument();
    var pList = [new RVector(0,0), new RVector(x,0), new RVector(x,z), new RVector(0,z)];
    var poly = new RPolyline(pList, true);
    var polyData = new RPolylineData(poly);
    var n = new RPolylineEntity(doc, polyData);
    addEntity(n);
    move(n, m);
}
"""
print(prnt_rect_funct)

for x in list_dims:
    print("rect(", x[0], ", ", x[1], ",", "[", x[2], ", ", x[3], "])")

Share and Enjoy !

Shares