# How to convert a cycles material node setup to a python material generation script?

Is there a way to convert a cycles material node setup to a python script which in it's turn can generate a new material with the same node setup?

Also, is it possible to somehow retain the x,y postion of these nodes for easy readability?

• Yep, that's possible. Hardest part is finding a way of holding the data (used nodes, their connections etc.) in any reliable way IMHO. – brockmann Aug 21 '17 at 15:10

You can probably create a node_group programmatically and then instantiate the group.

Do you know what the setup looks like in advance? I suggest you load a node_group from a template Blender scene.

Here is an example of how I append nodes from external Blender scenes, I personally like this approach a lot, you can find the source files here:

def import_material_node_groups():
""" Load the archilogic cycles material node groups from the node-library.blend file.
"""
filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources/node-library.blend')

data_to.node_groups = data_from.node_groups

for node_group in data_to.node_groups:
log.debug('Importing material node group: %s', node_group.name)
node_group.use_fake_user = True

# Material group node (The datablock is not yet assigned)