On 15/01/2018 at 01:51, xxxxxxxx wrote:
Hello,
I am trying to do a script that selects all the polygon islands inside a polygon object, and create a material for each one of them,
what I did is select the last polygon index in a list, which contains all of my polygons then select connected polys, which seems to work fine. Then it remove those selected polygons from the main list (which doesn't seem to work properly, as it only does half of them then give me an error. Also it has to erase the previous selection when it iterates again, which doesn't seem to work.
Any insight to fix this will be really appreciated :)
here is my script :
import c4d
from c4d import utils
import random
import math
def main() :
r = lambda: random.randint(0,100) #random gen
obj =doc.GetActiveObject() #selected object
pc = obj.GetPolygonCount() #poly count
all_p = obj.GetAllPolygons() #gets all polys
sel= obj.GetPolygonS()
ind = 12
con = 0
lis = [] #empty list to hold the values below
while (con<pc) :
con += 1
sel.DeselectAll()
c = c4d.Vector(r()*0.01,r()*0.01,r()*0.01) #random color
sel.Select(len(all_p)-1) #select a polgon, and the code below is to select connected
utils.SendModelingCommand(command = c4d.MCOMMAND_SELECTCONNECTED,
list = [obj],
mode = c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,
bc = c4d.BaseContainer(),
doc = doc)
si = obj.GetPolygonS().GetAll(pc) #get a list of all the selected polygons
n = 0 #a number to test which polygon index is selected or not,a loop to check that below
for i in si :
if (i == 1) :
lis.extend([n])
n += 1
tag = c4d.BaseTag(c4d.Tpolygonselection) #make a selection tag
tag[c4d.ID_BASELIST_NAME]= "selection" +str(r())
obj.InsertTag(tag)
tags = tag.GetBaseSelect() #gets the current selection of that tag and below it sets a new selection
mat = c4d.BaseMaterial(c4d.Mmaterial) #makes a new material
doc.InsertMaterial(mat)
mat[c4d.MATERIAL_COLOR_COLOR] = c
tex = c4d.TextureTag()
tex.SetMaterial(mat)
tex[c4d.TEXTURETAG_PROJECTION]=c4d.TEXTURETAG_PROJECTION_UVW
obj.InsertTag(tex)
for i in lis :
tags.Select(i)
tex[c4d.TEXTURETAG_RESTRICTION] = tag[c4d.ID_BASELIST_NAME]
for x in lis :
print x
all_p.pop(len(all_p)-1)
c4d.EventAdd()
if __name__=='__main__':
main()