I am learning how to use blender with python script. I want to draw a block with ramp. until here, I can generate a block but my problem is precisely on how do the ramp. if you can help me please.

enter image description here

I know the point from which I must start the ramp coordinates(0, 4, 5) and (0, -4, 5). the angle is 60°

Here is the script(this script allows to draw a cube. if you help me to do a ramp with coordinates and angle known please):

import bpy, bmesh
from math import *
from mathutils import Vector
import numpy as np

def create_mesh(polygon):
    coords = []
    pos = 0
    while pos < len(polygon) - 1 :
        i = polygon[pos]
        pos_suivant = pos + 1
        if pos_suivant < len(polygon):
            j = polygon[pos_suivant]
            pos += 1
    return coords

def draw(zTop, polygon):
    """this function receives 2 arguments :
    * zTop of type Integr: it's the maximum height
    * polygon of type list of tuple: it's coordinates of polygon 
    * this function allows to draw a cube
    coords = create_mesh(polygon)
    bm = bmesh.new()
    for v in coords:
   # print(bm.verts)
    #création de la base 
    base = bm.faces.new(bm.verts)

    #processus d'extrusion
    hauteur = bmesh.ops.extrude_face_region(bm, geom=[base])

    #ajout d'auteur d'extrusion
    bmesh.ops.translate(bm, vec=Vector((0,0,zTop)), verts=[v for v in hauteur["geom"] if isinstance(v,bmesh.types.BMVert)])

    me = bpy.data.meshes.new("Shape")
    # ajouter à la scene
    ob = bpy.data.objects.new("Shape",me)
coords = [(0,4, 0),(0,-4, 0), (4,-4, 0), (4,4, 0)]

draw(10, coords)
  • 2
    $\begingroup$ Please share the current state of your code. In general: Blender is a 3d tool and there are hundreds of ways/techniques to create the provided mesh: using a boolean operation, slicing a cube, extruding polyons, beveling a cube etc. so I'd recommend learn how to model geometry first and then try to implement that using python. Otherwise have a look into the docs and read about Bmesh which is the recommended way of creating meshes via python: docs.blender.org/api/current/… $\endgroup$
    – brockmann
    Apr 21 at 12:15
  • $\begingroup$ Thanks for the reaction, I come to add a script in my publication. if it's can permit you to help me please $\endgroup$ Apr 22 at 14:25

Add cube, resize, subdivide, move edge.

enter image description here Image showing 3 test runs as in listed in code below

There are many ways to skin this cat, thought I would add a method to

Add a unit cube, scale to desired width x depth x height, and translate such that origin is as in question. (Could IMO use COG as origin and translate later)

The 4 vertical edges of cube are always the same index, will subdivide these, Insert new vertices into face using pyhton & bmesh Notice that the subdivide percent of each edge needs to be flipped depending on vert 0 of edge.

Finally the 11th original edge of cube is moved using Pythagoras (tangent)

import bpy
import bmesh
from mathutils import Matrix
from math import radians, tan

context = bpy.context

def add_ramp(x, y, z, ramp, angle):
    bm = bmesh.new()
                Matrix.Diagonal((x, y, z, 1)) @
                Matrix.Translation((.5, .5, .5))
    pc = ramp / z
    edge = bm.edges[11]
    edges = [bm.edges[i] for i in (1, 3, 6, 9)]
                e : (pc if e.verts[1].co.z > e.verts[0].co.z else 1 - pc) 
                 for e in edges},
            vec=(0, (z - ramp) * tan(angle), 0),
    me = bpy.data.meshes.new("Shape")
    ob = bpy.data.objects.new("Shape",me)
add_ramp(4, 8, 10, 5, radians(30))
add_ramp(4, 8, 10, 6, radians(45))
add_ramp(4, 8, 10, 5, radians(-30))
  • $\begingroup$ Good ol' Pythagoras... tireless.:) $\endgroup$ Apr 23 at 15:57
  • $\begingroup$ Hello, Thanks @batFINGER for your help. I tried your code, it works. if I use my Cube, I have this image as result: link. can you help me how do you cut neatly the cube please. $\endgroup$ Apr 27 at 14:15
  • $\begingroup$ The answer code relies on the order of the edges when creating a cube using bmesh create cube operator. If using other code, create just the code, take note of the edge order and adjust accordingly. Added link re hardcoding edge indices, or finding vertical edges.. Was not replying to this the reason for un-accepting answer? $\endgroup$
    – batFINGER
    Apr 30 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.