0
$\begingroup$

This question already has an answer here:

So far I have seen this and this both which did not work for me. Currently I have:

import bpy

bpy.data.scenes['Scene'].render.engine = "CYCLES"

bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(1.00003, 0.176902, 0.72441),
                                layers=(True, False, False, False, False, False, False, False, False, False, False,
                                        False, False, False, False, False, False, False, False, False))
bpy.ops.object.material_slot_add()
bpy.ops.material.new()

mat_name = 'Material.001'
bpy.data.materials[mat_name].node_tree.nodes.new("ShaderNodeEmission")
bpy.data.materials[mat_name].node_tree.nodes["Emission"].inputs["Color"].default_value = (1, 0, 0.07, 1)
mat = bpy.data.materials.get(mat_name)
ob = bpy.context.active_object
ob.data.materials[0] = mat

When I perform this process in blender the hover text for use nodes in the material->surface section the code is: bpy.ops.cycles.use_shading_nodes() and I get: RuntimeError: Operator bpy.ops.cycles.use_shading_nodes.poll() failed, context is incorrect so I'm not sure if I can/have to perform this action or there is a prerequisite to doing it?

I tried starting blender with --debug-wm but this didn't give me more information of how to perform this action in a script.

Is there a better way to show what blender is doing than --debug-wm? Because I can do what I want in blender itself just not in a script.

I also read a lot of api documentations and the tips for blender scripting, but this didn't really help me.

$\endgroup$

marked as duplicate by Ray Mairlot, Mzidare, Duarte Farrajota Ramos, m.ardito, p2or Sep 7 '18 at 16:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Thank your for the answer. The important part was removing the other nodes of the material $\endgroup$ – Hakaishin Sep 5 '18 at 9:53
  • $\begingroup$ IMO the important part from link posted is addressing the material by reference by getting or creating via API with materials collection get and new methods respectively THe code in Q above adds a new material each time, but only ever sets nodes of a material named "Material.001" and assigns to slot 0. Suggest using a unique name for your material then need only create it once. Remove it to create it agai, other wise get it. This will avoid creating an endless list of materials whilst testing script. $\endgroup$ – batFINGER Sep 5 '18 at 10:05

Browse other questions tagged or ask your own question.