Merge pull request #2 from GreenXenith/script

Include transparency and material rewrite in script
This commit is contained in:
random-geek 2019-07-08 15:19:33 -07:00 committed by GitHub
commit 4c337a39e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 16 deletions

View File

@ -14,23 +14,9 @@ Use `/mesh1` and `/mesh2` to set the corners of the area you want exported, then
Once the model is exported, you should be able to import the `.obj` file with default settings. Make sure "Image Search" in the import settings is selected to ensure the textures are imported as well. Texture modifiers are ignored, so some materials will likely have to be fixed by hand.
#### Fixing interpolation
#### Fixing materials
If you intend to render the scene, you will want to set the interpolation mode of all the image textures to "Closest" in order to keep the pixelated look. This can either be done manually or by running this script in Blender's text editor:
```python
import bpy
for mat in bpy.data.materials:
try:
nodes = mat.node_tree.nodes
for node in nodes:
if node.type == "TEX_IMAGE":
node.interpolation = "Closest"
except:
continue
```
Blender's packaged material assigned to OBJ textures are not effective or easy to use. By default, textures will also appear blurry and lack alpha. The `materials.py` script is included in the mod to simplify the materials, change interpolation, and add transparency. Open the script in Blender's text editor and run the script with the mesh selected.
#### Fixing vertex normals

66
materials.py Normal file
View File

@ -0,0 +1,66 @@
# Original script (interpolation): Techy5
# Expanded script: GreenXenith
### Use better materials and support alpha ###
# Usage: Open or copy script in Blender
# Run script while object is selected
import bpy
for mat in bpy.data.materials:
try:
nodes = mat.node_tree.nodes
links = mat.node_tree.links
# Remove all nodes except texture
for node in nodes:
if node.type != "TEX_IMAGE":
nodes.remove(node)
# Change image interpolation
tex = nodes["Image Texture"]
tex.interpolation = "Closest"
tex.location = 0, 0
# Create texture coordinate node
coord = nodes.new("ShaderNodeTexCoord")
coord.location = -600, 0
# Create mapping node
map = nodes.new("ShaderNodeMapping")
map.location = -400, 0
# Create principled shader
prin = nodes.new("ShaderNodeBsdfPrincipled")
prin.location = 200, 0
prin.inputs["Specular"].default_value = 0
# Create transparent shader
trans = nodes.new("ShaderNodeBsdfTransparent")
trans.location = 400, -150
# Create mix shader
mix = nodes.new("ShaderNodeMixShader")
mix.location = 600, 0
# Create output
out = nodes.new("ShaderNodeOutputMaterial")
out.location = 800, 0
# Link everything
links.new(coord.outputs[2], map.inputs[0]) # Coord > Map
links.new(map.outputs[0], tex.inputs[0]) # Map > Texture
links.new(tex.outputs[0], prin.inputs[0]) # Texture > Principled
links.new(prin.outputs[0], mix.inputs[2]) # Principled > Mix
links.new(trans.outputs[0], mix.inputs[1]) # Transparent > Mix
links.new(tex.outputs[1], mix.inputs[0]) # Texture alpha > Mix factor
links.new(mix.outputs[0], out.inputs[0]) # Mix > Output
# Deselect all
for node in nodes:
node.select = False
except:
continue