Jump to content

Multiple search criteria using & operator


Recommended Posts

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)))

 

Screenshot 2024-02-16 085250.png

Link to comment

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)))

 

Link to comment

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)))

 

Screenshot 2024-02-16 131022.png

Link to comment

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 by twk
  • Like 2
Link to comment

@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)))

 

 

Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...