1
$\begingroup$

I am trying write a script that makes 2 spheres move around and change color at the same time. this is my code:

import bpy
from random import randint, uniform
import math

r,g,b = (0.1, 0.5, 0.7)
bpy.ops.mesh.primitive_uv_sphere_add(radius=5, location=(0, 0, 0), scale=(1, 1, 1))
bpy.ops.object.modifier_add(type='COLLISION')
bpy.ops.mesh.primitive_uv_sphere_add(radius=5, location=(5, 5, 5), scale=(1, 1, 1))
bpy.ops.object.modifier_add(type='COLLISION')

ob = bpy.data.objects["Sphere"]
for poly in ob.data.polygons:
    poly.use_smooth = True
ob2 = bpy.data.objects["Sphere.001"]
for poly in ob2.data.polygons:
    poly.use_smooth = True

mat = bpy.data.materials.new('MaterialName')
ob.active_material = mat
ob2.active_material = mat

frame_number = 0

for i in range(0, 50):
    x = randint(-20, 20)
    y = randint(-20, 20)
    z = randint(-20, 20)  
    x1 = randint(-20, 20)
    y1 = randint(-20, 20)
    z1 = randint(-20, 20)
    c1 = uniform(0.0, 1.0)
    c2 = uniform(0.0, 1.0)
    c3 = uniform(0.0, 1.0)
    c4 = uniform(0.0, 1.0)
    bpy.context.scene.frame_set(frame_number)
    mat.diffuse_color = (c1, c2, c3, c4 )
    ob.location = (x, y, z)
    ob2.location = (x1, y1, z1)
    ob.keyframe_insert(data_path="location", index=-1)
    ob2.keyframe_insert(data_path="location", index=-1)
    frame_number += 25
    

At the begining of a scirpt I am trying to add collision to my objects and it kinda works, they have Collision in physics properties but yet they penetrate each other. If anyone have idea what may be wrong I would love to hear that. However my main problem is that loop. Objects moves fine but colors doesn't change at all. My two spheres gets random color at the start of animation and it doesn't change untill I run the script again. What am I doing wrong?

$\endgroup$

1 Answer 1

1
$\begingroup$

You're correctly* keyframing the location of your objects, so I don't understand why you're not doing the same for your material color: mat.keyframe_insert("diffuse_color")

Keep in mind, the diffuse_color only describes the preview color in Solid Mode. For rendering (other than View > Render Viewport Image/Animation), you want to create a material, and insert a keyframe on some node input that affects color, like so:

base_col = mat.node_tree.nodes['Principled BSDF'].inputs['Base Color']
base_col.default_value = (c1, c2, c3, c4)
base_col.keyframe_insert("default_value")

*- I didn't test your code, but it seems so

$\endgroup$
1
  • $\begingroup$ As for physics, you should ask another question (on SE questions should be about a single thing), in short, physics simulation "tries" to avoid objects overlapping, but if you position them randomly, every now and then you will position them in an overlapped space and all the simulation can do at this point is to resolve the collision, often in a spectacularly volatile way due to the objects overlapping by a lot. You also probably need to animate the "dynamic" property. $\endgroup$ Nov 24, 2021 at 21:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .