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

Model JSON tweaks #469

Merged
merged 22 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 63 additions & 8 deletions MCprep_addon/materials/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
# ##### END GPL LICENSE BLOCK #####

import os
from typing import Dict, Optional, List, Any, Tuple
from typing import Dict, Optional, List, Any, Tuple, Union
from pathlib import Path
from dataclasses import dataclass
from enum import Enum

import bpy
from bpy.types import Context, Material, Image, Texture, Nodes, NodeLinks, Node
Expand All @@ -29,6 +30,11 @@

AnimatedTex = Dict[str, int]

class PackFormat(Enum):
SIMPLE = 0
SEUS = 1
SPECULAR = 2

# -----------------------------------------------------------------------------
# Material prep and generation functions (no registration)
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -293,7 +299,7 @@ class PrepOptions:
use_reflections: bool
use_principled: bool
only_solid: bool
pack_format: str # TODO: Enforce enum value assignment.
pack_format: PackFormat
use_emission_nodes: bool
use_emission: bool

Expand All @@ -319,8 +325,8 @@ def matprep_cycles(mat: Material, options: PrepOptions) -> Optional[bool]:
# if use_reflections and checklist(canon, "water"):
# res = matgen_special_water(mat, passes)
# if use_reflections and checklist(canon, "glass"):
# res = matgen_special_glass(mat, passes)
if options.pack_format == "simple":
# res = matgen_special_glass(mat, passes)
if options.pack_format == PackFormat.SIMPLE:
res = matgen_cycles_simple(mat, options)
elif options.use_principled:
res = matgen_cycles_principled(mat, options)
Expand Down Expand Up @@ -1112,6 +1118,55 @@ def texgen_seus(mat: Material, passes: Dict[str, Image], nodeInputs: List, use_r
# nodeTexDisp["MCPREP_disp"] = True
nodeTexDiff.image = image_diff

def generate_base_material(
context: Context, name: str,
path: Union[Path, str], useExtraMaps: bool
) -> Tuple[Optional[Material], Optional[str]]:
"""Generate a base material from name and active resource pack"""
try:
image = bpy.data.images.load(path, check_existing=True)
except: # if Image is not found
image = None
mat = bpy.data.materials.new(name=name)

engine = context.scene.render.engine
if engine in ['CYCLES','BLENDER_EEVEE']:
# need to create at least one texture node first, then the rest works
mat.use_nodes = True
nodes = mat.node_tree.nodes
node_diff = create_node(
nodes, 'ShaderNodeTexImage',
name="Diffuse Texture",
label="Diffuse Texture",
location=(-380, 140),
interpolation='Closest',
image=image
)
node_diff["MCPREP_diffuse"] = True

# The offset and link diffuse is for default no texture setup
links = mat.node_tree.links
for n in nodes:
if n.bl_idname == 'ShaderNodeBsdfPrincipled':
links.new(node_diff.outputs[0], n.inputs[0])
links.new(node_diff.outputs[1], n.inputs["Alpha"])
break

env.log("Added blank texture node")
# Initialize extra passes as well
if image:
node_spec = create_node(nodes, 'ShaderNodeTexImage')
node_spec["MCPREP_specular"] = True
node_nrm = create_node(nodes, 'ShaderNodeTexImage')
node_nrm["MCPREP_normal"] = True
# now use standard method to update textures
set_cycles_texture(image, mat, useExtraMaps)

else:
return None, "Only Cycles and Eevee supported"

return mat, None


def matgen_cycles_simple(mat: Material, options: PrepOptions) -> Optional[bool]:
"""Generate principled cycles material."""
Expand Down Expand Up @@ -1343,9 +1398,9 @@ def matgen_cycles_principled(mat: Material, options: PrepOptions) -> Optional[bo
nodes.remove(nodeEmitCam)
nodes.remove(nodeMixEmit)
# generate texture format and connect
if options.pack_format == "specular":
if options.pack_format == PackFormat.SPECULAR:
texgen_specular(mat, options.passes, nodeInputs, options.use_reflections)
elif options.pack_format == "seus":
elif options.pack_format == PackFormat.SEUS:
texgen_seus(mat, options.passes, nodeInputs, options.use_reflections, options.use_emission_nodes)

if options.only_solid is True or checklist(canon, "solid"):
Expand Down Expand Up @@ -1572,9 +1627,9 @@ def matgen_cycles_original(mat: Material, options: PrepOptions):
nodeBump.inputs["Normal"]]]

# generate texture format and connect
if options.pack_format == "specular":
if options.pack_format == PackFormat.SPECULAR:
texgen_specular(mat, options.passes, nodeInputs, options.use_reflections)
elif options.pack_format == "seus":
elif options.pack_format == PackFormat.SEUS:
texgen_seus(mat, options.passes, nodeInputs, options.use_reflections, options.use_emission_nodes)

if options.only_solid is True or checklist(canon, "solid"):
Expand Down
47 changes: 10 additions & 37 deletions MCprep_addon/materials/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ def execute(self, context):

if engine == 'CYCLES' or engine == 'BLENDER_EEVEE':
options = generate.PrepOptions(
passes=passes,
use_reflections=self.useReflections,
use_principled=self.usePrincipledShader,
only_solid=self.makeSolid,
pack_format=self.packFormat,
use_emission_nodes=self.useEmission,
use_emission=False # This is for an option set in matprep_cycles
passes,
self.useReflections,
self.usePrincipledShader,
self.makeSolid,
generate.PackFormatself.packFormat.upper(),
self.useEmission,
False # This is for an option set in matprep_cycles
)
res = generate.matprep_cycles(
mat=mat,
Expand Down Expand Up @@ -557,8 +557,9 @@ def execute(self, context):
"File not found! Reset the resource pack under advanced "
"settings (return arrow icon) and press reload materials"))
return {'CANCELLED'}
mat, err = self.generate_base_material(
context, mat_name, self.filepath)
# Create the base material node tree setup
mat, err = generate.generate_base_material(
context, mat_name, self.filepath, self.useExtraMaps)
if mat is None and err:
self.report({"ERROR"}, err)
return {'CANCELLED'}
Expand Down Expand Up @@ -590,34 +591,6 @@ def execute(self, context):
self.track_param = context.scene.render.engine
return {'FINISHED'}

def generate_base_material(self, context, name, path):
"""Generate a base material from name and active resource pack"""
image = bpy.data.images.load(path, check_existing=True)
mat = bpy.data.materials.new(name=name)

engine = context.scene.render.engine
if engine == 'CYCLES' or engine == 'BLENDER_EEVEE':
# need to create at least one texture node first, then the rest works
mat.use_nodes = True
nodes = mat.node_tree.nodes
node_diff = generate.create_node(nodes, 'ShaderNodeTexImage', image=image)
node_diff["MCPREP_diffuse"] = True

# Initialize extra passes as well
node_spec = generate.create_node(nodes, 'ShaderNodeTexImage')
node_spec["MCPREP_specular"] = True
node_nrm = generate.create_node(nodes, 'ShaderNodeTexImage')
node_nrm["MCPREP_normal"] = True

env.log("Added blank texture node")


# now use standard method to update textures
generate.set_cycles_texture(image, mat, self.useExtraMaps)
else:
return None, "Only Cycles and Eevee supported"

return mat, None

def update_material(self, context, mat):
"""Update the initially created material"""
Expand Down
Loading