# -*- coding: utf-8 -*- """Desenhos em traço (Freestyle) para as pranchas da blueprint. blender -b -P blueprint_blender.py -- vistas: frente, lado, topo, iso, explodida, interno Ortográficas com 1 px = 1 mm (escala exata) e JSON com pontos-âncora projetados (px) para cotas e balões: .json """ import bpy, math, os, sys, json from mathutils import Vector, Euler from bpy_extras.object_utils import world_to_camera_view ROOT = os.path.expanduser("~/robo_movel/render") argv = sys.argv[sys.argv.index("--") + 1:] VIEW, OUTP = argv[0], argv[1] CACHE = os.path.join(ROOT, "robo_bp_import.blend") # malha leve p/ traço bpy.ops.wm.read_factory_settings(use_empty=True) scn = bpy.context.scene with bpy.data.libraries.load(CACHE, link=False) as (src, dst): dst.objects = src.objects for o in dst.objects: scn.collection.objects.link(o) objs = list(dst.objects) # ---------------------------------------------------------------- material chapado branco white = bpy.data.materials.new("Branco") white.use_nodes = True nt = white.node_tree for n in list(nt.nodes): nt.nodes.remove(n) em = nt.nodes.new("ShaderNodeEmission") em.inputs["Color"].default_value = (1, 1, 1, 1) out = nt.nodes.new("ShaderNodeOutputMaterial") nt.links.new(em.outputs[0], out.inputs[0]) for o in objs: o.data.materials.clear() o.data.materials.append(white) def bbox_world(o): pts = [o.matrix_world @ Vector(c) for c in o.bound_box] return (min(p.x for p in pts), max(p.x for p in pts), min(p.y for p in pts), max(p.y for p in pts), min(p.z for p in pts), max(p.z for p in pts)) def inside_box(o): x0, x1, y0, y1, z0, z1 = bbox_world(o) return x0 > -0.2425 and x1 < 0.2425 and y0 > -0.2425 and y1 < 0.2425 and z0 > 0.0683 and z1 < 0.1385 def hide(o): o.hide_render = True o.hide_viewport = True # ---------------------------------------------------------------- visibilidade por vista if VIEW in ("frente", "lado", "topo", "iso", "explodida"): for o in objs: if inside_box(o): hide(o) if VIEW == "interno": for o in objs: x0, x1, y0, y1, z0, z1 = bbox_world(o) if z0 > 0.137 or o.name.startswith(("Tampo", "Prat_", "Barra_", "M8arrL_t", "M8porca_t", "M8nyl_t")): hide(o) # ---------------------------------------------------------------- explodida EXPL = [] if VIEW == "explodida": def move(prefixes, d): for o in objs: if o.name.startswith(prefixes): o.location += Vector(d) move(("Prat_Topo",), (0, 0, 0.30)) move(("Prat_Meio",), (0, 0, 0.16)) move(("Tampo", "M8arrL_t_", "M8porca_t_", "M8arrL_t2_", "M8nyl_t2_"), (0, 0, 0.09)) move(("Barra_",), (0, 0, 0.0)) for s, sx in (("D", 1), ("E", -1)): move(tuple("%s_%s" % (p, s) for p in ("Roda",)), (0.13 * sx, 0, -0.02)) move(tuple("%s_%s" % (p, s) for p in ("Motor", "Suporte", "M3x6_mot", "M4x25", "Calco", "Cabo")), (0, 0, -0.11)) bpy.context.view_layer.update() # ---------------------------------------------------------------- render (Eevee + Freestyle) scn.render.engine = "BLENDER_EEVEE_NEXT" if "BLENDER_EEVEE_NEXT" in bpy.types.RenderSettings.bl_rna.properties["engine"].enum_items.keys() else "BLENDER_EEVEE" scn.view_settings.view_transform = "Standard" world = bpy.data.worlds.new("W") scn.world = world world.use_nodes = True world.node_tree.nodes["Background"].inputs["Color"].default_value = (1, 1, 1, 1) scn.render.film_transparent = False scn.render.use_freestyle = True scn.render.line_thickness_mode = "ABSOLUTE" scn.render.line_thickness = 1.3 vl = scn.view_layers[0] vl.use_freestyle = True fs = vl.freestyle_settings fs.crease_angle = math.radians(140) ls = fs.linesets[0] if fs.linesets else fs.linesets.new("L") ls.select_by_visibility = True ls.visibility = "VISIBLE" ls.select_silhouette = True ls.select_border = True ls.select_crease = True ls.select_external_contour = True if ls.linestyle is None: ls.linestyle = bpy.data.linestyles.new("Traço") ls.linestyle.color = (0.05, 0.05, 0.07) ls.linestyle.thickness = 1.3 scn.render.image_settings.file_format = "PNG" scn.render.image_settings.color_mode = "RGB" 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 # 1 px = 1 mm nas ortográficas def ortho(w_mm, h_mm, loc, rot, center): scn.render.resolution_x, scn.render.resolution_y = int(w_mm), int(h_mm) scn.render.resolution_percentage = 100 cam_d.ortho_scale = max(w_mm, h_mm) / 1000.0 cam.location = Vector(loc) + Vector(center) cam.rotation_euler = Euler(rot, "XYZ") C = Vector((0, 0, 0.53)) if VIEW == "frente": # olhando da frente (+Y) para trás ortho(700, 1200, (0, 3, 0), (math.radians(90), 0, math.radians(180)), C) elif VIEW == "lado": # lado direito (+X) ortho(700, 1200, (3, 0, 0), (math.radians(90), 0, math.radians(90)), C) elif VIEW == "topo": ortho(700, 700, (0, 0, 3), (0, 0, 0), Vector((0, 0, 0))) elif VIEW == "interno": ortho(560, 560, (0, 0, 3), (0, 0, 0), Vector((0, 0, 0))) scn.render.resolution_percentage = 400 # 4 px/mm (desenho detalhado) scn.render.line_thickness = 1.0 else: # iso / explodida scn.render.resolution_x, scn.render.resolution_y = (2000, 2600) if VIEW == "explodida" else (1600, 2000) scn.render.resolution_percentage = 100 cam_d.ortho_scale = 1.95 if VIEW == "explodida" else 1.45 target = Vector((0, 0, 0.62 if VIEW == "explodida" else 0.53)) az, el = math.radians(145), math.radians(28) cam.location = target + Vector((math.sin(az) * math.cos(el), -math.cos(az) * math.cos(el), math.sin(el))) * 6 cam.rotation_euler = (target - cam.location).to_track_quat("-Z", "Y").to_euler() scn.render.filepath = OUTP bpy.context.view_layer.update() # pontos-âncora projetados (para cotas/balões) def proj(p): v = world_to_camera_view(scn, cam, Vector(p)) rx = scn.render.resolution_x * scn.render.resolution_percentage / 100 ry = scn.render.resolution_y * scn.render.resolution_percentage / 100 return [v.x * rx, (1 - v.y) * ry] def center_of(prefixes): pts = [] for o in objs: if o.name.startswith(prefixes) and not o.hide_render: x0, x1, y0, y1, z0, z1 = bbox_world(o) pts.append(Vector(((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2))) if not pts: return None return sum(pts, Vector()) / len(pts) anchors = {} for key, pref in { "base": ("Parede_Frontal",), "tampo": ("Tampo",), "prat_meio": ("Prat_Meio_Placa",), "prat_topo": ("Prat_Topo_Placa",), "barra": ("Barra_EF",), "borboleta": ("Prat_Topo_borb_EF",), "roda": ("Roda_EF",), "motor": ("Motor_EF",), "suporte": ("Suporte_EF",), "calco": ("Calco_EF",), "porca_base": ("M8nyl_b_EF",), "borda": ("Prat_Meio_Borda_F",), "nervura": ("Prat_Topo_Nervura_X",), "cam": ("ESP32CAM",), "chave": ("Chave",), "arduino": ("CNCShield",), "pb1": ("Protoboard_1",), "pb2": ("Protoboard_2",), "bateria": ("Bateria",), "lm": ("LM2596",), "wago": ("Wago_pos",), "xt60": ("XT60",), "fusivel": ("PortaFusivel",), "motor_int": ("Motor_DT",), }.items(): c = center_of(pref) if c is not None: anchors[key] = proj(c) # pontos geométricos para cotas for key, p in {"z0": (0, 0.25, 0), "z_tampo": (0, 0.25, 0.14138), "z_meio": (0, 0.25, 0.580), "z_topo": (0, 0.25, 1.020), "z_max": (0, 0.25, 1.060), "x_min": (-0.25, 0.25, 0), "x_max": (0.25, 0.25, 0), "roda_f": (0.2317, 0.185, 0.03), "roda_t": (0.2317, -0.185, 0.03), "roda_e": (-0.2317, 0.185, 0.03)}.items(): anchors[key] = proj(p) json.dump({"view": VIEW, "res": [scn.render.resolution_x * scn.render.resolution_percentage // 100, scn.render.resolution_y * scn.render.resolution_percentage // 100], "anchors": anchors}, open(OUTP + ".json", "w"), indent=1) bpy.ops.render.render(write_still=True) print("salvo", OUTP)