How to unregister your registered python script function?

How to unregister your registered python script function?

hyrro_velasquez
Advocate Advocate
2,653 Views
17 Replies
Message 1 of 18

How to unregister your registered python script function?

hyrro_velasquez
Advocate
Advocate

Good day Ma'am/Sir,

 

Those any one knows how can I unregistered my registered function on my python script, for example

I defined a function called "def Clamp01" then registered it on plant3D using "PLANTREGISTERCUSTOMSCRIPT", then I need to change something on that "def Clamp01", when I change it then registered it again what happen was the changes is not reflected, but when I change to "def Clamp02" the changes is reflected. it seems that the already registered function cannot be change again unless you will rename the function and register it again, but on our side we must retain the original function name(for some important reason). if anyone does have any information please can you share it, I'll really appreciate it.

Also I attached a screenshot of my sample script. thanks again in advance.

 

0 Likes
2,654 Views
17 Replies
Replies (17)
Message 2 of 18

rajendra.prajapat
Advisor
Advisor

@hyrro_velasquez  Hi , To see the changes you need to restart the plant 3d and then register again.

If my reply was helpful, please give a "Kudo" or click the "Accept as Solution" button below (or both).
Message 3 of 18

hyrro_velasquez
Advocate
Advocate

Sir @rajendra.prajapat 

thanks for your reply,

 

I'm fully aware of that process,

1. Plantregistercustomscript - (register your PY)

2. Restart Plant3D

3. (command "arx" "l" "Pnp3dacpadapter")

4. (testacpscript "Clamp01")  - (To Test your script)

 

The problem though is once I use a def name like Clamp01 on my script then register it by doing the process above to test if it  was ok. when it is ok, I want to add some details like bolts on the graphics so what I do is make some changes on the script, then without changing the def name I'll register it again by doing the process above, the changes I made on the python script was not reflected on the graphics when I test it, unless I'll change the def name like Clamp02 then register again using the process above. but in our case we have to retain the original name which is Clamp01, that's why I'm thinking may be there is also a way to unregister the python script that is already registered so that I can re-use the original def name of every python script I'm creating.

 

hope my questions make sense,

Again thank you so much.

Message 4 of 18

rajendra.prajapat
Advisor
Advisor

@hyrro_velasquez  please back up and then delete the python script & its related file  from the custom folder. Paste the latest script inside the custom folder and register it inside the new drawing.

If my reply was helpful, please give a "Kudo" or click the "Accept as Solution" button below (or both).
0 Likes
Message 5 of 18

George.Endrulat
Advisor
Advisor

I believe you can use a versioning inside the name i.e. Clamp01V01, proceed as normal, make a change to the script rename to Clamp01V02 and reregister without restarting.  Can anyone confirm this?

George

If my post offers a solution, please click "Accept as Solution"
Message 6 of 18

hyrro_velasquez
Advocate
Advocate

Sir @George.Endrulat 

 

good day,

 

thanks for your reply,

For now what we basically do was versioning which is also your suggestion, but we really need to retain the original def name to minimize the modification on the API side. but again thank you for your reply.

0 Likes
Message 7 of 18

matt.worland
Collaborator
Collaborator

Have you tried deleting the pyc files that are created after registering the scripts?

You should be able to delete the Clamp01.pyc file from your custom scripts folder, then re-register your scripts and it will recreate the Clamp01.pyc with your latest code. The pyc files were in the CusomScripts folder with the py files, however in 2022, they were moved into the _pycache_ folder.

If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
Message 8 of 18

hyrro_velasquez
Advocate
Advocate

@matt.worland 

 

Yes Sir I already done that, deleted PYC files then register again the py containing the original def name, but unfortunately it does not do the trick. thanks for your reply.

0 Likes
Message 9 of 18

matt.worland
Collaborator
Collaborator

Usually, we just clear the PYC files and purge the current drawing of any instances. You can also just start a new drawing and the new code should run, of course, after all the register scripts steps have been followed.

Is this all in the same drawing? P3D inserts a block of the item created by the script, have you purged that block out of the drawing?

If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
0 Likes
Message 10 of 18

nghiabt04
Advocate
Advocate

Hi @hyrro_velasquez ,

I think the problem is from block definition when you use testscript to test your python scripts, the test model is in block type and it writes a block definition to block database. When your change the script but do not change the name, it makes the same block definition on the block database, so the changes are not reflected.

The solution is does not save your drawing file when restarting P3D to test the new script. It's mean:

 - Write your script -> register and test your script -> change something in your script -> restart P3D without saving -> register and test your script again

Hope it help!

 

0 Likes
Message 11 of 18

hyrro_velasquez
Advocate
Advocate

Sir @matt.worland 

Good day,

thank you for your reply, I'm sorry but I'm not aware of purge, is that a command inside P3D?,

If its ok can please give me some sample commands.

again thank you so much.

0 Likes
Message 12 of 18

matt.worland
Collaborator
Collaborator

Hello @hyrro_velasquez ,

I use the standard AutoCAD PURGE command. Then under the Blocks section you will see a list of Plant3DCatalogItem_... blocks These are the blocks the P3D inserts into a drawing representing your custom part from Python. If you have erased all of the graphical instances of your python tests, the block should show in the Purgeable Items section of the dialog as shown below.

