0
$\begingroup$

CONTEXT:

I am trying to create a 3D Data-Viz project that will take values from a CSV file and use certain values to drive the Z-scale of simple cubes that will stretch vertically as a value gets higher. It would function similarly to this 3D-Viz asset from the Unity Asset Store, except instead of longitude and latitude, it would take custom tags assigned by hand: https://assetstore.unity.com/packages/templates/systems/globe-data-visualizer-80008

I have tried looking at a lot of SE questions, and Python/Blender API as well to try and reverse engineer some other approaches; some of the pages I have gone through:

https://docs.blender.org/api/blender_python_api_current/bpy.types.bpy_struct.html#bpy.types.bpy_struct.values

https://docs.blender.org/api/blender_python_api_current/bpy.types.GameProperty.html#bpy.types.GameProperty.name

https://stackoverflow.com/questions/21265676/how-do-i-read-out-custom-properties-in-blender-with-python

How to read a csv file and use the values as x and y points in blender?

https://stackoverflow.com/questions/50137473/how-to-change-properties-of-objects-in-blender-with-python

MY EFFORT(S):

My CSV File has a format that fits like

08-S33,Grind,2018,1,118654,1015122,1168.864432
04-LSE200,Gear,2018,1,61,1798,339.2658509

My scene currently has a few simple primitives with names "Cube.001" and Cube.002" I have given them Game Properties that have values that match the first column in my CSV like '04-LSE200':

first_Snippet

I have tried to write my own script that says:

import bpy, csv

fp = "E:\Data_Viz_Prototype\Data Files\Workcenter_DPPM.csv"

with open( fp ) as csvfile:
    rdr = csv.reader ( csvfile)
    for i, row in enumerate (rdr):
        if i == 0: continue #Skip colum titles
        if row[2:3] != 2018: continue #Skip any rows not from 2018
        if row[3:2] != 1: continue #Skip any rows not from January
        wc, dept, year, month, run, rej, dppm = row[:]

if bpy.data.objects["Cube.002"]['Work_Center'] == '04-LSE200':
    bpy.data.objects["Cube.002"].scale = (.5, .5, dppm)

However I get the following error:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
KeyError: 'bpy_struct[key]: key "Work_Center" not found'

I have tried some of the following code in the console window to make sure I can talk to Game properties:

import bpy
bpy.ops.object.game_property_new(type='FLOAT', name="Test")

obj = bpy.data.objects["Cube.002"]
cube = obj.data
print(bpy.types.GameStringProperty.values(cube))

Which will create a new property upon compile, but returns an empty list "[]" second_Snippet

FINAL THOUGHTS:

I am very aware this could be an "X-Y Problem"; in which case I think I need some serious redirection. I found this one SE question where they were trying to drive values from a CSV file using the node editor; maybe this is the correct approach but I don't even know how to start:

Animation nodes - Driving scale attribute from a CSV list to create a bar chart

My co-worker and I combined have a fair amount of experience in coding, game engines, and modeling; but neither of us is notably familiar with Blender.

$\endgroup$
  • $\begingroup$ Hi. Are you creating Game Properties because you absolutely need them or because you think you need them? Seems like this question could be simplified by asking why Game Properties aren't acting the way you think they should (while still maintaining the context of wanting to read a CSV file) as it seems this is actual problem. $\endgroup$ – Ray Mairlot Apr 26 at 19:08
  • $\begingroup$ In my final thoughts I mentioned this could be an X-Y Problem. $\endgroup$ – Paniom Apr 26 at 19:25
0
$\begingroup$

It seems I was confusing "Game Properties" in the Game Editor and "Custom Properties" in the Properties Editor. By moving my Game Property to a Custom Property, I was able to manipulate my cube through script.

My script had a handful of problems with it, distinctly: calling '.scale' instead of '.dimensions'.

My working code looks like this:

import bpy, csv

fp = "E:\Data_Viz_Prototype\Data Files\Workcenter_DPPM.csv"

obj = bpy.data.objects["Cube.002"]
cube = obj.data

with open( fp ) as csvfile:
    rdr = csv.reader ( csvfile)
    for i, row in enumerate (rdr):
        if i == 0: continue #Skip colum titles
        if row[2:3] != 2018: continue #Skip any rows not from 2018
        if row[3:2] != 1: continue #Skip any rows not from January
    wc, dept, year, month, run, rej, dppm = row[:]

if obj['Work_Center'] == '04-LSE200':
    obj.dimensions = (.5, .5, float(dppm))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.