# -*- coding: utf-8 -*- """Ilustrações passo a passo do manual (traço Freestyle, peças novas em laranja). blender -b -P passos_blender.py -- [passo ...]""" import bpy, math, os, sys from mathutils import Vector, Euler R = os.path.expanduser("~/robo_movel/render") argv = sys.argv[sys.argv.index("--") + 1:] OUT = argv[0] ONLY = set(argv[1:]) os.makedirs(OUT, exist_ok=True) bpy.ops.wm.read_factory_settings(use_empty=True) scn = bpy.context.scene with bpy.data.libraries.load(os.path.join(R, "robo_bp_import.blend"), link=False) as (src, dst): dst.objects = src.objects for o in dst.objects: scn.collection.objects.link(o) bpy.ops.wm.obj_import(filepath=os.path.join(R, "robo_fios.obj"), up_axis="Z", forward_axis="Y") objs = [o for o in scn.objects if o.type == "MESH"] HOME = {o.name: o.location.copy() for o in objs} def emit(name, rgb): m = bpy.data.materials.new(name) m.use_nodes = True nt = m.node_tree for n in list(nt.nodes): nt.nodes.remove(n) e = nt.nodes.new("ShaderNodeEmission") e.inputs["Color"].default_value = (*rgb, 1) o = nt.nodes.new("ShaderNodeOutputMaterial") nt.links.new(e.outputs[0], o.inputs[0]) return m M_OLD = emit("velho", (1, 1, 1)) M_NEW = emit("novo", (1.0, 0.62, 0.38)) # ------------------------------------------------------------------ grupos (na ordem de montagem) def pref(*p): return lambda n: n.startswith(p) G = [ ("base", pref("Base", "Nervura_"), (0, 0, 0.05)), ("paredes", pref("Parede_"), "radial"), ("cam_sup", pref("Suporte_Cam_"), (0, 0, 0.05)), ("calcos", pref("Calco_", "Suporte_DF", "Suporte_DT", "Suporte_EF", "Suporte_ET", "M4x25_", "M4arr_", "M4porca_"), (0, 0, -0.07)), ("motores", pref("Motor_", "M3x6_mot_"), "motor"), ("rodas", pref("Roda_"), "roda"), ("barras", pref("Barra_", "M8nyl_b_", "M8arrL_b_", "M8arrL_b2_", "M8porca_b2_"), (0, 0, 0.18)), ("arduino", pref("Esp_uno", "M3x8_uno", "M3x6_uno", "ArduinoUNO"), (0, 0, 0.05)), ("shield", pref("CNCShield"), (0, 0, 0.045)), ("drivers", pref("A4988_"), (0, 0, 0.035)), ("placas", pref("Protoboard_", "ESP32CAM", "Abracadeira"), (0, 0, 0.05)), ("energia", pref("Bateria", "Cinta", "LM2596", "Esp_lm", "M3x6_lm", "M3x8_lm", "Chave", "Wago_", "XT60", "PortaFusivel"), (0, 0, 0.05)), ("cabos_mot", pref("Cabo_"), (0, 0, 0.0)), ("fiacao", lambda n: not n.startswith(("Cabo_",)) and n.split("__")[0] in FIOS, (0, 0, 0.0)), ("tampo", pref("Tampo", "M8arrL_t_", "M8porca_t_", "M8arrL_t2_", "M8nyl_t2_"), (0, 0, 0.12)), ("prat", pref("Prat_"), (0, 0, 0.14)), ] # objetos vindos do OBJ de fios: os que não estavam no .blend leve FIOS = {o.name for o in objs if o.name not in {x.name for x in dst.objects}} # fios são tubos muito densos: simplifica (no traço não faz diferença) for o in objs: if o.name in FIOS and len(o.data.vertices) > 500: md = o.modifiers.new("dec", "DECIMATE") md.ratio = 0.15 bpy.context.view_layer.objects.active = o bpy.ops.object.modifier_apply(modifier="dec") def group_of(n): for i, (k, f, _) in enumerate(G): if f(n): return i return None GI = {o.name: group_of(o.name) for o in objs} # câmera por passo: (tipo, alvo z, escala, elevação) CAM = {"base": ("iso", 0.10, 0.85, 32), "paredes": ("iso", 0.10, 0.85, 32), "cam_sup": ("frente", 0.10, 0.70, 30), "calcos": ("baixo", 0.07, 0.9, -28), "motores": ("baixo", 0.05, 0.9, -24), "rodas": ("iso", 0.07, 0.9, 20), "barras": ("iso", 0.62, 1.75, 22), "arduino": ("int", 0.10, 0.34, 55), "shield": ("int", 0.10, 0.30, 50), "drivers": ("int", 0.10, 0.28, 55), "placas": ("int", 0.10, 0.72, 62), "energia": ("int", 0.10, 0.72, 62), "cabos_mot": ("int", 0.10, 0.80, 58), "fiacao": ("int", 0.10, 0.80, 62), "tampo": ("iso", 0.20, 0.95, 30), "prat": ("iso", 0.66, 1.95, 22)} HIDE_FOR_INT = pref("Barra_", "M8") def offset_for(o, spec, s=1.0): if isinstance(spec, tuple): return Vector(spec) * s c = sum((o.matrix_world @ Vector(v) for v in o.bound_box), Vector()) / 8 if spec == "radial": d = Vector((c.x, c.y, 0)) return (Vector((1 if d.x > 0 else -1, 0, 0)) if abs(d.x) > abs(d.y) else Vector((0, 1 if d.y > 0 else -1, 0))) * 0.07 * s if spec == "motor": return Vector((-0.07 if c.x > 0 else 0.07, 0, 0)) * s if spec == "roda": return Vector((0.08 if c.x > 0 else -0.08, 0, 0)) * s return Vector() # ------------------------------------------------------------------ render scn.render.engine = "BLENDER_EEVEE" scn.view_settings.view_transform = "Standard" w = bpy.data.worlds.new("W") scn.world = w w.use_nodes = True w.node_tree.nodes["Background"].inputs["Color"].default_value = (1, 1, 1, 1) scn.render.use_freestyle = True scn.render.line_thickness_mode = "ABSOLUTE" scn.render.line_thickness = 1.4 fs = scn.view_layers[0].freestyle_settings fs.crease_angle = math.radians(140) ls = fs.linesets[0] if fs.linesets else fs.linesets.new("L") if ls.linestyle is None: ls.linestyle = bpy.data.linestyles.new("T") ls.linestyle.color = (0.05, 0.05, 0.07) for a in ("select_silhouette", "select_border", "select_crease", "select_external_contour"): setattr(ls, a, True) scn.render.resolution_x, scn.render.resolution_y = 1600, 1200 scn.render.image_settings.file_format = "PNG" cam_d = bpy.data.cameras.new("C") cam_d.type = "ORTHO" cam = bpy.data.objects.new("C", cam_d) scn.collection.objects.link(cam) scn.camera = cam for k, (key, f, spec) in enumerate(G): if ONLY and key not in ONLY: continue kind, tz, scale, el = CAM[key] for o in objs: gi = GI[o.name] o.location = HOME[o.name] vis = gi is not None and gi <= k if kind == "int" and (o.name.startswith(("Tampo", "Prat_")) or HIDE_FOR_INT(o.name)): vis = False if key in ("tampo", "prat") and o.name in FIOS and not o.name.startswith(("Cabo_", "Chave")): vis = False # fiação fica dentro da caixa fechada o.hide_render = not vis o.data.materials.clear() o.data.materials.append(M_NEW if gi == k else M_OLD) if gi == k: o.location = HOME[o.name] + offset_for(o, spec) bpy.context.view_layer.update() cam_d.ortho_scale = scale if kind == "frente": target, az = Vector((0, 0.12, tz)), math.radians(160) elif kind == "int" and key in ("arduino", "shield", "drivers"): target, az = Vector((0, -0.20, tz)), math.radians(150) else: target, az = Vector((0, 0, tz)), math.radians(145) e = math.radians(el) cam.location = target + Vector((math.sin(az) * math.cos(e), -math.cos(az) * math.cos(e), math.sin(e))) * 6 cam.rotation_euler = (target - cam.location).to_track_quat("-Z", "Y").to_euler() scn.render.filepath = os.path.join(OUT, "passo_%02d_%s.png" % (k + 1, key)) bpy.ops.render.render(write_still=True) print("salvo", scn.render.filepath, flush=True)