浏览代码

fix camera rotation

Sergiu 8 月之前
父节点
当前提交
f6fe93e198

+ 1 - 1
03_blender/prepare_folder_for export.py

@@ -42,7 +42,7 @@ source_folder = (
     r"Z:/01_Production_AP/01_Library/03_Elements/_Brand_AssetName_Year_AssetNumber"
 )
 target_folder = r"Z:/01_Production_AP/01_Library/03_Elements/AP/Products"
-new_folder_name = "AP_DRLC_2024_0001"
+new_folder_name = "AP_LRDSPE_2024_0001"
 content_id = "ALL_DT_MAT"
 
 copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)

+ 91 - 11
03_blender/sd_blender/__init__.py

@@ -24,7 +24,6 @@ else:
     import os
     import base64
     import numpy as np
-    from pyquaternion import Quaternion
 
     # from . import zs_renderscene as zsrs  # noqa
 
@@ -418,6 +417,31 @@ def set_environment(scene_data):
             print("Group 'NG_Canvas_Background' not found")
 
 
+def calculate_focal_length(fov, film_height):
+    # Convert FOV from degrees to radians
+    fov_rad = math.radians(fov)
+
+    # Calculate the focal length
+    focal_length = (film_height / 2) / math.tan(fov_rad / 2)
+
+    return focal_length
+
+
+def quaternion_multiply(q1, q2):
+    """
+    Multiplies two quaternions.
+    q1 and q2 are arrays or lists of length 4.
+    Returns the resulting quaternion as a NumPy array.
+    """
+    w1, x1, y1, z1 = q1
+    w2, x2, y2, z2 = q2
+    w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2
+    x = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2
+    y = w1 * y2 - x1 * z2 + y1 * w2 + z1 * x2
+    z = w1 * z2 + x1 * y2 - y1 * x2 + z1 * w2
+    return np.array([w, x, y, z])
+
+
 def create_cameras(scene_data):
     # # Get the 05_Cameras collection, or create it if it doesn't exist
     collection_name = "05_Cameras"
@@ -444,18 +468,71 @@ def create_cameras(scene_data):
         camera.location.z = position[1]
 
         # Set the camera's rotation
-        rotation = camera_data["properties"]["transform"]["rotation"]
-        rotation_euler = Euler(
-            (
-                math.radians(rotation[0]),
-                math.radians(rotation[2]),
-                math.radians(rotation[1]),
-            ),
-            "XYZ",
+        # # euler
+        # rotation_euler = camera_data["properties"]["transform"]["rotation"]
+        # rotation_euler = Euler(
+        #     (
+        #         math.radians(rotation_euler[0] + 90),
+        #         math.radians(-rotation_euler[2]),
+        #         math.radians(rotation_euler[1]),
+        #     ),
+        #     "XYZ",
+        # )
+        # quaternion
+        rotation_quat_data = camera_data["properties"]["transform"]["rotation"]
+        # rotation_quat = (
+        #     rotation_quat[3],
+        #     rotation_quat[0],
+        #     rotation_quat[1],
+        #     rotation_quat[2],
+        # )
+
+        # new_quat = Quaternion((2**0.5 / 2, 2**0.5 / 2, 0.0, 0.0))
+
+        # current_quat = Quaternion(rotation_quat)
+
+        # result_quat = current_quat @ new_quat
+
+        # rotation_quat = (result_quat.w, result_quat.x, result_quat.y, result_quat.z)
+
+        rotation_quat = [
+            rotation_quat_data[3],
+            rotation_quat_data[0],
+            rotation_quat_data[1],
+            rotation_quat_data[2],
+        ]  # Example quaternion
+
+        # new_quat = [2**0.5 / 2, 2**0.5 / 2, 0.0, 0.0]
+
+        # result_quat = quaternion_multiply(rotation_quat, new_quat)
+
+        # Example quaternion from GLTF: [x, y, z, w]
+        gltf_quat = [0.0, 0.707, 0.0, 0.707]
+
+        # Convert the GLTF quaternion to Blender space (Y-up to Z-up)
+        converted_quat = Quaternion(
+            [
+                rotation_quat_data[3],
+                rotation_quat_data[0],
+                rotation_quat_data[1],
+                rotation_quat_data[2],
+            ]
         )
 
+        # Define the camera correction quaternion (from GLTF file)
+        camera_correction = Quaternion((2**0.5 / 2, 2**0.5 / 2, 0.0, 0.0))
+
+        # Apply the camera correction to the converted quaternion
+        corrected_quat = camera_correction @ converted_quat
+
+        # Apply the corrected quaternion to the camera's rotation
+        camera.rotation_mode = "QUATERNION"
+        camera.rotation_quaternion = corrected_quat
+
+        # camera.rotation_mode = "QUATERNION"
+        # camera.rotation_quaternion = rotation_quat
+
         # Apply the local rotation to the camera
-        camera.rotation_euler = rotation_euler
 
         # Set the camera's lens properties
         lens = camera_data["properties"]["lens"]
@@ -464,8 +541,11 @@ def create_cameras(scene_data):
             "ORTHOGRAPHIC": "ORTHO",
             "PANORAMIC": "PANO",
         }
