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?

  • 1
    $\begingroup$ Yep, that's possible. Hardest part is finding a way of holding the data (used nodes, their connections etc.) in any reliable way IMHO. $\endgroup$ – 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')

    with bpy.data.libraries.load(filepath) as (data_from, data_to):
        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)
node_group = node_tree.nodes.new('ShaderNodeGroup')
node_group.location = (0, 0)
node_group.node_tree = D.node_groups['group-name']

Alternatively you can programmatically create a node group (see this question, and then instantiate it like shown above.

Each node comes with a location data, so you can save your node locations with "location" or you can programmatically order your nodes if you know what they should look like.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.