Skip to content

Commit

Permalink
Merge pull request #4 from MozillaReality/moz-lightmaps
Browse files Browse the repository at this point in the history
Add lightmap support and upgrade to Blender 2.83
  • Loading branch information
netpro2k authored Jun 10, 2020
2 parents d12ba85 + b4d196d commit 60abe2e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fake_bpy_modules*
__pycache__
.DS_Store
.DS_Store
io_scene_gltf2
38 changes: 20 additions & 18 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from . import components
from . import operators
from . import panels
from .gather_properties import gather_properties
from . import nodes
from .gather_properties import gather_properties, gather_lightmap_texture_info

bl_info = {
"name" : "Hubs Blender Exporter",
"author" : "MozillaReality",
"description" : "Tools for developing GLTF assets for Mozilla Hubs",
"blender" : (2, 82, 0),
"version" : (0, 0, 2),
"blender" : (2, 83, 0),
"version" : (0, 0, 3),
"location" : "",
"wiki_url": "https://github.com/MozillaReality/hubs-blender-exporter",
"tracker_url": "https://github.com/MozillaReality/hubs-blender-exporter/issues",
Expand All @@ -23,42 +24,33 @@
def get_version_string():
return str(bl_info['version'][0]) + '.' + str(bl_info['version'][1]) + '.' + str(bl_info['version'][2])

# Monkey patch to add gather_gltf_hook, must be removed once handled upstream https://github.com/KhronosGroup/glTF-Blender-IO/issues/1009
# gather_gltf_hook does not expose the info we need, make a custom hook for now
# ideally we can resolve this upstream somehow https://github.com/KhronosGroup/glTF-Blender-IO/issues/1009
from io_scene_gltf2.blender.exp import gltf2_blender_export
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
orig_gather_gltf = gltf2_blender_export.__gather_gltf
def patched_gather_gltf(exporter, export_settings):
orig_gather_gltf(exporter, export_settings)
export_user_extensions('gather_gltf_hook', export_settings, exporter._GlTF2Exporter__gltf)
export_user_extensions('hubs_gather_gltf_hook', export_settings, exporter._GlTF2Exporter__gltf)
exporter._GlTF2Exporter__traverse(exporter._GlTF2Exporter__gltf.extensions)

# Monkey patch to add gather_joint_hook, must be removed once handled upstream
from io_scene_gltf2.blender.exp import gltf2_blender_gather_joints
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
orig_gather_joint = gltf2_blender_gather_joints.gather_joint
def patched_gather_joint(blender_bone, export_settings):
node = orig_gather_joint(blender_bone, export_settings)
export_user_extensions('gather_joint_hook', export_settings, node, blender_bone)
return node

def register():
gltf2_blender_export.__gather_gltf = patched_gather_gltf
gltf2_blender_gather_joints.gather_joint = patched_gather_joint

components.register()
settings.register()
operators.register()
panels.register()

nodes.register()

def unregister():
gltf2_blender_export.__gather_gltf = orig_gather_gltf
gltf2_blender_gather_joints.gather_joint = orig_gather_joint

components.unregister()
settings.unregister()
operators.unregister()
panels.unregister()
nodes.unregister()

unregister_export_panel()

Expand All @@ -83,12 +75,13 @@ def __init__(self):
# We need to wait until we create the gltf2UserExtension to import the gltf2 modules
# Otherwise, it may fail because the gltf2 may not be loaded yet
from io_scene_gltf2.io.com.gltf2_io_extensions import Extension

self.Extension = Extension
self.properties = bpy.context.scene.HubsComponentsExtensionProperties
self.hubs_settings = bpy.context.scene.hubs_settings
self.was_used = False

def gather_gltf_hook(self, gltf2_object, export_settings):
def hubs_gather_gltf_hook(self, gltf2_object, export_settings):
if not self.properties.enabled or not self.was_used: return

hubs_config = self.hubs_settings.hubs_config
Expand Down Expand Up @@ -116,6 +109,15 @@ def gather_material_hook(self, gltf2_object, blender_material, export_settings):

self.add_hubs_components(gltf2_object, blender_material, export_settings)

if blender_material.node_tree and blender_material.use_nodes:
lightmap_texture_info = gather_lightmap_texture_info(blender_material, export_settings)
if lightmap_texture_info:
gltf2_object.extensions["MOZ_lightmap"] = self.Extension(
name="MOZ_lightmap",
extension=lightmap_texture_info,
required=False,
)

def gather_joint_hook(self, gltf2_object, blender_pose_bone, export_settings):
if not self.properties.enabled: return

Expand Down
29 changes: 29 additions & 0 deletions gather_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import bpy
from io_scene_gltf2.blender.exp import gltf2_blender_gather_materials
from io_scene_gltf2.blender.com import gltf2_blender_extras
from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
from io_scene_gltf2.blender.exp.gltf2_blender_gather_texture_info import (
__filter_texture_info,
__gather_index,
__gather_tex_coord,
)
from .nodes import MozLightmapNode

def gather_properties(export_settings, blender_object, target, type_definition, hubs_config):
value = {}
Expand Down Expand Up @@ -70,3 +77,25 @@ def gather_collections_property(export_settings, blender_object, target, propert
filtered_collection_names.append(collection.name)

return filtered_collection_names

@cached
def gather_lightmap_texture_info(blender_material, export_settings):
nodes = blender_material.node_tree.nodes
lightmap_node = next((n for n in nodes if isinstance(n, MozLightmapNode)), None)

texture = lightmap_node.inputs.get("Lightmap")
intensity = lightmap_node.intensity

if not __filter_texture_info((texture,), export_settings):
return None

texture_info = {
"intensity": intensity,
"index": __gather_index((texture,), export_settings),
"texCoord": __gather_tex_coord((texture,), export_settings)
}

if texture_info["index"] is None:
return None

return texture_info
50 changes: 50 additions & 0 deletions nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import bpy

import nodeitems_utils
from nodeitems_utils import NodeCategory, NodeItem
from bpy.types import Node

class MozCategory(NodeCategory):
@classmethod
def poll(cls, context):
return context.space_data.tree_type == 'ShaderNodeTree'

node_categories = [
MozCategory("MOZ_NODES", "Hubs", items=[
NodeItem("moz_lightmap.node")
]),
]

class MozLightmapNode(Node):
"""MOZ_lightmap settings node"""
bl_idname = 'moz_lightmap.node'
bl_label = 'MOZ_lightmap settings'
bl_icon = 'LIGHT'
bl_width_min = 216.3
bl_width_max = 330.0

intensity: bpy.props.FloatProperty(name="Intensity", soft_min=0, soft_max=1, default=1)

def init(self, context):
lightmap = self.inputs.new('NodeSocketColor', "Lightmap")
lightmap.hide_value = True

self.width = 216.3

@classmethod
def poll(cls, ntree):
return ntree.bl_idname == 'ShaderNodeTree'

def draw_buttons(self, context, layout):
layout.prop(self, "intensity")

def draw_label(self):
return "MOZ_lightmap"

def register():
bpy.utils.register_class(MozLightmapNode)
nodeitems_utils.register_node_categories("MOZ_NODES", node_categories)

def unregister():
bpy.utils.unregister_class(MozLightmapNode)
nodeitems_utils.unregister_node_categories("MOZ_NODES")

0 comments on commit 60abe2e

Please sign in to comment.