0
$\begingroup$

I am trying to write a script that would write me .csv file containing custom properties, name, location, rotation and dimension info of selected objects. It works fine accept of info from custom properties. Here I have a problem because in file are written custom properties of one of the selected objects and not of each one.

Custom properties in my case are PosId_obj, ID_Equipment_obj, Line_ID_obj, GB_width_obj.

In the picture you see there are written custom properties of only one object

Snip from .csv file

import bpy, os, math

selection = bpy.context.selected_objects

result = ""

for sel in selection:

loc=sel.location
lx = float(round(sel.location.x, 2))
ly = float(round(sel.location.y, 2))
lz = float(round(sel.location.z, 2))

rot=sel.rotation_euler
rx = float(round(math.degrees(sel.rotation_euler.x), 2))
ry = float(round(math.degrees(sel.rotation_euler.y), 2))
rz = float(round(math.degrees(sel.rotation_euler.z), 2))

dims = sel.dimensions
x = float(round(sel.dimensions.x, 2))
y = float(round(sel.dimensions.y, 2))
z = float(round(sel.dimensions.z, 2))

PosID_obj = bpy.context.object["PosID"]
ID_Equipment_obj = bpy.context.object["ID_Equipment"]
Line_ID_obj = bpy.context.object["Line_ID"]
GB_Width_obj = bpy.context.object["GB_width"]


scr = "%s - %s - %s - %s - %s - %.02fin x %.02fin x %.02fin x %.02fin x %.02fin x %.02fin x %.02fin x %.02fin x %.02fin\n" % (PosID_obj, ID_Equipment_obj, Line_ID_obj, GB_Width_obj, sel.name, lx, ly, lz, rx, ry, rz, x, y, z)
print(scr)
   
result += "%s ,%s ,%s ,%s , %s , %.02f , %.02f , %.02f , %.02f , %.02f , %.02f , %.02f , %.02f , %.02f\n" % (PosID_obj, ID_Equipment_obj, Line_ID_obj, GB_Width_obj, sel.name, lx, ly, lz, rx, ry, rz, x, y, z)


tempFolder = bpy.path.abspath("//")

blendfilename = bpy.path.basename(bpy.context.blend_data.filepath) 
blendfilename = blendfilename.removesuffix(".blend")
blendfilename = blendfilename + "_BOM.csv"
filename = os.path.join (tempFolder, blendfilename)
os.makedirs(os.path.dirname(filename), exist_ok=True)

file = open(filename, "w")

file.write("PosID_obj, ID_Equipment_obj, Line_ID_obj, GB_Width_obj, Name, Location X,     Location Y, Location Z, Rotation X, Rotation Y, Rotation Z, Dimension X, Dimension Y, Dimension Z\n")
file.write(result)

file.close()

Does anybody know what I am doing wrong and how to get proper info for each object?

$\endgroup$
4
  • 1
    $\begingroup$ Please use the scripting tag when you ask a question about scripting. Thanks! $\endgroup$ Commented Mar 1 at 13:30
  • $\begingroup$ Use sel instead of bpy.context.object. $\endgroup$
    – scurest
    Commented Mar 1 at 14:54
  • $\begingroup$ the code indentation looks wrong. The 'for sel in selection' loop has no body. As a result, the code that follows the loop will only use the last 'sel' value and you will get only one result line. $\endgroup$
    – Blunder
    Commented Mar 2 at 3:07
  • $\begingroup$ Omg yes i see it now. sel instead of bpy.context.object because we want selected_object!. Thank you @scurest! $\endgroup$ Commented Mar 5 at 10:22

0

You must log in to answer this question.

Browse other questions tagged .