Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Theverat/0b5afc0010a579a6fba3f76e2c2b87a4 to your computer and use it in GitHub Desktop.
Save Theverat/0b5afc0010a579a6fba3f76e2c2b87a4 to your computer and use it in GitHub Desktop.
import bpy
tex_type_map = {
"IMAGE": "LuxCoreNodeTexImagemap",
}
def new_node(bl_idname, node_tree, previous_node, output=0, input=0):
node = node_tree.nodes.new(bl_idname)
node.location = (previous_node.location.x - 250, previous_node.location.y)
node_tree.links.new(node.outputs[output], previous_node.inputs[input])
return node
class LUXCORE_OT_convert_blender_internal(bpy.types.Operator):
bl_idname = "luxcore.convert_blender_internal"
bl_label = "Convert BI"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
converted = 0
skipped = 0
for mat in bpy.data.materials:
if mat.luxcore.node_tree:
skipped += 1
continue
print("[CONVERT] Material: " + mat.name)
node_tree = bpy.data.node_groups.new(name=mat.name,
type="luxcore_material_nodes")
node_tree.use_fake_user = True
mat.luxcore.node_tree = node_tree
mat.update_tag()
nodes = node_tree.nodes
output = nodes.new("LuxCoreNodeMatOutput")
output.location = (300, 200)
glossy = new_node("LuxCoreNodeMatGlossy2", node_tree, output,
"Material", "Material")
for i, slot in enumerate(mat.texture_slots):
if slot is None:
continue
print("[CONVERT] Slot %d" % i)
tex = slot.texture
# Texture type
try:
bl_idname = tex_type_map[tex.type]
except KeyError:
print("[ERROR] Can not convert texture type: " + tex.type)
continue
# Which socket of the glossy material node we use
# TODO should support multiple sockets
if slot.use_map_color_diffuse:
input = "Diffuse Color"
elif slot.use_map_normal:
input = "Bump"
elif slot.use_map_specular:
input = "Specular Color"
else:
print("[ERROR] Can not convert influence type")
continue
output = "Color" # TODO check if all textures have this output
tex_node = new_node(bl_idname, node_tree, glossy, output, input)
if bl_idname == "LuxCoreNodeTexImagemap":
tex_node.image = tex.image
tex_node.inputs["Brightness"].default_value = tex.intensity
if slot.use_map_normal:
tex_node.is_normal_map = True
elif slot.use_map_specular:
# BI has a different specular concept than LuxCore
tex_node.inputs["Brightness"].default_value = 0.05
print("[SUCCESS] Converted texture " + tex.name)
print("[SUCCESS] Converted material " + mat.name)
converted += 1
print("[DONE] Converted: %d, Skipped: %d" % (converted, skipped))
return {'FINISHED'}
def register():
bpy.utils.register_class(LUXCORE_OT_convert_blender_internal)
def unregister():
bpy.utils.unregister_class(LUXCORE_OT_convert_blender_internal)
if __name__ == "__main__":
register()
# test call
bpy.ops.luxcore.convert_blender_internal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment