Introduction to Blender Python API 简单介绍Blender Python API 入门篇

2 months ago

Introduction to Blender Python API 简单介绍Blender Python API 入门篇。

There's a built in Python interpreter within Blender, which can be used to automate certain work flow in Blender.

Blender 内有个Python的直释器, 可以用来自动化一些流程。

img

Click at the Editor Type on bottom left, select Python Console.

点击左下角的编辑种类,点选Python控制台

img

in the console type:

在控制台,输入

bpy.data.objects["Cube"].location.x += 0.5

where you can basically move the objects around in viewport using the Python commands. The "Cube" is the name of the object of choice which can renamed in object property panel.

基本上你可以用python 指令操作画面上的任何物件。 例子里的Cube是物件名称可以在物件属性编辑栏位编辑。

You can also set all x,y,z coordinates in one single command as such

也可以用以下这个 x,y,z 坐标指令

bpy.data.objects["Cube"].location.xyz = ( 1,2,1 )

You can also access the vertices, edges and faces of the cube using the following command.

你也可以访问物件的顶点,边,和面

bpy.data.objects["Cube"].data.vertices
bpy.data.objects["Cube"].data.edges
bpy.data.objects["Cube"].data.polygons

For example, if you want to move 1 single vertex, you can do

例子:假设你想换1个顶点的x 座标

bpy.data.objects["Cube"].data.vertices[0].co.x = 20

Note that the viewport needs to be in Object mode. It will not work in Editable mode

视端必须在物件模式而不是编辑模式才会反映指令的效果。

img

You can use type() function to check the data type of the object. For example

也可以透过 type() 来知道物件的种类。

img

then you can refer to the following document to see what properties and methods the objects contain.

知道物件种类后,可以参考官方文件来知道更多该物件的属性和函数。

https://docs.blender.org/api/current/


Using Blender Python to create 3D chart 简单使用Python来制造三维图表

Instead of writing your code in the Python interactive console, you can write it as a script.

也可以用文本编辑来写你的代码

img

we can start with a simple plane as such:

简单加个平面物件

img

Subdivide into 16x16 grid

分割成 16x16 宫格

img

简单正弦函数会有出现以下图标
Example of simple sine function on 3D plane.

img

Add more subdivide into the plane to add more resolution,
and create more interesting patterns such as this is

若想要更细的解析度,可以把物件分割更细,
然后也可以用比较有趣的函数。

import bpy
from random import *
from mathutils import *
import math

plane_mesh = bpy.data.objects["Plane"].data
row = 65
col = 65

for i in range(0,row):
    for j in range(0,col):
        v = plane_mesh.vertices[i * col + j].co
        v.z = ( math.sin( (v.x + 0.5)* 2 * math.pi ) * math.sin( (v.y + 0.5) * 2 *  math.pi )  )  * 0.5

img

If you want, you can even render your output as image.

高兴的话,也可以把图渲染出来。

img

Have fun!

玩的开心点。

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  trending

太棒了!!!我好喜欢这个软件,有教程我就省心了哈哈

Phython真的很万能,最后一张图想起了鸡蛋壳。

@tensaix2j got you a $1.59 @minnowbooster upgoat, nice!
@tensaix2j got you a $1.59 @minnowbooster upgoat, nice! (Image: pixabay.com)


Want a boost? Click here to read more!

This post has received a 1.04 % upvote from @drotto thanks to: @banjo.

Congratulations @tensaix2j! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!