The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Python
 How to connect external python with running Origin
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Edifier

2 Posts

Posted - 12/21/2021 :  10:53:48 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin):
OriginPro 2021 (64-bit)
9.8.0.200
Operating System:Win10 64bit 19044.1415

I use the example code on the official website to realize the connection between external python and Origin. But the problem is that the connection between external python and Origin can only be realized every time the Origin is reopened. It always takes a lot of time to open Origin each time.
I know that using the Originpro package can implement data processing and drawing in the background, but I need to manually adjust the detail styles every time I draw.
I want to use external python for some reasons. I know that the built-in python can easily connect.
So I would like to ask if there is a way, or with the help of OriginPro, to realize the connection between external python and Origin.

YimingChen

1618 Posts

Posted - 12/21/2021 :  1:50:05 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Please use the code below to link to an existing Origin instance. Make sure your originpro module needs to be 1.0.8 or later.


import originpro as op
op.attach()


James
Go to Top of Page

Edifier

2 Posts

Posted - 12/21/2021 :  9:33:05 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks, it works as the code below


if op.oext:
op.set_show(True)
# YOUR CODE HERE
origin_output()
op.attach()


set.show() must be added and set the ture value.

and is this attach() function just used for the running python runned by external python? how about the origin runned by click the link on the desktop? Is there some function to realise this?

quote:
Originally posted by YimingChen

Please use the code below to link to an existing Origin instance. Make sure your originpro module needs to be 1.0.8 or later.


import originpro as op
op.attach()


James

Go to Top of Page

YimingChen

1618 Posts

Posted - 12/22/2021 :  1:54:23 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Yes, it can connect to the Origin launched from desktop.
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/04/2022 :  11:40:07 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Following on from this question, please would you advise me if there is a recommended way to accomplish the below task?

1. Check whether there is a running instance of Origin.
2. If so, connect to that instance, so that plots will be generated within in it.
3. If not, start a new instance of Origin.

The context is that I am also trying to automate Origin using the external Python connection.
Go to Top of Page

YimingChen

1618 Posts

Posted - 03/07/2022 :  09:35:26 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
This script is doing exactly what you expect:
op.attach()

You can also show the Origin window:
op.set_show(True)

or release Origin:
op.exit()


James

Edited by - YimingChen on 03/07/2022 09:35:35 AM
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/08/2022 :  7:03:08 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi James,

Thank you for your reply however I am still having some trouble with this.

I adapted the example Origin code from the website, and based on your answer came up with the code shown below.

However, this fails in that if I run the code without an instance of Origin open, it completes successfully (Process finished with exit code 0) but no Origin project is shown.

I have tried various tweaks but none of these solved the issue. Please would you advise me of what changes are required in order for this to work as described in my previous post?

Thanks and regards,

Conrad

import originpro as op
import sys

def origin_shutdown_exception_hook(exctype, value, traceback):
'''Ensures Origin gets shut down if an uncaught exception'''
op.exit()
sys.__excepthook__(exctype, value, traceback)

if op and op.oext:
sys.excepthook = origin_shutdown_exception_hook

# Try to attach to existing Origin instance
try:
op.attach()

# If this fails, it means there is no existing Origin instance. So create a new one, and make it visible.
except:
if op.oext:
op.set_show(True)
op.new()

# Add a test folder to make sure it is working
op.po.RootFolder.Folders.Add("Test")
Go to Top of Page

YimingChen

1618 Posts

Posted - 03/09/2022 :  11:26:42 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
op.attach() will always execute either you attach to an existing Origin instance or create a new one. So you can't use Try...Exception... here. Maybe you can use op.get_show() to check if it connects to an existing instance (return true), or create a new one in the background (return false). See if it works.

James

Edited by - YimingChen on 03/09/2022 11:27:04 AM
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/09/2022 :  7:24:33 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you for this suggestion, based on this I tried the following code:

op.attach()

if not op.get_show():
    if op.oext:
        op.set_show(True)
    op.new()

However this also has a problem, because if I run this while an Origin instance is open, it closes it and opens a new instance.

Please would you advise me if I did something wrong, or if there is another way to accomplish this?

Edited by - ChemistryGuy on 03/09/2022 7:27:18 PM
Go to Top of Page

YimingChen

1618 Posts

Posted - 03/10/2022 :  10:19:50 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
If you have an Origin instance open, op.attach() will connect to that instance. Then the code in the block if not op.get_show(): will not execute. This is what I get when running the code. If I change it to if op.get_show():, then a new project is created in that Origin instance. Is this what you expect?

James
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/10/2022 :  5:13:11 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks again for your suggestion James, but this did not accomplish my goal. When I make your change and run the code with no Origin instance open, it does not open a new instance.

I was hoping for this block of code to function the same way as ApplicationSI in the automation server: https://www.originlab.com/doc/en/COM/Classes/ApplicationSI

That is, open a new instance of Origin if none is open. But if an instance of Origin is already open, generate the Test folder inside it.
Go to Top of Page

YimingChen

1618 Posts

Posted - 03/11/2022 :  12:36:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
OK. Can you try the code below with and without an Origin instance open, see if it does what you expect?

import originpro as op
op.attach()
op.set_show(True)
op.po.RootFolder.Folders.Add("Test")


