2
$\begingroup$

This is what it looks in Blenderenter image description here

This is what it looks after rendered

enter image description here

The atoms still looked fine but the bonds looked grainy even though (I think) I assigned the material to bonds the same way I did to atoms. This is the relevant part of my code

def colorize(data):
    for key in data.keys():

        D.materials.new(name=key)
        D.materials[key].diffuse_color = data[key]['color']
        D.materials[key].specular_intensity = 0.2

def draw(atoms, data):
    for atom in atoms:
        O.mesh.primitive_uv_sphere_add(size=data[atom['name']]['radii']*0.5, location=atom['location'])
        O.object.shade_smooth()
        C.active_object.data.materials.append(D.materials[atom['name']])

def bonds():
    for c1 in D.objects:
        for c2 in D.objects:
            distance = pow(sum([(c2-c1)**2 for c1, c2 in zip(c1.location, c2.location)]), 0.5)
            if c1.name.startswith('Sphere') and c2.name.startswith('Sphere') and distance < 3:

                m = D.meshes.new('connector')

                bm = bmesh.new()
                v1 = bm.verts.new( c1.location )
                v2 = bm.verts.new( c2.location )
                e  = bm.edges.new([v1,v2])
                C.active_object.data.materials.append(D.materials['Stick'])

                bm.to_mesh(m)

                bond = D.objects.new( 'connector', m )
                C.scene.objects.link(bond)

                # Hook connector vertices to respective obj
                for i, obj in enumerate([ c1, c2 ]):
                    O.object.select_all( action = 'DESELECT' )
                    obj.select = True
                    bond.select = True
                    C.scene.objects.active = bond # Set connector as active

                    # Select vertex
                    O.object.mode_set(mode='OBJECT')
                    bond.data.vertices[i].select = True
                    O.object.mode_set(mode='EDIT')

                    O.object.hook_add_selob() # Hook to obj

                    O.object.mode_set(mode='OBJECT')
                    bond.data.vertices[i].select = False

                m = bond.modifiers.new('Skin', 'SKIN')
                m.use_smooth_shade = True
                m = bond.modifiers.new('Subsurf', 'SUBSURF' )
                m.levels = 2
                m.render_levels = 2
                C.active_object.data.materials.append(D.materials['Stick'])

                O.object.select_all( action = 'DESELECT' )

    for each in D.objects:
        if each.name.startswith('connector'):
            for v in each.data.skin_vertices[0].data:
                v.radius = 0.1, 0.1
            # print(D.materials['Stick'])
            print(C.object.active_material.diffuse_color)
            C.object.active_material.diffuse_color = (0.5, 0.5, 0.5)
            # C.object.active_material.diffuse_color = (1.0, 0.0, 0.0)

$\endgroup$
  • $\begingroup$ It looks to me like you have overlapping (or duplicated) faces creating Z-fighting $\endgroup$ – user1853 May 14 '19 at 23:06
  • $\begingroup$ Hey, nice use of Python there! It would be helpful if you could post the .blend using Blend Exchange. Have you tried isolating the part of your script that handles materials and testing just that? $\endgroup$ – Mentalist May 15 '19 at 3:50
  • $\begingroup$ @cegaton I can't tell which part of the code in the bond() function actually caused the overlapping faces $\endgroup$ – Mick Krongchon May 15 '19 at 15:32
  • $\begingroup$ @Mentalist Oh. Thanks for letting me know about Blend Exchange. I added to my post. $\endgroup$ – Mick Krongchon May 15 '19 at 15:33
  • $\begingroup$ After taking a quick look at your .blend I can confirm that z-fighting is the culprit. Some part of your script is causing the same geometry to be created redundantly. Since this duplicate geometry occupies the same space, the render engine cannot tell which planar surface should receive precedence and hence the z-fighting. On each of the objects that form the bonds you have a modifier chain (Hook, Hook, Skin, Subsurf) that is sufficient for creating the bonds. Once you figure out how to create the geometry as a single instance you should be good. $\endgroup$ – Mentalist May 18 '19 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.