0
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ Worth noting: blender.stackexchange.com/questions/2848/why-avoid-bpy-ops $\endgroup$ – Ray Mairlot Jan 26 at 18:57
  • $\begingroup$ Could you please make this a bit clearer. Maybe with a 3 or 4 object example. $\endgroup$ – batFINGER Mar 26 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 at 11:24
2
+100
$\begingroup$

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
    bpy.ops.object.join(c)

if not obs[0].name.endswith(hp_suffix):
    obs[0].name += "_{}".format(hp_suffix)
$\endgroup$
  • $\begingroup$ No problem, glad it is working @DanylBekhoucha Let me know if you need something else. $\endgroup$ – brockmann Apr 2 at 9:03
  • 1
    $\begingroup$ Looks super sweet, thanks for adding how to override the context @p2or. $\endgroup$ – brockmann Apr 2 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 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 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.