Blender 2.9, Python, Select multiple meshes by coordinates

This is an experiment in which 3 * 5 * 5 spheres are created, and some of them are selected and colored under z-coordinate conditions. If I wasn't sure if I could do it soon because it was an extension of the last time, it took me a long time to think about "multiple mesh selection" alone. After a lot of research, I found that there is a convenient way to specify obj.location.z. The shades specified in 3 colors are from "Bochan Dumpling / Tsubo and Confectionery". In addition to the method of creating a lot of objects and selecting a part, there should be a method of creating one and duplicating it, so I plan to challenge that as well next time.

In addition, I referred to the object selection selection and activation activation. Activation of object [4th Python x Blender]. However, be careful that it is for blender 2.7. Video 1 second posted on twitter. blender 2.9, python. animation 1 sec. Video 1 second (We welcome your feedback. Please let us know if you can write python scripts more efficiently or if you don't need this one line.) b3d_boccha2.png

#bpy_nh21 (bochan dango)  create spheres, select multiple spheres,  assign material
import bpy

# ========= DELETE ALL mesh, light, camera,  =========
for item in bpy.data.objects:
    bpy.data.objects.remove(item)
# ========= 1st FLOOR height 
z1f = -6 # first floor sphere height 

# ============== "light_spot1" ==== HIGH 
# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_spot1", type='SPOT')
light_data.energy = 700
# create new object with our light datablock
light_object1 = bpy.data.objects.new(name="light_spot1", object_data=light_data)
# link light object
bpy.context.collection.objects.link(light_object1)
# make it active 
bpy.context.view_layer.objects.active = light_object1
#change location
light_object1.location = (-5, -7, 4+z1f)
light_object1.delta_rotation_euler = (1.3, 0, -0.3) #Look straight down at zero zero zero.
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()        

# ============== "light_spot2" ==== HIGH 
# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_spot1", type='SPOT')
light_data.energy = 2000
# create new object with our light datablock
light_object1 = bpy.data.objects.new(name="light_spot1", object_data=light_data)
# link light object
bpy.context.collection.objects.link(light_object1)
# make it active 
bpy.context.view_layer.objects.active = light_object1
#change location
light_object1.location = (10, -7, 7+z1f)
light_object1.delta_rotation_euler = (1.3, 0, 0.3) #Look straight down at zero zero zero.
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()        

#bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(10, -10, 8), rotation=(1.2, 0, 0.5)) # (fixed CAMERA)

# ====  hex COLOR CODE to R,G,B
def hex_to_rgb( hex_value ):
    b = (hex_value & 0xFF) / 255.0
    g = ((hex_value >> 8) & 0xFF) / 255.0
    r = ((hex_value >> 16) & 0xFF) / 255.0
    return r, g, b

# ==== spheres 5*5*3

for x in range (5):
    for y in range (5):
        for z in range (3):
            bpy.ops.mesh.primitive_uv_sphere_add(radius=1, align='WORLD', location=(x*2, y*2, z*2+z1f), scale=(1, 1, 1))

for obj in bpy.data.objects: #scan every object 
    obj.select_set(False) # deselect all 

# ==== select (obj.location.z) lowest 5*5
for obj in bpy.data.objects: #scan every object, select multiple objects with Z
    loc_z = (obj.location.z)
    if loc_z == z1f: 
        obj.select_set(True)
# ====  set material  1st floor
h = 0x8b4513  #saddlebrown#8b4513
for x in bpy.context.selected_objects:
    obj =  x.data
    mat1 = bpy.data.materials.new('Brown')
    mat1.diffuse_color = (*hex_to_rgb(h), 0)
    obj.materials.append(mat1)
    
for obj in bpy.data.objects: #scan every object 
    obj.select_set(False) # deselect all 
    
# ====  select with z 2nd floor
for obj in bpy.data.objects: #scan every object, select multiple objects with Z
    loc_z = (obj.location.z)
    if loc_z == z1f+2:
        obj.select_set(True)

bpy.data.objects['light_spot1'].select_set(False)

# ====  set material 2nd floor 
h = 0xffdead #navajowhite#ffdead
for x in bpy.context.selected_objects:
    obj =  x.data
    mat2 = bpy.data.materials.new('n_white')
    mat2.diffuse_color = (*hex_to_rgb(h), 0)
    obj.materials.append(mat2)
    
for obj in bpy.data.objects: #scan every object 
    obj.select_set(False) # deselect all 

# ====  select with z  3rd floor 
for obj in bpy.data.objects: #scan every object, select multiple objects with Z
    loc_z = (obj.location.z)
    if loc_z == z1f+4:
        obj.select_set(True)

bpy.data.objects['light_spot1'].select_set(False)

# ====  set material 3rd floor 
h = 0x808000   #olive#808000
for x in bpy.context.selected_objects:
    obj =  x.data
    mat3 = bpy.data.materials.new('olive')
    mat3.diffuse_color = (*hex_to_rgb(h), 0)
    obj.materials.append(mat3)
    
for obj in bpy.data.objects: #scan every object 
    obj.select_set(False) # deselect all 

#  ====== add a camera, camera movement (bpy_nh13)
bpy.ops.curve.primitive_bezier_circle_add(enter_editmode=False, align='WORLD', location=(0, 0, 0))
bpy.context.object.scale[0] = 12
bpy.context.object.scale[1] = 12
bpy.ops.object.empty_add(type='CUBE', align='WORLD', location=(0, 0, 0))
bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(0, 0, 0), rotation=(0, 0, 0))

