Giter Site home page Giter Site logo

Comments (2)

TheBricktop avatar TheBricktop commented on May 26, 2024 1

there is no main function in godot-python (this is because it's godot that decides which class should be loaded, then ask godot-python load the corresponding python file). So everything in you class's file will be executed and if you have a if name == "main": .... it will be skipped

but the question remains : how to stop godot python from autoexecuting every python script on start of the editor because its locking some of the features and hardware before the actual game can start.
I can put the aforementioned code behind if name == main but that stops it from being executed when the game starts.
Is there a way to do like if is_godot_engine == false : to lock some parts of code behind ?

My problem stems from need to block the execution of functions that im holding in a helper classes, so i dont know if i should to stuff all the class into the "if name" block.

also there is a weird bug that does not allow exporting the variables in the script to the editor, i cant put my finger on why.

let me post my script here to examine what is wrong with it :

from godot import exposed, export, ResourceLoader
from godot import *
import ctypes 
import subprocess
import sys
import cv2
import depthai as dai
from depthaiHelpers import dai_pipeline as pipeline
from depthaiHelpers import dai_getCalibration as calib
from depthaiHelpers import cv2_tracking as tracking

    import numpy as np
    
    @exposed
    class Main(Spatial):
    # member variables here, example:
texture = ImageTexture()

material = export(SpatialMaterial)

pool_array = PoolByteArray()
sensor = pipeline.Oakd_pipeline(True,True,True,True)
sensor.setupPipeline()
print("main is being run")

frame, height, width, channels, size = None,None,None,None,None

def frame_to_godot_image(self, frame, height, width, channels, size):
	print(width, height, channels)
	image = Image()
	

	with self.pool_array.raw_access() as ptr:
		numpy_array = np.ctypeslib.as_array(ctypes.cast(ptr.get_address(), ctypes.POINTER(ctypes.c_uint8)), (size,))
		numpy_array[:] = np.frombuffer(frame, dtype=np.uint8).reshape((height,width)[::-1]+(channels,)).flatten()

	image.create_from_data( width, height, False, Image.FORMAT_RGB8, self.pool_array)
	return image

def _ready(self):
	print("YEllo")

	
	frame = self.sensor.getColorFrames()
	self.height, self.width, self.channels = frame.shape
	self.size = self.width * self.height * self.channels
	self.pool_array.resize(self.size)
	#if self.material is not None:
	#	self.material.albedo_texture = self.texture

def _process(self,delta):
	frame = self.sensor.getColorFrames()
	image = self.frame_to_godot_image(frame,self.height, self.width, self.channels, self.size)
	self.texture.create_from_image(image) 
	self.material.emission_texture = self.texture

EDIT:
After some tinkering ive managed to understand what You've meant and moved all the functional code to the def _ready: and def _process: to avoid premature actions.

from godot-python.

touilleMan avatar touilleMan commented on May 26, 2024

Hi @TheBricktop

You are talking about Godot editor, so you know there is multiple way Godot run a project:

  • Godot running the game (i.e. calling something like godot --path path/to/my/project/) this is typically the way you run in production. In this mode there is no Godot editor running.
  • Godot editor running (i.e. calling godot --python path/to/my/project --editor) this time you are not running your game but the project of your game (remember the Godot editor is actually a Godot game !). So in this mode your python code is not running given you're not running your game (unless you mark a class as tool then the class will run only in the editor and not when running the game)
  • Godot editor running the game. This is typically the debug mode when hitting F5 in the editor. Each time you do that you create a new process that will run the game.

So I advice you to make sure you are not by mistake running multiple instance of the python code that would then fight over connecting to your camera...

The possible culprit might be that godot language server runs through all of the init and main functions in provided

there is no main function in godot-python (this is because it's godot that decides which class should be loaded, then ask godot-python load the corresponding python file). So everything in you class's file will be executed and if you have a if __name__ == "__main__": .... it will be skipped

from godot-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.