Skip to content

Commit

Permalink
usable but didn't pass pyright
Browse files Browse the repository at this point in the history
  • Loading branch information
Gino authored and Gino committed Feb 4, 2025
1 parent 3493779 commit 2a81ad5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 116 deletions.
144 changes: 77 additions & 67 deletions editor-blender/core/actions/state/load/floor.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
from typing import cast

import bpy
from typing import cast

from ....config import config
from ....utils.convert import rgb_to_float

def setup_floor():
def setup_floor() -> None:
if not bpy.context:
return
data_objects = cast(dict[str, bpy.types.Object], bpy.data.objects)

data_objects: dict[str, bpy.types.Object] = cast(dict[str, bpy.types.Object], bpy.data.objects)

# Create floor
stage_scale: float = getattr(config, "stage_scale")
stage_width: float = getattr(config, "stage_width") * stage_scale
stage_length: float = getattr(config, "stage_length") * stage_scale
stage_stroke = 0.02
stage_color = (*rgb_to_float((38, 123, 216)), 1)
stage_scale: float = cast(float, getattr(config, "stage_scale", 1.0))
stage_width: float = cast(float, getattr(config, "stage_width", 1.0)) * stage_scale
stage_length: float = cast(float, getattr(config, "stage_length", 1.0)) * stage_scale
stage_stroke: float = 0.02
stage_color: tuple[float, float, float, float] = (*rgb_to_float((38, 123, 216)), 1.0)

