matteoluigi Posted October 17 Share Posted October 17 I just am coding sth to change PIO and Symbolnames. Objectnames in VW-Files are supposed to be unique. So, if the Name already exists, the script is supposed to crash. To prevent that, I write a condition-code to check, wether an object of that name already exists. Problem: all my tests produce endless loops and I don't understand why. For example: try that code in an empty file. Insert any name. The loop DOESN'T end. Every object I try to address with "vs.GetObject" turns out to "exist" I suppose a "while vs.GetObject(TESTNAME) is not None:"-loop to finish when no object in the file turns out to have the TESTNAME, and, else the loop get's finished. WHAT'S WRONG THERE? TESTNAME = 'START' while vs.GetObject(TESTNAME) is not None: TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME) if vs.GetObject(TESTNAME) is not None: vs.AlrtDialog('Object with Name '+TESTNAME+'already exists') else: break or TESTNAME = 'START' while True: TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME) TESTNAMEh = vs.GetObject(TESTNAME) TESTNAMETEST = vs.GetName(TESTNAMEh) if TESTNAMETEST =='': vs.AlrtDialog('Object with Name '+TESTNAME+'already exists') else: break NAMETESTv2024.zip Quote Link to comment
Marionette Maven Marissa Farrell Posted October 17 Marionette Maven Share Posted October 17 GIve this a shot: TESTNAME = 'START' while vs.GetObject(TESTNAME) != vs.Handle(0): TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME) if vs.GetObject(TESTNAME) != vs.Handle(0): vs.AlrtDialog('Object with Name '+TESTNAME+'already exists') else: break 1 Quote Link to comment
matteoluigi Posted October 17 Author Share Posted October 17 doesn't work. This one instead works: TESTNAME = 'START' while True: TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME) if vs.GetObject(TESTNAME) != vs.Handle(0): vs.AlrtDialog('Object with Name '+TESTNAME+' already exists') if vs.GetObject(TESTNAME) == vs.Handle(0): break Quote Link to comment
Letti R Posted October 17 Share Posted October 17 Hello, Marisas code works fine. Its just that the while loop is not running at all if you dont have an object with the name "START" to begin with, wich may not be what you want. One could say that the two loops you posted in the beginning are two different kind of while loops. The first one is a normal while loop that runs as long as a condition is true, but if the condition is false right from the beginning, than the body of loop never gets executed. The second loop behaves more like a "do-while" loop. Its mostly the same as the while loop, with the difference that its body is executed at least one time. However in your last example you dont realy need the second if statement. Something like this: TESTNAME = 'START' while True: TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME) if vs.GetObject(TESTNAME) != vs.Handle(0): vs.AlrtDialog('Object with Name '+TESTNAME+' already exists') else: break or this: TESTNAME = 'START' while True: TESTNAME = vs.StrDialog('TESTNAME: ',TESTNAME) if vs.GetObject(TESTNAME) != vs.Handle(0): vs.AlrtDialog('Object with Name '+TESTNAME+' already exists') continue break also works. Regards, Letti Quote Link to comment
twk Posted October 17 Share Posted October 17 my code for checking if a named object exists is below: test_name = vs.StrDialog("Test Name: ", "") if vs.GetObject(test_name) not in [0, None, vs.Handle(0)]: # do stuff here pass Quote Link to comment
matteoluigi Posted October 18 Author Share Posted October 18 yeah, now it works, don't know what I made wrong yesterday... funny, that neither the Script Editor nor the function reference on the developer page know vs.Handle(). Seems to be not a very importan function, however it exists. what does it do and how does it work? Why is there a 0 in the brackets? Quote Link to comment
Marionette Maven Marissa Farrell Posted October 18 Marionette Maven Share Posted October 18 vs.Handle(0) just indicates an empty handle, since vs.GetObject() returns an object handle, it's comparing the same type. We use it in Marionette, otherwise I wouldn't know about it. 1 Quote Link to comment
matteoluigi Posted October 18 Author Share Posted October 18 thank you, maybe sb should mention it in the function reference on developer.vectorworks.net? 2 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.