[PYTHON] How to make a 3D geometric figure with one click [From triangular pyramid to fractal]

Conclusion

Use blender python image.png

Writer's specs

・ Non-information system ・ I have never messed with the 3D model ・ Of course, blender is almost the first time

It's easy because you can do this!

What to make here

Make a 3D version of the above "Sierpinski Gasket"

How to do

I think that the screen of blender is divided into several, but you can execute it interactively by selecting "python console" from the switching among them. image.png

However, there are many things that cannot be done with this, so if you select "Text Editor" as a recommendation, you will see something like the one below! image.png Press the "New" button below here. That's all you need to do.

code

There are various notations, but it's basically easy. Let's take a look at the whole thing first.

import bpy
from math import sqrt

def duplicate_object_rename(arg_objectname='Default', arg_dupname='', position=(0,0,0)):
   for ob in bpy.context.scene.objects:
     ob.select=False
   selectob=bpy.context.scene.objects[arg_objectname]
   #bpy.context.scene.objects.active = selectob
   selectob.select=True
   bpy.ops.object.duplicate_move(
     OBJECT_OT_duplicate=None, TRANSFORM_OT_translate=None)
   # move object
   bpy.ops.transform.translate(value=position)
   selectob.select=False
   if len(arg_dupname) > 0:
     duplicated_objectname=arg_objectname + ".001"
     duplicatedob=bpy.data.objects[duplicated_objectname]
     duplicatedob.name=arg_dupname
   return

scale = 0.01
bpy.ops.mesh.primitive_cone_add(vertices=3,radius1=1*scale,depth=sqrt(2)*scale,location=(scale*sqrt(3)*(.5+.5*1+1),scale*1.5*(1+3*1)/3,scale*sqrt(2)/2),rotation=(0,0,0))
duplicate_object_rename(arg_objectname='Cone', arg_dupname='Duplicate1',position=(scale*sqrt(3),0,0))
duplicate_object_rename(arg_objectname='Cone', arg_dupname='Duplicate2',position=(scale*sqrt(3)/2,scale*1.5,0))
duplicate_object_rename(arg_objectname='Cone', arg_dupname='Duplicate3',position=(scale*sqrt(3)/2,scale*1/2,scale*sqrt(2)))
bpy.ops.object.select_by_type(type = 'MESH')
bpy.ops.object.join()

namehead = 'Duplicate'

for i in range(1,7):
    namebufprev = namehead + str(i)
    namebuf = namehead + str(i+1)
    duplicate_object_rename(arg_objectname=namebufprev, arg_dupname=namebuf,position=(scale*sqrt(3)*2*2**(i-1),0,0))
    duplicate_object_rename(arg_objectname=namebufprev, arg_dupname='buf2',position=(scale*sqrt(3)*2**(i-1),scale*3*2**(i-1),0))
    duplicate_object_rename(arg_objectname=namebufprev, arg_dupname='buf3',position=(scale*sqrt(3)*2**(i-1),scale*2**(i-1),scale*2*sqrt(2)*2**(i-1)))
    bpy.ops.object.select_by_type(type = 'MESH'))
    bpy.ops.object.join()

bpy.ops.object.origin_set(type = 'ORIGIN_GEOMETRY', center = 'MEDIAN') 

I will explain in order

1. Library

import bpy
from math import sqrt

I'm importing a module called bpy here, which is a blender function. These are installed from the beginning without using pip, so let's import without thinking about anything. Other basic modules are included from the beginning. Here, I'm importing math because I had to take the square root to find the position of the center of gravity of the triangle. It does not include pandas etc. You can do it if you want to install it yourself, but we won't cover it here.

2. Method

And how do you do it? I think it will be a story, but in fact the answer is already prepared. When I play with blender, the code pops up, and this is the method corresponding to that operation. For example

bpy.ops.mesh.primitive_cone_add(vertices=3,radius1=1*scale,depth=sqrt(2)*scale,location=(scale*sqrt(3)*(.5+.5*1+1),scale*1.5*(1+3*1)/3,scale*sqrt(2)/2),rotation=(0,0,0))

But this is equivalent to the cone of a primitive shape. You can understand it only by the pop-up, so I will try to make it partially by myself → I think that it will be a flow of automation. Therefore, those who can freely manipulate blender will not be so difficult, and those who do not will be caught in the operation rather than the coding.

