1
$\begingroup$

I am trying to take the 1s and 0s in the B column, and use those to apply the mat_available (1) and the mat_occupied (0) to the planes.

I am a beginner in python, so my knowledge is limited. I was able to get the planes assigned to a material through the for loop, but I am unaware of how to use the csv as a guide to which planes get what material.

Example: "Unit 301" would be the first plane ("Plane") , and that plane would be assigned the 'mat_available' material since the B column is a 1. Then the next plane would get assigned its material based on column B and so on.

If I failed at explaining, please tell me.

Thanks!

import csv
import bpy
#---------------------------------------------------------#
# available material
mat_available = bpy.data.materials.new(name='available')
mat_available.use_nodes=True
mat_available_nodes = mat_available.node_tree.nodes
mat_available_links = mat_available.node_tree.links
mat_available_nodes['Principled BSDF'].inputs['Emission Strength'].default_value = (1)
mat_available_nodes['Principled BSDF'].inputs['Emission'].default_value = (1, 1, 1, 1)

# occupied material
mat_occupied = bpy.data.materials.new(name='occupied')
mat_occupied.use_nodes=True
mat_occupied_nodes = mat_occupied.node_tree.nodes
mat_occupied_links = mat_occupied.node_tree.links
mat_occupied_nodes['Principled BSDF'].inputs['Emission Strength'].default_value = (0)
mat_occupied_nodes['Principled BSDF'].inputs['Emission'].default_value = (1, 1, 1, 1)
#---------------------------------------------------------#

# De-selects objects
bpy.ops.object.select_all(action='DESELECT')

# Loads CSV
with open('C:/Users/Bryce/Desktop/python_test/occupancy.csv') as ufile:
    readout = list(csv.reader(ufile))
    
lights = bpy.data.collections['lights'].objects

for obj in lights:
    if readout[1] == 1:
        obj.data.materials[0] = mat_available
    else:
        obj.data.materials[0] = mat_occupied
    

Current code and setup.

csv

$\endgroup$

1 Answer 1

1
$\begingroup$

Three main issues to watch out for but all handled by updating your for loop. Technically you should also verify len(lights.objects) == len(readout) to avoid possible issues running through the loop or decide some method to handle too many objects compared to readout data.

for i, obj in enumerate(lights):
    # verify a `material_slot` is available
    if len(obj.data.materials):
        # need to iterate through your readout array
        # also values are frequently read in as string type instead of numbers
        if readout[i][1] == '1':
            obj.data.materials[0] = mat_available
        else:
            obj.data.materials[0] = mat_occupied
    else:
        # add a slot and assign material
        if readout[i][1] == '1':
            obj.data.materials.append(mat_available)
        else:
            obj.data.materials.append(mat_occupied)
$\endgroup$
1
  • $\begingroup$ You are a godsend! Thank you so much. I don't believe I will need to add a length comparison, only because the data I am pulling is always going to be the same length as the collection I am going to be adding it to. I just kept having a hard time figuring out how to structure it to iterate through the readout too. THANK YOU! $\endgroup$
    – Furm
    Commented Apr 10, 2022 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .