[Blender×Python]   モディファイアのつかいかた

#目次

0.Subdivision Surface
1.Array
2.Bevel
3.Boolean
4.Build
5.Decimate
6.Mirror
7.Screw
8.Skin
9.Solidify
10.ShrinkWrap
(11.Shape Key)


※コードのコメントは後で追加します。



#0.Subdivision Surface

![ssss.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/be7f3f4b-a326-aabc-6470-caed03e03d2f.png)


```.py
import bpy

def subSurf(level = 3):
    bpy.ops.object.modifier_add(type='SUBSURF')
    bpy.context.object.modifiers["Subdivision"].levels = level

bpy.ops.mesh.primitive_monkey_add()
subSurf()
```





----------------------------------------------------------





#1.Array
![aaaa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/7c427b72-469a-e624-a964-424bd1f44175.png)


```.py
import bpy

def array(num = 5,o_x = 2,o_y = 0,o_z = 0):
    bpy.ops.object.modifier_add(type='ARRAY')
    
    array = bpy.context.object.modifiers["Array"]
    array.count = num
    
    array.relative_offset_displace[0] = o_x
    array.relative_offset_displace[1] = o_y
    array.relative_offset_displace[2] = o_z


bpy.ops.mesh.primitive_cube_add()
array(o_x = 5,o_y = 5,o_z = 5)
```




----------------------------------------------------------



#2.Bevel

![tttttt.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/c7a3fe6c-f441-37c7-ba8e-45ed1e8ef907.png)



```.py
import bpy

def bevel(_amount = 0.3,_segments = 1):
    bpy.ops.object.modifier_add(type='BEVEL')

    bevel = bpy.context.object.modifiers["Bevel"]
    bevel.width = _amount
    bevel.segments = _segments

bpy.ops.mesh.primitive_ico_sphere_add()
bevel()
```

---

#2-1.Bevel_animation


![ezgif.com-gif-maker (30).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/abd995a2-1b8f-05bb-b5fb-8e258692f0e0.gif)

```.py
import bpy

frame_num = 0
bpy.context.scene.frame_set(frame_num)
bpy.context.scene.frame_end = 50

bpy.ops.mesh.primitive_cube_add()

bpy.ops.object.modifier_add(type='BEVEL')
bevel = bpy.context.object.modifiers["Bevel"]

for i in range(0,2):
    bevel.width = i
    bevel.keyframe_insert(data_path = "width",frame = frame_num)
    frame_num += 50
```



----------------------------------------------------------



#3.Boolean
![eee.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/3e03a8be-21de-2d90-171a-e949b6f6e8df.png)

```.py
import bpy

def boolean(obj):
    bpy.ops.object.modifier_add(type='BOOLEAN')
    
    boolean = bpy.context.object.modifiers["Boolean"]
    boolean.operation = 'DIFFERENCE'
    boolean.object = bpy.data.objects[obj]
    bpy.ops.object.modifier_apply(modifier="Boolean")
    

bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_cube_add(location = (1,1,1))
boolean("Cube")
```



----------------------------------------------------------





#4.Build

```.py
import bpy

def build(f):
    bpy.ops.object.modifier_add(type='BUILD')
    
    build = bpy.context.object.modifiers["Build"]
    build.frame_start = f
    build.use_random_order = False
    build.seed = 0
    

bpy.ops.mesh.primitive_monkey_add()
build(f = 0)
```

---------------------------------------------------------
#4-1.Build_animation
![ezgif.com-gif-maker (27).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/90ed35e2-842c-2cec-c2aa-3c3e072e83cf.gif)



```.py
import bpy

frame_num = 0
bpy.context.scene.frame_set(frame_num)
bpy.context.scene.frame_end = 120

bpy.ops.mesh.primitive_monkey_add()

bpy.ops.object.modifier_add(type='BUILD')
build = bpy.context.object.modifiers["Build"]

for i in range(0,2):
    build.frame_start = 12 -i * 110
    build.keyframe_insert(data_path = "frame_start",frame = frame_num)
    frame_num -= 110
```


----------------------------------------------------------










#5.Decimate
![wwwww.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/ece1133e-5ef2-3711-39e9-bb2c67ba199b.png)


```.py
import bpy

def decimate(limit_num):
    bpy.ops.object.modifier_add(type='DECIMATE')
    
    decim = bpy.context.object.modifiers["Decimate"]
    decim.decimate_type = 'DISSOLVE'
    decim.delimit = {'NORMAL'}
    decim.angle_limit = limit_num
    

bpy.ops.mesh.primitive_monkey_add()
decimate(limit_num = 0.8)
```





----------------------------------------------------------




#6.Mirror

