Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to export Material as Spatial Material #276

Merged
merged 2 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add option to export Material as Spatial Material
  • Loading branch information
Jason0214 committed Sep 2, 2019
commit a55346d1376237ece258a7220897e81b1e445e2d
27 changes: 22 additions & 5 deletions io_scene_godot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ class ExportGodot(bpy.types.Operator, ExportHelper):
"own AnimationPlayer holding its actions",
default=True,
)
use_export_material: BoolProperty(
name="Export Material",
description="Export all the material associated with mesh surfaces",
default=True,
)
use_export_shape_key: BoolProperty(
name="Export Shape Key",
description="Export all the shape keys in mesh objects",
Expand Down Expand Up @@ -151,6 +146,28 @@ class ExportGodot(bpy.types.Operator, ExportHelper):
)
)
)
material_mode: EnumProperty(
name="Material Mode",
description="Configuration of how mesh surface Material being "
"exported.",
default="SCRIPT_SHADER",
items=(
(
"NONE", "None",
"Do not export any materials"
),
(
"SPATIAL", "Spatial Material",
"Export all eligible materials as Spatial Material"
),
(
"SCRIPT_SHADER", "Script Shader Material",
"Export all eligible materials as Shader Material "
"with Script Shader"
)
)

)
material_search_paths: EnumProperty(
name="Material Search Paths",
description="Search for existing Godot materials with names that "
Expand Down
20 changes: 15 additions & 5 deletions io_scene_godot/converters/material/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def export_material(escn_file, export_settings, bl_object, material):
return "SubResource({})".format(resource_id)


def export_as_spatial_material(material_rsc_name, material):
"""Export a Blender Material as Godot Spatial Material"""
mat = InternalResource("SpatialMaterial", material_rsc_name)
mat['albedo_color'] = gamma_correct(material.diffuse_color)
return mat


def generate_material_resource(escn_file, export_settings, bl_object,
material):
"""Export blender material as an internal resource"""
Expand All @@ -68,22 +75,25 @@ def generate_material_resource(escn_file, export_settings, bl_object,
# to convert material to external file
material_rsc_name = ''

if (engine in ('CYCLES', 'BLENDER_EEVEE') and
if (export_settings['material_mode'] == 'SCRIPT_SHADER' and
engine in ('CYCLES', 'BLENDER_EEVEE') and
material.node_tree is not None):
mat = InternalResource("ShaderMaterial", material_rsc_name)
try:
export_script_shader(
escn_file, export_settings, bl_object, material, mat
)
except ValidationError as exception:
mat = None # revert to SpatialMaterial
# fallback to SpatialMaterial
mat = export_as_spatial_material(material_rsc_name, material)
logging.error(
"%s, in material '%s'", str(exception), material.name
)

if mat is None:
mat = InternalResource("SpatialMaterial", material_rsc_name)
mat['albedo_color'] = gamma_correct(material.diffuse_color)
elif export_settings['material_mode'] == 'SPATIAL':
mat = export_as_spatial_material(material_rsc_name, material)

assert mat is not None

# make material-object tuple as an identifier, as uniforms is part of
# material and they are binded with object
Expand Down
4 changes: 2 additions & 2 deletions io_scene_godot/converters/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def export_object_link_material(escn_file, export_settings, mesh_object,
if slot.link == 'OBJECT' and slot.material is not None:
surface_id = mesh_resource.get_surface_id(index)
if (surface_id is not None and
export_settings['use_export_material']):
export_settings['material_mode'] != 'NONE'):
gd_node['material/{}'.format(surface_id)] = export_material(
escn_file,
export_settings,
Expand Down Expand Up @@ -280,7 +280,7 @@ def generate_surfaces(self, escn_file, export_settings, mesh):
if mesh.materials:
mat = mesh.materials[face.material_index]
if (mat is not None and
export_settings['use_export_material']):
export_settings['material_mode'] != 'NONE'):
surface.material = export_material(
escn_file,
export_settings,
Expand Down