Real2Virtual202111

٤Ƴ٤Ĥ
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
 
 
 
-
|
|
!
-
!
-
!
-
!
 
 
-
!
 
-
!
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
-
|
!
 
 
 
 
 
 
 
 
-
!
 
 
 
-
!
-
!
 
 
-
!
 
 
-
!
 
 
 
 
-
!
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
-
!
 
 
-
!
 
 
-
!
 
 
 
 
-
!
 
-
!
 
 
 
 
 
 
 
 
 
-
!
-
!
 
 
-
!
 
 
-
!
 
 
 
 
-
!
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
!
-
!
 
-
!
 
 
-
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
!
 
 
 
 
 
 
 
 
 
 
-
!
 
-
|
|
!
 
 
 
 
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
-
!
-
!
-
!
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)
 

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-06-26 (日) 23:55:03 (92d)