+
+        camera.data.lens = lens["focalLength"]
+
         camera.data.type = type_mapping.get(lens["type"].upper(), "PERSP")
-        camera.data.angle = math.radians(lens["fov"])
+
         camera.data.clip_start = lens["near"]
         camera.data.clip_end = lens["far"]
 

二进制
03_blender/sd_blender/__pycache__/__init__.cpython-310.pyc


二进制
03_blender/sd_blender/__pycache__/__init__.cpython-311.pyc


二进制
03_blender/sd_blender/sample_scene/Canvas_Render_Scene.blend


+ 2 - 2
03_blender/sd_blender/sample_scene/assets_database.json

@@ -1,12 +1,12 @@
 [
   {
-    "id": "LNG Sleeping Mask",
+    "id": "f8761a95-c28a-47ec-9254-e76cc3b693c7",
     "name": "LNG_AntiAging_SleepingMask_MY2023",
     "web_path": "https://www.laneige.com/LNG_AntiAging_SleepingMask_MY2023.glb",
     "type": "product"
   },
   {
-    "id": "LNG PerfectRenew",
+    "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89f8",
     "name": "LNG_PerfectRenew_Serum_MY2023",
     "web_path": "https://www.laneige.com/LNG_PerfectRenew_Serum_MY2023.glb",
     "type": "product"

+ 20 - 33
03_blender/sd_blender/sample_scene/scene_info.json

@@ -2,10 +2,10 @@
   "scene": {
     "objects": [
       {
-        "name": "LNG Sleeping Mask",
+        "name": "LNG PerfectRenew",
         "type": "group",
         "group_type": "product",
-        "id": "LNG Sleeping Mask 4",
+        "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89f8",
         "properties": {
           "transform": {
             "position": [
@@ -15,32 +15,12 @@
             ],
             "rotation": [
               0,
-              0,
-              0
-            ],
-            "scale": [
-              10,
-              10,
-              10
-            ]
-          },
-          "visible": true
-        }
-      },
-      {
-        "name": "LNG Sleeping Mask",
-        "type": "group",
-        "group_type": "product",
-        "id": "LNG Sleeping Mask 4",
-        "properties": {
-          "transform": {
-            "position": [
               0,
               0,
-              0.8122250653238932
+              1
             ],
-            "rotation": [
-              45.00000000000001,
+            "rotationEuler": [
+              0,
               0,
               0
             ],
@@ -56,24 +36,31 @@
     ],
     "cameras": [
       {
-        "name": "Camera 1",
+        "name": "Camera",
         "type": "camera",
         "properties": {
           "transform": {
             "position": [
-              3.077126336045053,
-              0.4348491214286826,
-              0.3981070047067921
+              2.1541220170510824,
+              0.9446412195038136,
+              -2.5454586143216757
             ],
             "rotation": [
-              -179.99999999999997,
-              88.77522425185553,
+              -3.623,
+              0.942,
+              -1.29,
+              0.33
+            ],
+            "rotationEuler": [
+              180,
+              39.236164794715414,
               180
             ]
           },
           "lens": {
             "type": "perspective",
-            "fov": 46.8,
+            "fov": 38.58009243837747,
+            "focalLength": 50,
             "near": 0.1,
             "far": 100
           },
@@ -102,5 +89,5 @@
     }
   },
   "user_id": "1125441",
-  "project_id": "ebae7542-1794-4c07-8902-343db346dd39"
+  "project_id": "2eda9202-086f-4152-9f9a-cf01f78351ae"
 }