Skip to content

Instantly share code, notes, and snippets.

@OmerShapira
Created December 6, 2012 18:14
Show Gist options
  • Save OmerShapira/4226693 to your computer and use it in GitHub Desktop.
Save OmerShapira/4226693 to your computer and use it in GitHub Desktop.
Cinema4D Random Walk
import c4d
from c4d import documents
from c4d import Vector
from random import seed
from random import uniform as rand
from math import *
#########################################################################
#Constants
RANDOM_SEED = 8
SPHERE_RADIUS = 1
PIPE_RADIUS = 0.5
NUM_NODES = 20
#########################################################################
# Where to start randomizing
means= Vector(0,1000,0)
# Range?
ranges = Vector(1200,1200,1200)
SphereScale = Vector(SPHERE_RADIUS,SPHERE_RADIUS,SPHERE_RADIUS)
spheres = []
cylinders = []
#Random Seed
seed(RANDOM_SEED)
def GetRandomVector(v = means):
return Vector(rand(v.x-ranges.x,
v.x+ranges.x),rand(v.y-ranges.y, v.y+ranges.y),rand(v.z-ranges.z, v.z+ranges.z))
def AddSphere(i=0, position=Vector()):
spheres.append(c4d.BaseObject(c4d.Osphere)) #Allocate a new Sphere object at (0,0,0)
spheres[i].SetAbsPos(position)
spheres[i].SetAbsScale(SphereScale)
doc.InsertObject(spheres[i])
def main():
#doc.StartUndo()
AddSphere() #Add first one. for every other sphere, add a connector as well.
for i in xrange(1,NUM_NODES): # TODO change position
there = GetRandomVector(spheres[i-1].GetAbsPos())
AddSphere(i=i, position=there)
myCylinder = c4d.BaseObject(c4d.Ocylinder)
cylinders.append(myCylinder)
delta = spheres[i].GetAbsPos()- spheres[i-1].GetAbsPos()
pipeScale = Vector(PIPE_RADIUS, delta.GetLength()/200 - SPHERE_RADIUS ,PIPE_RADIUS)
cylinders[i-1].SetAbsScale(pipeScale)
cylinders[i-1].SetAbsPos(spheres[i-1].GetAbsPos() + delta/2)
cylinders[i-1].SetRelRot(c4d.utils.VectorToHPB(-delta) + Vector(0,pi/2,0))
doc.InsertObject(cylinders[i-1])
return cylinders + spheres
# doc.AddUndo()
# doc.EndUndo()
#c4d.EventAdd() #Refresh the managers to show the new object
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment