Grafika2023/skript_bl
2024-03-04 23:13:45 +01:00

88 lines
2.3 KiB
Plaintext

import bpy
from math import radians
import os
from bpy_extras.io_utils import ExportHelper
context = bpy.context
scene = context.scene
vl = context.view_layer
bpy.ops.wm.ply_import(filepath="C:/Users/")
bpy.ops.object.select_all(action='DESELECT')
texture_path = "C:/Users/neryt/Grafika2023/PlanetCreator/"
texture = bpy.data.images.load(texture_path)
material = bpy.data.materials.new(name="TextureMaterial")
material.use_nodes = True
nodes = material.node_tree.nodes
links = material.node_tree.links
for node in nodes:
if node.type == 'BSDF_PRINCIPLED':
nodes.remove(node)
shader_node = nodes.new(type='ShaderNodeTexImage')
shader_node.image = texture
output_node = nodes.get('Material Output')
links.new(shader_node.outputs['Color'], output_node.inputs['Surface'])
for obj in scene.objects:
if (obj.type == 'MESH'):
vl.objects.active = obj
obj.select_set(True)
print(obj.name)
lm = obj.data.uv_layers.get("LightMap")
if not lm:
lm = obj.data.uv_layers.new(name="LightMap")
lm.active = True
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT') # for all faces
bpy.ops.uv.smart_project(angle_limit=66, island_margin = 0.02)
bpy.ops.object.editmode_toggle()
if obj.type == 'MESH':
obj.data.materials.append(material)
obj.select_set(False)
for obj in bpy.context.selected_objects:
if obj.type == 'MESH':
obj.data.materials.append(material)
output_folder = "C:\\Users\\"
os.makedirs(output_folder, exist_ok=True)
for idx, obj in enumerate(bpy.data.objects):
if obj.type == "MESH":
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
obj_name = f"roslina{idx + 1}"
file_path = os.path.join(output_folder, f"{obj_name}.obj")
bpy.ops.wm.obj_export(filepath=file_path,
check_existing=False,
export_selected_objects=True,
apply_modifiers=True,
export_smooth_groups=False,
smooth_group_bitflags=False,
export_normals=True,
export_uv=True)
obj.select_set(False)