LoginSignup
0
1

More than 3 years have passed since last update.

blender, python, 球の動作

Last updated at Posted at 2020-08-18

blender, python, 球の動作
blender 2.8, python, の記事。続きです。今日やったのは立方体の配置、照明配置カメラ配置、着色。キーフレーム指定での球の動きと円柱の動き。球の着色に少し手間取りました。R,G,B,alphaと4つ数値を並べるのがわかるようでわからないまま。
twitterに1秒間の動画blender 2.8, python. 動画1秒, 33kB を投稿しています。画質は400px*300pxで荒いまま。
qiita_s20200818.png

import bpy
# nh06 ( キーフレーム応用で 円筒形と球形を動かす)

# 既存 mesh, light, camera, みな削除
for item in bpy.data.objects:
    bpy.data.objects.remove(item)

#立方体 original script は  ---- http://tips.hecomi.com/entry/20120818/1345307205

START = 0
END   = 100
N     = 3

# Add color cubes
for x in range(0, N):
    for y in range(0, N):
        for z in range(0, N):
            # Add a color cube
            bpy.ops.mesh.primitive_cube_add( location=(x*3, y*3, z*3) )
            #obj = bpy.context.scene.objects.active #(old blender2.7script)
            obj = bpy.context.view_layer.objects.active
            #mat.diffuse_color = (x/N, y/N, z/N)#(old blender2.7script)
            mat = bpy.data.materials.new('Cube')
            mat.diffuse_color = (x/N, y/N, z/N, 0)
            #mat.use_transparency = True #(この行は2.8で試していない)
            #mat.alpha = 0.6 #(この行は2.8で試していない)
            obj.data.materials.append(mat)


# new camera
bpy.ops.object.add(radius=1.0, type='CAMERA', enter_editmode=False, align='WORLD', location=(20.0, -6.0, 8.0), rotation=(1.4, 0.0, 1.2))

# new lamps (出典 stack overflow Can you add a light source in blender using python)
# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_2.80", type='POINT')
light_data.energy = 3000
# create new object with our light datablock
light_object = bpy.data.objects.new(name="light_2.80", object_data=light_data)
# link light object
bpy.context.collection.objects.link(light_object)
# make it active 
bpy.context.view_layer.objects.active = light_object
#change location
light_object.location = (5, -4, 10)
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()

# 背景 world - surface - background
bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[0].default_value = (0.01, 0.15, 0.25, 1)
bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[1].default_value = 0.7            
#=====

# ================
# cylinder 動かす ( nh02 動きの応用)
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 30
bpy.context.scene.frame_current = 10
# MOVE Cylinder mesh with KEY FRAME 
bpy.ops.mesh.primitive_cylinder_add(rotation=(-0.8, 0.0, 0.0))
bpy.context.scene.frame_current = 1 # set frame to 1
cdnow  = bpy.context.object          # get new object just created
cdnow.location = (1,-1,1) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_current = 10 # set frame to 10
cdnow.location = (1,7,7)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_current = 20 # set frame to 20
cdnow.location = (8,6,3)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_current = 30 # set frame to 30
cdnow.location = (1,-1,1)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
#  ==================


# MOVE UV Sphere mesh with KEY FRAME (# Add UV Sphere)
bpy.ops.mesh.primitive_uv_sphere_add (segments=32, ring_count=16, radius=1.0, calc_uvs=True, enter_editmode=False, align='WORLD', location=(4.0, -2.0, 6.0), rotation=(0.0, 0.0, 0.0))

mat = bpy.data.materials.new('Sphere')
obj = bpy.context.view_layer.objects.active
mat.diffuse_color = (0.8, 0.8, 0.1, 0.4)  # sphere color (R,G,B,Alpha)
obj.data.materials.append(mat)

bpy.context.scene.frame_current = 1 # set frame to 1
cdnow  = bpy.context.object    # get new object just created
cdnow.location = (4.0, -2.0, 6.0) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_current = 15 # set frame to 15
cdnow.location = (4.0, 1.0, 7.0)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_current = 22 # set frame to 22
cdnow.location = (4,3,4)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_current = 30 # set frame to 30
cdnow.location = (4.0, -2.0, 6.0)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
#  ==================

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1