How to get the script name from python?

Hi,

I have a component with a property which decides how many of the same scripts it will run at the same time. So if i set it to 4 i will have Script1, Script2, Script3, Script4.

The scripts will all listen to the same string signal, which is on the format “SciptThatShouldlIsten, Information1, Information2…”. This means that i want to retrieve the script name from inside the script on some sort of function similar to getComponent().

comp.findBehavioursByType or comp.findBehaviour doesn’t really give me much, since the index of the specific script has to be found for itself.

Any ideas?

 

Hi Antjoha

 

I wouldn’t know of a way on how to retrieve the name of the current script.

What I do is, I always use one signal behaviour for each script.

So in your example it would be Script1, Script1Signal, Script2, Script2Signal, etc…

 

Hope that helps.

Hi,

To find the name of the script that you are in right now you can use this:

print comp.findBehaviour(__file__.split("::")[1]).Name

if you want to add a signal to the script you are in right now you can use:

currentScript = comp.findBehaviour(__file__.split("::")[1])
signal = comp.findBehaviour("someSignal")
if not currentScript in signal.Connections:
  connections = signal.Connections
  connections.append(thisScript)

I hope this helps, if not, let me know!

1 Like

Genious!

Didn’t think about that

or just:

print __file__.split("::")[1]

 

Yes, that’s true, it was a lazy copy-paste action from my script haha!

Normally instead of print I define it as a variable (and without .Name)

Good addition!