#author("2022-06-26T23:54:20+09:00","default:Real2Virtual202111","Real2Virtual202111") #author("2022-06-26T23:55:03+09:00","default:Real2Virtual202111","Real2Virtual202111") [[Real2Virtual202111]] #code(Python){{ import bpy import sys, os ## エディタ実行時に追加ライブラリを参照するため ## ファイル読み込みディレクトリをシステムパスに追加する # 自身のファイル名を取得 script_filename = os.path.basename(__file__) # Blenderの読み込みファイルリストからファイルパスを取得 script_filepath = bpy.data.texts[script_filename].filepath # 読み込み元のディレクトリパスを取得 script_dirpath = os.path.dirname(script_filepath) # 読み込み元のディレクトリパスをシステムパスに追加 sys.path += [script_dirpath] from Space import Space #bpy.context.space_data.shading.type = 'MATERIAL' def delete_all(): #clear for item in bpy.data.meshes: bpy.data.meshes.remove(item) for item in bpy.data.materials: bpy.data.materials.remove(item) for collection in bpy.data.collections: print(collection.name) if collection.name != "Collection": bpy.data.collections.remove(collection) # カラーテーブルを作っておく ctable = [] ctable.append( (1,0,0,1) ) ctable.append( (0,1,0,1) ) ctable.append( (0.2,0.2,0.2,1) ) ctable.append( (0,0,1,1) ) ctable.append( (1,1,0,1) ) ctable.append( (1,1,1,1) ) ctable.append( (1,0.5,0,1)) ctable.append( (0.2, 0.5, 0.8, 1)) verts = [(-0.5, -0.5, 0.5), ( 0.5, -0.5, 0.5), ( 0.5, 0.5, 0.5), (-0.5, 0.5, 0.5), (-0.5, -0.5, -0.5), ( 0.5, -0.5, -0.5), ( 0.5, 0.5, -0.5), (-0.5, 0.5, -0.5),] # # 3------- 2 # 0 ------ 1- | # | | | | # | | | | # | 7--- | 6 # 4--------5 # # z # | y # | / # |/ # +------>x # faces = [(0,1,2,3), (0,4,5,1), (1,5,6,2), (2,6,7,3), (0,3,7,4), (4,7,6,5)] # LED-s(+) and a photo-tr(*) on a face # # 3----------------2 # | | # | + | # | + | # | | # | | # | + | # | * + | # | | # 0----------------1 # # led_v = [( 0.29, -0.41, 0.5), #LED_0 ( 0.31, -0.41, 0.5), ( 0.31, -0.39, 0.5), ( 0.29, -0.39, 0.5), ( 0.39, 0.29, 0.5), #LED_1 ( 0.41, 0.29, 0.5), ( 0.41, 0.31, 0.5), ( 0.39, 0.31, 0.5), (-0.31, 0.39, 0.5), #LED_2 (-0.29, 0.39, 0.5), (-0.29, 0.41, 0.5), (-0.31, 0.41, 0.5), (-0.41, -0.31, 0.5), #LED_3 (-0.39, -0.31, 0.5), (-0.39, -0.29, 0.5), (-0.41, -0.29, 0.5)] led_faces = [(0,1,2,3),(4,5,6,7),(8,9,10,11),(12,13,14,15)] phtr_v = [( -0.31, -0.41, 0.5), ( -0.29, -0.41, 0.5), ( -0.29, -0.39, 0.5), ( -0.31, -0.39, 0.5)] phtr_face=[(0,1,2,3)] scene = bpy.context.scene def make_cube_mesh(): cube_mesh = bpy.data.meshes.new(name="cube_seed_mesh") cube_mesh.from_pydata(verts, [], faces) cube_mesh.update() obj = bpy.data.objects.new(name="cube_seed", object_data=cube_mesh) ##scene.objects.link(obj) scene.collection.objects.link(obj) bpy.context.view_layer.objects.active = obj #i = 0 ##for face in mesh.polygons: # 全ての面について処理する for p in range(len(cube_mesh.polygons)): face=cube_mesh.polygons[p] print("face index:{0:2} ".format(face.index), end="") print("vertices:", end="") for vi in face.vertices: print("{0:2}, ".format(vi), end="") print("") # マテリアルの存在確認 mat_name = "mat" + str(p) material = bpy.data.materials.get(mat_name) print("mat_name:"+mat_name) # マテリアルがなければ作成する if material is None: #if True : print("material is None") material = bpy.data.materials.new(mat_name) # マテリアルをオブジェクトに設定する obj.data.materials.append(material) material.use_nodes = True # 色を設定 pBSDF = material.node_tree.nodes["Principled BSDF"] pBSDF.inputs[0].default_value = ctable[p] print(mat_name+" color=",end="") print(ctable[p]) # マテリアルのIndexを取得する matindex = bpy.data.materials.find(mat_name) print("face index:{0:2} ".format(face.index)) # 面に使用するマテリアルを設定する face.material_index = matindex def make_led_seed_mesh(): led_seed_mesh = bpy.data.meshes.new(name="led_seed_mesh") led_seed_mesh.from_pydata(led_v, [], led_faces) led_seed_mesh.update() led_face_obj=bpy.data.objects.new(name="led_seed", object_data=led_seed_mesh) scene.collection.objects.link(led_face_obj) phtr_seed_mesh = bpy.data.meshes.new(name="phtr_seed_mesh") phtr_seed_mesh.from_pydata(phtr_v, [], phtr_face) phtr_seed_mesh.update() phtr_obj=bpy.data.objects.new(name="phtr",object_data=phtr_seed_mesh) scene.collection.objects.link(phtr_obj) mat_name = "mat" + str(6) material = bpy.data.materials.get(mat_name) print("mat_name:"+mat_name) # マテリアルがなければ作成する if material is None: #if True : print("material is None") material = bpy.data.materials.new(mat_name) # マテリアルをオブジェクトに設定する led_face_obj.data.materials.append(material) material.use_nodes = True # 色を設定 pBSDF = material.node_tree.nodes["Principled BSDF"] pBSDF.inputs[0].default_value = ctable[6] print(mat_name+" color=",end="") print(ctable[6]) # マテリアルのIndexを取得する matindex = bpy.data.materials.find(mat_name) # 面に使用するマテリアルを設定する for p in range(len(led_seed_mesh.polygons)): face=led_seed_mesh.polygons[p] print("face index:{0:2} ".format(face.index)) face.material_index = matindex mat_name = "mat" + str(6) mat_name="mat_"+str(7) material = bpy.data.materials.get(mat_name) print("mat_name:"+mat_name) # マテリアルがなければ作成する if material is None: #if True : print("material is None") material = bpy.data.materials.new(mat_name) # マテリアルをオブジェクトに設定する led_face_obj.data.materials.append(material) material.use_nodes = True # 色を設定 pBSDF = material.node_tree.nodes["Principled BSDF"] pBSDF.inputs[0].default_value = ctable[7] print(mat_name+" color=",end="") print(ctable[7]) # マテリアルのIndexを取得する matindex = bpy.data.materials.find(mat_name) # 面に使用するマテリアルを設定する face=phtr_seed_mesh.polygons[0] print("face index:{0:2} ".format(face.index)) face.material_index = matindex face_dev_collection=bpy.data.collections.new("face_light_devices") bpy.context.scene.collection.children.link(face_dev_collection) face_dev_collection.objects.link(led_face_obj) face_dev_collection.objects.link(phtr_obj) bpy.context.scene.collection.objects.unlink(led_face_obj) bpy.context.scene.collection.objects.unlink(phtr_obj) def set_object_name(obj, name): name = obj.name obj.name = name def get_object_from_name(name): return bpy.data.objects[name] def mk_cubic_led_phtr(): make_led_seed_mesh() def print_objects(): print(bpy.data.objects) print(bpy.data.objects.keys()) print(bpy.context.object) print(bpy.context.selected_objects) ##bpy.ops.object.select_by_type(type='MESH') #bpy.data.objects['cube'].select_set(True) #bpy.data.objects['led_seed'].select_set(True) # 現時点でアクティブ化されているオブジェクトの名前を print if bpy.context.active_object!=None: print("Active object (before): "+bpy.context.active_object.name) # 現時点で選択されているオブジェクトの名前を print selected_name_list = [obj.name for obj in bpy.context.selected_objects] print("Selected objects (before): ", end="") #obj = bpy.context.active_object #mesh = obj.data def move_to_work_0(): obj=bpy.ops.transform.translate(value=(0, 15, -0), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, True, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True) return obj def move_to_work_1(): context = bpy.context for ob in context.selected_objects: name = ob.name copy = ob.copy() copy.data = copy.data.copy() # linked = False ob.name = f"{name}_high" copy.name = f"{name}_low" ob.location.x += 5 context.collection.objects.link(copy) obj=bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, -5, 0), "orient_type":'GLOBAL', "orient_matrix":((1, 0, 0), (0, 1, 0), (0, 0, 1)), "orient_matrix_type":'GLOBAL', "constraint_axis":(False, False, False), "mirror":True, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "cursor_transform":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False, "use_automerge_and_split":False}) #bpy.ops.outliner.item_rename() return obj def get_objects_in_the_collection(c): print(c) for collection in bpy.data.collections: print(collection.name) if collection.name ==c: for o in collection.objects: o.select_set(True) if collection.children !=None: for cc in collection.children: get_objects_in_the_collection(cc.name) #Recursivly transverse layer_collection for a particular name def recurLayerCollection(layerColl, collName): found = None if (layerColl.name == collName): return layerColl for layer in layerColl.children: found = recurLayerCollection(layer, collName) if found: return found def duplicate_collection(c): print("duplicate_collection") print(c) nc=c.copy() nc.name=c.name+'_c' for o in c.objects: #print("object") #print(o) no=o.copy() no.name=o.name+'_c' nc.objects.unlink(o) nc.objects.link(no) if c.children!=None: for cc in c.children: ncc=duplicate_collection(cc) nc.children.link(ncc) return nc delete_all() #make_cube_mesh() make_led_seed_mesh() print_objects() #get_objects_in_the_collection("face_light_devices") #move_to_work_0() #move_to_work_1() tc = bpy.data.collections.get("face_light_devices") cc=duplicate_collection(tc) cc.name='face_dev_00' bpy.context.scene.collection.children.link(cc) get_objects_in_the_collection("face_dev_00") bpy.ops.collection.objects_add_active() bpy.ops.transform.rotate(value=3.1415926535/2, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True) # tc = bpy.data.collections.get("face_light_devices") cc=duplicate_collection(tc) cc.name='face_dev_01' bpy.context.scene.collection.children.link(cc) get_objects_in_the_collection("face_dev_01") bpy.ops.collection.objects_add_active() bpy.ops.transform.rotate(value=-3.1415926535/2, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True) tc = bpy.data.collections.get("face_light_devices") cc=duplicate_collection(tc) cc.name='face_dev_02' bpy.context.scene.collection.children.link(cc) get_objects_in_the_collection("face_dev_02") bpy.ops.collection.objects_add_active() bpy.ops.transform.rotate(value=3.1415926535/2, orient_axis='Y', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True) #cc.transrom.rotate(value=3.1415926535/2, orient_axis='X', orient_type='GLOBAL',orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True) #bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, -5, 0), "orient_type":'GLOBAL', "orient_matrix":((1, 0, 0), (0, 1, 0), (0, 0, 1)), "orient_matrix_type":'GLOBAL', "constraint_axis":(False, False, False), "mirror":True, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "cursor_transform":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False, "use_automerge_and_split":False}) #bpy.ops.transform.rotate(value=3.1415926535/2, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True) #bpy.ops.transform.rotate(value=-3.1415926535/2, orient_axis='Z', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, False, True), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True) }}