0

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".

Danyl Bekhoucha
  • 3,722
  • 13
  • 47
  • 96
  • 1
    Worth noting: https://blender.stackexchange.com/questions/2848/why-avoid-bpy-ops – Ray Mairlot Jan 26 '19 at 18:57
  • Could you please make this a bit clearer. Maybe with a 3 or 4 object example. – batFINGER Mar 26 '19 at 08:36
  • 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? – brockmann Apr 01 '19 at 11:24

1 Answers1

2

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': high_poly_objects.append(ob)

Deselect all objects

bpy.ops.object.select_all(action='DESELECT')

Select all High-Poly objects to call the join operator

for ob in high_poly_objects: ob.select_set(True) bpy.context.view_layer.objects.active = ob

Join all objects selected before

bpy.ops.object.join()

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 ob.select_set(True) else: ob.select_set(False)


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: obs.append(ob)

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

# Blender 3.2+
with C.temp_override(c):
    bpy.ops.object.join()

# Blender 2.8+
#bpy.ops.object.join(c)

if not obs[0].name.endswith(hp_suffix): obs[0].name += "_{}".format(hp_suffix)

p2or
  • 15,860
  • 10
  • 83
  • 143
brockmann
  • 12,613
  • 4
  • 50
  • 93