Trigonometric functions, logarithmic functions, cycloids ... There are so many functions, figures, and graphs in this world that you can count. You want to see such a graph in 3D, right?
This time I would like to use Blender and Excel. Put the coordinate data in the Excel file and create an object with Blender based on it.
Now that I understand how it works ...
windows 10 python 3.7 Blender 2.8 pip 20.2.1
First, let's install a library to mess with Excel files.
$pip install openpyxl
The code looks like this
function_3Dgraph.py
import openpyxl
import math
class funcdate():
def create_wb(self,wb_name):
new_file=openpyxl.Workbook() #Create the data in the Excel file.
new_file.save(wb_name+'.xlsx') #Save it once.
print('Excel file is created')
return new_file
def write_wb(self,cell_name,cell_value,file_name):
file=openpyxl.load_workbook(file_name+'.xlsx',data_only=True) #Read the Excel file.
new_wb=file.active #Load the sheet.
new_wb[cell_name]=cell_value #cell_mane(For example A1)Put a value in the cell.
def write_func(self,t,file_name):
file=openpyxl.load_workbook(file_name+'.xlsx',data_only=True)
new_wb=file.active
print(new_wb)
for i in range(1,t+1):
if(i==0):
i=1
x=math.cos(i*math.pi/180) #The formula here can be anything.
y=math.sin(i*math.pi/180) #The formula here can be anything.
new_wb['A'+str(i)]=i #I will put data in each coordinate.
new_wb['B'+str(i)]=x
new_wb['C'+str(i)]=y
print(new_wb['A'+str(i)].value,new_wb['B'+str(i)].value,new_wb['C'+str(i)].value)
else:
file.save(file_name+'.xlsx')
print('I was able to fill in')
def get_func(self,file_name,t):
file=openpyxl.load_workbook(file_name+'.xlsx',data_only=True)
wb=file.active
graph_t=wb['A'+str(t)].value #Enter the value of each cell in each coordinate.
graph_x=wb['B'+str(t)].value
graph_y=wb['C'+str(t)].value
a=[graph_t,graph_x,graph_y]
return a
test=funcdate()
test.create_wb('test_function')
test.write_func(360,'test_function')
print(test.get_func('test_function',10))
Let's run it now.
$python function_3Dgraph.py
When you execute it, the Excel file should be completed, so please open it. It's OK if it is in the table like this. By the way, it looks like this in a graph. ・ X-axis and y-axis are separated ・ X-axis and y-axis combined
First, let's download Blender. Download Blender Once downloaded, launch it and open the python editor. ↓ Here Then try typing the code below. It is easy to see if you delete the first generated cube (x key).
$import bpy
$bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=1.0, calc_uvs=True, enter_editmode=False, align='WORLD', location=(0.0, 0.0, 0.0), rotation=(0.0, 0.0, 0.0))
If a sphere appears at the coordinates (0.0, 0.0, 0.0), it is successful.
Let's add it now
function_3Dgraph.py
import openpyxl
import math
import bpy
class funcdate():
def create_wb(self,wb_name):
new_file=openpyxl.Workbook()
new_file.save(wb_name+'.xlsx')
print('Excel file is created')
return new_file
def write_wb(self,cell_name,cell_value,file_name):
file=openpyxl.load_workbook(file_name+'.xlsx',data_only=True)
new_wb=file.active
new_wb[cell_name]=cell_value
def write_func(self,t,file_name):
file=openpyxl.load_workbook(file_name+'.xlsx',data_only=True)
new_wb=file.active
print(new_wb)
for i in range(1,t+1):
if(i==0):
i=1
x=math.cos(i*math.pi/180)
y=math.sin(i*math.pi/180)
new_wb['A'+str(i)]=i
new_wb['B'+str(i)]=x
new_wb['C'+str(i)]=y
print(new_wb['A'+str(i)].value,new_wb['B'+str(i)].value,new_wb['C'+str(i)].value)
else:
file.save(file_name+'.xlsx')
print('I was able to fill in')
def get_func(self,file_name,t):
file=openpyxl.load_workbook(file_name+'.xlsx',data_only=True)
wb=file.active
graph_t=wb['A'+str(t)].value
graph_x=wb['B'+str(t)].value
graph_y=wb['C'+str(t)].value
a=[graph_t,graph_x,graph_y]
return a
test=funcdate()
test.create_wb('test_function')
test.write_func(360,'test_function')
for i in range(1,360):
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=1.0, calc_uvs=True, enter_editmode=False, align='WORLD', location=test.get_func('test_function',i), rotation=(0.0, 0.0, 0.0))
The bpy library can only be used in Blender, so run it in Blender.
** and before ** Let's make the openpyxl library available in Blender.
Detailed explanation on how to insert
Start as an administrator to use openpyxl with Blender. (Because it is said that you do not have authority ...)
Put Blender in Script mode to execute the file you just created. You can still go. Then open function_3Dgraph.py and run it.
If the graph comes out, it's a success! (1m of Blender is small, so it is difficult to understand, but it is a sine wave with an amplitude of 1. It will be easier to understand if the amplitude is set to 10.)
Well, the graph was completed like this.
By the way, the Excel file created by Blender is saved in C: \ Program Files \ Blender Foundation \ Blender 2.83.
Here's a little mathematical story ... This time, I made a graph by representing x and y with the parameter t. The formula looks like this.
\left\{
\begin{array}{ll}
x=f(t) \\
y=g(t)
\end{array}
\right.
This will create a 3D graph of $ y = f (x) $. Also, if $ t = z $, then $ ax = by = cz (a, b, c are arbitrary constants) $ and a three-dimensional straight line can be created. … So far, we have talked about orthogonal graphs. So what about the polar format? For example, suppose you have an expression like this:
r=f(θ)
The straight line represented by this equation is decomposed into a horizontal axis (x axis) and a vertical axis (y axis), respectively.
\left\{
\begin{array}{ll}
x=f(θ) \cos(θ)\\
y=f(θ) \sin(θ)
\end{array}
\right.
Now you can draw a polar graph. Actually, it's a good idea to use an Excel file for all this. ~~ If you don't use it, k ~~
Then why did you use it, for example ...
It is used when the graph does not understand such a formula. If there are values in the Excel file, you can create a graph by referring to them.
I think it's shrinking a little, but it's okay. There is no problem if you multiply it by n when taking the value ...
This time I tried to make a 3D graph with Blender using Excel. Next, I would like to move this graph like an animation.
Recommended Posts