Skip to content

Commit

Permalink
LIGHTS!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-Morgan committed Oct 17, 2023
1 parent 6086ceb commit 4c1a815
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
17 changes: 10 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def automate_function(
}

Path("./automate_data.json").write_text(json.dumps(data))


print("Starting blender")

process = run(
[
'blender',
Expand All @@ -42,16 +44,17 @@ def automate_function(
capture_output=True,
text=True,
)
print(process.stdout)
print(process.stderr)

if (returncode := process.returncode) != 0:
automate_context.mark_run_failed(f"The blender process exited with code {returncode}\n{process.stdout}")
automate_context.mark_run_failed(f"The blender process exited with error code {returncode}\n{process.stdout}")
else:
print(process.stdout)
print(process.stderr)

for file_path in Path("./Screenshots").glob("**/*.png") :
files = Path("./Screenshots").glob("**/*.png")
for file_path in files:
automate_context.store_file_result(file_path)

automate_context.mark_run_success("YAYAY!!! we did it!")
automate_context.mark_run_success(f"YAYAY!!! we did it! - {len(files}} exported")



Expand Down
24 changes: 17 additions & 7 deletions speckle_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from pathlib import Path

from mathutils import Vector
from mathutils import Color, Vector
from bpy_speckle.convert.to_native import _deep_conversion, can_convert_to_native, convert_to_native, display_value_to_native, get_scale_factor

from specklepy.api import operations
Expand Down Expand Up @@ -41,22 +41,29 @@ def set_filename(fileName):
converted_objects = {}
_deep_conversion(root_object, converted_objects, True)

#Make all materials blank
#Make all materials white
for mat in bpy.data.materials:
mat.diffuse_color = (1.0, 1.0, 1.0, 1.0)
COLOR = (1.0, 1.0, 1.0, 1.0)
mat.diffuse_color = COLOR
inputs = mat.node_tree.nodes["Principled BSDF"].inputs
inputs["Base Color"].default_value = COLOR


# Convert all rooms as lights
traversal_func = get_default_traversal_func(can_convert_to_native)
rooms = [x.current for x in traversal_func.traverse(root_object) if x.current.speckle_type == "Objects.BuiltElements.Room"]

light = bpy.data.lights.new("myLight", 'POINT')
light.color = Color((1, 0.34, 0.1))
light.energy = 100 #watts

print(f"Found {len(rooms)} rooms")
for room in rooms:
(converted, _) = display_value_to_native(room, f"FakeRoom{room.id}", get_scale_factor(room))

#calculate bounds
minimum = Vector((-math.inf, -math.inf, -math.inf))
maximum = Vector((math.inf, math.inf, math.inf))
minimum = Vector((+math.inf, +math.inf, +math.inf))
maximum = Vector((-math.inf, -math.inf, -math.inf))

for vert in converted.vertices:
minimum.x = min(minimum.x, vert.co.x)
Expand All @@ -66,9 +73,12 @@ def set_filename(fileName):
maximum.y = max(maximum.y, vert.co.y)
maximum.z = max(maximum.z, vert.co.z)

bpy.data.objects.new(f"FakeLight{room.id}", light)
light_pos = minimum.lerp(maximum, 0.5)

light_object = bpy.data.objects.new(f"FakeLight{room.id}", light)
light_object.matrix_local.translation = light_pos

bpy.context.collection.objects.link(light_object)


all_cameras = [o for o in bpy.context.scene.objects if o.type == "CAMERA"]
Expand All @@ -83,6 +93,6 @@ def set_filename(fileName):
# Render camera to output directory
set_filename(f"{ob.name}")
bpy.ops.render.render(write_still=True)
print(f"Render {i}/{total} complete! saved at {bpy.context.scene.render.filepath}")
print(f"Render {i + 1}/{total} complete! saved at {bpy.context.scene.render.filepath}")

print(f"All Done!")

0 comments on commit 4c1a815

Please sign in to comment.