Skip to content

Instantly share code, notes, and snippets.

@kolibril13
Created November 21, 2024 16:46
Show Gist options
  • Save kolibril13/722dbe32fa3c06623a02289cfb168693 to your computer and use it in GitHub Desktop.
Save kolibril13/722dbe32fa3c06623a02289cfb168693 to your computer and use it in GitHub Desktop.
# /// script
# requires-python = ">=3.11,<3.12"
# dependencies = [
# "bpy==4.2.0",
# "marimo",
# "numpy==2.1.3",
# "polars==1.14.0",
# "quak==0.2.1",
# ]
# ///
import marimo
__generated_with = "0.9.19"
app = marimo.App(width="medium")
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""# California: Self-contained""")
return
@app.cell
def __():
import urllib.request
from pathlib import Path
files = {
"a_df.csv": "https://huggingface.co/spaces/sci-blender/bpy_marimo_california/resolve/main/a_df.csv?download=true",
"b_reference_frame.csv": "https://huggingface.co/spaces/sci-blender/bpy_marimo_california/resolve/main/b_reference_frame.csv?download=true",
"housing_data_igor.blend": "https://huggingface.co/spaces/sci-blender/bpy_marimo_california/resolve/main/housing_data_igor.blend?download=true",
}
output_dir = Path.cwd()
output_dir.mkdir(exist_ok=True)
for name, url in files.items():
file_path = output_dir / name
if not file_path.exists():
urllib.request.urlretrieve(url, file_path)
print(f"Downloaded {name}")
else:
print(f"{name} already exists.")
return Path, file_path, files, name, output_dir, url, urllib
@app.cell
def __():
import bpy
import marimo as mo
from math import radians
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.samples = 128
bpy.context.scene.render.resolution_x = 500
bpy.context.scene.render.resolution_y = 500
return bpy, mo, radians
@app.cell
def __(bpy, mo):
#run with
#cd california_housing
#uv run marimo edit marimo_california.py
import numpy as np
import polars as pl
import quak
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,
df,
mesh,
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()
@kolibril13
Copy link
Author

img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment