# blender-scripting
This is a collection of simple to more involved examples to scripting in [Blender](https://www.blender.org/) with Python.
## Table of Contents
- [Requirements](#requirements)
- [Resources](#resources)
- [Utils](#utils)
- [Simple Sphere](#simple-sphere)
- [Parametric Torus](#parametric-torus)
- [Metaballs](#metaballs)
- [Voronoi Landscape](#voronoi-landscape)
- [Tetrahedron Fractal](#tetrahedron-fractal)
- [Phyllotaxis Flower](#phyllotaxis-flower)
- [Rugged Donut](#rugged-donut)
- [Fisher Iris Visualization](#fisher-iris-visualization)
- [Voronoi Sphere](#voronoi-sphere)
## Requirements
`Blender 2.5+`
To run the examples, open your favorite console in the example folder, make sure that the Blender executable is an environment variable or in the PATH environment variable in Windows and run the following command. Make sure to edit in [run_script.py](run_script.py) the `scriptFile` variable to the Python script in the [scripts](scripts) folder you want to execute.
```
blender -b -P run_script.py
```
Another option is to open the script in Blender and run [run_script.py](run_script.py) inside Blender, which is a nice way to test and tweak the files and to see and play with the generated result before rendering.
## Resources
- [Blender Cookbook](https://wiki.blender.org/index.php/Dev:Py/Scripts/Cookbook)
- [Blender 3D Python Scripting](https://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Advanced_Tutorials/Python_Scripting/Introduction)
- [Blender Scripting Blog](http://blenderscripting.blogspot.co.at/)
## Utils
[utils](scripts/utils/__init__.py)
Some frequently used functions in blender, which will be used in most of the scripts.
## Simple Sphere
[simple_sphere.py](scripts/simple_sphere.py)
Simple rendering of a smooth sphere. First an icosphere is added with
```python
import bpy
bpy.ops.mesh.primitive_ico_sphere_add(location=(0, 0, 0))
obj = bpy.context.object
```
Then the subdivision surface modifier is added to the object to increase the resolution of the mesh and afterwards all the faces of the object are set to a smooth shading
```python
modifier = obj.modifiers.new('Subsurf', 'SUBSURF')
modifier.levels = 2
modifier.render_levels = 2
mesh = obj.data
for p in mesh.polygons:
p.use_smooth = True
```
Alternatively the icosphere can be subdivided with the `subdivisions` argument in the function
```python
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=4, location=(0, 0, 0))
```
![Simple Sphere](/img/simple_sphere.png)
## Parametric Torus
[parametric_torus.py](scripts/parametric_torus.py)
Parametric generation of a torus. The [torus](https://en.wikipedia.org/wiki/Torus) is created with the following parameterization of a grid of the variables u, v
![Torus Formula](/img/torus_formula.png)
where the values u, v are between 0 and 1 and are then mapped to x, y, z coordinates. In [parametric_torus.py](scripts/parametric_torus.py), the function `torusSurface(r0, r1)` returns the surface parameterization function for a torus which is then used in `createSurface(surface, n, m)` as the first argument, which creates the object from a n by m grid. The function `createSurface(surface, n, m)` can be also used for other parameterizations such as [surfaces of revolution](https://en.wikipedia.org/wiki/Surface_of_revolution) or other [parametric surfaces](https://en.wikipedia.org/wiki/Parametric_surface).
![Parametric Torus](/img/parametric_torus.png)
## Metaballs
[metaballs.py](scripts/metaballs.py)
Generate random metaballs in Blender inspired by this [tutorial](http://blenderscripting.blogspot.co.at/2012/09/tripping-metaballs-python.html).
![Metaballs](/img/metaballs.png)
## Voronoi Landscape
[voronoi_landscape.py](scripts/voronoi_landscape.py)
This is a more advanced example for using a [Voronoi diagram](https://en.wikipedia.org/wiki/Voronoi_diagram). The Voronoi diagram is implemented with the module `scipy.spatial` which can be added with [Scipy](https://www.scipy.org/), or can be found in the Python distribution [Anaconda](https://www.continuum.io/downloads). The steps to use Anaconda as the Interpreter in Blender 2.77 are shown in this [solution](http://til.janakiev.com/using-anaconda-in-blender/).
![Voronoi Landscape](/img/vornoi_landscape.png)
## Tetrahedron Fractal
[tetrahedron_fractal.py](scripts/tetrahedron_fractal.py)
This is an example for a fractal [tetrahedron](http://mathworld.wolfram.com/RegularTetrahedron.html), where each tetrahedron is subdivided into smaller pieces with a recursive function. In order to create a material for the tetrahedron the material is assigned as shown here:
```python
color = (0.5, 0.5, 0.5)
mat = bpy.data.materials.new('Material')
# Diffuse
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 0.9
mat.diffuse_color = color
# Specular
mat.specular_intensity = 0
obj.data.materials.append(mat)
```
![Tetrahedron Fractal](/img/tetrahedron_fractal.png)
## Phyllotaxis Flower
[phyllotaxis_flower.py](scripts/phyllotaxis_flower.py)
This script implements a [Phyllotaxis](https://en.wikipedia.org/wiki/Phyllotaxis) Flower which aranges leaves or the petals according to the [golden angle](https://en.wikipedia.org/wiki/Golden_angle). Additionally The flower is animated by appending an [application handler](https://docs.blender.org/api/blender_python_api_current/bpy.app.handlers.html) for frame change by
```python
def handler(scene):
frame = scene.frame_current
# Create new geometry for new frame
# ...
# Append frame change handler on frame change for playback and rendering (before)
bpy.app.handlers.frame_change_pre.append(handler)
```
In order to render all frames you can run
```python
bpy.ops.render.render(animation=True)
```
The animation is inspired by the mesmerizing sculptures by [John Edmark](http://www.johnedmark.com/).
![Phyllotaxis Flower](/img/phyllotaxis_flower.gif)
## Rugged Donut
[rugged_donut.py](scripts/rugged_donut.py)
This script implements a number of different things available in Blender. For one it applies a [Displace modifier](https://docs.blender.org/manual/de/dev/modeling/modifiers/deform/displace.html) to a torus which displaces the object with a texture as follows.
```python
# Create musgrave texture
texture = bpy.data.textures.new('Texture', 'MUSGRAVE')
# Create displace modifier and apply texture
displace = obj.modifiers.new('Displace', 'DISPLACE')
displace.texture = texture
```
Further we can control the texture by an object such as an [Empty object](https://docs.blender.org/manual/ja/dev/modeling/empties.html)
```python
# Create Empty to control texture coordinates
empty = bpy.data.objects.new('Empty', None)
bpy.context.scene.objects.link(empty)
# Take the texture coordinates from empty’s coordinate system
displace.texture_coords = 'OBJECT'
displace.texture_coords_object = empty
```
Additionally we want to add a material with additional bump map to our torus object which is done in the following way.
```python
# Create bump map texture
bumptex = bpy.data.textures.new('BumpMapTexture', 'CLOUDS')
# Create material
mat = bpy.data.materials.new('BumpMapMaterial')
# Add texture slot for material and add texture to this slot
slot = mat.texture_slots.add()
slot.texture = bumptex
slot.texture_coords = 'GLOBAL'
slot.use_map_color_diffuse = False
slot.use_map_normal = True
# Append material to object
obj.data.materials.append(mat)
```
Now we want to animate the empty in order to animate the texture. We can achieve this by inserting keyframes for the location of our empty as shown in this quick [tutorial](blenderscripting.blogspot.co.at/2011/05/inspired-by-post-on-ba-it-just-so.html) and in the next snippet.
```python
for frame in range(1, num_frames):
t = frame / num_frames
x = 0.7*cos(2*pi*t)
y = 0.7*sin(2*pi*t)
z = 0.4*sin(2*pi*t)
empty.location = (x, y, z)
empty.keyframe_insert(data_path="location", index=-1, frame=frame)
```
![Rugged Donut](/img/rugged_donut.
没有合适的资源?快使用搜索试试~ 我知道了~
blender-scripting:Blender脚本介绍
共36个文件
png:14个
py:11个
gif:3个
需积分: 7 12 下载量 111 浏览量
2021-05-04
10:42:25
上传
评论
收藏 7.43MB ZIP 举报
温馨提示
搅拌机脚本 这是在使用Python编写脚本的一些简单到更复杂的示例的集合。 目录 要求 Blender 2.5+ 要运行示例,请在示例文件夹中打开您喜欢的控制台,确保Blender可执行文件是环境变量或Windows中的PATH环境变量,然后运行以下命令。 确保在run_script.py中将scriptFile变量编辑为要执行的scripts文件夹中的Python脚本。 blender -b -P run_script.py 另一个选择是在Blender中打开脚本并在Blender中运行run_script.py ,这是测试和调整文件以及在渲染之前查看和播放生成的结果的好方法。 资源 搅拌机食谱 Blender 3D Python脚本 Blender脚本博客 实用程序 实用程序 Blender中一些常用的功能,将在大多数脚本中使用。 简单球体 simple_sphere.py
资源推荐
资源详情
资源评论
收起资源包目录
blender-scripting-master.zip (36个子文件)
blender-scripting-master
img
metaballs.png 136KB
tetrahedron_fractal.png 167KB
fisher_iris_visualization.gif 1.51MB
rugged_donut.gif 1.76MB
torus_formula.png 4KB
parametric_torus.png 100KB
voronoi_sphere.png 199KB
simple_sphere.png 75KB
vornoi_landscape.png 370KB
phyllotaxis_flower.gif 1.93MB
.github
FUNDING.yml 675B
LICENSE 1KB
run_script.py 684B
.gitignore 1KB
README.md 11KB
scripts
rendering
metaballs.png 136KB
tetrahedron_fractal.png 167KB
phyllotaxis_flower.png 201KB
parametric_torus.png 100KB
voronoi_sphere.png 199KB
simple_sphere.png 75KB
vornoi_landscape.png 362KB
metaballs.py 945B
data
iris
bezdekIris.data 4KB
iris.names 3KB
Index.txt 105B
iris.data 4KB
parametric_torus.py 2KB
voronoi_sphere.py 3KB
phyllotaxis_flower.py 4KB
simple_sphere.py 1KB
tetrahedron_fractal.py 2KB
voronoi_landscape.py 3KB
rugged_donut.py 3KB
utils
__init__.py 6KB
fisher_iris_visualization.py 6KB
共 36 条
- 1
资源评论
火石创造
- 粉丝: 30
- 资源: 4667
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功