0
$\begingroup$

I am trying to write a script that will end up baking materials from a high poly object to a low poly object. The problem i am having is that i will never know the object names beforehand, as my script is importing obj's from a folder into blender then duplicating them and applying a decimate filter to them. So far i have all of that functioning correctly, but i'm stuck on the final part of selecting the low poly at the target object of the bake.

>>> bpy.ops.object.select_by_type(type='MESH')
{'FINISHED'}

>>> 
>>> for obj in bpy.context.scene.objects:
...     if obj.name.endswith("001"):
...         print(ob.name)
...         bpy.context.view_layer.objects.active = ob
...         
Hat 1.001

>>> 

I have the script printing out the name of the object that has a name ending in 001 since i know that my low poly duplicate will always end with that. My problem is that i need to take that printed name and apply it to the next command like this example

bpy.context.scene.targetobj = "Hat 1.001"

I can't figure out how to take the name that gets printed out and apply it like in that example. Thanks in advance.

$\endgroup$

2 Answers 2

0
$\begingroup$

If you just need the name of the object as a string, then store it in a variable. I'm not sure bpy.context.scene.targetobj exists though.

for obj in bpy.context.scene.objects:
    if obj.name.endswith("001"):
        # store the name into a variable
        object_name = obj.name
        # use the variable
        bpy.context.scene.targetobj = object_name
$\endgroup$
1
  • $\begingroup$ Worked perfectly and I even found a few other spots I could use the name variable, thank you so much! $\endgroup$
    – Sapps
    Mar 26, 2020 at 1:50
1
$\begingroup$

Use a pointer property

Can set up a pointer to any blender ID type objects, in this case bpy.types.Object instances of which can be found in bpy.data.objects collection.

Assign the property to class

import bpy
from bpy.types import Object, Scene
from bpy.props import PointerProperty


Scene.target_obj = PointerProperty(type=Object)

Test.

>>> C.object
bpy.data.objects['Cube']

>>> bpy.ops.object.duplicate()
{'FINISHED'}

>>> C.object
bpy.data.objects['Cube.001']

>>> C.scene.target_obj = C.object
>>> C.object.name = "Foo"
>>> C.scene.target_obj
bpy.data.objects['Foo']
$\endgroup$

You must log in to answer this question.

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