[PYTHON] Create a devilish picture with Blender scripts

** * Blender version is 2.80 **

Some modeling in Blender can be catastrophically reduced in time using Python scripts. The work of arranging a large number of objects is the best. I will show you an example.

Arrange simple shapes

Arrange primitives (simple figures).

If you split the window and press Shift + F11 with the cursor on one side, it will be as follows.

image.png

Press "New" to create a new script.

image.png

Copy the following text.


import bpy

#reset objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(True)

max = 20

for i in range(0,max):
    for j in range(0,max):
        bpy.ops.mesh.primitive_cone_add(
        radius1=.7,
        location=(i,j,0)
        )

Click "Run Script" or enter ʻAlt + P`, and the following picture will be created.

image.png

You can specify the repeat width by playing with the value of max. If it's about 20, I think it's okay with most computers, but if it's 100, it may freeze for a few minutes. radius1 specifies the radius of the bottom, and 0.7 specifies a value close to $ \ frac {1} {\ sqrt {2}} $ so that the bottoms of each other barely touch each other. To do.

More dramatic

image.png


import bpy
import math

#reset objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(True)

max = 20

for i in range(0,max):
    for j in range(0,max):
        height = 2+math.sin(i+j)
        bpy.ops.mesh.primitive_cone_add(
        radius1=.7,
        location=(i,j,height/2),
        depth = height)

Add variation to the height of the cone. Use trigonometric functions to change the height periodically. Since the height of the bottom does not match unless half of the height is specified by depth to the z position, depth = height and location = (i, j, height / 2) are used. I will.

Also use height

image.png

import bpy

#reset objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(True)

max = 5

for i in range(-max,max):
    for j in range(-max,max):
        for k in range(-max,max):
            bpy.ops.mesh.primitive_cone_add(
            location=(i*4,j*4,k*4)
            )

Feel the universe. The range of iterations is from 0 <max to -max <max for the convenience of future calculations.

Add changes (random)

image.png


import bpy
import numpy

#reset objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(True)

max = 5

for i in range(-max,max):
    for j in range(-max,max):
        for k in range(-max,max):
            bpy.ops.mesh.primitive_cone_add(
            location=(i*4,j*4,k*4),
            rotation=numpy.random.rand(3)*3,
            )

A random number is specified for rotation. This is beautiful with this.

Add changes (regular)

image.png

import bpy
import math

#reset objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(True)

max = 5

for i in range(-max,max):
    for j in range(-max,max):
        for k in range(-max,max):
            dist = math.sqrt(i*i+j*j)
            zx = math.atan2(j,i)
            zy = -math.pi/2-math.atan2(k,dist)
            bpy.ops.mesh.primitive_cone_add(
            location=(i*8,j*8,k*8),
            depth=dist*2,
            rotation=(0,zy,zx)
            )

All cones are centered. $ \ tan ^ {-1} (\ frac {j} {i}) $ for longitude, $ \ tan ^ {-1} (\ frac {k} {\ sqrt {i ^ 2 + j ^ 2}}) The latitude is calculated by $. The depth is also adjusted to create a feeling of being toward the center.

Copy and arrange arbitrary shapes

Since it is boring if it is only primitives, I will copy and arrange arbitrary objects. We will do it by arranging and selecting the one that will be the copy source, and copying it earnestly.

image.png

This is,

image.png

Like this.

import bpy
import numpy

for i in range(0,500):
    bpy.ops.object.duplicate()
    bpy.context.object.location = numpy.random.rand(3)*40-20
    bpy.context.object.rotation_euler = numpy.random.rand(3)*3

Import and arrange a large number of shapes

If you want to make further changes to the figure, you can prepare fbx, obj, or ply in advance and import a large amount of them. Just before We have generated a large number of human body models, so let's import them all at once and arrange them. It is necessary to prepare a large number of files in a specific folder in advance and prepare them so that they can be processed collectively by serial number.

image.png

Is it Neo Exdes?

import bpy
import numpy

#Enter the file path (up to just before the serial number) here
dir = r'~~~' 

for i in range(0,500):
    #Specify the serial number and extension (obj with this code)
    path = dir + str(i) + '.obj' 
    bpy.ops.import_scene.obj(filepath=path)

for i in range(0,500):
    bpy.data.objects[i].location = numpy.random.rand(3)*20-10

Recommended Posts

Create a devilish picture with Blender scripts
Create a star system with Blender 2.80 script
Create a homepage with django
Create a heatmap with pyqtgraph
Create a directory with python
Create a virtual environment with Python!
Create a file uploader with Django
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create a GUI app with Python's Tkinter
Create a large text file with shellscript
Create a virtual environment with Python_Mac version
Create a VM with a YAML file (KVM)
Create a simple web app with flask
Create a word frequency counter with Python 3.4
Create a Connecting Nearest Neighbor with NetworkX
Create a web service with Docker + Flask
Create a private repository with AWS CodeArtifact
Create a car meter with raspberry pi
Create a matrix with PythonGUI (text box)
Create a graph with borders removed with matplotlib
Create a frame with transparent background with tkinter [Python]
Create a GUI executable file created with tkinter
Create a game UI from scratch with pygame2!
Create a PDF file with a random page size
Create a virtual environment with conda in Python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
Bring your life to life procedurally with Blender scripts
Create a bulletin board with Heroku, Flask, SQLAlchemy
Create a dashboard for Network devices with Django!
Create a matrix with PythonGUI (tkinter combo box)
Create a color bar with Python + Qt (PySide)
Steps to create a Twitter bot with python
Create a decision tree from 0 with Python (1. Overview)
Create a color-specified widget with Python + Qt (PySide)
How to create a multi-platform app with kivy
Create a one-file hello world application with django
Create a Photoshop format file (.psd) with python
Create a cylinder with open3d + STL file output
Create a "Hello World" (HTTP) server with Tornado
Create a translation tool with the Translate Toolkit
Create a table of contents with IPython notebook
Try to dynamically create a Checkbutton with Python's Tkinter
Create a virtual environment with Anaconda installed via Pyenv
[Python] Create a ValueObject with a complete constructor using dataclasses
Create a Todo app with the Django REST framework
Why not create a stylish table easily with Python?
[Blender] Complement Blender's Python API with a text editor
Create a python development environment with vagrant + ansible + fabric
code-server Online environment (2) Create a virtual network with Boto3
Create a chatbot that supports free input with Word2Vec
Create a Todo app with Django ③ Create a task list page
Create a batch of images and inflate with ImageDataGenerator
Create a tweet heatmap with the Google Maps API
Create a random number with an arbitrary probability density
Create a Layer for AWS Lambda Python with Docker
Create a home music server with Centos8 + Universal Media Server