Blender frozen in python script? - python

I am new to both blender and python.
I tried to manipulate some properties of an object via python script in script console of blender.
What I don't understand is I can do it in this way.
bpy.data.object['Cube'].rotation_euler.x+=1
but when I put it in a loop.
import time
i=1
while i<100:
i+=1
bpy.data.object['Cube'].rotation_euler.x+=1
print('run once')
time.sleep(5)
Blender freezes without any output of 'run once'.
Would someone please tell me what is wrong with this code.

Your script isn't freezing, blender just isn't getting a chance to update during the loop.
The time.sleep(5) command sleeps for 5 seconds, being run 100 times means the script takes 8 minutes to run at which stage blender updates it's interface again.
You may want to look at a modal operator - there are several samples within the python templates available in blender's text editor.

Related

Working with Excel 4.0 for work, trying to use a button to launch a python script

So, the shop where I work use Excel 4.0 for all its inventory management and orders.
Since the guy before me left without explaining anything, I inherited an old system that works, but is... eh.
The bosses don't want to change to a new Excel nor another program, so I must do what I can with what I have.
Now, I've made a script in python 2.5.4 (this version is needed because the newer versions won't work on the Windows 98 computer they use...) to automate some processes that would be impossible with Excel 4.0 macros, and the script works perfectly for what I need.
But since the bosses want to "only work with Excel", and won't want to go outside of Excel and click the script icon to start it (or, heaven forbid, open cmd and start it manually), I would need to put a button in Excel to start the script.
I've tried to sift through the macros available, but except perhaps "Initiate" (which I don't wholly understand as of now), I can't think of a macro to interact with the script, and haven't found much help with what's available online...
SO, could anyone please help me in making the macro for the button? The only thing the button would need to do is to start the python script, there's no other interactions needed, the rest is done by the script.
Like, the script "foo.py" is in the same folder as "bar.xls", and I only need a button in "bar.xls" to launch "foo.py".
Thanks.
Okay, I found a roundabout way, so I'm gonna share it with y'all.
MacroName
=LAUNCH("cmd",1)
=SEND.KEYS("foo.py~";TRUE)
=SEND.KEYS("exit~")
=RETURN()
It opens a cmd instance, show it for a split second (can't use SEND.KEYS without it being the active app), writes the name of the python script and presses enter, before quitting.
I would like if it didn't need to show the cmd window, but it works for now. Perhaps there'll be another way, but if anyone else wanna do what I did, it does work.
You probably need to get the book out - Excel 4 came with one book called the Function Reference which listed all the commands available.
Commands that we used back in the day were:
EXEC: starts another program
EXECUTE: runs commands in another program called by Initiate
INITIATE: sets a channel to a program
SEND.KEYS: sends keystrokes to a program (we used to send data to a slow server this way...)
Not sure what will be on the web for Excel macro 4, it was retired as vba came out and Excel moved over...
I still use my copy of the book, but it would be worth finding, although the help should list the commands as well. I just used the book as I had macros running...

Keyboard.press only working once with threads python

I've been working on a small script in python to press some keys when I press one specific (I created a macro).
All works fine since I try to use it in any other application like for example a videogame or other programs.
The problem is that it only executes once, and then the only way it executes again is chainging to my IDE tab (Visual studio code) and returning back to the application, and this is quite annoying.
Any idea about it and how to solve that window problem? Here is the code
def macros():
t=threading.Timer(0.1,macros)
t.start()
if keyboard.is_pressed('k'):
keyboard.press('e')
keyboard.press('q')
print("active")

Python - Trying to find if a sikuli script is running at the moment

So I have a sikuli script running which monitors and executes a said action every 10 minutes continuously. However for various reasons sometimes the run is interrupted and there is no way to alert if the script stops running.
So I tried running a python script, which would monitor the window of the sikuli IDE. When the script runs, the window is no longer visible. So if the window is visible again the python script would run a batch file which would trigger the alert required. The following is the script which I made from seeing other examples here in this site:
WindowName = "SikulixIDE 1.1.3 - C:\\Users\\TestUser\\Downloads\\testing2.sikuli"
while True:
try:
if win32ui.FindWindow(None, WindowName):
subprocess.call([r'C:\Users\TestUser\Documents\notification.bat'])
break
except win32ui.error:
#print("its not running!")
continue
The problem I am running into with the above code is, even when the sikuli script is running and the IDE window (one with the WindowName) is not actually visible to me, it still finds it and goes into the if block. I am not sure what's going wrong here, if the window is not visible in Task Manager, FindWindow shouldn't be able to find it, correct?
I would say you are trying to do this in very complicated way.
I will strongly suggest to look at '-r' option of sikuli and to write only one sikuli script that will handle exceptions by itself and will not need monitoring.
In this case if you really need additional monitoring for the script it will be easier since you can look for process with specific command line(instead of visible window)

Python shell issue when using Data Nitro

I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).

speed python script on command line vs launched in IDLE shell

I noticed several times that a simple python script (with some straightforward algebraic computation in a loop) runs considerably faster (up to a factor 6) when launched on a shell command line as compared to a run via IDLE's shell. There is nothing fancy going on in the script. I only print a loop variable to visually follow progress in the loop.
Surely just this print statement cannot be the reason for the speed loss in IDLE, or can it ?
Can someone explain me why this is ?
The Tkinter text widget is slow, and IDLE uses this to print text to screen.
See this question.

Categories

Resources