0
$\begingroup$

I have a csv list of values i want to use to change the Bevel modifier amount for a collection of a big number of cubes. I don't really know Python but tried something based on things I found in youtube, as follows:

import csv
import bpy

bar_spacing = 1.5
bar_width = 1

with open ('C:/Users/USER/Downloads/HD1.csv') as f:
    readout = list(csv.reader(f))

for a in readout:
    placement = readout.index (a)
    bpy.ops.mesh.primitive_cube_add(size=1)
    bpy.ops.object.modifier_add(type='BEVEL')
    new_bar = bpy.context.object
    
    for vert in new_bar.data.vertices:
        vert.co[1] += 0.5
        vert.co[0] += placement*bar_spacing + 0.5
        
        new_bar.'bevel' amount = (bar.'bevel' amount, float (a[1])

I took the example from this video on how to create data visualization modifying bar sizes, and tried to alter it. The next picture is what I want to achieve, but with the complete set of 1080 diferent cubes: to have their own bevel amount value from the data, as in the next image:

example of the intended result

And example of the value/table,

enter image description here


UPDATE! With @cod3R help, I managed to get every cube with it's specific Bevel from the data. Now a new problem: How to distribute them into rows? This is how it looks now and the code i used:

enter image description here

loc_y = 0.0
for data in (0.08,0.39,0.06,0.12,0.27,0.65,0.02,0.55,0.01,0.47,0.13,0.03,0.18,0.14,0.19,0.21,0.01,0.38,0.22,0.10,0.05,0.33,0.13,0.30,0.17,0.49,0.35,0.16,0.53,0.11,0.24,0.39,0.01,0.20,0.26,0.29,0.02,0.18,0.56,0.33,0.94,0.53,0.36,0.24,0.22,0.15,0.15,0.22,0.20,0.30,0.09,0.23,0.02,0.15,0.14,0.44,0.00,0.00,0.20,0.88,0.32,0.05,0.05,0.00,0.16,0.16,0.77,0.57,0.41,0.21,0.08,0.22,0.40,0.83,0.32,0.20,0.04,0.00,0.63,0.24,0.05,0.05,0.60,0.25,0.31,0.08,0.05,0.06,0.00,0.36,0.12,0.29,0.32,0.06,0.55,0.20,0.01,0.30,0.21,0.21,0.18,0.03,0.43,0.27,0.21,0.04,0.16,0.01,0.06,0.00,0.17,0.37,0.00,0.22,0.17,0.35,0.31,0.15,0.01,0.24,0.02,0.06,0.15,0.28,0.18,0.16,0.10,0.38,0.06,0.38,0.25,0.67,1.00,0.53,0.27,0.02,0.27,0.58,0.06,0.01,0.15,0.47,0.13,0.18,0.10,0.00,0.28,0.31,0.30,0.22,0.00,0.04,0.18,0.21,0.06,0.18,0.00,0.33,0.26,0.30,0.22,0.34,0.18,0.38,0.18,0.24,0.48,0.27,0.27,0.30,0.29,0.39,0.49,0.28,0.08,0.51,0.11,0.48,0.24,0.35,0.03,0.26,0.05,0.11,0.20,0.44,0.33,0.15,0.53,0.37,0.37,0.32,0.19,0.36,0.25,0.31,0.01,0.27,0.09,0.36,0.36,0.11,0.23,0.41,0.01,0.26,0.31,0.30,0.13,0.11,0.49,0.13,0.45,0.11,0.25,0.15,0.37,0.12,0.76,0.02,0.19,0.04,0.29,0.27,0.47,0.15,0.02,0.31,0.00,0.47,0.24,0.41,0.24,0.29,0.34,0.10,0.39,0.01,0.28,0.00,0.20,0.39,0.36,0.49,0.44,0.57,0.53,0.64,0.54,0.28,0.36,0.71,0.21,0.50,0.16,0.40,0.19,0.33,0.23,0.00,0.60,0.14,0.39,0.13,0.10,0.00,0.12,0.00,0.01,0.05,0.05,0.20,0.73,0.18,0.06,0.02,0.27,0.47,0.00,0.13,0.19,0.66,0.26,0.08,0.42,0.42,0.37,0.16,0.13,0.18,0.00,0.30,0.34,0.01,0.10,0.29,0.22,0.17,0.17,0.13,0.12,0.27,0.48,0.28,0.08,0.24,0.01,0.11,0.00,0.29,0.30,0.12,0.09,0.34,0.30,0.18,0.08,0.34,0.74,0.18,0.40,0.12,0.23,0.32,0.03,0.66,0.03,0.03,0.31,0.22,0.03,0.41,0.20,0.13,0.45,0.35,0.46,0.12,0.11,0.22,0.35,0.37,0.28,0.47,0.32,0.70,0.33,0.63,0.31,0.28,0.35,0.61,0.39,0.31,0.33,0.35,0.28,0.18,0.58,0.35,0.31,0.26,0.27,0.31,0.13,0.24,0.39,0.26,0.30,0.18,0.48,0.30,0.35,0.22,0.29,0.55,0.30,0.22,0.15,0.15,0.19,0.30,0.33,0.30,0.38,0.39,0.66,0.62,0.52,0.06,0.14,0.44,0.46,0.44,0.44,0.24,0.13,0.25,0.40,0.60,0.25,0.63,0.17,0.00,0.21,0.44,0.25,0.20,0.16,0.06,0.15,0.25,0.18,0.11,0.71,0.09,0.35,0.26,0.27,0.25,0.01,0.01,0.04,0.03,0.08,0.12,0.00,0.03,0.01,0.00,0.01,0.04,0.12,0.04,0.00,0.00,0.00,0.10,0.00,0.06,0.03,0.09,0.02,0.01,0.01,0.12,0.06,0.01,0.01,0.01,0.01,0.08,0.01,0.01,0.01,0.21,0.03,0.03,0.04,0.01,0.01,0.07,0.03,0.01,0.07,0.01,0.05,0.01,0.08,0.01,0.10,0.19,0.06,0.00,0.01,0.14,0.06,0.18,0.01,0.16,0.01,0.02,0.15,0.00,0.01,0.13,0.01,0.01,0.21,0.08,0.06,0.01,0.00,0.00,0.13,0.00,0.11,0.13,0.16,0.00,0.22,0.01,0.15,0.07,0.08,0.00,0.04,0.13,0.17,0.10,0.33,0.42,0.00,0.36,0.13,0.15,0.36,0.01,0.42,0.00,0.01,0.01,0.01,0.25,0.07,0.08,0.35,0.17,0.21,0.32,0.06,0.14,0.44,0.00,0.00,0.32,0.34,0.37,0.19,0.00,0.01,0.24,0.38,0.17,0.01,0.26,0.05,0.00,0.08,0.15,0.01,0.01,0.08,0.22,0.01,0.10,0.17,0.09,0.15,0.01,0.10,0.14,0.09,0.35,0.19,0.02,0.07,0.16,0.18,0.63,0.51,0.34,0.07,0.18,0.26,0.00,0.41,0.12,0.01,0.01,0.23,0.00,0.00,0.05,0.33,0.37,0.09,0.13,0.01,0.18,0.15,0.04,0.11,0.20,0.16,0.01,0.20,0.10,0.23,0.00,0.11,0.19,0.25,0.02,0.22,0.10,0.47,0.05,0.40,0.37,0.42,0.00,0.18,0.01,0.60,0.00,0.55,0.30,0.13,0.01,0.14,0.15,0.18,0.21,0.02,0.07,0.08,0.21,0.23,0.09,0.12,0.20,0.25,0.28,0.18,0.04,0.12,0.29,0.01,0.09,0.30,0.15,0.43,0.52,0.11,0.01,0.11,0.12,0.11,0.21,0.01,0.05,0.18,0.32,0.01,0.11,0.16,0.00,0.01,0.18,0.25,0.15,0.10,0.26,0.19,0.08,0.04,0.07,0.12,0.09,0.01,0.13,0.01,0.18,0.11,0.15,0.06,0.14,0.12,0.20,0.11,0.29,0.17,0.20,0.01,0.27,0.18,0.00,0.06,0.13,0.16,0.17,0.01,0.21,0.31,0.15,0.30,0.12,0.51,0.15,0.13,0.01,0.03,0.01,0.20,0.00,0.00,0.01,0.01,0.05,0.04,0.18,0.29,0.00,0.12,0.01,0.01,0.21,0.20,0.00,0.04,0.01,0.06,0.00,0.11,0.14,0.01,0.04,0.18,0.11,0.02,0.01,0.01,0.01,0.01,0.07,0.35,0.13,0.01,0.18,0.27,0.19,0.00,0.15,0.10,0.19,0.01,0.02,0.14,0.01,0.22,0.01,0.06,0.49,0.40,0.14,0.07,0.01,0.03,0.27,0.06,0.13,0.56,0.02,0.00,0.12,0.17,0.07,0.16,0.06,0.24,0.28,0.19,0.07,0.18,0.02,0.00,0.01,0.00,0.46,0.01,0.09,0.01,0.16,0.01,0.12,0.01,0.13,0.10,0.00,0.12,0.01,0.56,0.00,0.13,0.12,0.01,0.23,0.32,0.01,0.40,0.05,0.26,0.14,0.21,0.38,0.20,0.27,0.15,0.17,0.12,0.31,0.13,0.17,0.30,0.01,0.26,0.18,0.00,0.20,0.05,0.10,0.05,0.01,0.28,0.18,0.08,0.09,0.38,0.30,0.18,0.12,0.00,0.25,0.10,0.00,0.10,0.12,0.01,0.09,0.28,0.00,0.11,0.24,0.13,0.00,0.15,0.05,0.37,0.09,0.01,0.04,0.28,0.09,0.33,0.01,0.01,0.05,0.32,0.11,0.34,0.44,0.58,0.16,0.04,0.57,0.01,0.24,0.27,0.08,0.01,0.19,0.23,0.47,0.19,0.34,0.35,0.59,0.35,0.11,0.08,0.29,0.32,0.33,0.15,0.28,0.29,0.27,0.13,0.07,0.48,0.17,0.27,0.25,0.34,0.01,0.34,0.25,0.04,0.10,0.18,0.61,0.27,0.15,0.53,0.02,0.59,0.14,0.43,0.40,0.20,0.13,0.77,0.37,0.31,0.30,0.24,0.20,0.13,0.23,0.72,0.35,0.11,0.27,0.11,0.18,0.07,0.17,0.14,0.19,0.16,0.31,0.41,0.25,0.21,0.49,0.42,0.37,0.44,0.28,0.34,0.15,0.45,0.01,0.22,0.18,0.08,0.25,0.07,0.10,0.38,0.34,0.23,0.18,0.16,0.05,0.21,0.20,0.10,0.11,0.17,0.02,0.16,0.14,0.15,0.13,0.07,0.04,0.13,0.11,0.12,0.41,0.13,0.10,0.36,0.09,0.08,0.32,0.40,0.84,0.45,0.49,0.23,0.07,0.05,0.24,0.07,0.42,0.06,0.19,0.30,0.13,0.05,0.14,0.11,0.07,0.36,0.17,0.11,0.30,0.07,0.19,0.07,0.04,0.26,0.12,0.32,0.23,0.34,0.34,0.16,0.24,0.11,0.28,0.25,0.29,0.26,0.21,0.04,0.28,0.14,0.51,0.66,0.06,0.22,0.09,0.30,0.24,0.04,0.03,0.19,0.27,0.09,0.32,0.17,0.09,0.38,0.37,0.25,0.05,0.51,0.15,0.04,0.52,0.62,0.11,0.06,0.04,0.28,0.18,0.27,0.14,0.28,0.49,0.20,0.19,0.14,0.18,0.26,0.03,0.10,0.22,0.21,0.29,0.27,0.36,0.06,0.30,0.09,0.07,0.28,0.39,0.39,0.02,0.04,0.11,0.23,0.26,0.01,0.00):
    # Add the cube to the scene and get it
    bpy.ops.mesh.primitive_cube_add(location=(0.0, loc_y, 0.0))
    cube = bpy.context.object
    # Add the bevel modifier
    bevel_mod = cube.modifiers.new(name="MY-Bevel", type='BEVEL')
    bevel_mod.width = data
    # offset the intial location
    loc_y += 3.0

Thank you for any advise, example, etc :)

