Skip to main content
added 317 characters in body
Source Link
Noob Cat
  • 1.4k
  • 3
  • 25
  • 67

I need to have access to the active material and its nodes (if they exist), so I wrote this script that has a problem, when the object has no materials, when the object does not exist, and of course if the object is type = 'MESH' type

import bpy 

ob = bpy.context.active_object
index = ob.active_material_index     
mat = ob.material_slots[index].material
nodes = mat.node_tree.nodes
my_node = nodes.get("My node name")

if my_node:
    print("My node is inside!")
else:
    print("My node not inside!")

work only if object is not none, object is 'MESH' type and If inside have slot material. else rightly give me that error:

IndexError: bpy_prop_collection[index]: index 0 out of range, size 0

I can't figure out how to assign variables in an if statement, because I know that in python you can't (I think)

I need to have access to the active material and its nodes (if they exist), so I wrote this script that has a problem, when the object has no materials, when the object does not exist, and of course if the object is type = 'MESH'

import bpy 

ob = bpy.context.active_object
index = ob.active_material_index     
mat = ob.material_slots[index].material
nodes = mat.node_tree.nodes
my_node = nodes.get("My node name")

if my_node:
    print("My node is inside!")
else:
    print("My node not inside!")

I need to have access to the active material and its nodes (if they exist), so I wrote this script that has a problem, when the object has no materials, when the object does not exist, and of course if the object is 'MESH' type

import bpy 

ob = bpy.context.active_object
index = ob.active_material_index     
mat = ob.material_slots[index].material
nodes = mat.node_tree.nodes
my_node = nodes.get("My node name")

if my_node:
    print("My node is inside!")
else:
    print("My node not inside!")

work only if object is not none, object is 'MESH' type and If inside have slot material. else rightly give me that error:

IndexError: bpy_prop_collection[index]: index 0 out of range, size 0

I can't figure out how to assign variables in an if statement, because I know that in python you can't (I think)

Source Link
Noob Cat
  • 1.4k
  • 3
  • 25
  • 67

How to put if condition for empty material index

I need to have access to the active material and its nodes (if they exist), so I wrote this script that has a problem, when the object has no materials, when the object does not exist, and of course if the object is type = 'MESH'

import bpy 

ob = bpy.context.active_object
index = ob.active_material_index     
mat = ob.material_slots[index].material
nodes = mat.node_tree.nodes
my_node = nodes.get("My node name")

if my_node:
    print("My node is inside!")
else:
    print("My node not inside!")