File size: 3,109 Bytes
91e132c
 
40bcc93
 
91e132c
 
 
 
40bcc93
 
91e132c
 
 
40bcc93
 
 
 
 
 
 
 
 
91e132c
40bcc93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c49acc3
 
 
 
40bcc93
 
 
 
c49acc3
 
40bcc93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c49acc3
 
 
 
 
91e132c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import marimo

__generated_with = "0.9.20"
app = marimo.App()


@app.cell
def __(mo):
    mo.md("""# Marimo + Blender""")
    return


@app.cell
def __():
    #run with
    #cd california_housing
    #uv run marimo edit marimo_california.py 
    import marimo as mo
    import numpy as np
    import polars as pl
    import quak
    import bpy

    blend_file_path = "housing_data_igor.blend"
    bpy.ops.wm.open_mainfile(filepath=blend_file_path)

    bpy.context.scene.render.engine = "BLENDER_EEVEE_NEXT"
    bpy.context.scene.render.resolution_x = 800
    bpy.context.scene.render.resolution_y = 500

    df = pl.read_csv("a_df.csv")
    reference_frame = pl.read_csv("b_reference_frame.csv")

    vertices = [(row["longitude_normalized"], row["latitude_normalized"], 0) for row in reference_frame.iter_rows(named=True)]

    mesh = bpy.data.meshes.new("NormalizedMesh")
    obj = bpy.data.objects.new("CaliforninaNormalizedObject", mesh)
    bpy.context.collection.objects.link(obj)

    mesh.from_pydata(vertices, [], [])
    mesh.update()
    obj.modifiers.new(name="GeometryNodes", type='NODES').node_group = bpy.data.node_groups["geo_house"]
    bpy.context.view_layer.objects.active = obj
    obj.select_set(True)
    reference_frame.head()

    def render_result():

        bpy.ops.render.render()
        bpy.data.images['Render Result'].save_render(filepath="img.png")
        return mo.image(src="img.png")
    return (
        blend_file_path,
        bpy,
        df,
        mesh,
        mo,
        np,
        obj,
        pl,
        quak,
        reference_frame,
        render_result,
        vertices,
    )


@app.cell
def __(df, mo, quak):
    widget = mo.ui.anywidget(quak.Widget(df))
    widget
    return (widget,)


@app.cell
def __(np, obj, pl, reference_frame, render_result, widget):
    widget_df = widget.data().pl()

    plotting_frame = reference_frame.with_columns(
        pl.lit(0).alias("custom_plotting")
    ).join(
        widget_df.select(["short_id", "median_house_value"]), 
        on="short_id", 
        how="left"
    ).with_columns(
        pl.when(pl.col("median_house_value").is_not_null())
        .then(pl.col("median_house_value"))
        .otherwise(pl.col("custom_plotting"))
        .alias("custom_plotting")
    ).select(["short_id", "custom_plotting"])

    custom_plotting_list = plotting_frame["custom_plotting"].to_list()
    normalized_values = list(np.interp(custom_plotting_list, 
                                       (min(custom_plotting_list), max(custom_plotting_list)), 
                                       (0.1, 3)))

    attr_name = 'median_house_value'
    attr = obj.data.attributes.get(attr_name) or obj.data.attributes.new(
        name=attr_name,
        type='FLOAT',
        domain='POINT'
    )
    attr.data.foreach_set('value', normalized_values)
    obj.data.update()

    render_result()
    return (
        attr,
        attr_name,
        custom_plotting_list,
        normalized_values,
        plotting_frame,
        widget_df,
    )


@app.cell
def __():
    return


if __name__ == "__main__":
    app.run()