Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code snippet: Import equation as image plane #6

Open
kolibril13 opened this issue Feb 5, 2025 · 0 comments
Open

Code snippet: Import equation as image plane #6

kolibril13 opened this issue Feb 5, 2025 · 0 comments

Comments

@kolibril13
Copy link
Owner

Image

from pathlib import Path
import typst
import tempfile
import bpy
from PIL import Image

# Create and compile Typst file
temp_dir = Path(tempfile.gettempdir())
typst_file = temp_dir / "hello.typ"
png_file = temp_dir / "hello.png"

typst_file.write_text("""
#set page(width: auto, height: auto, margin: 0cm, fill: none)
#set text(size: 80pt)

#let korange() = text(fill: orange)[$k$]
#let nblue() = text(fill: blue)[$n$]

$ sum_(#korange() = 1)^#nblue() #korange() = (nblue()(nblue()+1)) / 2 $
// $ sum_(k=1)^n k = (n(n+1)) / 2 $
"""
)

typst.compile(typst_file, format="png", output=str(png_file))

# Get the image dimensions
with Image.open(png_file) as img:
    width, height = img.size
aspect_ratio = width / height

# Add plane and set its size to match the image dimensions
bpy.ops.mesh.primitive_plane_add(location=(0, 0, 0))
plane = bpy.context.active_object
plane.scale = (3 * aspect_ratio, 3, 1)  # Adjust scale factor as needed

# Create material and set up nodes
material = bpy.data.materials.new(name="TransparentImageMaterial")
material.use_nodes = True
nodes = material.node_tree.nodes
links = material.node_tree.links
bsdf = nodes["Principled BSDF"]
tex_image = nodes.new('ShaderNodeTexImage')
tex_image.image = bpy.data.images.load(str(png_file))
tex_image.image.pack()
mix_shader = nodes.new("ShaderNodeMixShader")
transparent_shader = nodes.new("ShaderNodeBsdfTransparent")

# Connect nodes
links.new(tex_image.outputs['Color'], bsdf.inputs['Base Color'])
links.new(tex_image.outputs['Alpha'], mix_shader.inputs['Fac'])
links.new(bsdf.outputs['BSDF'], mix_shader.inputs[2])
links.new(transparent_shader.outputs['BSDF'], mix_shader.inputs[1])
links.new(mix_shader.outputs['Shader'], nodes["Material Output"].inputs['Surface'])
plane.data.materials.append(material)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant