Skip to content

Commit

Permalink
Make distance based material application an optional User Pref
Browse files Browse the repository at this point in the history
  • Loading branch information
oRazeD committed Jan 7, 2025
1 parent ccdd3a8 commit 069bd62
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
46 changes: 28 additions & 18 deletions preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GRABDOC_MT_presets(Menu):


class GRABDOC_PT_presets(PresetPanel, Panel):
bl_label = 'GrabDoc Presets'
bl_label = 'Bake Presets'
preset_subdir = 'grab_doc'
preset_operator = 'script.execute_preset'
preset_add_operator = 'grab_doc.preset_add'
Expand Down Expand Up @@ -136,28 +136,38 @@ class GRABDOC_AP_preferences(AddonPreferences):
bl_idname = __package__

marmo_executable: StringProperty(
name="",
description="Path to Marmoset Toolbag 3 or 4 executable",
default="",
subtype="FILE_PATH"
name="Marmoset EXE Path", default="", subtype="FILE_PATH"
)

render_within_frustrum: BoolProperty(
name="Render Within Frustrum",
description="""Only render objects within the camera viewing frustrum.
class GRABDOC_property_group(PropertyGroup):
MAP_TYPES = (
('none', "None", ""),
('normals', "Normals", ""),
('curvature', "Curvature", ""),
('occlusion', "Ambient Occlusion", ""),
('height', "Height", ""),
('id', "Material ID", ""),
('alpha', "Alpha", ""),
('color', "Base Color", ""),
('emissive', "Emissive", ""),
('roughness', "Roughness", ""),
('metallic', "Metallic", "")
Improves render speed but it may apply materials incorrectly (void objects).""",
default=False
)

def draw(self, _context):
layout = self.layout
col = layout.column()
col.prop(self, 'marmo_executable')
col.prop(self, 'render_within_frustrum')


class GRABDOC_property_group(PropertyGroup):
MAP_TYPES = (('none', "None", ""),
('normals', "Normals", ""),
('curvature', "Curvature", ""),
('occlusion', "Ambient Occlusion", ""),
('height', "Height", ""),
('id', "Material ID", ""),
('alpha', "Alpha", ""),
('color', "Base Color", ""),
('emissive', "Emissive", ""),
('roughness', "Roughness", ""),
('metallic', "Metallic", ""))

def update_export_name(self, _context: Context):
if not self.export_name:
self.export_name = "untitled"
Expand Down Expand Up @@ -188,7 +198,7 @@ def update_scale(self, context: Context):

map_range = height_ng.nodes.get('Map Range')
map_range.inputs[1].default_value = \
- self.height[0].distance + gd_camera_ob_z
-self.height[0].distance + gd_camera_ob_z
map_range.inputs[2].default_value = gd_camera_ob_z

map_range_alpha = \
Expand Down
40 changes: 17 additions & 23 deletions utils/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..constants import Global


def is_valid_gd_object(
def is_object_gd_valid(
ob: Object,
has_prefix: bool=False,
render_visible: bool=True,
Expand Down Expand Up @@ -37,29 +37,18 @@ def in_viewing_frustrum(vector: Vector) -> bool:
within the cameras viewing frustrum"""
bg_plane = bpy.data.objects[Global.BG_PLANE_NAME]
viewing_frustrum = (
Vector
(
(
bg_plane.dimensions.x * -1.25 + bg_plane.location[0],
Vector((bg_plane.dimensions.x * -1.25 + bg_plane.location[0],
bg_plane.dimensions.y * -1.25 + bg_plane.location[1],
-100
)
),
Vector(
(
bg_plane.dimensions.x * 1.25 + bg_plane.location[0],
-100)),
Vector((bg_plane.dimensions.x * 1.25 + bg_plane.location[0],
bg_plane.dimensions.y * 1.25 + bg_plane.location[1],
100
)
)
100))
)
for i in range(0, 3):
if (
vector[i] < viewing_frustrum[0][i] \
and vector[i] < viewing_frustrum[1][i] \
or vector[i] > viewing_frustrum[0][i] \
and vector[i] > viewing_frustrum[1][i]
):
if (vector[i] < viewing_frustrum[0][i] \
and vector[i] < viewing_frustrum[1][i] \
or vector[i] > viewing_frustrum[0][i] \
and vector[i] > viewing_frustrum[1][i]):
return False
return True

Expand All @@ -73,18 +62,23 @@ def get_rendered_objects() -> set | None:
if coll.gd_bake_collection is False:
continue
objects.update(
[ob for ob in coll.all_objects if is_valid_gd_object(ob)]
[ob for ob in coll.all_objects if is_object_gd_valid(ob)]
)
# TODO: Old method; profile it
#for ob in coll.all_objects:
# if is_valid_grabdoc_object(ob):
# rendered_obs.add(ob.name)
return objects

package = __package__.rsplit(".", maxsplit=1)[0]
preferences = bpy.context.preferences.addons[package].preferences
for ob in bpy.context.view_layer.objects:
if not is_valid_gd_object(ob):
if not is_object_gd_valid(ob):
continue

if not preferences.render_within_frustrum:
objects.add(ob)
continue
# Distance based filter; preference locked
local_bbox_center = .125 * sum(
(Vector(ob) for ob in ob.bound_box), Vector()
)
Expand Down

0 comments on commit 069bd62

Please sign in to comment.