bpy.data.objects['Empty'].select_set(True)
bpy.data.objects['Camera'].select_set(True)

bpy.context.view_layer.objects.active = bpy.data.objects['Empty']
bpy.ops.object.parent_set(type='OBJECT')

bpy.data.objects['Camera'].select_set(False)
bpy.data.objects['Empty'].select_set(True)

bpy.ops.object.constraint_add(type='FOLLOW_PATH')
bpy.context.object.constraints["Follow Path"].target = bpy.data.objects["BezierCircle"]

bpy.context.object.constraints["Follow Path"].use_curve_follow = True
bpy.context.object.constraints["Follow Path"].use_fixed_location = True

bpy.data.objects['Empty'].select_set(False)
bpy.data.objects['Camera'].select_set(True)

bpy.ops.object.constraint_add(type='TRACK_TO')
bpy.context.object.constraints["Track To"].target = bpy.data.objects["Sphere.032"]
bpy.context.object.constraints["Track To"].up_axis = 'UP_Y'
bpy.context.object.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'  #5m00sec
#Camera Keyframe #(Insert keyframe to object's Offset Factor Python API - stack exchange)
bpy.data.objects['Camera'].select_set(False)
bpy.data.objects['Empty'].select_set(True)
bpy.context.scene.frame_current = 1
bpy.context.object.constraints["Follow Path"].offset_factor = 0
ob = bpy.context.object
# ob.constraints['Follow Path']
# bpy.data.objects['Empty'].constraints["Follow Path"]
# [bpy.data.objects['Empty'].constraints["Follow Path"]]
con = ob.constraints.get("Follow Path")
con.offset_factor = 0.0
con.keyframe_insert("offset_factor", frame=1)
con.offset_factor = 0.15
con.keyframe_insert("offset_factor", frame=8)
con.offset_factor = 0.4
con.keyframe_insert("offset_factor", frame=16)
con.offset_factor = 0.3
con.keyframe_insert("offset_factor", frame=18)
con.offset_factor = 0.0
con.keyframe_insert("offset_factor", frame=30)
# ======= 

Recommended Posts

Blender 2.9, Python, Select multiple meshes by coordinates
[Python] Sort iterable by multiple conditions
[Python] What is inherited by multiple inheritance?
How to plot multiple fits images side by side in galactic coordinates using python
Blender 2.9 Python Extrude extrude
Primality test by Python
Communication processing by Python
[Python] Create multiple directories
blender, python, spiral staircase
Run Blender with python
blender, python, sphere behavior
Operate Blender with Python
Beamformer response by python
[Blender x Python] Blender Python tips (11/100)
[Python] Send gmail with python: Send one by one with multiple image files attached