```.py
import bpy

def mirror(obj):
    bpy.ops.object.modifier_add(type='MIRROR')
    
    mir = bpy.context.object.modifiers["Mirror"]
    mir.use_axis[0] = True
    mir.mirror_object = bpy.data.objects[obj]

    

bpy.ops.mesh.primitive_monkey_add()
bpy.ops.mesh.primitive_cube_add(location = (3,0,0))
bpy.context.view_layer.objects.active = bpy.data.objects["Suzanne"]
mirror("Cube")
```


----------------------------------------------------------

#7.Screw

```.py
import bpy

def subSurf(level = 3):
    bpy.ops.object.modifier_add(type='SUBSURF')
    bpy.context.object.modifiers["Subdivision"].levels = level

def screw(angle = 6.28,screw = 10,itr = 1,ax = 'Z'):
    bpy.ops.object.modifier_add(type='SCREW')
    
    scr = bpy.context.object.modifiers["Screw"]
    scr.angle = angle
    scr.screw_offset = screw
    scr.iterations = 2
    scr.axis = ax

bpy.ops.mesh.primitive_grid_add()
screw(itr = 2)
subSurf()
```

---

#7-1.Screw_animation

![ezgif.com-gif-maker (29).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/43393851-d304-fb3d-9d95-a5ffeded5302.gif)



```.py
import bpy

frame_num = 0
bpy.context.scene.frame_set(frame_num)
bpy.context.scene.frame_end = 120

bpy.ops.mesh.primitive_grid_add()


bpy.ops.object.modifier_add(type='SCREW')
scr = bpy.context.object.modifiers["Screw"]

_angle = 0
_screw_offset = 10


for i in range(0,2):
    scr.angle = _angle
    scr.screw_offset = _screw_offset
    #scr.iterations = i + 1
    scr.keyframe_insert(data_path = "angle",frame = frame_num)
    scr.keyframe_insert(data_path = "screw_offset",frame = frame_num)
    scr.keyframe_insert(data_path = "iterations",frame = frame_num)
    frame_num  += 110
    _angle += 15
    _screw_offset += 15
```


----------------------------------------------------------

#8.Skin

![ssssss.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/24cbdd64-1090-dc67-4732-b5ec90efeb32.png)



```.py
import bpy

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.ops.object.mode_set(mode='EDIT')

bpy.ops.mesh.delete(type='EDGE_FACE')
bpy.ops.mesh.select_all(action='SELECT')

bpy.ops.object.modifier_add(type='SKIN')
bpy.ops.transform.skin_resize(value=(0.1, 0.1, 0.1))

bpy.ops.object.mode_set(mode='OBJECT')
```



----------------------------------------------------------




#9.Solidify


```.py
import bpy

def solid(thick = -0.15,ofs = -1):
    bpy.ops.object.modifier_add(type='SOLIDIFY')
    solid = bpy.context.object.modifiers["Solidify"]
    solid.thickness = thick
    solid.offset = ofs

bpy.ops.mesh.primitive_monkey_add()
solid()
```








----------------------------------------------------------










#10.ShrinkWrap

![ezgif.com-gif-maker (24).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/aea56751-efcb-a679-91ed-ccd19d71853a.gif)


```.py
import bpy

def subSurf(level = 3):
    bpy.ops.object.modifier_add(type='SUBSURF')
    bpy.context.object.modifiers["Subdivision"].levels = level

def shrinkWrapAnim(obj):
    bpy.ops.object.modifier_add(type='SHRINKWRAP')
    bpy.context.object.modifiers["Shrinkwrap"].target = bpy.data.objects[obj]
    bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=False, modifier="Shrinkwrap")
    
def shapeKeyInsert(frame_num = 10,shape_value = 1):
    obj = bpy.context.object
    obj.active_shape_key_index = 1
    
    for i in range(0,2):
        obj.active_shape_key.value = shape_value
        obj.active_shape_key.keyframe_insert(data_path = "value",frame = frame_num)
        
        frame_num += 50
        shape_value -= 1

s = 0.3
bpy.context.scene.frame_set(0)
bpy.context.scene.frame_end = 50

bpy.ops.mesh.primitive_cube_add(scale = (s,s,s))
bpy.ops.mesh.primitive_monkey_add()
subSurf()
shrinkWrapAnim("Cube")
shapeKeyInsert()
```







#11.Shape Key

![ezgif.com-gif-maker (25).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/25cd5cc6-7b4d-8111-84c6-266ee871fefc.gif)


```.py
import bpy

bpy.ops.mesh.primitive_cube_add()
bpy.ops.object.shape_key_add(from_mix=False)

obj = bpy.context.object
deform_num = 2

for i in range(0,deform_num):
    obj.active_shape_key_index = i + 1

    obj.data.vertices[i * 2].co.z  += 5
    bpy.ops.object.shape_key_add(from_mix=False)
    
    frame_num = 0
    for f in range(0,deform_num + 1):
        
        if(f == i + 1):
            val = 1
        else:
            val = 0
            
        obj.active_shape_key.value = val
        obj.active_shape_key.keyframe_insert(data_path = "value",frame = frame_num)
        frame_num += 50
```