Skip to content

Commit

Permalink
Added Scaling with hold right mouse button and F key then scroll mous…
Browse files Browse the repository at this point in the history
…e to scale and same but holding R key to rotate and holding only G key sets the Y orientation to world Y
  • Loading branch information
Fleischkuechle committed Jan 22, 2024
1 parent 5b5424d commit 465fc91
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 1,541 deletions.
185 changes: 124 additions & 61 deletions addons/object_placer/object_placer_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,16 @@ var enteredTree=false
@onready var last_selected_index:int
@onready var do_we_have_scenes_available=true
@onready var process_mouse_wheel_event=false #helper to prevent catching too many mouse wheel events


@onready var scale_factor= Vector3(0.5,0.5,0.5)
@onready var rot_angle=15.0
func getFilePathsByExtension(directoryPath: String, extension: String, recursive: bool = true) -> Array:
var filePaths := []
var dir = DirAccess.open(directoryPath)
if dir:
#print(dir.get_drive_name())
#if dir.open(directoryPath) != OK:
#printerr("Warning: could not open directory: ", directoryPath)
#return []
#
#if dir.list_dir_begin(true, true) != OK:
#printerr("Warning: could not list contents of: ", directoryPath)
#return []

dir.list_dir_begin()

var fileName :String = dir.get_next()
print( 'das ist der filename ',fileName)
print_debug( 'das ist der filename ',fileName)

