César Vonc
04/02/2012, 01h11
http://code.vonc.fr/details/s9-01.png Troueur v1.3
Voici un script Python qui troue les polygones sélectionnés en créant automatiquement un booléen et un cylindre adéquat.
http://code.vonc.fr/details/s9-00.png
import c4d
from c4d import gui
class Dialogue(gui.GeDialog):
annuler = True
cotes = 6
bool = True
inter = False
rotb = 0
def CreateLayout(self):
self.SetTitle("Troueur v1.3")
self.GroupBegin(10, 2, 2)
self.AddStaticText(11, c4d.BFH_SCALEFIT, 0, 0, "Côtés :", 0)
self.AddEditNumberArrows(12, c4d.BFH_SCALEFIT, 0, 0)
self.AddStaticText(17, c4d.BFH_SCALEFIT, 0, 0, "Rotation B :", 0)
self.AddEditNumberArrows(18, c4d.BFH_SCALEFIT, 0, 0)
self.AddCheckbox(13, c4d.BFH_SCALEFIT, 0, 0, "Booléen")
self.AddCheckbox(14, c4d.BFH_SCALEFIT, 0, 0, "Intersection")
self.GroupEnd()
self.AddSeparatorV(15)
self.AddButton(19, c4d.BFH_SCALEFIT, initw = 100, inith = 20, name = "Trouer")
self.AddStaticText(16, c4d.BFH_SCALEFIT, 0, 0, "César Vonc - code.vonc.fr", 1)
return True
def InitValues(self) :
self.SetLong(12, 6, 3, 1000, 1)
self.SetBool(13, True)
self.SetBool(16, True)
self.SetDegree(18, 0)
return True
def Command(self, id, msg):
if (id == 13):
self.Enable(14, self.GetBool(13))
if (id == 19):
self.annuler = False
self.cotes = self.GetLong(12)
self.bool = self.GetBool(13)
self.inter = self.GetBool(14)
self.rotb = self.GetReal(18)
self.Close()
return True
def tailleur(pMax, pMin, p):
if pMax is None:
pMax = c4d.Vector(p.x, p.y, p.z)
else:
if pMax.x < p.x: pMax.x = p.x
if pMax.y < p.y: pMax.y = p.y
if pMax.z < p.z: pMax.z = p.z
if pMin is None:
pMin = c4d.Vector(p.x, p.y, p.z)
else:
if pMin.x > p.x: pMin.x = p.x
if pMin.y > p.y: pMin.y = p.y
if pMin.z > p.z: pMin.z = p.z
return pMax, pMin
def troueur():
if not op: return
if (op.GetType() != c4d.Opolygon): return
polSel = op.GetPolygonS()
polSel_nb = polSel.GetCount()
if (polSel_nb == 0): return
dial = Dialogue()
dial.Open(c4d.DLG_TYPE_MODAL)
if (dial.annuler is True): return
pol_nb = op.GetPolygonCount()
polSel_nor = c4d.Vector(0, 0, 0)
polSel_tMax = None
polSel_tMin = None
for i, sel in enumerate(polSel.GetAll(pol_nb)):
if not sel: continue
pol = op.GetPolygon(i)
pA = pol.a
pB = pol.b
pC = pol.c
pD = pol.d
posA = op.GetPoint(pA)
posB = op.GetPoint(pB)
posC = op.GetPoint(pC)
posD = op.GetPoint(pD)
v1 = posA - posC
v2 = posB - posD
v3 = v1.Cross(v2)
polSel_nor += v3
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posA)
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posB)
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posC)
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posD)
polSel_nor.Normalize()
polSel_pos = (polSel_tMax + polSel_tMin) / 2
vdiff = polSel_tMax - polSel_tMin
polSel_long = vdiff.GetLength() / 4
doc.StartUndo()
c4d.CallCommand(13324) # Tout désélectionner
# Cylindre
lissage = c4d.BaseTag(c4d.Tphong)
lissage[c4d.PHONGTAG_PHONG_ANGLELIMIT] = True
lissage[c4d.PHONGTAG_PHONG_USEEDGES] = False
cylindre = c4d.BaseObject(c4d.Ocylinder)
cylindre.SetName("Trou")
doc.AddUndo(c4d.UNDOTYPE_NEW, cylindre)
cylindre.InsertTag(lissage)
doc.InsertObject(cylindre, op)
cylindre[c4d.PRIM_CYLINDER_SEG] = dial.cotes
cylindre[c4d.PRIM_CYLINDER_HEIGHT] = polSel_long * 1.5
cylindre[c4d.PRIM_CYLINDER_RADIUS] = polSel_long
cylindre[c4d.ID_BASEOBJECT_REL_POSITION] = polSel_pos
cylindre[c4d.PRIM_AXIS] = 4
cylindre[c4d.ID_BASEOBJECT_REL_ROTATION] = c4d.utils.VectorToHPB(polSel_nor)
cylindre[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_Z] = dial.rotb
if ((dial.inter is True) and (dial.bool is True)):
cylindre[c4d.PRIM_CYLINDER_CAPS] = False
# Booléen
if dial.bool is True:
booleen = c4d.BaseObject(c4d.Oboole)
booleen[c4d.BOOLEOBJECT_SINGLE_OBJECT] = True
booleen[c4d.BOOLEOBJECT_HIDE_NEW_EDGES] = True
if dial.inter is True :
booleen[c4d.BOOLEOBJECT_TYPE] = 3
doc.AddUndo(c4d.UNDOTYPE_NEW, booleen)
doc.InsertObject(booleen, None, op)
doc.AddUndo(c4d.UNDOTYPE_CHANGE, op)
cyGlob = cylindre.GetMg()
doc.InsertObject(cylindre, booleen)
cylindre.SetMg(cyGlob)
op.Remove()
op.InsertUnder(booleen)
doc.SetActiveObject(booleen)
doc.EndUndo()
c4d.EventAdd()
if __name__=='__main__':
troueur()
Lien alternatif (http://code.vonc.fr/?a=9)
Voici un script Python qui troue les polygones sélectionnés en créant automatiquement un booléen et un cylindre adéquat.
http://code.vonc.fr/details/s9-00.png
import c4d
from c4d import gui
class Dialogue(gui.GeDialog):
annuler = True
cotes = 6
bool = True
inter = False
rotb = 0
def CreateLayout(self):
self.SetTitle("Troueur v1.3")
self.GroupBegin(10, 2, 2)
self.AddStaticText(11, c4d.BFH_SCALEFIT, 0, 0, "Côtés :", 0)
self.AddEditNumberArrows(12, c4d.BFH_SCALEFIT, 0, 0)
self.AddStaticText(17, c4d.BFH_SCALEFIT, 0, 0, "Rotation B :", 0)
self.AddEditNumberArrows(18, c4d.BFH_SCALEFIT, 0, 0)
self.AddCheckbox(13, c4d.BFH_SCALEFIT, 0, 0, "Booléen")
self.AddCheckbox(14, c4d.BFH_SCALEFIT, 0, 0, "Intersection")
self.GroupEnd()
self.AddSeparatorV(15)
self.AddButton(19, c4d.BFH_SCALEFIT, initw = 100, inith = 20, name = "Trouer")
self.AddStaticText(16, c4d.BFH_SCALEFIT, 0, 0, "César Vonc - code.vonc.fr", 1)
return True
def InitValues(self) :
self.SetLong(12, 6, 3, 1000, 1)
self.SetBool(13, True)
self.SetBool(16, True)
self.SetDegree(18, 0)
return True
def Command(self, id, msg):
if (id == 13):
self.Enable(14, self.GetBool(13))
if (id == 19):
self.annuler = False
self.cotes = self.GetLong(12)
self.bool = self.GetBool(13)
self.inter = self.GetBool(14)
self.rotb = self.GetReal(18)
self.Close()
return True
def tailleur(pMax, pMin, p):
if pMax is None:
pMax = c4d.Vector(p.x, p.y, p.z)
else:
if pMax.x < p.x: pMax.x = p.x
if pMax.y < p.y: pMax.y = p.y
if pMax.z < p.z: pMax.z = p.z
if pMin is None:
pMin = c4d.Vector(p.x, p.y, p.z)
else:
if pMin.x > p.x: pMin.x = p.x
if pMin.y > p.y: pMin.y = p.y
if pMin.z > p.z: pMin.z = p.z
return pMax, pMin
def troueur():
if not op: return
if (op.GetType() != c4d.Opolygon): return
polSel = op.GetPolygonS()
polSel_nb = polSel.GetCount()
if (polSel_nb == 0): return
dial = Dialogue()
dial.Open(c4d.DLG_TYPE_MODAL)
if (dial.annuler is True): return
pol_nb = op.GetPolygonCount()
polSel_nor = c4d.Vector(0, 0, 0)
polSel_tMax = None
polSel_tMin = None
for i, sel in enumerate(polSel.GetAll(pol_nb)):
if not sel: continue
pol = op.GetPolygon(i)
pA = pol.a
pB = pol.b
pC = pol.c
pD = pol.d
posA = op.GetPoint(pA)
posB = op.GetPoint(pB)
posC = op.GetPoint(pC)
posD = op.GetPoint(pD)
v1 = posA - posC
v2 = posB - posD
v3 = v1.Cross(v2)
polSel_nor += v3
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posA)
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posB)
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posC)
polSel_tMax, polSel_tMin = tailleur(polSel_tMax, polSel_tMin, posD)
polSel_nor.Normalize()
polSel_pos = (polSel_tMax + polSel_tMin) / 2
vdiff = polSel_tMax - polSel_tMin
polSel_long = vdiff.GetLength() / 4
doc.StartUndo()
c4d.CallCommand(13324) # Tout désélectionner
# Cylindre
lissage = c4d.BaseTag(c4d.Tphong)
lissage[c4d.PHONGTAG_PHONG_ANGLELIMIT] = True
lissage[c4d.PHONGTAG_PHONG_USEEDGES] = False
cylindre = c4d.BaseObject(c4d.Ocylinder)
cylindre.SetName("Trou")
doc.AddUndo(c4d.UNDOTYPE_NEW, cylindre)
cylindre.InsertTag(lissage)
doc.InsertObject(cylindre, op)
cylindre[c4d.PRIM_CYLINDER_SEG] = dial.cotes
cylindre[c4d.PRIM_CYLINDER_HEIGHT] = polSel_long * 1.5
cylindre[c4d.PRIM_CYLINDER_RADIUS] = polSel_long
cylindre[c4d.ID_BASEOBJECT_REL_POSITION] = polSel_pos
cylindre[c4d.PRIM_AXIS] = 4
cylindre[c4d.ID_BASEOBJECT_REL_ROTATION] = c4d.utils.VectorToHPB(polSel_nor)
cylindre[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_Z] = dial.rotb
if ((dial.inter is True) and (dial.bool is True)):
cylindre[c4d.PRIM_CYLINDER_CAPS] = False
# Booléen
if dial.bool is True:
booleen = c4d.BaseObject(c4d.Oboole)
booleen[c4d.BOOLEOBJECT_SINGLE_OBJECT] = True
booleen[c4d.BOOLEOBJECT_HIDE_NEW_EDGES] = True
if dial.inter is True :
booleen[c4d.BOOLEOBJECT_TYPE] = 3
doc.AddUndo(c4d.UNDOTYPE_NEW, booleen)
doc.InsertObject(booleen, None, op)
doc.AddUndo(c4d.UNDOTYPE_CHANGE, op)
cyGlob = cylindre.GetMg()
doc.InsertObject(cylindre, booleen)
cylindre.SetMg(cyGlob)
op.Remove()
op.InsertUnder(booleen)
doc.SetActiveObject(booleen)
doc.EndUndo()
c4d.EventAdd()
if __name__=='__main__':
troueur()
Lien alternatif (http://code.vonc.fr/?a=9)