TIPSSS

memos

peastShaderNodeFromClip.py
```` ### ノード生成スクリプト ### https://qiita.com/yukimituki11/items/dc945cca0b1fc429aa07 import bpy import json from mathutils import Vector # データからノード情報の作成 def make_nodes(node_tree, node_set): node_list = [] for n_ in node_set: if "bl_idname" in n_: # ノードの作成 node = node_tree.nodes.new( n_.pop("bl_idname") ) # アトリビュートの設定 for attr_name in n_.keys(): if hasattr(node, attr_name): if attr_name == "inputs": for i_,var_ in enumerate(n_[attr_name]): if var_: node.inputs[i_].default_value = var_ elif attr_name in ['image', 'object' , 'operation', 'uv_map']: try:setattr(node, attr_name, n_[attr_name]) except:print('%s:%s was not set' % (node.name, attr_name)) else: setattr(node, attr_name, n_[attr_name]) node_list.append(node) if "node_links" in n_: for L in n_["node_links"]: node_tree.links.new( node_list[ L[0] ].outputs[ L[1] ], node_list[ L[2] ].inputs[ L[3] ] ) obj = bpy.context.active_object node_tree = obj.active_material.node_tree #新規にノードグループを作成して その中にノードを生成する場合 # node_tree = bpy.data.node_groups.new('ProjectionNode', 'ShaderNodeTree') angle = 90 pos_ = [0,-1.0,2.0] # node_set = [ # {'bl_idname': 'ShaderNodeBsdfPrincipled', 'name': 'Principled BSDF', 'location': Vector((10.0, 300.0)), 'inputs': [[0.07220958173274994, 0.039725761860609055, 0.8000000715255737, 1.0], 0.0, [1.0, 0.20000000298023224, 0.10000000149011612], [0.800000011920929, 0.800000011920929, 0.800000011920929, 1.0], 1.399999976158142, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.029999999329447746, 1.4500000476837158, 0.0, 0.0, [0.0, 0.0, 0.0, 1.0], 1.0, 1.0, [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], 0.0]}, # {'bl_idname': 'ShaderNodeOutputMaterial', 'name': 'Material Output', 'location': Vector((300.0, 300.0))}, # {'node_links': [[0, 0, 1, 0]]}, # ] # クリップボードのテキストを利用して生成したい場合 data_string = bpy.context.window_manager.clipboard # JSON形式の文字列を使ってPythonのオブジェクトに変換するコード # JSON形式の文字列をeval()を使用してPythonのオブジェクトに変換し、辞書内のlocationキーについてはVectorオブジェクトに、 # inputsキーについては内部のリストをすべてVectorオブジェクトに変換します。結果を出力すると、正しいPythonオブジェクトが得られます data_list = eval(data_string) for item in data_list: if 'location' in item: item['location'] = Vector(item['location']) if 'inputs' in item: item['inputs'] = [Vector(vec) if isinstance(vec, list) else vec for vec in item['inputs']] node_set = data_list print(node_set) #ノードの作成 make_nodes(node_tree, node_set) ````
getShaderNode.py
```` ### ノード書き出しスクリプト ### https://qiita.com/yukimituki11/items/dc945cca0b1fc429aa07 import bpy import json # 取得する属性の一覧 attr_list = ['bl_idname', 'name', 'location', 'attribute_name','image', 'object' , 'operation', 'uv_map'] """ 主なノード属性 'bl_idname' :Blenderでノードを作成するための名前 'name' :ノードの名前 'label' :ノードのラベル 'height','width' :ノードの大きさ 'location' :ノードの位置 'attribute_name' :属性ノードの入力値 'image' :画像テクスチャで指定している画像 'object' :テクスチャ座標等で指定しているオブジェクト 'operation' :計算の種類等の設定 'uv_map' :UVマップ 'parent' :親のフレーム等 'select' :選択しているか """ # 画像テクスチャノードの画像も記録したい場合は 'image' を追加 def get_nodes(node_tree, attr_list): nodes = list(node_tree.nodes) node_settings = "[\n" for n in nodes: dic_ = {} for attr in attr_list: if hasattr(n, attr): val_ = getattr(n,attr) dic_[attr] = val_ if hasattr(n, "inputs") and n.bl_idname != 'ShaderNodeOutputMaterial': list_ = [] for i_ in n.inputs: if hasattr(i_, 'default_value'): if i_.type in ['VALUE','STRING']: list_ .append( i_.default_value ) else: list_ .append( list(i_.default_value) ) dic_["inputs"] = list_ node_settings += str(dic_) + ',\n' node_links = [] for L in node_tree.links: if L.from_socket.is_output: from_node_id = nodes.index(L.from_node) to_node_id = nodes.index(L.to_node) from_soc = list(L.from_socket.node.outputs).index(L.from_socket) to_soc = list(L.to_socket.node.inputs).index(L.to_socket) else: to_node_id = nodes.index(L.from_node) from_node_id = nodes.index(L.to_node) to_soc = list(L.from_socket.node.inputs).index(L.from_socket) from_soc = list(L.to_socket.node.outputs).index(L.to_socket) node_links.append( [from_node_id, from_soc, to_node_id, to_soc] ) node_settings += str({"node_links" : node_links}) + ',\n' node_settings += ']' return( node_settings ) # ノードの情報を取得 # アクティブオブジェクトのノード node_tree = bpy.context.active_object.active_material.node_tree # ワールドのノードを取得する場合 #node_tree =bpy.contextscene.world.node_tree # 特定のグループノード内を取得する場合 #node_tree = bpy.data.node_groups[0] txt = get_nodes(node_tree, attr_list) print(txt) # json.dumps(txt) # print(txt) # テキストをクリップボードに bpy.context.window_manager.clipboard = txt ````