James
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/12/2022 :  01:10:21 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I gave this a try, and it successfully attaches if there is an Origin instance open. But if I run it without an Origin instance open, it does not open a new instance.

However I believe that I came up with a solution:

op.attach()
if not op.get_show():
    op.exit()
    op.new()
    op.set_show(True)
op.po.RootFolder.Folders.Add("Test")


I realise this is inefficient, but it was the only way I could get it to behave as required. So if you have a better way please do let me know, but for now I believe this achieves the goal.
Go to Top of Page

minimax

351 Posts

Posted - 03/13/2022 :  11:45:00 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi ChemistryGuy,

Would you mind to check if your originpro package's version is 1.1.0?

If not, would you mind to upgrade it to latest first and then re-try Yiming's script?

And what is your Origin version?

PS, you can use following method in originpro package to create a folder
#create a subfolder "test" in same level as current active folder
op.pe.mkdir("../test")


https://www.originlab.com/python/doc/originpro/namespaceoriginpro_1_1pe.html
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/14/2022 :  7:51:46 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Minimax,

I can confirm my OriginPro version is 1.1.0.

I tested Yiming's code again, and the same thing happened: When I ran it with an Origin instance open, it worked as expected. When I ran it without Origin open, it executed without error, but did not create a new Origin instance.

In case it helps, my other details are:
Origin Version: OriginPro 2022 (64-bit) Beta5
Operating System: Windows 11

Also, thank you for sharing that code and documentation, this was very useful.

Edited by - ChemistryGuy on 03/14/2022 7:52:38 PM
Go to Top of Page

minimax

351 Posts

Posted - 03/14/2022 :  9:20:37 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi ChemistryGuy,

How did you execute the py code?

like in Windows' Command Prompt? IDLE? PyCharm? or what else?
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/14/2022 :  10:44:27 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I am running the code from PyCharm.

In case it makes a difference my Python version is 3.9

Edited by - ChemistryGuy on 03/14/2022 10:46:13 PM
Go to Top of Page

minimax

351 Posts

Posted - 03/14/2022 :  11:26:20 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
OK.

We can reproduce the problem with PyCharm.

We will check soon and update you later.
Go to Top of Page

minimax

351 Posts

Posted - 03/15/2022 :  04:47:50 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi

Would you mind to try if following script works in PyCharm?
import time
import originpro as op
op.attach()
time.sleep(1)
op.set_show()
wks=op.new_sheet()
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/16/2022 :  03:22:55 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
This code worked the first time I ran it.
But when I ran it twice in a row, Origin crashed.
I submitted the crash report, mentioning that I ran the code provided on the forum.

Also, I am happy to keep testing code if it helps you fine tune the Python library. But if you have other priorities, then the solution that I came up with earlier in this thread works for my needs :)
Go to Top of Page

minimax

351 Posts

Posted - 03/16/2022 :  06:22:05 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi ChemistryGuy,

It is appreciated you can help to verify the problem.

We made some changes on the package and would you mind to try following?

1.open the originpro package files folder in Windows Explorer, like following



2. replace the 2 py file with attached modified ones. (it is good to backup the original first)
https://my.originlab.com/ftp/forum_and_kbase/Images/newpy.zip

3. Then retry Yiming's script. Does it work?
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 03/18/2022 :  8:12:26 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Sorry for my slow reply, I tested these new Python files and Yiming's code (the version with time.sleep(1)) now works.

However, after I run the code, I cannot close Origin because I get the following error: "Origin cannot be closed because it is being controlled by another application".

I do not have this problem with the code that I came up with. So my feedback is that this new Origin library works but it would be great if Origin could be closed afterwards, without using Task Manager.
Go to Top of Page

minimax

351 Posts

Posted - 03/20/2022 :  11:36:05 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi ChemistryGuy,

Never mind, it is fine.

The time.sleep(1) script should be not necessary with the new py file.

As to the can-not-close issue, would you mind to add "op.detach()" as the last sentence and then check if Origin can be closed?
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 04/06/2022 :  9:38:15 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I tried the code without "time.sleep(1)", and adding "op.detach()" as the last line. It worked perfectly, thank you for fixing it so quickly! :)
Go to Top of Page

minimax

351 Posts

Posted - 04/07/2022 :  9:28:15 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi ChemistryGuy,

Thanks for the verification. We will check how to improve it in the end.
Go to Top of Page

AmandaLu

439 Posts

Posted - 05/19/2022 :  05:08:32 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi ChemistryGuy,

Origin 2022b is available!

The issue you reported in this post,
py attach failed to work in third party IDE like PyCharm (ORG-24938)
has been fixed in this version.

Please download Origin 2022b from the link below, install and try it.
http://www.originlab.com/demodownload.aspx

We are looking for your feedback! Any suggestion will be appreciated!

Thanks,
Amanda
OriginLab Technical Service
Go to Top of Page

ChemistryGuy

Australia
49 Posts

Posted - 06/07/2022 :  7:41:31 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Amanda,

Thank you for letting me know about this new version. I updated to Origin 2022b and also updated the library to OriginPro 1.1.1.

I then tested the code mentioned above, both for attaching to an existing project and opening a new project. My experience was that both cases now work perfectly!

So thank you for solving this issue and for letting me know! :)

Edited by - ChemistryGuy on 06/07/2022 7:42:10 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000