0
$\begingroup$

I am batch processing asset files of which the scene contains just 4 objects. Each of these objects and its associated data currently have a generic name.

  1. The mesh should be named "VEG_" + [Object name]

Note: the mesh data is linked across the object

For the object name, I want to:

  1. Apply a prefix "VEG_"
  2. Apply a suffix for each of the seasons "_Spring", "_Summer", "_Winter", "_Autumn", so one for each object

The material name needs to correspond to the object's initial name and receive prefixes.

  1. Apply the prefix "VEG_[Object name]"
  2. Apply the suffix "_Spring", "_Summer", "_Winter", "_Autumn"

Can I rename the objects through python? See an example below of the current naming scheme and the desired scheme.

enter image description here

enter image description here

$\endgroup$
2
  • $\begingroup$ How computer know which object use "_Spring", which one use "_Summer" ? $\endgroup$
    – X Y
    Commented Apr 22, 2022 at 12:56
  • $\begingroup$ The first object is [object name], which could be "_Spring". The second is [object name].001, which would use "_Summer", the third is [object name].002, etc. $\endgroup$
    – Hologram
    Commented Apr 22, 2022 at 13:09

1 Answer 1

1
$\begingroup$
# suppose only 4 objects
import bpy

# generator
gen = (s for s in ["_Spring", "_Summer", "_Winter", "_Autumn"])

bpy.data.objects[0].name = bpy.data.objects[0].name + ".000"

for i, oj in enumerate(bpy.data.objects):
    s = next(gen)
    name = oj.name[: -4]
    oj.name = f"VEG_{name}{s}"
    oj.data.name = oj.name
    if oj.material_slots[0].material:
        mat_name = oj.material_slots[0].material.name
        oj.material_slots[0].material.name = f"VEG_{name}_{mat_name}{s}"
    if oj.material_slots[1].material:
        mat_name = oj.material_slots[1].material.name
        oj.material_slots[1].material.name = f"VEG_{name}.{mat_name}{s}"

reference of f-strings: https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/

reference of generator: https://python-course.eu/advanced-python/generators-iterators.php

$\endgroup$
2
  • $\begingroup$ Thanks, I will try this out tomorrow. Do you happen to know what I could do to not hardcode "_leaf" and "_trunk"? Some materials have additional slots and therefore different names. Can I process them in a similar way, by adding the suffix to the existing object name, which is treated as a variable in itself? $\endgroup$
    – Hologram
    Commented Apr 22, 2022 at 20:31
  • 1
    $\begingroup$ I update a new one which will follow your material name. $\endgroup$
    – X Y
    Commented Apr 23, 2022 at 3:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .