Align new Model drawings to viewport by default

Previously it was creating not very useful top-down camera at `(0,0,0)`, now it should be more intuitive.

Example - https://imgur.com/a/GHrvCSJ
This commit is contained in:
Andrej730
2025-05-08 19:01:48 +05:00
parent def9352ade
commit be657748f3
3 changed files with 20 additions and 6 deletions
+1 -1
View File
@@ -249,7 +249,7 @@ def add_drawing(
assert location_hint is not None assert location_hint is not None
drawing_name = drawing.ensure_unique_drawing_name(drawing.generate_drawing_name(target_view, location_hint)) drawing_name = drawing.ensure_unique_drawing_name(drawing.generate_drawing_name(target_view, location_hint))
drawing_matrix = drawing.generate_drawing_matrix(target_view, location_hint) drawing_matrix = drawing.generate_drawing_matrix(target_view, location_hint)
camera = drawing.create_camera(drawing_name, drawing_matrix, location_hint) camera = drawing.create_camera(drawing_name, drawing_matrix, location_hint, target_view)
element = drawing.run_root_assign_class( element = drawing.run_root_assign_class(
obj=camera, obj=camera,
ifc_class="IfcAnnotation", ifc_class="IfcAnnotation",
+6 -2
View File
@@ -86,8 +86,11 @@ class Blender(bonsai.core.tool.Blender):
def activate_camera(cls, obj: bpy.types.Object) -> None: def activate_camera(cls, obj: bpy.types.Object) -> None:
area = cls.get_view3d_area() area = cls.get_view3d_area()
is_local_view = area.spaces[0].local_view is not None assert area
assert isinstance((space := area.spaces[0]), bpy.types.SpaceView3D)
is_local_view = space.local_view is not None
assert bpy.context.screen and bpy.context.scene
if is_local_view: if is_local_view:
# Turn off local view before activating drawing, and then turn it on again. # Turn off local view before activating drawing, and then turn it on again.
for a in bpy.context.screen.areas: for a in bpy.context.screen.areas:
@@ -100,7 +103,8 @@ class Blender(bonsai.core.tool.Blender):
else: else:
bpy.context.scene.camera = obj bpy.context.scene.camera = obj
area.spaces[0].region_3d.view_perspective = "CAMERA" assert space.region_3d
space.region_3d.view_perspective = "CAMERA"
@classmethod @classmethod
def get_area_props(cls, context: bpy.types.Context) -> bpy.types.PropertyGroup: def get_area_props(cls, context: bpy.types.Context) -> bpy.types.PropertyGroup:
+13 -3
View File
@@ -299,7 +299,11 @@ class Drawing(bonsai.core.tool.Drawing):
@classmethod @classmethod
def create_camera( def create_camera(
cls, name: str, matrix: Matrix, location_hint: Union[LocationHintLiteral, int] cls,
name: str,
matrix: Matrix,
location_hint: Union[LocationHintLiteral, int],
target_view: ifcopenshell.util.representation.TARGET_VIEW,
) -> bpy.types.Object: ) -> bpy.types.Object:
camera = bpy.data.objects.new(name, (camera_data := bpy.data.cameras.new(name))) camera = bpy.data.objects.new(name, (camera_data := bpy.data.cameras.new(name)))
props = cls.get_camera_props(camera_data) props = cls.get_camera_props(camera_data)
@@ -311,7 +315,11 @@ class Drawing(bonsai.core.tool.Drawing):
props.camera_type = "ORTHO" props.camera_type = "ORTHO"
camera_data.ortho_scale = 50 # The default of 6m is too small camera_data.ortho_scale = 50 # The default of 6m is too small
camera_data.clip_start = 0.002 # 2mm is close to zero but allows any GPU-drawn lines to be visible. camera_data.clip_start = 0.002 # 2mm is close to zero but allows any GPU-drawn lines to be visible.
camera_data.clip_end = 10 # A slightly more reasonable default if target_view == "MODEL_VIEW":
assert (space := tool.Blender.get_view3d_space())
camera_data.clip_end = max(space.clip_end, 10)
else:
camera_data.clip_end = 10 # A slightly more reasonable default
if bpy.context.scene.unit_settings.system == "IMPERIAL": if bpy.context.scene.unit_settings.system == "IMPERIAL":
props.diagram_scale = '1/8"=1\'-0"|1/96' props.diagram_scale = '1/8"=1\'-0"|1/96'
else: else:
@@ -628,7 +636,9 @@ class Drawing(bonsai.core.tool.Drawing):
elif location_hint == "WEST": elif location_hint == "WEST":
return mathutils.Matrix(((0, 0, 1, x), (1, 0, 0, y), (0, 1, 0, z), (0, 0, 0, 1))) return mathutils.Matrix(((0, 0, 1, x), (1, 0, 0, y), (0, 1, 0, z), (0, 0, 0, 1)))
elif target_view == "MODEL_VIEW": elif target_view == "MODEL_VIEW":
return mathutils.Matrix(((1, 0, 0, x), (0, 1, 0, y), (0, 0, 1, z), (0, 0, 0, 1))) assert (space := tool.Blender.get_view3d_space())
assert (r3d := space.region_3d)
return r3d.view_matrix.inverted()
return mathutils.Matrix() return mathutils.Matrix()
@classmethod @classmethod