[Blender x Python] How to create an original object

table of contents

  1. Generate a plane
  2. Generate a cube
  3. Generate a quadrangular pyramid
  4. Generate a polygon
  5. Use modifiers
  6. Generate a randomly shaped mesh
  7. Generate a wavy mesh

0. Generate a plane

01.png

import bpy
 
#Define vertices and faces that form a plane
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]
faces = [(0,1,2,3)]
 
#Define a mesh
mesh = bpy.data.meshes.new("Plane_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

#Define an object from mesh data
obj = bpy.data.objects.new("Plane", mesh)   
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)

1. Generate a cube

02.png

import bpy
 
#Define the vertices and faces that form the cube
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
faces = [(0,1,2,3), (4,5,6,7), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]
 
#Define a mesh
mesh = bpy.data.meshes.new("Cube_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

#Define an object from mesh data
obj = bpy.data.objects.new("Cube", mesh)   
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)

2. Generate a quadrangular pyramid

03.png

import bpy
 
#Define the vertices and faces that form the quadrangular pyramid
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(2.5,2.5,4.5)]
faces = [(0,1,2,3), (0,4,1), (1,4,2), (2,4,3), (3,4,0)]
 
#Define a mesh
mesh = bpy.data.meshes.new("Pyramid_mesh")
#Generate a mesh from vertex and face data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

#Define an object from mesh data
obj = bpy.data.objects.new("Pyramid", mesh)   
#Specify the location where the object is created with the cursor
obj.location = bpy.context.scene.cursor.location
#Link objects to the scene(display)Let
bpy.context.scene.collection.objects.link(obj)

3. Generate a polygon

04.png

import bpy
import math

#Specify how many squares
p = 5

#Place the vertices starting from the center
verts = [(0,0,0)]
for i in range(0,p + 1):
    x = 2 *math.pi / p * i
    verts.append([math.cos(x),math.sin(x),0])
    
#A surface is formed by a total of 3 points, 2 points on the center and the circumference.
faces = []
for i in range(0,p):
    faces.append([0,i + 1,i + 2])
    
mesh = bpy.data.meshes.new("Polygon_mesh") 
mesh.from_pydata(verts, [], faces) 
obj = bpy.data.objects.new("Polygon", mesh) 
bpy.context.scene.collection.objects.link(obj)

#Put into edit mode
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
 
#Remove duplicate vertices
bpy.ops.mesh.remove_doubles()

#Return to object mode
bpy.ops.object.mode_set(mode='OBJECT')

4. Use modifiers

05.png

import bpy
 
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
faces = [(0,1,2,3), (7,6,5,4), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]
 
mesh = bpy.data.meshes.new("Cube_mesh")
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

obj = bpy.data.objects.new("Cube", mesh)
obj.location = bpy.context.scene.cursor.location

#Adapt subdivision surface
obj.modifiers.new("subd", type='SUBSURF')
#Determine the level of subdivision surface
obj.modifiers['subd'].levels = 3
bpy.context.scene.collection.objects.link(obj)
 
 
#Adapt smooth shade
mypolys = mesh.polygons
for p in mypolys:
    p.use_smooth = True

5. Generate a randomly shaped mesh

07.png

import bpy
import random

verts = []
faces = []
 
#Variables related to mesh
numX = 20
numY = 20
 
#Variables related to wave shape
amp = 0.5
scale = 1
 
#Generate vertex coordinates
for i in range (0, numX):
    for j in range(0,numY):
        x = scale * i
        y = scale * j
        z = (i*random.random())*amp
        vert = (x,y,z) 
        verts.append(vert)
 
#Generate a face from four vertices
count = 0
for i in range (0, numY *(numX-1)):
    if count < numY-1:
        A = i
        B = i+1
        C = (i+numY)+1
        D = (i+numY)
        face = (A,B,C,D)
        faces.append(face)
        count = count + 1
    else:
        count = 0
 
mesh = bpy.data.meshes.new("random mesh")
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

obj = bpy.data.objects.new("random mesh",mesh)
obj.location = bpy.context.scene.cursor.location
bpy.context.scene.collection.objects.link(obj)

 
#Adapt subdivision surface
obj.modifiers.new("subd", type='SUBSURF')
obj.modifiers['subd'].levels = 3
 
#Adapt smooth shade
mypolys = mesh.polygons
for p in mypolys:
    p.use_smooth = True

6. Generate a wavy mesh

08.png

import bpy
import math

verts = []
faces = []
 
#Variables related to mesh
numX = 10
numY = 10
 
#Variables related to wave shape
freq = 1
amp = 1
scale = 1

#Generate vertex coordinates
for i in range (0, numX):
    for j in range(0,numY):
 
        x = scale * i
        y = scale * j
        z = scale*((amp*math.cos(i*freq))+(amp*math.sin(j*freq)))
 
        vert = (x,y,z) 
        verts.append(vert)
 
#Generate a face from four vertices
count = 0
for i in range (0, numY *(numX-1)):
    if count < numY-1:
        A = i
        B = i+1
        C = (i+numY)+1
        D = (i+numY)
 
        face = (A,B,C,D)
        faces.append(face)
        count = count + 1
    else:
        count = 0
 
mesh = bpy.data.meshes.new("wave")
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

obj = bpy.data.objects.new("wave",mesh)
obj.location = (0,0,0)
bpy.context.scene.collection.objects.link(obj)

#Adapt subdivision surface
obj.modifiers.new("subd", type='SUBSURF')
obj.modifiers['subd'].levels = 3
 
#Adapt smooth shade
mypolys = mesh.polygons
for p in mypolys:
    p.use_smooth = True

Reference site Nathan's Blender Python Notebook

Recommended Posts

[Blender x Python] How to create an original object
[Blender x Python] How to make an animation
[Blender x Python] How to use modifiers
[Blender x Python] How to make vertex animation
How to create an image uploader in Bottle (Python)
How to create an email user
[Python Kivy] How to create an exe file with pyinstaller
How to know the internal structure of an object in Python
python3 How to install an external module
How to create an NVIDIA Docker environment
How to convert Python to an exe file
How to create a heatmap with an arbitrary domain in Python
How to erase Python 2.x on Mac.
How to create a function object from a string
How to create a JSON file in Python
How to crop an image with Python + OpenCV
How to create an OCF compliant resource agent
How to generate a Python object from JSON
[Algorithm x Python] How to use the list
How to install Python
How to install python
[Blender x Python] Blender Python tips (11/100)
How to create an article from the command line
How to know the current directory in Python in Blender
How to create a submenu with the [Blender] plugin
[Python] How to create Correlation Matrix and Heat Map
[Python] How to create a 2D histogram with Matplotlib
How to create a kubernetes pod from python code
[Blender] How to get the selection order of vertices, edges and faces of an object
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
An introduction to Python Programming
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Convert python 3.x code to python 2.x
How to use Python bytes
[Python] If you suddenly want to create an inquiry form
How to convert an array to a dictionary with Python [Application]
How to swap elements in an array in Python, and how to reverse an array.
Create an LCD (16x2) game with Raspberry Pi and Python
[Circuit x Python] How to solve circuit equations symbolically using sympy
[Python] How to write an if statement in one sentence.
How to make a Python package (written for an intern)
[Python Kivy] How to create a simple pop up window
How to run an app built with Python + py2app built with Anaconda
[Python] How to output a pandas table to an excel file
How to read an Excel file (.xlsx) with Pandas [Python]
How to create an ISO file (CD image) on Linux