3. Notation

This is almost no substitute for ordinary python. To put it bluntly, it's hard to tell what's wrong with debugging, so be patient. It consumes memory unexpectedly, so if you don't write it well, the operation tends to be slow. Even though I am using a proper PC this time, it is not output immediately, but it is output after one beat.

4. VCI conversion

After that, just output fbx and plunge into unity, but before that, be careful When using vci, it seems that if the number of subitems is too large, it will suddenly become heavy, and in my environment, if I put in 800, it will be a mess. And the number of objects in fractals increases rapidly just by touching the floor. (This time it's on the 7th floor, but if you don't put it together, the virtual cast will drop ...) So it's better to put the created objects together at the end. To put together

bpy.ops.object.select_by_type(type = 'MESH')
bpy.ops.object.join()    

Etc. would be convenient. Even if the size is heavy, the operation is not so slow because it is heavy to load, but the operation becomes heavy when the number of objects is large, and in some cases the seed online upload is rejected. The error message at that time was "chunk size is too large".

20191013-220416_89.png I brought it to the VR world with this

development

If you learn how to do it image.png

Like this image.png

Because this is image.png

reference

This article is almost like this second decoction You're doing a 3D version of Koch's snowflake here. https://qiita.com/hoji1107/items/99b2b669225d05990b5b I studied while modifying this code.

Click here for an explanation of the fractal itself https://ja.wikipedia.org/wiki/%E3%83%95%E3%83%A9%E3%82%AF%E3%82%BF%E3%83%AB

Is the English version better for Sierpinski Gasket? https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle

Recommended Posts

How to make a 3D geometric figure with one click [From triangular pyramid to fractal]
How to make a command to read the configuration file with pyramid
I came up with a way to make a 3D model from a photo.
How to make a dictionary with a hierarchical structure.
How to make a shooting game with toio (Part 1)
[Python] How to create a 2D histogram with Matplotlib
I came up with a way to make a 3D model from a photo. 0 Projection to 3D space
How to make a Cisco Webex Teams BOT with Flask
How to make a simple Flappy Bird game with pygame
I want to make a click macro with pyautogui (outlook)
How to make a Japanese-English translation
How to make a slack bot
How to make a surveillance camera (Security Camera) with Opencv and Python
How to make a recursive function
Try to make a web service-like guy with 3D markup language
How to make a .dylib library from a .a library on OSX (El Capitan)
[Blender] How to make a Blender plugin
How to make a crawler --Basic
How to display legend marks in one with Python 2D plot
How to create a clone from Github
How to add a package with PyCharm
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
Make a partially zoomed figure with matplotlib
How to create a repository from media
How to make a Backtrader custom indicator
How to make a Pelican site map
Let's make an A to B conversion web application with Flask! From scratch ...
How to drop Google Docs in one folder in a .txt file with python
SSH login to the target server from Windows with a click of a shortcut
WEB scraping with python and try to make a word cloud from reviews
How to make a program to solve Rubik's Cube starting from PC Koshien 2014 Floppy Cube
How to execute a query from psycopg2 built with SAM to Postgres launched with Docker
A new form of app that works with GitHub: How to make GitHub Apps
How to make a dialogue system dedicated to beginners
How to send a message to LINE with curl
A memorandum to make WebDAV only with nginx
How to open a web browser from python
How to draw a 2-axis graph with pyplot
How to create a function object from a string
How to draw a 3D graph before optimization
How to develop a cart app with Django
I want to make a game with Python
How to generate a Python object from JSON
Try to make a "cryptanalysis" cipher with Python
How to make a QGIS plugin (package generation)
How to extract coefficients from a fractional formula
One liner to make Lena images with scipy
I read "How to make a hacking lab"
Try to make a dihedral group with Python
How to create a multi-platform app with kivy
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
I tried to easily create a high-precision 3D image with one photo [0]. (Confirmed how to capture the space, put a net)
How to make Linux compatible with Japanese keyboard
(Memorandum) Make a 3D scatter plot with matplodlib
[Learning memo] How to make an application with Django ~ From virtual environment to pushing to github ~
Make one repeating string with a Python regular expression.
How to convert / restore a string with [] in python
How to scrape image data from flickr with python
Try to make a command standby tool with python
How to create a submenu with the [Blender] plugin