htranbos Posted February 16 Share Posted February 16 I am making a simple python script to calculate the number of items added to a list. I have multiple rectangles and nurb curves in the scene. My script is below. According to VS:Search Criteria, multiple search criteria are created using the & operator to chain individual search criteria terms. However, I got the unsupported & operator error. Does anyone know how to perform multiple search criteria with Python script? Thanks fam! myList = [] def sel(h): myList.append(h) vs.ForEachObject(sel, "T=RECT" & "T=NURBSCURVE") vs.AlrtDialog('Number of selected objects: {}'.format(len(myList))) Quote Link to comment
Pat Stanford Posted February 16 Share Posted February 16 I think your criteria are wrong. I am a Vectorscript guy not Python, but I think you need parentheses and not quotes. Open the VS editor and use the Criteria builder there to generate the criteria you need and then paste it into your script. I always fight to get the parentheses correct, but from memory I think you need: vs.ForEachObject(sel, ((T=RECT) & (T=NURBSCURVE))) Quote Link to comment
htranbos Posted February 16 Author Share Posted February 16 Hi Pat, I tried your suggestion and got the following error message. I didn't know about the criteria builder inside VS editor. This is awesome. Thank you!! Quote Link to comment
htranbos Posted February 16 Author Share Posted February 16 It works if I break down the code like below: myList = [] def sel(h): myList.append(h) vs.ForEachObject(sel, "T=RECT") vs.ForEachObject(sel, "T=NURBSCURVE") vs.AlrtDialog('Number of selected objects: {}'.format(len(myList))) Quote Link to comment
Pat Stanford Posted February 16 Share Posted February 16 The criteria builder gives: (((T=RECT) & (T=NURBSCURVE))) But Looking at the ForEachObject sample code on the developer site it looks like you may need to use quotes in Python. https://developer.vectorworks.net/index.php?title=VS:ForEachObject ((("T=RECT") & ("T=NURBSCURVE"))) Good luck. Please report back when you figure it out. Quote Link to comment
JBenghiat Posted February 16 Share Posted February 16 1 hour ago, Pat Stanford said: But Looking at the ForEachObject sample code on the developer site it looks like you may need to use quotes in Python. In Python, the entire criteria needs to be a string 1 Quote Link to comment
Pat Stanford Posted February 16 Share Posted February 16 So like this? "(((T=RECT) & (T=NURBSCURVE)))" Or does that have one to many sets of parens? Or do the individual criteria also need to be quoted separately? Quote Link to comment
JBenghiat Posted February 16 Share Posted February 16 I feel like with criteria, you can never have too man parentheses, but at least the outer-most set is redundant. You don’t need more quotes than with vs, so just record and field names, object names, etc. Should have single quotes. Quote Link to comment
htranbos Posted February 16 Author Share Posted February 16 I am able to make the script run without any error message.... but the output of this code should be 1 not 0. I don't know what I did wrong. myList = [] def sel(h): myList.append(h) vs.ForEachObject(sel, "(T='NURBSCURVE') & (VSEL=TRUE)") vs.AlrtDialog('Number of selected objects: {}'.format(len(myList))) Quote Link to comment
twk Posted February 17 Share Posted February 17 (edited) 1) For your first post: The second parameter for the ForEachObject must be a criteria string. so: vs.ForEachObject(sel, "T=RECT" & "T=NURBSCURVE") should actually be written as: vs.ForEachObject(sel, '"T=RECT" & "T=NURBSCURVE"') 2) For your most recent post: why are you using VSEL and SEL? shouldn't just SEL work? tips: You could place criteria strings in variables: crit_string_1 = '"T=RECT" & "T=NURBSCURVE"' so it would be: vs.ForEachObject(sel, crit_string_1) and also leveraging pythons f-string capabilities: type_1 = 'RECT' type_2 = 'NURBSCURVE' crit_string_1 = f'"T={type_1}" & "T={type_2}"' Edited February 17 by twk 2 Quote Link to comment
htranbos Posted February 17 Author Share Posted February 17 Thanks @twk. I will try SEL and see if it the result is correct. Quote Link to comment
htranbos Posted February 18 Author Share Posted February 18 @twk I tried your suggestion placing criteria strings in variables. Each method works and produced no error. However, the output is still 0 instead of 1. For this test, I only have 1 Nurbs curve object in the scene and it is selected. The output of this script should be 1 instead of 0. I don't know why I can't get it to produce the correct result. myList = [] def sel(h): myList.append(h) crit_string_1 = '"SEL=TRUE" & "T=NURBSCURVE"' vs.ForEachObject(sel, crit_string_1) vs.AlrtDialog('Number of selected objects: {}'.format(len(myList))) Quote Link to comment
htranbos Posted February 18 Author Share Posted February 18 Hello all, I finally made it to work. In order for the above code to produce the correct result, you need to put each search criteria inside a () like below. crit_string_1 = "(SEL=TRUE) & (T=NURBSCURVE)" 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.