From a285db3c41e8f3f06f729024576cc0f2b58c1155 Mon Sep 17 00:00:00 2001 From: Yanis42 <35189056+Yanis42@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:17:41 +0200 Subject: [PATCH 1/3] improved "add path" button --- fast64_internal/oot/oot_utility.py | 71 +++++++++++++++++++++++++- fast64_internal/oot/tools/operators.py | 38 +++++++++++--- 2 files changed, 102 insertions(+), 7 deletions(-) diff --git a/fast64_internal/oot/oot_utility.py b/fast64_internal/oot/oot_utility.py index 1d7ca39d4..2a2f8ce01 100644 --- a/fast64_internal/oot/oot_utility.py +++ b/fast64_internal/oot/oot_utility.py @@ -1,5 +1,10 @@ -import bpy, math, os, re +import bpy +import math +import os +import re + from ast import parse, Expression, Num, UnaryOp, USub, Invert, BinOp +from mathutils import Vector from bpy.utils import register_class, unregister_class from typing import Callable from .oot_constants import ootSceneIDToName @@ -861,3 +866,67 @@ def _eval(node): raise ValueError(f"Unsupported AST node {node}") return f"0x{_eval(node.body):X}" + + +def getNewPath(type: str, isClosedShape: bool): + """ + type: the path's type (square, line, circle, etc) + radius: how big the shape will be + """ + + # create a new curve + newCurve = bpy.data.curves.new("New Path", "CURVE") + newCurve.dimensions = "3D" + + # add a new spline to the curve + newSpline = newCurve.splines.new("NURBS") # comes with 1 point + + # generate shape based on 'type' parameter + scaleDivBy2 = bpy.context.scene.ootBlenderScale / 2 + match type: + case "Line": + newSpline.points.add(1) + for i, point in enumerate(newSpline.points): + point.co.x = i * bpy.context.scene.ootBlenderScale + point.co.w = 1 + case "Triangle": + newSpline.points.add(2) + for i, point in enumerate(newSpline.points): + point.co.x = i * scaleDivBy2 + if i == 1: + point.co.y = (len(newSpline.points) * scaleDivBy2) / 2 + point.co.w = 1 + case "Square" | "Trapezium": + newSpline.points.add(3) + for i, point in enumerate(newSpline.points): + point.co.x = i * scaleDivBy2 + if i in [1, 2]: + if type == "Square": + point.co.y = (len(newSpline.points) - 1) * scaleDivBy2 + if i == 1: + point.co.x = newSpline.points[0].co.x + else: + point.co.x = point.co.y + else: + point.co.y = 1 * scaleDivBy2 + point.co.w = 1 + case _: + raise PluginError("ERROR: Invalid Path Type!") + + if isClosedShape and type != "Line": + newSpline.points.add(1) + newSpline.points[-1].co = newSpline.points[0].co + + # make the curve's display accurate to the point's shape + newSpline.use_cyclic_u = True + newSpline.use_endpoint_u = False + newSpline.resolution_u = 64 + newSpline.order_u = 2 + + # create a new object and add the curve as data + newPath = bpy.data.objects.new("New Path", newCurve) + newPath.show_name = True + newPath.location = Vector(bpy.context.scene.cursor.location) + bpy.context.scene.collection.objects.link(newPath) + + return newPath diff --git a/fast64_internal/oot/tools/operators.py b/fast64_internal/oot/tools/operators.py index 03b73e177..a7069a93d 100644 --- a/fast64_internal/oot/tools/operators.py +++ b/fast64_internal/oot/tools/operators.py @@ -1,9 +1,10 @@ from mathutils import Vector from bpy.ops import mesh, object, curve from bpy.types import Operator -from bpy.props import FloatProperty, StringProperty +from bpy.props import FloatProperty, StringProperty, EnumProperty, BoolProperty from ...operators import AddWaterBox, addMaterialByName from ...utility import parentObject, setOrigin +from ..oot_utility import getNewPath class OOT_AddWaterBox(AddWaterBox): @@ -164,19 +165,44 @@ def execute(self, context): class OOT_AddPath(Operator): bl_idname = "object.oot_add_path" bl_label = "Add Path" - bl_options = {"REGISTER", "UNDO", "PRESET"} + bl_options = {"REGISTER", "UNDO"} + + isClosedShape: BoolProperty(name="", default=True) + pathType: EnumProperty( + name="", + items=[ + ("Line", "Line", "Line"), + ("Square", "Square", "Square"), + ("Triangle", "Triangle", "Triangle"), + ("Trapezium", "Trapezium", "Trapezium"), + ], + default="Line", + ) def execute(self, context): if context.mode != "OBJECT": object.mode_set(mode="OBJECT") object.select_all(action="DESELECT") - location = Vector(context.scene.cursor.location) - curve.primitive_nurbs_path_add(radius=1, align="WORLD", location=location[:]) - pathObj = context.view_layer.objects.active - pathObj.name = "New Path" + pathObj = getNewPath(self.pathType, self.isClosedShape) + activeObj = context.view_layer.objects.active + if activeObj.type == "EMPTY" and activeObj.ootEmptyType == "Scene": + pathObj.parent = activeObj object.select_all(action="DESELECT") pathObj.select_set(True) context.view_layer.objects.active = pathObj return {"FINISHED"} + + def invoke(self, context, event): + return context.window_manager.invoke_props_dialog(self, width=320) + + def draw(self, context): + layout = self.layout + layout.label(text="Path Settings") + props = [("Path Type", "pathType"), ("Closed Shape", "isClosedShape")] + + for desc, propName in props: + split = layout.split(factor=0.30) + split.label(text=desc) + split.prop(self, propName) From 371255fbba6f282e9931592fc825d462d15cc4fd Mon Sep 17 00:00:00 2001 From: Yanis42 <35189056+Yanis42@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:21:27 +0200 Subject: [PATCH 2/3] fix func description --- fast64_internal/oot/oot_utility.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fast64_internal/oot/oot_utility.py b/fast64_internal/oot/oot_utility.py index 2a2f8ce01..60d54caaa 100644 --- a/fast64_internal/oot/oot_utility.py +++ b/fast64_internal/oot/oot_utility.py @@ -870,8 +870,11 @@ def _eval(node): def getNewPath(type: str, isClosedShape: bool): """ - type: the path's type (square, line, circle, etc) - radius: how big the shape will be + Returns a new Curve Object with the selected spline shape + + Parameters: + - ``type``: the path's type (square, line, etc) + - ``isClosedShape``: choose if the spline should have an extra point to make a closed shape """ # create a new curve From cc3b1f70510c107ba431ed5b9efa44dc62b8d6ff Mon Sep 17 00:00:00 2001 From: Yanis42 <35189056+Yanis42@users.noreply.github.com> Date: Wed, 11 Oct 2023 17:47:51 +0200 Subject: [PATCH 3/3] fix collection --- fast64_internal/oot/oot_utility.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fast64_internal/oot/oot_utility.py b/fast64_internal/oot/oot_utility.py index 60d54caaa..e175ae9f5 100644 --- a/fast64_internal/oot/oot_utility.py +++ b/fast64_internal/oot/oot_utility.py @@ -930,6 +930,6 @@ def getNewPath(type: str, isClosedShape: bool): newPath = bpy.data.objects.new("New Path", newCurve) newPath.show_name = True newPath.location = Vector(bpy.context.scene.cursor.location) - bpy.context.scene.collection.objects.link(newPath) + bpy.context.view_layer.active_layer_collection.collection.objects.link(newPath) return newPath