while fileName != "":
if dir.current_is_dir():
Expand All @@ -47,13 +38,13 @@ func getFilePathsByExtension(directoryPath: String, extension: String, recursive
if fileName.get_extension() == extension:
var filePath = dir.get_current_dir() + "/" + fileName
var objectName=fileName.get_slice(extension,0)
print("das sollte nur der name sein ",objectName )
print_debug("das sollte nur der name sein ",objectName )
filePaths.append(filePath)

fileName = dir.get_next()
print('das ist der filename ',fileName)
print_debug('das ist der filename ',fileName)
else:
print('no dir ')
print_debug('no dir ')
return filePaths

func preload_sceens(directoryPath: String, extension: String, recursive: bool = true) -> Array:
Expand All @@ -62,7 +53,7 @@ func preload_sceens(directoryPath: String, extension: String, recursive: bool =
if dir:
dir.list_dir_begin()
var fileName :StringName = dir.get_next()
#print( 'das ist der filename ',fileName)
#print_debug( 'das ist der filename ',fileName)
while fileName != "":
if dir.current_is_dir():
if recursive:
Expand All @@ -71,14 +62,14 @@ func preload_sceens(directoryPath: String, extension: String, recursive: bool =
else:
if fileName.get_extension() == extension:
var filePath:String = dir.get_current_dir() + "/" + fileName
print(filePath)
print_debug(filePath)
var scene_tscn=load(filePath)

#var scene=preload(name)
sceens_tscn.append(scene_tscn)
fileName = dir.get_next()
else:
print('no dir ')
print_debug('no dir ')
return sceens_tscn

func turn_off_collider(in_node):
Expand Down Expand Up @@ -118,13 +109,13 @@ func _ready() -> void:
if len(sceens_blend)>0:
sceens_tscn.append_array(sceens_blend)

print(sceens_tscn)
print_debug(sceens_tscn)
ray_cast.set_collision_mask_value(1, true)
add_child(ray_cast)
if len(sceens_tscn)<1:
do_we_have_scenes_available=false
#create a shape
print("Creating sphere as marker because there was no scene found in the plugin/object folder")
print_debug("Creating sphere as marker because there was no scene found in the plugin/object folder")
marker_replacer.mesh =BoxMesh.new()
marker_node_3d=marker_replacer
else:
Expand All @@ -135,7 +126,7 @@ func _ready() -> void:

add_child(marker_node_3d)
marker_node_3d.hide()
print('on ready geht')
print_debug('on ready geht')
## The line below is required to make the node visible in the Scene tree dock
## and persist changes made by the tool script to the saved scene file.
#node.owner = get_tree().edited_scene_root
Expand All @@ -145,56 +136,86 @@ func _forward_3d_gui_input(camera: Camera3D, event: InputEvent) -> int:
if event is InputEventMouseMotion:
#camera.size
new_click_location = event.position
#print(new_click_location)

doTheRaycast(camera)
##if new_click_location != Vector2.INF:
if is_mouse_pressed:
await add_child_to_collision_object(new_intersection_result_dict)
##print("Forward 3d GUI. in mouse motion Mouse buttons: %d, pos: %.1v, global_pos: %.1v" % [ event.button_mask, event.position, event.global_position ])
if process_mouse_wheel_event==false:
run_timer()
add_child_to_collision_object(new_intersection_result_dict)
##print_debug("Forward 3d GUI. in mouse motion Mouse buttons: %d, pos: %.1v, global_pos: %.1v" % [ event.button_mask, event.position, event.global_position ])
pass
if event is InputEventMouseButton and event.is_pressed() and event.button_index == 1:
is_mouse_pressed=true
await add_child_to_collision_object(new_intersection_result_dict)
if process_mouse_wheel_event==false:
run_timer()
add_child_to_collision_object(new_intersection_result_dict)
if event is InputEventMouseButton and event.is_released() and event.button_index == 1:
is_mouse_pressed=false
print('maus released')
print_debug('maus released')
pass
#mouse wheel up with right mouse button pressed
if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 4 and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT): #● MOUSE_BUTTON_WHEEL_UP = 4
#mouse wheel up with right mouse button pressed and not F key pressed and not R key pressed
if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 4 and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and not Input.is_key_pressed(KEY_F) and not Input.is_key_pressed(KEY_R): #● MOUSE_BUTTON_WHEEL_UP = 4

print('mouse wheel up')
print_debug('mouse wheel up')
if process_mouse_wheel_event==false:
run_timer()
select_scene_up()

else:
print('skipped one mouse wheel up event because of process_mouse_wheel_event =true')

if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 5 and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):#● MOUSE_BUTTON_WHEEL_DOWN = 5 ● MOUSE_BUTTON_RIGHT = 2
print_debug('skipped one mouse wheel up event because of process_mouse_wheel_event =true')

#mouse wheel down with right mouse button pressed and not F key pressed and not R key pressed
if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 5 and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and not Input.is_key_pressed(KEY_F) and not Input.is_key_pressed(KEY_R):#● MOUSE_BUTTON_WHEEL_DOWN = 5 ● MOUSE_BUTTON_RIGHT = 2
#is_mouse_pressed=false
if process_mouse_wheel_event==false:
run_timer()
select_scene_down()
print('mouse wheel down')
print_debug('mouse wheel down')
else:
print('skipped one mouse wheel down event because of process_mouse_wheel_event =true')

pass


print_debug('skipped one mouse wheel down event because of process_mouse_wheel_event =true')
#mouse wheel down with F key pressed and right mouse button pressed and not R Key pressed
if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 5 and Input.is_key_pressed(KEY_F) and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and not Input.is_key_pressed(KEY_R):#● MOUSE_BUTTON_WHEEL_DOWN = 5 ● MOUSE_BUTTON_RIGHT = 2
#is_mouse_pressed=false
if process_mouse_wheel_event==false:
run_timer()
scale_down_marker_Node_3D()
print_debug('mouse scale down')
else:
print_debug('skipped one scale down event because of process_mouse_wheel_event =true')
#mouse wheel up with F key pressed and right mouse button pressed and not R key pressed
if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 4 and Input.is_key_pressed(KEY_F) and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and not Input.is_key_pressed(KEY_R): #● MOUSE_BUTTON_WHEEL_UP = 4
#print_debug()
print_debug('mouse wheel scaling up')
if process_mouse_wheel_event==false:
run_timer()
scale_up_marker_Node_3D()
else:
print_debug('skipped scale up event because of process_mouse_wheel_event =true')
#Rotate + mouse wheel up with R key pressed and right mouse button pressed and not F key pressed
if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 4 and Input.is_key_pressed(KEY_R) and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and not Input.is_key_pressed(KEY_F): #● MOUSE_BUTTON_WHEEL_UP = 4
if process_mouse_wheel_event==false:
run_timer()
rotate_up_marker_Node_3D()
else:
print_debug('skipped rotate up event because of process_mouse_wheel_event =true')
#Rotate - mouse wheel up with R key pressed and right mouse button pressed and not F key pressed
if event is InputEventMouseButton and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)==false and event.button_index == 5 and Input.is_key_pressed(KEY_R) and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and not Input.is_key_pressed(KEY_F): #● MOUSE_BUTTON_WHEEL_UP = 4
if process_mouse_wheel_event==false:
run_timer()
rotate_down_marker_Node_3D()
else:
print_debug('skipped rotate up event because of process_mouse_wheel_event =true')

