Help converting a label system

I have a script that uses
a list to check against,

it grabs the last 2 pieces of a material name with “hitID = int(hitMat[-2:])”

like Sensor_01

it would turn into

01,

this works great, until I get to 10,

can some one help me change the system so that it does not break if I get to 10?
I would just use lables like A B C D etc, but the script is set to do math using the label…

is there a easy way to do item = item -“Sensor_”

hitMat = hitPoly.material_name
# check the material name if it contains a string value
if ‘Sensor_’ in hitMat:
# get the suffix of the material name
hitID = int(hitMat[-2:])
# get the material’s pair from the pairs list
pair = pairs[hitID-1]

You’d probably want to use regex. I’ve not much experience with it but there are many tools on the Internet that can help you. It’s available in the python standards library

thank you, I am looking at all the /*[] 's and going crosseyed :slight_smile:

I will take some time and try and master it :slight_smile:

Thank you kind sir :slight_smile:

how about this???

if hostID => 10
hitID = int(hitMat[-3:])

The reason it’s breaking when you get to 10 is because of hitMat[-2:]. If you change this to -3 and start using the naming convention Sensor_001 you should be able to handle more objects. Or [-4:] and use Sensor_0001 etc…

I tried that about 2 hrs ago,
Index out of range

not that I know why,

I tried

if ‘Sensor_’ in hitMat:
if hostID=>10:
hitID = int(hitMat[-3:])
if hostID<10:
# get the suffix of the material name
hitID = int(hitMat[-2:])

http://www.pasteall.org/40196/python
here is the script in full @ the moment it generates a null without failing

Check this out:
http://txt2re.com/index-python.php3?s=Cube.001&5


import re


txt='Cube0001'


def get_suffix_int(string):
    regex = re.compile('.*?(\\d+)',re.IGNORECASE|re.DOTALL)
    search = regex.search(string)
    if not search:
        return
    
    int_a = search.group(1)
    return int_a


print("({})".format(get_suffix_int(txt)))

JOY!!! thank you kind sir

import re
txt=‘Sensor_005’re1=’.*?’ # Non-greedy match on fillerre2=’(005)’ # Integer Number 1rg = re.compile(re1+re2,re.IGNORECASE|re.DOTALL)m = rg.search(txt)if m: int1=m.group(1) print “(”+int1+")"+"
"
:slight_smile:

now to figure out how to turn re-write it to edit the material list and output a “index”

It’s a function and an import statement. Simply copying the function and import then passing a name to the function will return the integer as a string.

Another option you have is to String.split("_") your names.


str = "Sensor_01"
str2 = "Sensor_100"

print(str.split("_")[1])
print(str.split("_")[1])

This assumes that you don’t use “_” in the same of your sensors.

Thank you, that makes this much easier,

hitID = int(hitMat.split("_")[1])

or

dataset = [‘Sensor_01’, ‘Sensor_02’, ‘Sensor_03’]

for i in range(len(dataset)): dataset[i] = dataset[i].replace(‘Sensor_’, ‘’)
print data-set
:slight_smile:

Ok so it is now working :slight_smile:

Now I just need to work on firing the script if the property “FaceIndex_X” changes from “empty” instead of using hostID,

then I will have it pass the property “FaceProperty_X”

Edit:Sorry about double posting