$\endgroup$

1 Answer 1

2
$\begingroup$

Use the low-level function to add modifiers which allows for direct property access: https://docs.blender.org/api/current/bpy.types.ObjectModifiers.html#bpy.types.ObjectModifiers.new

import bpy

# Get the object
obj = bpy.context.scene.objects['Cube']

# Add the modifier the low level function and set the bevel amount
bevel_mod = obj.modifiers.new(name="MY-Bevel", type='BEVEL')
bevel_mod.width = 0.01

########################################################

One simple loop to add cubes based on the data should do:

import bpy

loc_y = 0.0
for data in (1.0, 0.5, 0.25, 0.1, 0.01):
    # Add the cube to the scene and get it
    bpy.ops.mesh.primitive_cube_add(location=(0.0, loc_y, 0.0))
    cube = bpy.context.object
    # Add the bevel modifier
    bevel_mod = cube.modifiers.new(name="MY-Bevel", type='BEVEL')
    bevel_mod.width = data
    # Offset the initial location
    loc_y += 3.0

enter image description here

EDIT Setting up the rows is also fairly simple:

import bpy

bevel_amounts = [1.0, 0.5, 0.25, 0.1, 0.01]
loc_x = 0.0
loc_y = 0.0
offset = 3.0
items_row = 7

for i, data in enumerate(bevel_amounts, start=1):
    # Add the cube to the scene and get it
    bpy.ops.mesh.primitive_cube_add(location=(loc_x, loc_y, 0.0))
    cube = bpy.context.object
    # Add the bevel modifier
    bevel_mod = cube.modifiers.new(name="MY-Bevel", type='BEVEL')
    bevel_mod.width = data
    # Offset the initial location
    loc_y += offset
    # Add a new row 
    if i % items_row == 0:
        loc_x += offset
        # Reset y
        loc_y = 0.0

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you! that worked perfectly. I've updated the question with a new (smaller) question. I now have the correct cubes with their specific bevel. How can I distribute them into rows (say, 7 cubes per raw)? $\endgroup$ Commented Jan 2, 2022 at 21:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.