return EditorPlugin.AFTER_GUI_INPUT_PASS

func run_timer():
# This method is called when the timer times out
print('running timer')
print_debug('running timer')
if process_mouse_wheel_event==false:
process_mouse_wheel_event=true
await get_tree().create_timer(0.05).timeout
print('stopping timer')
print_debug('stopping timer')
process_mouse_wheel_event=false
print("Mouse wheel up event has been triggered, do something here")
print_debug("Mouse wheel up event has been triggered, do something here")

func _physics_process(_delta):
pass
Expand All @@ -214,7 +235,7 @@ func select_scene_up():

if is_instance_valid(marker_node_3d):
marker_node_3d.queue_free()
#print('bis hier')
#print_debug('bis hier')

marker_tscn=sceens_tscn[new_index]
marker_node_3d=marker_tscn.instantiate()
Expand All @@ -223,8 +244,7 @@ func select_scene_up():
marker_node_3d.position=temp_position
marker_node_3d.rotation =temp_rotation
last_selected_index=new_index
print('new index: ',last_selected_index)

print_debug('new index: ',last_selected_index)
func select_scene_down():

var new_index:int=-1
Expand All @@ -241,7 +261,7 @@ func select_scene_down():

if is_instance_valid(marker_node_3d):
marker_node_3d.queue_free()
#print('bis hier')
#print_debug('bis hier')

marker_tscn=sceens_tscn[new_index]
marker_node_3d=marker_tscn.instantiate()
Expand All @@ -250,8 +270,32 @@ func select_scene_down():
marker_node_3d.position=temp_position
marker_node_3d.rotation =temp_rotation
last_selected_index=new_index
print('new index: ',last_selected_index)

print_debug('new index: ',last_selected_index)
func scale_up_marker_Node_3D():
#var up_scale_factor= Vector3(0.1,0.1,0.1)
print_debug("upscaleing", marker_node_3d.scale)
#marker_node_3d.global_scale(marker_node_3d.scale+up_scale_factor)
marker_node_3d.scale+= scale_factor
func rotate_up_marker_Node_3D():
#var up_scale_factor= Vector3(0.1,0.1,0.1)
print_debug("up Rotating", marker_node_3d.rotation)
#marker_node_3d.global_scale(marker_node_3d.scale+up_scale_factor)
marker_node_3d.rotate_y(deg_to_rad(rot_angle))
func rotate_down_marker_Node_3D():
#var up_scale_factor= Vector3(0.1,0.1,0.1)
print_debug("down Rotating", marker_node_3d.rotation)
#marker_node_3d.global_scale(marker_node_3d.scale+up_scale_factor)
marker_node_3d.rotate_y(deg_to_rad(-rot_angle))




func scale_down_marker_Node_3D():
#var down_scale_factor= Vector3(0.1,0.1,0.1)
if not marker_node_3d.scale.x <=0.5:
print_debug("Downscaling", marker_node_3d.scale)
#marker_node_3d.global_scale(marker_node_3d.scale+down_scale_factor)
marker_node_3d.scale-= scale_factor
func doTheRaycast(camera: Camera3D):
if Engine.is_editor_hint():
marker_node_3d.hide()
Expand All @@ -263,28 +307,28 @@ func doTheRaycast(camera: Camera3D):

