blender, python, sphere behavior

blender, python, sphere behavior Blender 2.8, python, article. It is a continuation. What I did today was cube placement, lighting placement, camera placement, and coloring. The movement of the sphere and the movement of the cylinder by specifying the key frame. It took some time to color the sphere. It seems that you can see that four numbers are arranged as R, G, B, alpha, but I do not know. I have posted a 1 second video blender 2.8, python. Video 1 second, 33kB on twitter. The image quality remains rough at 400px * 300px. qiita_s20200818.png

import bpy
# nh06 (Move cylinders and spheres with keyframe applications)

#Existing mesh, light, camera,Delete all
for item in bpy.data.objects:
	bpy.data.objects.remove(item)

#Cube 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 #(This line is 2.Not tried in 8)
            #mat.alpha = 0.6 #(This line is 2.Not tried in 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 (source 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()

#Background 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 move(nh02 Motion application)
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
#  ==================

Recommended Posts

blender, python, sphere behavior
Blender 2.9 Python Extrude extrude
Python default argument behavior
[python] behavior of argmax
blender, python, spiral staircase
Run Blender with python
Blender 2.8, Python Cube Scaling
Operate Blender with Python
[Blender x Python] Blender Python tips (11/100)
Blender 2.9, Python Buildings, Cars, Videos
Blender 2.9, Python Odd Even Building
Blender 2.9, Python, hexadecimal color specification
Blender 2.8, Python, light Spot lighting
Blender Python API in Houdini (Python 3)
[Python] Chapter 03-02 turtle graphics (turtle behavior)
Generate 8 * 8 (64) cubes in Blender Python
Check the behavior when assigning Python
Python
Behavior when listing in Python heapq
Blender 2.8, Python Cube, Lighting, Camera Circumference
Install Pytorch on Blender 2.90 python on Windows
[Blender x Python] Particle Animation (Part 1)
Use blender as a python module
Behavior of python3 by Sakura's server
[Blender x Python] Let's master random !!
[Blender x Python] Let's master rotation !!
Access Blender Shader Nodes from Python
Easy modeling with Blender and Python
Get started with Python in Blender
Blender 2.9, Python background light color test
Build an environment for Blender built-in Python
Check the behavior of destructor in Python
[Blender x Python] Let's master the material !!
Blender 2.82 or later + python development environment notes
[python, multiprocessing] Behavior for exceptions when using multiprocessing
To reference environment variables in Python in Blender
Python interpreter in Maya, Houdini, blender, Nuke
Blender Python Mesh Data Access Cheat Sheet
Blender 2.9, Python, Select multiple meshes by coordinates
[Blender x Python] How to use modifiers
[Blender x Python] Let's get started with Blender Python !!