In a Blender 2.8 script I want to bake multiple high poly objects on one low poly.

I want to create a script in Blender 2.8 that bakes in selected to active mode. The active selection must be named "low poly", the other selections are joined and are named "high poly".

  • 1
    $\begingroup$ Worth noting: blender.stackexchange.com/questions/2848/why-avoid-bpy-ops $\endgroup$ – Ray Mairlot Jan 26 '19 at 18:57
  • $\begingroup$ Could you please make this a bit clearer. Maybe with a 3 or 4 object example. $\endgroup$ – batFINGER Mar 26 '19 at 8:36
  • $\begingroup$ I'd like to help but I don't get what you want. What's your situation exactly? And what's your actual goal? Just baking all and join them? Why a random selection when the low-res geometry is already prepared? $\endgroup$ – brockmann Apr 1 '19 at 11:24

I hope that's what you're looking for.

Select the low-poly object and run the script to add a <Name>_LowPoly suffix to the objects name in the first place, join all other mesh objects of the collection and rename the result to <Name>_HighPoly:

import bpy

suffix = "_LowPoly"
act_obj = bpy.context.view_layer.objects.active

# Append the suffix to the active object
if suffix not in act_obj.name:
    act_obj.name = act_obj.name + suffix

# Get the collection of the active object
obj_coll = act_obj.users_collection[0]

# Create an empty list
high_poly_objects = []

# Append all mesh objects (of the collection) to the list
for ob in obj_coll.objects:   
    if not "Low" in ob.name and ob.type == 'MESH':

# Deselect all objects

# Select all High-Poly objects to call the join operator
for ob in high_poly_objects:
    bpy.context.view_layer.objects.active = ob

# Join all objects selected before

# Get the object in context and re-name it
if "High" not in bpy.context.object.name:
    bpy.context.object.name += "_HighPoly"

# Optional -> Invert the selection for fun
for ob in obj_coll.objects:
    if "Low" in ob.name:
        bpy.context.view_layer.objects.active = ob

You can also override the context of bpy.ops.object.join() to avoid any viewport selection changes. Select all objects, make the low-poly object the 'Active Object' and run the script:

import bpy

hp_suffix = "HIPOLY"
lp_suffix = "LOWPOLY"

C = bpy.context
scene = C.scene

ob_active = C.active_object
if not ob_active.name.endswith(lp_suffix):
    ob_active.name += "_{}".format(lp_suffix)

obs = []
for ob in C.selected_editable_objects:
    if ob.type == 'MESH' and ob != ob_active:

if len(obs) > 1:
    c = {}
    c["object"] = c["active_object"] = obs[0]
    c["selected_objects"] = c["selected_editable_objects"] = obs

if not obs[0].name.endswith(hp_suffix):
    obs[0].name += "_{}".format(hp_suffix)
  • $\begingroup$ No problem, glad it is working @DanylBekhoucha Let me know if you need something else. $\endgroup$ – brockmann Apr 2 '19 at 9:03
  • 1
    $\begingroup$ Looks super sweet, thanks for adding how to override the context @p2or. $\endgroup$ – brockmann Apr 2 '19 at 10:46
  • $\begingroup$ In the script of p2or, the line "if len(obs) > 1:" must be set to 0 otherwise we need 3 objects selected minimum (so 2 high polys) to add the suffix "_HIPOLY". $\endgroup$ – Danyl Bekhoucha Apr 2 '19 at 12:20
  • 1
    $\begingroup$ Fixed it. Wouldn't up the number, prevents to call the join operator in case there is nothing to join :) - just re-indented the last lines. Does that work @DanylBekhoucha? $\endgroup$ – p2or Apr 2 '19 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.