THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 22/06/2011 at 00:32, xxxxxxxx wrote:
This is your code.
def SelectWalls(op,param) : #function to find direction of walls and assign to seperate selection tags
polys = op.GetAllPolygons() #get list of polygons in op
doc = c4d.documents.BaseDocument()
i=0
for poly in polys: #funtion to find normals
pt_a = op.GetPoint(poly.a)
pt_b = op.GetPoint(poly.b)
pt_c = op.GetPoint(poly.c)
pt_d = op.GetPoint(poly.d)
vector1 = pt_a-pt_b
vector2 = pt_b-pt_c
normal = vector1.Cross(vector2)
normal.Normalize()
polyselx = op.GetPolygonS()
polysely = op.GetPolygonS()
polyselz = op.GetPolygonS()
# print polyselection
nx = normal.x
ny = normal.y
nz = normal.z
if(nx!=0) : #check x normal
# print "poly",i,poly,"is facing x to a degree of:",nx
polyselx.Select(i)
i+=1
elif(ny!=0) : #check y normal
# print "poly",i,poly,"is facing y to a degree of:",ny
polysely.Select(i)
i+=1
elif(nz!=0) : #check z normal
# print "poly",i,poly,"is facing z to a degree of:",nz
polyselz.Select(i)
i+=1
else:
# print "something went wrong"
pass
i+=1
if param ==0:
return polyselx
if param ==1:
return polysely
if param ==2:
return polyselz
1. Do not obtain the Selection ever and ever again in the Loop, this is redundant calling of functions and leads to higher resource neccessarity.
Instead of putting polyselx = op.GetPolygonS() in the loop, put it outside, before it.
2. Remember when I said that you obtain a real instance of the BaseSelect when using GetPolygonS() ?
That means, calling the function 3 times and assigning the BaseSelect to 3 different variables, modifing one of the BaseSelect's will modify all as they point to the same BaseSelect instance.
3. You don't need to call GetPolygonS() if you do not want to copy the selection to the Object's Polygonselection or do not want to obtain the selected Polygons. Just create another BaseSelect instance.
from c4d import BaseSelect
mySelection = BaseSelect()
mySelection.Select(12) # Enable the Bit with index 12
3.1 If you want to copy this selection to the Polygonselection:
# code from above, plus:
mySelection.CopyTo(op.GetPolygonS()) # Copys the bits from 'mySelection' to the original Polygon BaseSelect instance
Outside the Function, your code looks like this:
x_faces = SelectWalls(cube2,0)
y_faces = SelectWalls(cube2,1)
z_faces = SelectWalls(cube2,2)
Why calculating all stuff again and again and again ? Instead, return a tuple or list or dictionary !
Instead of
def SelectWalls(op,param) : #function to find direction of walls and assign to seperate selection tags
[ .... ]
if param ==0:
return polyselx
if param ==1:
return polysely
if param ==2:
return polyselz
Use this:
def SelectWalls(op) : #function to find direction of walls and assign to seperate selection tags
[ .... ]
return polyselx, polysely, polyselz
x_faces, y_faces, z_faces = SelectWalls(op)
But this is some Standart Python and does not have to do with Cinema 4D at all, I recommend to read some more Python (not Py4D) Documentation. :wink:
Copying selection to SelectionTag:
1. Why do you want to copy it to a SelectionTag ?
2. Well, pretty simple:
tag = op.MakeTag(c4d.Tpolygonselection)
tag_sel = tag.GetBaseSelect() # Wrong documented in the SDK, use:
op.GetPolygonS().CopyTo(tag_sel) #
# for i in dir(tag) :
# if "sel" in i.lower() :
# print i
#
# to print all attributes of 'tag' containing the word "sel"
Cheers,