# How create a ramp with angle with python script

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.

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]
coords.append(i)
coords.append(j)
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:
bm.verts.new(v)
# 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)])
bm.normal_update()

me = bpy.data.meshes.new("Shape")
bm.to_mesh(me)
# 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)

• 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/… Apr 21 at 12:15
• Thanks for the reaction, I come to add a script in my publication. if it's can permit you to help me please Apr 22 at 14:25

Add cube, resize, subdivide, move edge.

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

context = bpy.context

def add_ramp(x, y, z, ramp, angle):
bm = bmesh.new()
bmesh.ops.create_cube(
bm,
size=1,
matrix=(
Matrix.Diagonal((x, y, z, 1)) @
Matrix.Translation((.5, .5, .5))
)
)
pc = ramp / z
bm.edges.ensure_lookup_table()
edge = bm.edges[11]
edges = [bm.edges[i] for i in (1, 3, 6, 9)]
bmesh.ops.subdivide_edges(
bm,
edges=edges,
edge_percents={
e : (pc if e.verts[1].co.z > e.verts[0].co.z else 1 - pc)
for e in edges},
use_grid_fill=True,
cuts=1,
)
bmesh.ops.translate(
bm,
verts=edge.verts,
vec=(0, (z - ramp) * tan(angle), 0),
)
me = bpy.data.meshes.new("Shape")
bm.to_mesh(me)
ob = bpy.data.objects.new("Shape",me)