edge_locations = [
edge_locations: list[tuple[float, float, float]] = [
(0, stage_width / 2, 0),
(0, -stage_width / 2, 0),
(stage_length / 2, 0, 0),
(-stage_length / 2, 0, 0),
]
edge_scales = [
edge_scales: list[tuple[float, float, float]] = [
(stage_length + stage_stroke, stage_stroke, stage_stroke),
(stage_length + stage_stroke, stage_stroke, stage_stroke),
(stage_stroke, stage_width + stage_stroke, stage_stroke),
Expand All @@ -32,61 +32,74 @@ def setup_floor():

for i in range(4):
name = f"FloorEdge{i}"
if data_objects.get(name) is not None:
if name in data_objects:
bpy.data.objects.remove(data_objects[name])

bpy.ops.mesh.primitive_cube_add(size=1)
if not (edge_obj := bpy.context.object):
edge_obj: bpy.types.Object | None = bpy.context.object
if edge_obj is None:
return
edge_obj.name = f"FloorEdge{i}"

edge_obj.name = name
edge_obj.location = edge_locations[i]
edge_obj.scale = edge_scales[i]
edge_obj.color = cast(bpy.types.bpy_prop_array, stage_color)
edge_obj.color = stage_color
edge_obj.hide_select = True

for obj in cast(list[bpy.types.Object], bpy.context.view_layer.objects.selected):
obj.select_set(False)

#new---------------------
material_wooden = bpy.data.materials.new( name = "Wooden" )
# Floor Material setup
material_wooden = bpy.data.materials.new(name="Wooden")
material_wooden.use_nodes = True

material_output = material_wooden.node_tree.nodes.get("Material Output")
material_output.location = (400,500)
pri_bsdf = material_wooden.node_tree.nodes.get("Principled BSDF")
pri_bsdf.location = (100,500)
brick_texture = material_wooden.node_tree.nodes.new("ShaderNodeTexBrick")
brick_texture.location = (-600,300)
map1 = material_wooden.node_tree.nodes.new("ShaderNodeMapping")
map1.location = (-800,300)
coord1 = material_wooden.node_tree.nodes.new("ShaderNodeTexCoord")
coord1.location = (-1000,300)
bump = material_wooden.node_tree.nodes.new("ShaderNodeBump")
bump.location = (-400,200)
noise = material_wooden.node_tree.nodes.new("ShaderNodeTexNoise")
noise.location = (-600,750)
map2 = material_wooden.node_tree.nodes.new("ShaderNodeMapping")
map2.location = (-800,750)
coord2 = material_wooden.node_tree.nodes.new("ShaderNodeTexCoord")
coord2.location = (-1000,750)
mix = material_wooden.node_tree.nodes.new("ShaderNodeMixRGB")
mix.location = (-400,500)
ramp = material_wooden.node_tree.nodes.new("ShaderNodeValToRGB")
ramp.location = (-200,500)
node_tree = material_wooden.node_tree
if node_tree is None:
return

#Connecting Nodes
material_wooden.node_tree.links.new(coord1.outputs[3],map1.inputs[0])
material_wooden.node_tree.links.new(coord2.outputs[3],map2.inputs[0])
material_wooden.node_tree.links.new(map1.outputs[0],brick_texture.inputs[0])
material_wooden.node_tree.links.new(map2.outputs[0],noise.inputs[0])
material_wooden.node_tree.links.new(brick_texture.outputs[0],mix.inputs[2])
material_wooden.node_tree.links.new(noise.outputs[0],mix.inputs[1])
material_wooden.node_tree.links.new(mix.outputs[0],ramp.inputs[0])
material_wooden.node_tree.links.new(ramp.outputs[0],pri_bsdf.inputs[0])
material_wooden.node_tree.links.new(bump.outputs[0],pri_bsdf.inputs[5])
material_output = node_tree.nodes.get("Material Output")
pri_bsdf = node_tree.nodes.get("Principled BSDF")

#Modifying the Nodes

if not material_output or not pri_bsdf:
return

material_output.location = (400, 500)
pri_bsdf.location = (100, 500)

# Create nodes
brick_texture = node_tree.nodes.new("ShaderNodeTexBrick")
map1 = node_tree.nodes.new("ShaderNodeMapping")
coord1 = node_tree.nodes.new("ShaderNodeTexCoord")
bump = node_tree.nodes.new("ShaderNodeBump")
noise = node_tree.nodes.new("ShaderNodeTexNoise")
map2 = node_tree.nodes.new("ShaderNodeMapping")
coord2 = node_tree.nodes.new("ShaderNodeTexCoord")
mix = node_tree.nodes.new("ShaderNodeMixRGB")
ramp = node_tree.nodes.new("ShaderNodeValToRGB")

# Set node locations (For manual adjustment)
brick_texture.location = (-600, 300)
map1.location = (-800, 300)
coord1.location = (-1000, 300)
bump.location = (-400, 200)
noise.location = (-600, 750)
map2.location = (-800, 750)
coord2.location = (-1000, 750)
mix.location = (-400, 500)
ramp.location = (-200, 500)

# Connect nodes
node_tree.links.new(coord1.outputs[3], map1.inputs[0])
node_tree.links.new(coord2.outputs[3], map2.inputs[0])
node_tree.links.new(map1.outputs[0], brick_texture.inputs[0])
node_tree.links.new(map2.outputs[0], noise.inputs[0])
node_tree.links.new(brick_texture.outputs[0], mix.inputs[2])
node_tree.links.new(noise.outputs[0], mix.inputs[1])
node_tree.links.new(mix.outputs[0], ramp.inputs[0])
node_tree.links.new(ramp.outputs[0], pri_bsdf.inputs[0])
node_tree.links.new(bump.outputs[0], pri_bsdf.inputs[5])

# Modify node values
brick_texture.inputs[1].default_value = (0.226, 0.226, 0.226, 1)
brick_texture.inputs[2].default_value = (0.413, 0.413, 0.413, 1)
brick_texture.inputs[4].default_value = 7.00
Expand All @@ -96,22 +109,20 @@ def setup_floor():
brick_texture.inputs[8].default_value = 4.00
brick_texture.inputs[9].default_value = 0.60


noise.inputs[2].default_value = 6.00
noise.inputs[3].default_value = 16.00
noise.inputs[4].default_value = 0.80
noise.inputs[8].default_value = 4.00

map2.inputs[3].default_value[1] = 10.00

mix.inputs[0].default_value = 1.00
mix.blend_type = 'MULTIPLY'

ramp.color_ramp.elements.new(0.200)
ramp.color_ramp.elements.new(0.500)
ramp.color_ramp.elements[3].position = (0.450)
ramp.color_ramp.elements[2].position = (0.250)
ramp.color_ramp.elements[1].position = (0.100)
ramp.color_ramp.elements[3].position = 0.450
ramp.color_ramp.elements[2].position = 0.250
ramp.color_ramp.elements[1].position = 0.100
ramp.color_ramp.elements[3].color = (1.00, 0.89, 0.81, 1.00)
ramp.color_ramp.elements[2].color = (0.90, 0.65, 0.47, 1.00)
ramp.color_ramp.elements[1].color = (0.42, 0.27, 0.18, 1.00)
Expand All @@ -120,18 +131,17 @@ def setup_floor():

flr = "wooden_floor"

# remove the existing floor
# Remove existing floor
if flr in bpy.data.objects:
obj = bpy.data.objects.get(flr)
bpy.data.objects.remove(obj,do_unlink=True)
if obj:
bpy.data.objects.remove(obj, do_unlink=True)

# adding floor
bpy.ops.mesh.primitive_cube_add(scale = (stage_width/2,stage_length/2,0.01))
# Add new floor
bpy.ops.mesh.primitive_cube_add(scale=(stage_width / 2, stage_length / 2, 0.01))
obj = bpy.context.object
obj.rotation_euler.z = 1.5708
obj.name = flr # 設定名稱
obj.color = (0.00315199, 0.00315199, 0.00315199, 1)

#add this material onto the floor
obj.active_material = material_wooden
return
if obj:
obj.rotation_euler.z = 1.5708
obj.name = flr
obj.color = (0.00315199, 0.00315199, 0.00315199, 1)
obj.active_material = material_wooden
7 changes: 3 additions & 4 deletions editor-blender/core/actions/state/load/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ async def import_model_to_asset(

# Ensure the object has valid data
if obj.data is not None and hasattr(obj.data, 'materials'):
# Create a new Emission node

# Create new nodes and some setup
bsdf_node = material.node_tree.nodes.new(type='ShaderNodeBsdfPrincipled')
object_node = material.node_tree.nodes.new(type="ShaderNodeObjectInfo")

# Set default color to blue (Just to test, it needs to be black)

material.node_tree.links.new(object_node.outputs[1], bsdf_node.inputs[0])
material.node_tree.links.new(object_node.outputs[1], bsdf_node.inputs[26])
bsdf_node.inputs[27].default_value = 5.0
Expand All @@ -129,7 +129,6 @@ async def import_model_to_asset(
if not material_output_node:
material_output_node = material.node_tree.nodes.new(type='ShaderNodeOutputMaterial')

# Connect the Emission node to the Material Output
material.node_tree.links.new(bsdf_node.outputs[0], material_output_node.inputs['Surface'])

# Assign material to the object
Expand Down
48 changes: 19 additions & 29 deletions editor-blender/operators/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@ def toggle_camera_view(context):
scene = context.scene
area = next(area for area in context.screen.areas if area.type == 'VIEW_3D')

# 如果我們在 Camera 模式下,則記錄當前視角的位置與角度
# Control On/Off based on whether the user is in Camera View
if area.spaces[0].region_3d.view_perspective != 'CAMERA':
# 切換到相機視角
area.spaces[0].region_3d.view_perspective = 'CAMERA'

# 確保有名為 'lightdance_camera' 的相機
# Ensure 'lightdance_camera' Camera
camera = bpy.data.objects.get('lightdance_camera')
if not camera:
bpy.ops.object.camera_add()
camera = bpy.context.object
camera.name = 'lightdance_camera'

# 設置為場景的當前相機
scene.camera = camera

# 設置預設相機位置和角度
camera.location = mathutils.Vector((10, 0, 2.7)) # 設定預設位置
target = mathutils.Vector((0, 0, 1.5)) # 設定目標位置
# Default location and rotaion.
camera.location = mathutils.Vector((10, 0, 2.7))
target = mathutils.Vector((0, 0, 1.5))
direction = camera.location - target
camera.rotation_euler = direction.to_track_quat('Z', 'Y').to_euler()

# 紀錄當前視角位置與角度
# Remember the previous location and rotaion.
scene['pre_camera_location'] = area.spaces[0].region_3d.view_location.copy()
scene['pre_camera_rotation'] = area.spaces[0].region_3d.view_rotation.copy()

# 設置渲染視圖
# Switch rendering modes and set rendering-related settings
bpy.context.space_data.shading.type = 'RENDERED'
bpy.context.space_data.overlay.show_overlays = False
bpy.context.scene.render.engine = 'BLENDER_EEVEE_NEXT'
Expand All @@ -39,12 +37,11 @@ def toggle_camera_view(context):
bpy.context.scene.eevee.ray_tracing_options.trace_max_roughness = 0.95
bpy.context.space_data.lock_camera = True
else:
# 恢復原來的視角設定
# Return to the original location/rotation.
if 'pre_camera_location' in scene and 'pre_camera_rotation' in scene:
area.spaces[0].region_3d.view_location = scene['pre_camera_location']
area.spaces[0].region_3d.view_rotation = scene['pre_camera_rotation']

# 切回到透視模式
area.spaces[0].region_3d.view_perspective = 'PERSP'
bpy.context.space_data.shading.type = 'SOLID'
bpy.context.space_data.overlay.show_overlays = True
Expand All @@ -60,7 +57,7 @@ def execute(self, context):

if camera:
camera.location.y = self.target_y
scene.camera_y = self.target_y # 更新滑桿的值
scene.camera_y = self.target_y # Update the value on slider

return {'FINISHED'}

Expand All @@ -75,7 +72,7 @@ def execute(self, context):

if camera:
camera.location.x = self.target_x
scene.camera_x = self.target_x # 更新滑桿的值
scene.camera_x = self.target_x # Updtate the value on slider

return {'FINISHED'}

Expand All @@ -89,56 +86,49 @@ def execute(self, context):
camera = bpy.data.objects.get('lightdance_camera')

if camera:
camera.data.lens = self.target_focal_length # 設定相機焦距
scene.camera_focal_length = self.target_focal_length # 更新滑桿的值
camera.data.lens = self.target_focal_length
scene.camera_focal_length = self.target_focal_length # Update the value on slider

return {'FINISHED'}

# 開啟或關閉相機視角的操作

class ToggleCameraOperator(bpy.types.Operator):
bl_idname = "view3d.toggle_camera_operator"
bl_label = "Toggle Camera View"

def execute(self, context):
scene = context.scene

# 切換 camera_on 狀態
scene["camera_on"] = not scene.get("camera_on", False)

# 執行切換相機視角函式
toggle_camera_view(context)

return {'FINISHED'}

# 自動Fit到全螢幕的操作

class AutoFitFullscreenOperator(bpy.types.Operator):
bl_idname = "view3d.auto_fit_fullscreen"
bl_label = "Auto Fit to Fullscreen"

def execute(self, context):
# 使用 view_center_camera 來自動調整視角
bpy.ops.view3d.view_center_camera()
return {'FINISHED'}

def update_camera(self, context):
# 在這裡不再新增相機,而是只更新現有相機的屬性
camera = bpy.data.objects.get('lightdance_camera')
if camera:
# 更新相機位置
# update camera position
camera.location.y = context.scene.camera_y
camera.location.x = max(4.5, min(context.scene.camera_x, 17.5))

# 根據 X 位置調整 Z 位置
# adjust z postion according to x
if 4.5 <= camera.location.x <= 5.5:
camera.location.z = 0.3 + (camera.location.x - 4.5) * 1.5
camera.location.z = 0.3 + (camera.location.x - 4.5) * 1.5 #the first row is relatively low.
else:
camera.location.z = 1.8 + (camera.location.x - 5.5) * 0.2

# 更新相機焦距
camera.data.lens = context.scene.camera_focal_length

# 計算相機的朝向
target = mathutils.Vector((0, 0, 1.5)) # 假設目標在原點
# Let camera face the center of the stage
target = mathutils.Vector((0, 0, 1.5))
direction = camera.location - target
camera.rotation_euler = direction.to_track_quat('Z', 'Y').to_euler()

Expand Down
Loading

0 comments on commit 2a81ad5

Please sign in to comment.