Created
September 30, 2012 19:55
-
-
Save zeffii/3808289 to your computer and use it in GitHub Desktop.
storing material as dicts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cool_mat must be present in current_blend | |
# this script is written to deal with only one specific material | |
# i'm looking at a way to write a generic cycles json exporter | |
import bpy | |
material_name = 'cool_mat' | |
materials = bpy.data.materials | |
nodes = materials[material_name].node_tree.nodes | |
links = materials[material_name].node_tree.links | |
print('-'*80) | |
print("""\ | |
import bpy | |
from mathutils import Vector\n | |
my_mat = bpy.data.materials.new('cool_mat') | |
my_mat.use_nodes = True""") | |
print('\nnodes = {') | |
for idx, node in enumerate(nodes): | |
separator = ('\n}' if idx == len(nodes)-1 else ',') | |
loc = tuple(round(k, 0) for k in node.location) | |
print(' \'{0.name}\': [\'{0.type}\', {1}]{2}'.format(node, loc, separator)) | |
print('') | |
# mix shader as two identical inputkeys. | |
# for node in nodes, make node, set node location | |
print("""\ | |
for k, v in nodes.items(): | |
location = Vector(v[1]) | |
if not k in my_mat.node_tree.nodes: | |
cur_node = my_mat.node_tree.nodes.new(v[0]) | |
cur_node.location = location | |
else: | |
my_mat.node_tree.nodes[k].location = location""") | |
print('') | |
for idx, link in enumerate(links): | |
list_from = '[\'{0.from_node.name}\',\'{0.from_socket.name}\',' | |
list_to = '\'{0.to_node.name}\',\'{0.to_socket.name}\']' | |
list_members = list_from + list_to | |
link_name = 'link{:02d} = '.format(idx) | |
print(link_name + list_members.format(link)) | |
print('') | |
print("""\ | |
links = [link00, link01, link02, link03, link04]""") | |
# for link in links: | |
# from_node, from_socket, to_node, to_socket | |
print("""\ | |
node_target = 1 | |
for link in links: | |
from_node = my_mat.node_tree.nodes[link[0]] | |
output_socket = from_node.outputs[link[1]] | |
to_node = my_mat.node_tree.nodes[link[2]] | |
if link[0] in ['Emission', 'Diffuse BSDF']: | |
input_socket = to_node.inputs[node_target] | |
node_target += 1 | |
else: | |
input_socket = to_node.inputs[link[3]] | |
my_mat.node_tree.links.new(output_socket, input_socket)""") | |
print('') | |
print('nodes = my_mat.node_tree.nodes'.format(material_name)) | |
print('') | |
# Emission: col / strength | |
node = nodes['Emission'] | |
emission_color = node.inputs['Color'].default_value[:] | |
emission_color = tuple(round(k, 0) for k in emission_color) | |
emission_strength = node.inputs['Strength'].default_value | |
print('node = nodes[\'Emission\']') | |
print('node.inputs[\'Color\'].default_value = {}'.format(emission_color)) | |
print('node.inputs[\'Strength\'].default_value = {}'.format(emission_strength)) | |
print('') | |
# Diffuse BSDF: col roughness | |
node = nodes['Diffuse BSDF'] | |
diffuse_color = node.inputs['Color'].default_value[:] | |
diffuse_color = tuple(round(k, 0) for k in diffuse_color) | |
diffuse_roughness = node.inputs['Roughness'].default_value | |
print('node = nodes[\'Diffuse BSDF\']') | |
print('node.inputs[\'Color\'].default_value = {}'.format(diffuse_color)) | |
print('node.inputs[\'Roughness\'].default_value = {}'.format(diffuse_roughness)) | |
print('') | |
# Noise Texture / Scale - Detail - Distortion | |
node = nodes['Noise Texture'] | |
noise_scale = node.inputs['Scale'].default_value | |
noise_detail = node.inputs['Detail'].default_value | |
noise_distortion = node.inputs['Distortion'].default_value | |
print('node = nodes[\'Noise Texture\']') | |
print('node.inputs[\'Scale\'].default_value = {}'.format(noise_scale)) | |
print('node.inputs[\'Detail\'].default_value = {}'.format(noise_detail)) | |
print('node.inputs[\'Distortion\'].default_value = {}'.format(noise_distortion)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from mathutils import Vector | |
my_mat = bpy.data.materials.new('cool_mat') | |
my_mat.use_nodes = True | |
nodes = { | |
'Geometry': ['NEW_GEOMETRY', (-85.0, 598.0)], | |
'Emission': ['EMISSION', (-111.0, 409.0)], | |
'Diffuse BSDF': ['BSDF_DIFFUSE', (-116.0, 304.0)], | |
'Mix Shader': ['MIX_SHADER', (193.0, 469.0)], | |
'Material Output': ['OUTPUT_MATERIAL', (699.0, 435.0)], | |
'Noise Texture': ['TEX_NOISE', (377.0, 299.0)] | |
} | |
for k, v in nodes.items(): | |
location = Vector(v[1]) | |
if not k in my_mat.node_tree.nodes: | |
cur_node = my_mat.node_tree.nodes.new(v[0]) | |
cur_node.location = location | |
else: | |
my_mat.node_tree.nodes[k].location = location | |
# from_node, from_socket, to_node, to_socket | |
# unfortunately Mix Shader has two input nodes called 'Shader' | |
# this means you have to assign to input index instead. | |
link00 = ['Emission','Emission','Mix Shader','Shader'] | |
link01 = ['Mix Shader','Shader','Material Output','Surface'] | |
link02 = ['Diffuse BSDF','BSDF','Mix Shader','Shader'] | |
link03 = ['Geometry','Normal','Mix Shader','Fac'] | |
link04 = ['Noise Texture','Fac','Material Output','Displacement'] | |
links = [link00, link01, link02, link03, link04] | |
node_target = 1 | |
for link in links: | |
from_node = my_mat.node_tree.nodes[link[0]] | |
output_socket = from_node.outputs[link[1]] | |
to_node = my_mat.node_tree.nodes[link[2]] | |
if link[0] in ['Emission', 'Diffuse BSDF']: | |
input_socket = to_node.inputs[node_target] | |
node_target += 1 | |
else: | |
input_socket = to_node.inputs[link[3]] | |
my_mat.node_tree.links.new(output_socket, input_socket) | |
nodes = my_mat.node_tree.nodes | |
node = nodes['Emission'] | |
node.inputs['Color'].default_value = (0.0, 1.0, 1.0, 1.0) | |
node.inputs['Strength'].default_value = 0.5 | |
node = nodes['Diffuse BSDF'] | |
node.inputs['Color'].default_value = (1.0, 0.0, 0.0, 1.0) | |
node.inputs['Roughness'].default_value = 0.0 | |
node = nodes['Noise Texture'] | |
node.inputs['Scale'].default_value = 5.0 | |
node.inputs['Detail'].default_value = 2.0 | |
node.inputs['Distortion'].default_value = 0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment