I tried to get started with blender python script_Part 02

To write the continuation three years later ...

I touched Blender's Python console three years ago, and I had the opportunity to touch it for the first time in three years, so I would like to continue writing.

Blender version

Blender 2.8.1a: It looks like there have been major UI and API changes.

Debug environment on Blender

The layout is as follows.

無題.png

I placed the Text Editor at the bottom left of the figure display and the Python Editor at the bottom right.

Try to display the minimum point data.

The script is based on the reference Visualize point cloud with Blender + Python article.

import bpy
import numpy as np

def render(points):
    bpy.ops.object.select_all(action="SELECT")
    bpy.ops.object.delete(True)

    mat = bpy.data.materials.new("BLUE")
    mat.diffuse_color = (.33, .43, .95, 1)

    bpy.ops.mesh.primitive_plane_add(location=(0, 0, -1))
    plane = bpy.context.object
    plane.scale = (100, 100, 1)

    for x, y, z in points:
        bpy.ops.mesh.primitive_uv_sphere_add(location=(x, y, z))
        sphere = bpy.context.object
        sphere.scale = (0.02, 0.02, 0.02)
        sphere.data.materials.append(mat)

    for loc, rot in [[(0, -5, 5), (-np.pi * 1 / 4, 0, np.pi)],
                     [(0, 5, 5), (np.pi * 1 / 4, 0, -np.pi)],
                     [(-5, 0, 5), (-np.pi * 1 / 4, 0, np.pi / 2)],
                     [(5, 0, 5), (-np.pi * 1 / 4, 0, -np.pi / 2)]]:
        light_data = bpy.data.lights.new(name="light", type="AREA")
        light_data.energy = 500
        light_object = bpy.data.objects.new(name="light", object_data=light_data)
        bpy.context.collection.objects.link(light_object)
        bpy.context.view_layer.objects.active = light_object
        light_object.location = loc
        light_object.rotation_euler = rot

    bpy.ops.object.camera_add(location=(3.25, -2.48, 0.745))
    camera_rotations = (np.pi * 78 / 180, 0, 52.5 * np.pi / 180)
    bpy.data.objects["Camera"].rotation_euler = camera_rotations

    bpy.context.scene.render.resolution_x = 500
    bpy.context.scene.render.resolution_y = 500
    bpy.context.scene.render.resolution_percentage = 100
    bpy.context.scene.camera = bpy.context.object
    # bpy.context.scene.render.image_settings.file_format = "PNG"
    bpy.ops.render.render(write_still=True)

if __name__ == "__main__":
    points = np.loadtxt("C:\\Blender_works\\min_sample.csv", delimiter=",")
    render(points)

display2.png

Let's visualize about 1000 data.

Data creation script

import numpy as np
import pandas as pd
N = 1000
df =pd.DataFrame({
    "x": np.random.uniform(0.0, 10., N),
    "y": np.random.uniform(0.0, 10., N),
    "z": np.random.uniform(0.0, 10., N)
})
df.to_csv("./random_data.csv", header=False, index=False)

display3.png

next time

I would like to investigate the cooperation of Jupyter notebook.

reference

Recommended Posts

I tried to get started with blender python script_Part 01
I tried to get started with blender python script_Part 02
I tried to get started with Hy
I tried to get CloudWatch data with Python
[Python] A memo that I tried to get started with asyncio
Link to get started with python
How to get started with Python
Get started with Python in Blender
I tried to get started with Hy ・ Define a class
[Blender x Python] Let's get started with Blender Python !!
I tried to get started with Bitcoin Systre on the weekend
I tried to output LLVM IR with Python
I tried to automate sushi making with python
A layman wants to get started with Python
I tried fp-growth with python
I tried scraping with Python
I tried to get the authentication code of Qiita API with Python.
Get started with Python! ~ ② Grammar ~
I tried gRPC with Python
I tried scraping with python
I tried to get the movie information of TMDb API with Python
I tried to implement Minesweeper on terminal with python
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
[Python] I tried to get Json of squid ring 2
What I did to get started with Linux commands
I tried to solve AOJ's number theory with Python
I tried to touch Python (installation)
How to get started with Scrapy
How to get started with Django
I want to debug with Python
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried to make various "dummy data" with Python faker
I tried various methods to send Japanese mail with Python
Minimum knowledge to get started with the Python logging module
I tried "How to get a method decorated in Python"
[Python] I tried to visualize tweets about Corona with WordCloud
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python
I tried to summarize Python exception handling
I tried to implement PLSA in Python
Step notes to get started with django
I tried to solve the ant book beginner's edition with python
I tried to implement Autoencoder with TensorFlow
I tried to implement permutation in Python
I tried to visualize AutoEncoder with TensorFlow
A memorandum when I tried to get it automatically with selenium
I tried scraping Yahoo News with Python
I started python
I tried to implement PLSA in Python 2
[3rd] I tried to make a certain authenticator-like tool with python
Python3 standard input I tried to summarize
I wrote a script to get you started with AtCoder fast!
I wanted to solve ABC160 with Python