Replacing name

Hi!
Im having trouble with a script. what im trying to do is to take in huge oil platforms, and merge them based on naming of parts.
and afterwards rename the final part to a name predefined in the script,

filteredOccurrences = scene.getFilteredOccurrences(“Property("Name").Matches("^.BOX 3.$")”)
scene.select(filteredOccurrences)
selection = scene.getSelectedOccurrences()
scene.mergeParts(filteredOccurrences)
setProperty(occurrence, “BOX”, occurrence_name)

but i cant get the last line here right (Line 9)…


i get this message
Do anyone have an idea?

9350576--1307942--1284020-d9ecd131dfa7a16a1a2306ca3ccd9c26.png
9350576--1307945--1284044-e6d6cb2d610b498eab63ebce77e90399.png

Hi,

About your error, it’s because you need to call **core.**setProperty(occurrence, “BOX”, occurrence_name)

To find all the occurences by name, i recommand using scene.findByProperty(“Name”, “YouRegex”)

So the behavior you described could be:

occurrencesFilteredByRegex = scene.findByProperty("Name", "^.*BOX 3.*$")

occurrencesMerged = scene.mergeParts(occurrencesFilteredByRegex)

myCustomName = "My Custom Name"
core.setProperty(occurrencesMerged[0], "Name", myCustomName)

I hope it will help.
–Selim

Awesome! this was super helpful, and now everything works as intended.
Thanks alot!

I encountered a new problem now :frowning:
whenever the models does not contain that word im searching for, it just merges the entire root. there should probably be a if true statement here, but im a total noob in Pyton:eyes:
can you help with this as well?:roll_eyes:

Probably what is happening here is that your “findByProperty” is returning an empty array (nothing is found). And because of that, when this empty array is passed to “mergeParts” it will be interpreted as if you want to merge the root node.

So maybe a solution could be to check the size of your array and check that findProperty found at least one occurrence with that name before merging.

It could look like:

occurrencesFilteredByRegex = scene.findByProperty("Name", "^.*BOX 3.*$")

if len(occurrencesFilteredByRegex) > 0:
    occurrencesMerged = scene.mergeParts(occurrencesFilteredByRegex)

    myCustomName = "MyCustomName"
    core.setProperty(occurrencesMerged[0], "Name", myCustomName)

This is awesome!
This will save us A LOT of time

BTW for those that want to copy this line 4 is missing an r in (occurrencesFilteredByRegex)

Thanks alot!

Thanks. Good catch! I edited the sample code.

Have a good day.