hello,
I’m trying to position and modify geometry of a set of metaballs (capsules), but i cannot figure out why the “size_x” attribute is not available. Where is it?
Also, I’m really upset that I cannot find a way to display or determine attributes of a structure. I understand that there is no ‘debug’ in blender/python but at least I would like to know what the attributes of a structure are… i’ve searched on this and come up with nothing.
how the hell do you program anything if you can’t debug/find the attributes of an array or structure???
so far everything works below except i cannot set the magnitude (or length) of the capsule. nothing works.
I’ve attached my code below but for some reason it looks very strange on my screen (all white). doing the best i can here. it should be simple.
The problem part of the code is at the ‘cylinder.size_x = mag’ line where it just fails and says that there is no ‘size_x’ attribute. HOWEVER, that is clearly an attribute when i try to edit it in the blender gui. i’ve tried going into edit mode (in python) and performing the mag change but I just get the same error.
WTF!!! i feel really stupid.
"""
Blender script. Draws metaball 'capsules' along lines.
Written by T.C.Doeh (tensortek.com) 4/11/2013
Based upon code by Patrick Fuller, [email protected]
"""
import bpy
from math import acos, degrees, pi
from mathutils import Vector
import json
colors = {"medgray": (50, 50, 50), "light_gray": (118, 118, 118), "gray": (11, 11, 11)}
# Normalize to [0,1] and make blender materials
for key, value in colors.items():
value = [x / 255.0 for x in value]
bpy.data.materials.new(name=key)
bpy.data.materials[key].diffuse_color = value
bpy.data.materials[key].specular_intensity = 0.5
# Don't specify more parameters if these colors
if key == "gray" or key == "light_gray" or key == "medgray":
continue
# Transparency parameters
bpy.data.materials[key].use_transparency = True
bpy.data.materials[key].transparency_method = "RAYTRACE"
bpy.data.materials[key].alpha = 0.1 if key == "clear" else 0.95
bpy.data.materials[key].raytrace_transparency.fresnel = 0.1
bpy.data.materials[key].raytrace_transparency.ior = 1.15
def draw_network(network, edge_thickness=1.2, node_size=2.1, directed=True):
""" Takes assembled network/molecule data and draws to blender """
bpy.ops.object.select_all(action='DESELECT')
# Draw edges
ecount = 0
for edge in network["edges"]:
ecount += 1
# Get source and target locations by drilling down into data structure
source_loc = network["nodes"][edge["source"]]["location"]
target_loc = network["nodes"][edge["target"]]["location"]
diff = [c2 - c1 for c2, c1 in zip(source_loc, target_loc)]
cent = [(c2 + c1) / 2 for c2, c1 in zip(source_loc, target_loc)]
mag = sum([(c2 - c1) ** 2
for c1, c2 in zip(source_loc, target_loc)]) ** 0.5
# Euler rotation calculation
v_axis = Vector(diff).normalized()
v_obj = Vector((1, 0, 0))
v_rot = v_obj.cross(v_axis)
angle = acos(v_obj.dot(v_axis))
# Copy mesh primitive to create edge
#bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.metaball_add(type = 'CAPSULE',rotation = (0,-pi/2,0))
cylinder = bpy.context.object
cylinder.active_material = bpy.data.materials["light_gray"]
cylinder.location = cent
cylinder.size_x = mag
#return
#edge_cylinder.size_x = mag
#bpy.ops.object.mode_set(mode='OBJECT')
cylinder.rotation_mode = "AXIS_ANGLE"
cylinder.rotation_axis_angle = [angle] + list(v_rot)
#bpy.context.scene.objects.link(cylinder)
#shapes.append(edge_cylinder)
#shapes_to_smooth.append(edge_cylinder)
# If the starting cube is there, remove it
if "Cube" in bpy.data.objects.keys():
bpy.data.objects.get("Cube").select = True
bpy.ops.object.delete()
# Join shapes
#for shape in shapes:
# shape.select = True
#bpy.context.scene.objects.active = shapes[0]
#bpy.ops.object.join()
# Center object origin to geometry
bpy.ops.object.origin_set(type="ORIGIN_GEOMETRY", center="MEDIAN")
# Refresh scene
bpy.context.scene.update()
# If main, load json and run
if __name__ == "__main__":
with open("edgenode.json") as network_file:
network = json.load(network_file)
draw_network(network)