if result != null and result.is_empty()==false:
result_position = result["position"]
print(result_position)
#print_debug(result_position)
new_intersection_result_dict=result

##print('kein Result gefunden.')
##print_debug('kein Result gefunden.')
if result_position == Vector3.INF:
marker_node_3d.hide()
print('no hit position')
print_debug('no hit position')
else:
if result != null and result.is_empty()==false:
var col_nor = result.normal
#print(col_nor)
#print_debug(col_nor)
var col_point = result_position

marker_node_3d.show()


marker_node_3d.transform=align_with_normal(marker_node_3d.transform,col_nor)
if Input.is_key_pressed(KEY_G):
marker_node_3d.transform=align_with_normal(marker_node_3d.transform,Vector3.UP)
else:
marker_node_3d.transform=align_with_normal(marker_node_3d.transform,col_nor)
marker_node_3d.position = col_point
print(marker_node_3d.transform)
#print_debug(marker_node_3d.transform)
new_click_location = Vector2.INF



func align_with_normal(xform: Transform3D, n2: Vector3) -> Transform3D:
var n1 = xform.basis.y.normalized()
Expand All @@ -296,12 +340,20 @@ func align_with_normal(xform: Transform3D, n2: Vector3) -> Transform3D:
if axis == Vector3.ZERO:
axis = Vector3.FORWARD # normals are in opposite directions
return xform.rotated(axis, alpha)

func is_instanced_from_scene(p_node):


print(p_node.NodePath)
if not p_node.filename.is_empty():
return true
return false

func add_child_to_collision_object(collision_result:Dictionary):
if collision_result != null and collision_result.is_empty()==false:
var collision_object:Node3D= collision_result.collider #collision_result.collider #collision_result.collider
var collision_point:Vector3 = collision_result.position # Kollisionspunkt des Raycasts
var collision_normal:Vector3 = collision_result.normal # Kollisionspunkt des Raycasts
#var selected_node = get_tree().get_edited_scene_root().get_editor_selection().get_node()

var instanciated_object:Node3D
if do_we_have_scenes_available:
Expand All @@ -310,13 +362,24 @@ func add_child_to_collision_object(collision_result:Dictionary):
var marker_replacer: MeshInstance3D = MeshInstance3D.new()
marker_replacer.mesh =BoxMesh.new()
instanciated_object =marker_replacer
#var sceneroot:Node3D

#print_debug(sceneroot.name)

#var editor = get_editor_interface().get_selection()
#var selected = editor.get_selected_nodes()
#if selected != null and selected.is_empty()==false and selected[0] is Node3D and is_instanced_from_scene(selected[0])==false:
#sceneroot = selected[0]
#print(sceneroot)
#else:
var sceneroot=get_tree().edited_scene_root
#print(sceneroot.name)
sceneroot.add_child(instanciated_object,true)
#sceneroot.add_child(instanciated_object,true)
instanciated_object.owner =sceneroot
instanciated_object.position=marker_node_3d.position
instanciated_object.global_position=marker_node_3d.position
instanciated_object.rotation=marker_node_3d.rotation
print("das itst der new transform", instanciated_object.transform)
instanciated_object.scale=marker_node_3d.scale
#print_debug("das itst der new transform", instanciated_object.transform)


6 changes: 3 additions & 3 deletions addons/object_placer/plugin.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[plugin]

name="Object_Placer"
description=""
author="Ich"
version=""
description="Editor tool for placing sceens(.tscn,.blend) on any collider(Terrain) see also: https://www.youtube.com/@Umocap "
author="Fleischkuechle"
version="0.0.1"
script="object_placer_plugin.gd"
Loading

0 comments on commit 465fc91

Please sign in to comment.