Shake0
11-26-2010, 02:46 PM
Hi All!
I'm really new to coding, .. like I'm use to rely to Xpresso even for a simple if statement...
However, it's a week I'm getting along with Python and it looks surprisingly easy to learn.
I write a script that randomly select poly/point on the model:
(this may not look like an object-oriented program )
import c4d
from c4d import gui
import random
# IDs
Gp_ID1,Gp_ID2=1000,1010
Button1=1001
Button2=1002
OFFSET=1003
STEP=1004
SEED=1015
PCENT=1016
DES1,DES2,DES3,DES4=1200,1201,1202,1203
ABS=1005
class Dialog(gui.GeDialog):
def CreateLayout(self):
self.SetTitle("Random Selection")
self.GroupBegin(Gp_ID1, c4d.BFH_SCALEFIT,5)
self.AddButton(Button1, c4d.BFH_SCALE,100,name="Sequence")
self.AddStaticText(DES1,c4d.BFH_RIGHT,50,name="Offset")
self.AddEditNumberArrows(OFFSET,c4d.BFH_SCALEFIT)
self.AddStaticText(DES2,c4d.BFH_RIGHT,50,name="Step")
self.AddEditNumberArrows(STEP,c4d.BFH_SCALEFIT)
self.GroupEnd()
self.AddSeparatorH(200)
self.GroupBegin(Gp_ID2, c4d.BFH_SCALEFIT,5)
self.AddButton(Button2, c4d.BFH_SCALE,100,name="Random")
self.AddStaticText(DES3,c4d.BFH_RIGHT,50,name="Seed")
self.AddEditNumberArrows(SEED,c4d.BFH_SCALEFIT)
self.AddStaticText(DES4,c4d.BFH_RIGHT,50,name="Selection%")
self.AddEditSlider(PCENT,c4d.BFH_SCALEFIT,0,0)
self.AddCheckbox(2054,c4d.BFH_CENTER,0,0,name="Polygon")
self.GroupEnd()
return True
def InitValues(self):
self.SetReal(STEP,1)
self.SetReal(SEED,2783)
return True
def Command(self,id,msg):
global ch
if id == Button1:
ch=1,int(self.GetReal(OFFSET)),int(self.GetReal(STEP)),self.GetBool(2054)
self.Close()
elif id == Button2:
ch=2,int(self.GetReal(SEED)),int(self.GetReal(PCENT)),self.GetBool(2054)
self.Close()
return True
def process(count):
if ch[0]==1: # SEQUENCE
pointOS=[]
for i in range(count):
if select.IsSelected(i) == True:
pointOS.append(i)
lengthOS = len(pointOS)
for i in range (ch[1],lengthOS,ch[2]):
select.Select(i)
elif ch[0]==2: # RANDOM
pointOS=[]
for i in range(count):
if select.IsSelected(i) == True:
pointOS.append(i)
pointNS=[]
lengthOS = len(pointOS)-1
lengthOS2 = (ch[2]*len(pointOS))/100
random.seed(ch[1])
while len(pointNS)<lengthOS2:
rdn_p = random.randint(0,(len(pointOS)-1))
pointNS.append(pointOS.pop(rdn_p))
select.DeselectAll()
for i in pointNS:
select.Select(i)
return select
def main():
global obj, select
obj = c4d.documents.GetActiveDocument().GetActiveObject()
if obj == None:
pass
else:
Dlg = Dialog()
Dlg.Open(c4d.DLG_TYPE_MODAL,defaultw=400)
if ch[3]==False:
select = obj.GetPointS()
count = len(obj.GetAllPoints())
process(count)
elif ch[3]==True:
select = obj.GetPolygonS()
count = len(obj.GetAllPolygons())
process(count)
main()
The user has to specify if the selection is on polygon or not.
Is there a way to detect if the document is on Polygon Tool mode or Point Tool mode?
Then, I would like to display the user area in the Attribute menage, instead of a pop-up dialog. But there is no class to access attributes manager :curious:
I'm really new to coding, .. like I'm use to rely to Xpresso even for a simple if statement...
However, it's a week I'm getting along with Python and it looks surprisingly easy to learn.
I write a script that randomly select poly/point on the model:
(this may not look like an object-oriented program )
import c4d
from c4d import gui
import random
# IDs
Gp_ID1,Gp_ID2=1000,1010
Button1=1001
Button2=1002
OFFSET=1003
STEP=1004
SEED=1015
PCENT=1016
DES1,DES2,DES3,DES4=1200,1201,1202,1203
ABS=1005
class Dialog(gui.GeDialog):
def CreateLayout(self):
self.SetTitle("Random Selection")
self.GroupBegin(Gp_ID1, c4d.BFH_SCALEFIT,5)
self.AddButton(Button1, c4d.BFH_SCALE,100,name="Sequence")
self.AddStaticText(DES1,c4d.BFH_RIGHT,50,name="Offset")
self.AddEditNumberArrows(OFFSET,c4d.BFH_SCALEFIT)
self.AddStaticText(DES2,c4d.BFH_RIGHT,50,name="Step")
self.AddEditNumberArrows(STEP,c4d.BFH_SCALEFIT)
self.GroupEnd()
self.AddSeparatorH(200)
self.GroupBegin(Gp_ID2, c4d.BFH_SCALEFIT,5)
self.AddButton(Button2, c4d.BFH_SCALE,100,name="Random")
self.AddStaticText(DES3,c4d.BFH_RIGHT,50,name="Seed")
self.AddEditNumberArrows(SEED,c4d.BFH_SCALEFIT)
self.AddStaticText(DES4,c4d.BFH_RIGHT,50,name="Selection%")
self.AddEditSlider(PCENT,c4d.BFH_SCALEFIT,0,0)
self.AddCheckbox(2054,c4d.BFH_CENTER,0,0,name="Polygon")
self.GroupEnd()
return True
def InitValues(self):
self.SetReal(STEP,1)
self.SetReal(SEED,2783)
return True
def Command(self,id,msg):
global ch
if id == Button1:
ch=1,int(self.GetReal(OFFSET)),int(self.GetReal(STEP)),self.GetBool(2054)
self.Close()
elif id == Button2:
ch=2,int(self.GetReal(SEED)),int(self.GetReal(PCENT)),self.GetBool(2054)
self.Close()
return True
def process(count):
if ch[0]==1: # SEQUENCE
pointOS=[]
for i in range(count):
if select.IsSelected(i) == True:
pointOS.append(i)
lengthOS = len(pointOS)
for i in range (ch[1],lengthOS,ch[2]):
select.Select(i)
elif ch[0]==2: # RANDOM
pointOS=[]
for i in range(count):
if select.IsSelected(i) == True:
pointOS.append(i)
pointNS=[]
lengthOS = len(pointOS)-1
lengthOS2 = (ch[2]*len(pointOS))/100
random.seed(ch[1])
while len(pointNS)<lengthOS2:
rdn_p = random.randint(0,(len(pointOS)-1))
pointNS.append(pointOS.pop(rdn_p))
select.DeselectAll()
for i in pointNS:
select.Select(i)
return select
def main():
global obj, select
obj = c4d.documents.GetActiveDocument().GetActiveObject()
if obj == None:
pass
else:
Dlg = Dialog()
Dlg.Open(c4d.DLG_TYPE_MODAL,defaultw=400)
if ch[3]==False:
select = obj.GetPointS()
count = len(obj.GetAllPoints())
process(count)
elif ch[3]==True:
select = obj.GetPolygonS()
count = len(obj.GetAllPolygons())
process(count)
main()
The user has to specify if the selection is on polygon or not.
Is there a way to detect if the document is on Polygon Tool mode or Point Tool mode?
Then, I would like to display the user area in the Attribute menage, instead of a pop-up dialog. But there is no class to access attributes manager :curious:
