Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Replacing name

Discussion in 'Pixyz' started by Ovh_PIXYZ, Sep 21, 2023.

  1. Ovh_PIXYZ

    Ovh_PIXYZ

    Joined:
    Sep 20, 2023
    Posts:
    4
    Hi!
    I`m having trouble with a script.
    what i`m 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).. Skjermbilde 1.PNG

    i get this message Skjermbilde2.PNG

    Do anyone have an idea?
     

    Attached Files:

  2. Selim_B

    Selim_B

    Unity Technologies

    Joined:
    Sep 12, 2022
    Posts:
    6
    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:

    Code (Python):
    1. occurrencesFilteredByRegex = scene.findByProperty("Name", "^.*BOX 3.*$")
    2.  
    3. occurrencesMerged = scene.mergeParts(occurrencesFilteredByRegex)
    4.  
    5. myCustomName = "My Custom Name"
    6. core.setProperty(occurrencesMerged[0], "Name", myCustomName)
    I hope it will help.
    --Selim
     
    AxelJacquet likes this.
  3. Ovh_PIXYZ

    Ovh_PIXYZ

    Joined:
    Sep 20, 2023
    Posts:
    4
    Awesome! this was super helpful, and now everything works as intended.
    Thanks alot!
     
    Selim_B likes this.
  4. Ovh_PIXYZ

    Ovh_PIXYZ

    Joined:
    Sep 20, 2023
    Posts:
    4
    I encountered a new problem now :(
    whenever the models does not contain that word i`m searching for, it just merges the entire root.
    there should probably be a if true statement here, but i`m a total noob in Pytono_O
    can you help with this as well?:rolleyes:
     
  5. Selim_B

    Selim_B

    Unity Technologies

    Joined:
    Sep 12, 2022
    Posts:
    6
    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:
    Code (Python):
    1.  
    2. occurrencesFilteredByRegex = scene.findByProperty("Name", "^.*BOX 3.*$")
    3.  
    4. if len(occurrencesFilteredByRegex) > 0:
    5.     occurrencesMerged = scene.mergeParts(occurrencesFilteredByRegex)
    6.  
    7.     myCustomName = "MyCustomName"
    8.     core.setProperty(occurrencesMerged[0], "Name", myCustomName)
    9.  
     
    Last edited: Sep 26, 2023
    Ovh_PIXYZ likes this.
  6. Ovh_PIXYZ

    Ovh_PIXYZ

    Joined:
    Sep 20, 2023
    Posts:
    4
    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!
     
  7. Selim_B

    Selim_B

    Unity Technologies

    Joined:
    Sep 12, 2022
    Posts:
    6
    Thanks. Good catch! I edited the sample code.

    Have a good day.