Python Beginer: Assigning Groups via Python

Hi all,

I want to dupligroup a cube to an empty via python.

Here is what I have:


import Blender
from Blender import *
 
def makeCube(x,y,name,passedMesh,passedScene):
 ob = Object.New("Mesh",name)
 ob.LocX=x
 ob.LocY=y
 
 ob.link(passedMesh)
 passedScene.link(ob)
 return ob
 
def makeEmpty (x,y,name, passedScene):
 ob = Object.New("Empty",name)
 ob.LocX=x
 ob.LocY=y
 passedScene.link(ob)
 return ob
 
#Create a cube with materials.
localScene = Scene.GetCurrent()
matRed = Blender.Material.New('matRed')
matRed.rgbCol = 1,0,0
redMesh = Mesh.Primitives.Cube(1)
redCube = makeCube(0,0,"redCube",redMesh,localScene)
cubeGroupName = "redCube"
#need a line of code here to assign cube to group.
redMesh.materials = [matRed]
 
#Create an empty.
tempEmpty = makeEmpty(0,10,"Empty",localScene)
tempEmpty.enableDupGroup = True
tempEmpty.DupGroup = cubeGroupName  # I error out here.
 
Redraw(-1)
localScene.update(1)

I get an error on assigning the DupGroup name because I don’t know how to make the cube part of a group.

Does anyone know how to assign an object to a group?

Thanks!

I am not 100% sure what you are trying to do or why. I did figure out how to solve your problem one way. Here’s the solution I used:


import Blender 
from Blender import * 
  
def makeCube(x,y,name,passedMesh,passedScene): 
 ob = Object.New("Mesh",name) 
 ob.LocX=x 
 ob.LocY=y 
  
 ob.link(passedMesh) 
 passedScene.link(ob) 
 return ob 
  
def makeEmpty (x,y,name, passedScene): 
 ob = Object.New("Empty",name) 
 ob.LocX=x 
 ob.LocY=y 
 passedScene.link(ob) 
 return ob 
  
#Create a cube with materials. 
localScene = Scene.GetCurrent() 
matRed = Blender.Material.New('matRed') 
matRed.rgbCol = 1,0,0 
redMesh = Mesh.Primitives.Cube(1) 
redCube = makeCube(0,0,"redCube",redMesh,localScene) 
cubeGroupName = "redCube" 
cubeGroup = Blender.Group.New(cubeGroupName) 
cubeGroup.objects = [redCube, ] 
redMesh.materials = [matRed] 
  
#Create an empty. 
tempEmpty = makeEmpty(0,10,"Empty",localScene) 
tempEmpty.enableDupGroup = True 
tempEmpty.DupGroup = cubeGroup 
  
Redraw(-1) 
localScene.update(1)

As you see I used the Group module to create a new group with given name. Then I assigned the cube object to the group and finally set this group to DupGroup. I hope this gives you some clue about how to solve the issue.

Thanks BeBraw!

I just discovered what I needed in the API as well.


import Blender
from Blender import *
#Not typical to Blender, but part of Python.
import colorsys
 
def makeCube(x,y,name,passedMesh,passedScene):
 ob = Object.New("Mesh",name)
 ob.LocX=x
 ob.LocY=y
 
 ob.link(passedMesh)
 passedScene.link(ob)
 return ob
def makeEmpty (x,y,name, passedScene):
 ob = Object.New("Empty",name)
 ob.LocX=x
 ob.LocY=y
 passedScene.link(ob)
 return ob
 
#Create a cube with materials.
localScene = Scene.GetCurrent()
matRed = Blender.Material.New('matRed')
matRed.rgbCol = 1,0,0
redMesh = Mesh.Primitives.Cube(1)
redCube = makeCube(0,0,"redCube",redMesh,localScene)
 
# New Group
cubeGroupName = "redCube"
grp= Group.New(cubeGroupName)
grp.objects= localScene.objects
 
redMesh.materials = [matRed]
#Create an empty.
tempEmpty = makeEmpty(0,10,"Empty",localScene)
tempEmpty.enableDupGroup = True
tempEmpty.DupGroup = grp
 
Redraw(-1)
localScene.update(1)