mattworland_0-1638203974215.png

 

If you do not see your def Clamp01 in the list, look in the Find Non-Purgeable Items list, then use the tools in there to locate those items and erase them from the drawing area to be purged.

 

After the blocks are purged from the drawing, AutoCAD should use your modified python code.

If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
0 Likes
Message 13 of 18

George.Endrulat
Advisor
Advisor

A lisp idea then:

A script to:

  1. Query the user for the script / block "name" 
  2. delete the "name".pyc
  3. purge the "name" block
  4. PLANTREGISTERCUSTOMSCRIPT "name"
  5. testacpscript [parameters] "name"

Am I missing anything?  I feel that we could get this down to a simple lisp script that would greatly benefit the whole community.

 

George

If my post offers a solution, please click "Accept as Solution"
0 Likes
Message 14 of 18

matt.worland
Collaborator
Collaborator

Hi @George.Endrulat 

I believe you could take care of part of this with AutoLISP. I've attached a quick writeup that could be expanded and more error checking added.

(defun c:PurgePythonScript (/ scriptFileNameandPath scriptFileName pycFile) 
  ;|
    Written by mworland@ecedesign.com
    11/29/221
    provided as is for educational purposes
    with no warrenty to its usefulness or correctness
    not responsible for any results of this code.
  |;
  (setq scriptFileNameandPath (getfiled "Select a Script File to Purge" "c:/" "py" 0))
  (princ scriptFileNameandPath)
  (setq scriptFileName (vl-filename-base scriptFileNameandPath))
  (princ scriptFileName)
  (command "-purge" "Blocks" (strcat "*" scriptFileName "*") "No")

  (if (>= (atof (getvar 'AcadVer)) 24.1) 
    (setq pycFile (strcat (vl-filename-directory scriptFileNameandPath)  "/__pycache__/" scriptFileName ".cpython-37.pyc"))
    (setq pycFile (strcat (vl-filename-directory scriptFileNameandPath) "/" scriptFileName ".pyc"))
  )
  (princ pycFile)
  (if (findfile pycFile) 
    (progn 
      (if (not (vl-file-delete pycFile)) 
        (princ (strcat "Failed to delete your pyc file. Please delete the file:" fileToDelete "manually and try again."))
        (princ (strcat pycFile " has been deleted!"))
      )
    )
  )
  (command "PLANTREGISTERCUSTOMSCRIPTS")
  (princ "Registered the custom scripts.")
  (command "arx" "L" "Pnp3dacpadapter")
  (princ)
  (testacpscript scriptFileName)
)

 

The main problem is that P3D only allows you to register custom scripts once per session of P3D. I would hope there is a workaround or hack for this, but I haven't found it yet.

 

If you have registered a custom script once in a drawing and you modify the python script, you must restart P3D so it will re-register the changes when you run the above code.

 

Let me know if you have any questions,

matt 

If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
0 Likes
Message 15 of 18

George.Endrulat
Advisor
Advisor

@matt.worland I guess the question is then does it let you register multiple custom scripts at different times during the same session?  If that's the case then we can apply versioning to the file name / script, no?  We'd have to consider the pollution of all of the registered scripts though.

 

We need to figure out where the registering of the scripts takes place.

Possibilities:

CustomScripts\variants.map

CustomScripts\variants.xml

CustomScripts\ScriptGroup.xml

 

Is there a possible database location of registered scripts?  It seems like it has to be either plant 3d scoped or project scoped, figuring that out might help.

 

I'd like for us to keep going on this, to make some progress, and to generate some tooling.

 

George

If my post offers a solution, please click "Accept as Solution"
0 Likes
Message 16 of 18

matt.worland
Collaborator
Collaborator
I believe it's 'in memory' of the Plant3D session, and not in the current project or drawing. Even if you switch drawings it uses the first registered version of the python script, not any modifications. The only solution I've found is to restart the entire application and re-run the register command. Very frustrating, but doable.

I'm not a fan of adding a version number to a working filename and would avoid that.

I haven't looked through the API, but wouldn't think there is an option in there either.

In the end, the workflow is slow but doable when testing.
1. Write your code.
2. Start a fresh session of P3D.
3. Open an empty testing drawing from a project.
4. Register your scripts.
5. Test your new script.
6. If it works move on, if not Close P3D without saving and adjust your python and start back at step 1.

Rarely have I had to delete the PYC files since registering the scripts replaces them and the associated XML files. You just have to make sure you don't have an instance of that block already in the drawing you are testing. Any changes you make to the script, you restart P3D.

Hopefully someone else has more experience they can share, but in our testing restarting P3D is a must.
If my reply was helpful, please give a "Thumbs Up" or "Accept as Solution"
0 Likes
Message 17 of 18

George.Endrulat
Advisor
Advisor

@matt.worland Thank you for the writeup.  It seems that this isn't a solvable problem without script versioning, which no one seems to like and probably would have issues all it's own.  This is a shame, like how database communication was stripped out of python scripts.

George

If my post offers a solution, please click "Accept as Solution"
0 Likes
Message 18 of 18

baleti3266
Advocate
Advocate

reinstall autocad each time you sneeze

0 Likes